App-SD
view release on metacpan or search on metacpan
lib/App/SD/ForeignReplica.pm view on Meta::CPAN
warn "couldn't load ticket #$uuid_or_luid";
return undef;
};
my $prop = $self->uuid . '-id';
my $id = $ticket->prop( $prop )
or warn "ticket #$uuid_or_luid has no property '$prop'";
return $id;
}
sub _set_remote_id_for_uuid {
my $self = shift;
my %args = validate(
@_,
{ uuid => 1,
remote_id => 1
}
);
require App::SD::Model::Ticket;
my $ticket = App::SD::Model::Ticket->new(
app_handle => $self->app_handle,
type => 'ticket'
);
$ticket->load( uuid => $args{'uuid'} );
$ticket->set_props( props => { $self->uuid.'-id' => $args{'remote_id'} } );
}
=head2 record_remote_id_for_pushed_record
When pushing a record created within the prophet cloud to a foreign replica, we
need to do bookkeeping to record the prophet uuid to remote id mapping.
=cut
sub record_remote_id_for_pushed_record {
my $self = shift;
my %args = validate(
@_,
{ uuid => 1,
remote_id => 1
}
);
$self->_set_uuid_for_remote_id(%args);
$self->_set_remote_id_for_uuid(%args);
}
sub record_upstream_last_modified_date {
my $self = shift;
my $date = shift;
return $self->store_local_metadata('last_modified_date' => $date);
}
sub upstream_last_modified_date {
my $self = shift;
return $self->fetch_local_metadata('last_modified_date');
}
=head2 login_loop
Loop on prompting for username/password until login is successful; user can
abort with ^C.
Saves username and password to the replica's configuration file
upon successful login.
params:
- uri # login url
- username # optional; a pre-seeded username
- password # optional; a pre-seeded password
- username_prompt # optional; custom username prompt
- secret_prompt # optional; custom secret prompt
- login_callback # coderef of code that attempts login; should throw exception
# on error
- catch_callback # optional; process thrown exception message (e.g. munge
# in some way and then print to STDERR)
returns: ($username, $password)
=cut
sub login_loop {
my $self = shift;
my %args = @_;
my $login_successful = 0;
my ($username, $password);
my %login_args = ( uri => $args{uri}, username => $username );
$login_args{username_prompt} = $args{username_prompt}
if $args{username_prompt};
$login_args{secret_prompt} = $args{secret_prompt}
if $args{secret_prompt};
# allow prompting for just password if username already specified
# and vice-versa for password
# if both are specified, we still want to loop in case the
# credentials are wrong
$login_args{username} = $args{username} if $args{username};
$login_args{password} = $args{password} if $args{password};
while (!$login_successful) {
( $username, $password ) = $self->prompt_for_login(%login_args);
try {
$args{login_callback}->($self, $username, $password);
$login_successful = 1;
} catch {
if ($args{catch_callback}) {
$args{catch_callback}->($_);
}
else {
warn "\n$_\n\n";
}
# in the case of a failed login, reset username/password
# to nothing so we re-prompt for both in the case of
# having used saved values
($login_args{username}, $login_args{password}) = (undef, undef);
};
$self->foreign_username($username) if ($username);
}
# only save username/password if login was successful
$self->save_username_and_token( $username, $password );
return ($username, $password);
}
=head2 extract_auth_from_uri( $uri_string )
Given a server URI string, possibly containing auth info, extract the
auth info if it exists.
Also sets the remote_url and url attribute to the server URI with the auth
information removed.
returns: ($username, $password)
=cut
sub extract_auth_from_uri {
my ($self, $uri_string) = @_;
my $uri = URI->new($uri_string);
my ($username, $password);
if ( $uri->can('userinfo') && ( my $auth = $uri->userinfo ) ) {
( $username, $password ) = split /:/, $auth, 2;
$uri->userinfo(undef);
}
$self->remote_url("$uri");
$self->url("$uri");
return ($username, $password);
}
sub foreign_username {
die "replica class must implement foreign_username";
}
__PACKAGE__->meta->make_immutable;
no Any::Moose;
1;
( run in 0.852 second using v1.01-cache-2.11-cpan-6aa56a78535 )