Fix Client to expect X-Percona-Resource-Type else links. Add headers to Mock/UserAgent. Start testing run_agent(). As TO_JSON() magic to Run so encode can encode Service contain blessed Run objs. Use BUILDARGS to convert Run as hashref to real objs.

This commit is contained in:
Daniel Nichter
2012-12-26 13:00:46 -07:00
parent 87080d44b2
commit 66fb54e793
9 changed files with 427 additions and 66 deletions

View File

@@ -135,9 +135,8 @@ sub get {
# Transform the resource representations into an arrayref of hashrefs.
# Each hashref contains (hopefully) all the attribs necessary to create
# a corresponding resource object.
my $res;
eval {
$res = decode_json($self->response->content);
my $res = eval {
decode_json($self->response->content);
};
if ( $EVAL_ERROR ) {
warn sprintf "Error decoding resource: %s: %s",
@@ -147,31 +146,34 @@ sub get {
}
my $objs;
my $res_type = $self->response->headers->{'x-percona-webapi-content-type'};
if ( $res_type ) {
if ( my $type = $self->response->headers->{'x-percona-resource-type'} ) {
eval {
my $type = "Percona::WebAPI::Resource::$res_type";
my $type = "Percona::WebAPI::Resource::$type";
# Create resource objects using the server-provided attribs.
if ( ref $res->{content} eq 'ARRAY' ) {
PTDEBUG && _d('Got a list of', $res_type, 'resources');
foreach my $attribs ( @{$res->{content}} ) {
if ( ref $res eq 'ARRAY' ) {
PTDEBUG && _d('Got a list of', $type, 'resources');
foreach my $attribs ( @$res ) {
my $obj = $type->new(%$attribs);
push @$objs, $obj;
}
}
else {
PTDEBUG && _d('Got a', $res_type, 'resource');
$objs = $type->new(%{$res->{content}});
PTDEBUG && _d('Got a', $type, 'resource');
$objs = $type->new(%$res);
}
};
if ( $EVAL_ERROR ) {
warn "Error creating $res_type resource objects: $EVAL_ERROR";
warn "Error creating $type resource objects: $EVAL_ERROR";
return;
}
}
$self->update_links($res->{links});
elsif ( $res ) {
$self->update_links($res);
}
else {
warn "Did not get X-Percona-Resource-Type or content from $url\n";
}
return $objs;
}
@@ -277,7 +279,7 @@ sub _set {
return;
}
$self->update_links($response->{links});
$self->update_links($response);
return;
}
@@ -318,7 +320,7 @@ sub _request {
url => $url,
content => $content,
status => $response->code,
error => $response->content,
error => "Failed to $method $url"
);
}

View File

@@ -57,8 +57,8 @@ sub as_string {
my $self = shift;
chomp(my $error = $self->error);
$error =~ s/\n/ /g;
return sprintf "Error: %s\nStatus: %d\nRequest: %s %s %s\n",
$error, $self->status, $self->method, $self->url, $self->content || '';
return sprintf "%s\nRequest: %s %s %s\nStatus: %d\n",
$error, $self->method, $self->url, $self->content || '', $self->status;
}
no Lmo;

View File

@@ -46,6 +46,8 @@ has 'output' => (
required => 1,
);
sub TO_JSON { return { %{ shift() } }; }
no Lmo;
1;
}

View File

@@ -34,12 +34,25 @@ has 'schedule' => (
required => 1,
);
has 'run' => (
has 'runs' => (
is => 'ro',
isa => 'ArrayRef[Percona::WebAPI::Resource::Run]',
required => 1,
);
sub BUILDARGS {
my ($class, %args) = @_;
if ( ref $args{runs} eq 'ARRAY' ) {
my @runs;
foreach my $run_hashref ( @{$args{runs}} ) {
my $run = Percona::WebAPI::Resource::Run->new(%$run_hashref);
push @runs, $run;
}
$args{runs} = \@runs;
}
return $class->SUPER::BUILDARGS(%args);
}
no Lmo;
1;
}

View File

@@ -30,6 +30,8 @@ our @EXPORT_OK = (qw(resource_diff));
sub resource_diff {
my ($x, $y) = @_;
return 0 if !$x && !$y;
return 1 if ($x && !$y) || (!$x && $y);
return md5_hex(Percona::WebAPI::Representation::as_json($x))
cmp md5_hex(Percona::WebAPI::Representation::as_json($y));
}