Fix failure to apply bucket policy when creating a bucket from scratch (#3091)

This commit is contained in:
Ryan Brown 2016-10-14 12:19:28 -04:00 committed by Matt Clay
parent 9aec480f59
commit 25da992785

View file

@ -198,30 +198,26 @@ def _create_or_update_bucket(connection, module, location):
# Policy # Policy
try: try:
current_policy = bucket.get_policy() current_policy = json.loads(bucket.get_policy())
except S3ResponseError as e: except S3ResponseError as e:
if e.error_code == "NoSuchBucketPolicy": if e.error_code == "NoSuchBucketPolicy":
current_policy = None current_policy = {}
else:
module.fail_json(msg=e.message)
if current_policy is not None:
if policy == {}:
try:
bucket.delete_policy()
changed = True
current_policy = bucket.get_policy()
except S3ResponseError as e:
if e.error_code == "NoSuchBucketPolicy":
current_policy = None
else: else:
module.fail_json(msg=e.message) module.fail_json(msg=e.message)
if policy is not None: if policy is not None:
if json.loads(current_policy) != json.loads(policy): if isinstance(policy, basestring):
policy = json.loads(policy)
if not policy:
bucket.delete_policy()
# only show changed if there was already a policy
changed = bool(current_policy)
elif current_policy != policy:
try: try:
bucket.set_policy(policy) bucket.set_policy(json.dumps(policy))
changed = True changed = True
current_policy = bucket.get_policy() current_policy = json.loads(bucket.get_policy())
except S3ResponseError as e: except S3ResponseError as e:
module.fail_json(msg=e.message) module.fail_json(msg=e.message)
@ -352,7 +348,7 @@ def main():
argument_spec.update( argument_spec.update(
dict( dict(
force=dict(required=False, default='no', type='bool'), force=dict(required=False, default='no', type='bool'),
policy=dict(required=False, type='json'), policy=dict(required=False, default=None, type='json'),
name=dict(required=True, type='str'), name=dict(required=True, type='str'),
requester_pays=dict(default='no', type='bool'), requester_pays=dict(default='no', type='bool'),
s3_url=dict(aliases=['S3_URL'], type='str'), s3_url=dict(aliases=['S3_URL'], type='str'),