Paws XVIII

Well the first thing for my S3 todo fixes was to get everything back up to github for my fix for 'GetBucketLocation' once that was done I decided to tackle the rest of the 'todo' tests.

I started out with 'GetBucketPolicy' but I notices that when I set up a real test for the code I get this as the content response;


'content' => '{"Version":"2012-10-17","Id":"S3-Console-Auto-Gen-Policy-1567770368320","Statement":[{"Sid":"S3PolicyStmt-DO-NOT-MODIFY-1567770368320","Effect":"Allow","Principal":{"Service":"s3.amazonaws.com"},"Action":"s3:PutObject","Resource":"arn:aws:s3:::dev.cargotel.storage/*","Condition":{"StringEquals":{"aws:SourceAccount":"985173205561","s3:x-amz-acl":"bucket-owner-full-control"},"ArnLike":{"aws:SourceArn":"arn:aws:s3:::dev.cargotel.storage"}}}]}',

Very odd I am getting JSON back from 'AWS' not 'XML' maybe something has changed ? I think this time on the AWS side of things or maybe my botocode is out of date? I will have to do a little digging.

On the aws site I found this

Response Syntax HTTP/1.1 200

{ Policy in JSON format }

in the latest version of the S# API for the 'GetBucketPolicy'.

The API returns both XML and JSON/Text response!

So much for really good API design on the AWS side of things.

Look at the boto JSON for that call the endpoint I want is 'GetBucketPolicyOutput';


"GetBucketPolicyOutput":{
"type":"structure",
"members":{
"Policy":{
"shape":"Policy",
"documentation":"<p>The bucket policy as a JSON document.</p>"
}
},
"payload":"Policy"
}, 

and I noticed on this one it has a 'payload' attribute which might help me here. The good thing 'Policy' is just a string;


"Policy":{"type":"string"}, 

as is the content comming back from AWS. All I need to do is look for that payload and link it back to the 'content' of the response.

In he 'template' callresult_class.tt I see that payload is already there;


 [%- IF (stream_param or shape.payload == param_name or keep_root) %]
  use MooseX::ClassAttribute;
…
  class_has_payload => (is => 'ro', default => '[% param_name %]');[% END %]
  [%- END %]

and if I look in the 'Paws::S3::GetBucketPolicyOutput;' I see that all I need is there as well;


…
  has Policy => (is => 'ro', isa => 'Str');
  use MooseX::ClassAttribute;
  class_has _payload => (is => 'ro', default => 'Policy');
...

So back into the 'response_to_object' sub of 'RestXMLResponse.pm' and I tried this patch;


...
  my $unserialized_struct;
--  if ($ret_class->can('_stream_param') ) {
++ if ($ret_class->can('_stream_param') or $ret_class->can('_payload')) {

$unserialized_struct = {}
} else {

if ($returns){
if ($ret_class->can('_stream_param')) {
$unserialized_struct->{ $ret_class->_stream_param } = $content
}
++ if ($ret_class->can('_payload')) {
++ $unserialized_struct->{ $ret_class->_payload } = $content
++ }

and I gave it a run and got;


bless( {
                 'Policy' => '{"Version":"2012-10-17","Id":"S3-Console-Auto-Gen-Policy-1567770368320","Statement":[{"Sid":"S3PolicyStmt-DO-NOT-MODIFY-1567770368320","Effect":"Allow","Principal":{"Service":"s3.amazonaws.com"},"Action":"s3:PutObject","Resource":"arn:aws:s3:::dev.cargotel.storage/*","Condition":{"StringEquals":{"s3:x-amz-acl":"bucket-owner-full-control","aws:SourceAccount":"985173205561"},"ArnLike":{"aws:SourceArn":"arn:aws:s3:::dev.cargotel.storage"}}}]}',
                 '_request_id' => '0326E9BCC8CE1C28'
               }, 'Paws::S3::GetBucketPolicyOutput' );

perfect!

A quick update to the test case 't/10_responses/s3-get-bucket-policy.response.test.yml';


-todo: test not completed
 call: GetBucketPolicy
 service: S3
+tests:
+  - expected: '{"Version":"2012-10-17","Statement":[{"Sid":"AddPerm","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::bucket-name/*"}]}'
+    op: eq
+    path: Policy

and a quick test run I get

… ok 10052 - Call S3->GetBucketPolicy from t/10_responses/s3-get-bucket-policy.response ok 10053 - Got Policy eq {"Version":"2012-10-17","Statement":[{"Sid":"AddPerm","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::bucket-name/*"}]} from result …

So that is great one more to check in and mark off. Now something else.

392551E000000578-3824442-image-a-20_1475786744330.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations