mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
s3_bucket: handle not implemented operations (#46746)
Don't fail when policy, requestPayment, tagging or versioning API is not implemented by the endpoint and if related parameters policy, requester_pays, tags or versioning are None.
This commit is contained in:
parent
1814af9f34
commit
d14f9dc685
2 changed files with 107 additions and 79 deletions
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
minor_changes:
|
||||||
|
- s3_bucket - avoid failure when ``policy``, ``requestPayment``, ``tags`` or
|
||||||
|
``versioning`` operations aren't supported by the endpoint and related
|
||||||
|
parameters aren't set
|
|
@ -72,6 +72,11 @@ options:
|
||||||
extends_documentation_fragment:
|
extends_documentation_fragment:
|
||||||
- aws
|
- aws
|
||||||
- ec2
|
- ec2
|
||||||
|
notes:
|
||||||
|
- If C(requestPayment), C(policy), C(tagging) or C(versioning)
|
||||||
|
operations/API aren't implemented by the endpoint, module doesn't fail
|
||||||
|
if related parameters I(requester_pays), I(policy), I(tags) or
|
||||||
|
I(versioning) are C(None).
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
|
@ -130,6 +135,7 @@ def create_or_update_bucket(s3_client, module, location):
|
||||||
tags = module.params.get("tags")
|
tags = module.params.get("tags")
|
||||||
versioning = module.params.get("versioning")
|
versioning = module.params.get("versioning")
|
||||||
changed = False
|
changed = False
|
||||||
|
result = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
bucket_is_present = bucket_exists(s3_client, name)
|
bucket_is_present = bucket_exists(s3_client, name)
|
||||||
|
@ -151,9 +157,12 @@ def create_or_update_bucket(s3_client, module, location):
|
||||||
# Versioning
|
# Versioning
|
||||||
try:
|
try:
|
||||||
versioning_status = get_bucket_versioning(s3_client, name)
|
versioning_status = get_bucket_versioning(s3_client, name)
|
||||||
except (ClientError, BotoCoreError) as e:
|
except BotoCoreError as exp:
|
||||||
module.fail_json_aws(e, msg="Failed to get bucket versioning")
|
module.fail_json_aws(exp, msg="Failed to get bucket versioning")
|
||||||
|
except ClientError as exp:
|
||||||
|
if exp.response['Error']['Code'] != 'NotImplemented' or versioning is not None:
|
||||||
|
module.fail_json_aws(exp, msg="Failed to get bucket versioning")
|
||||||
|
else:
|
||||||
if versioning is not None:
|
if versioning is not None:
|
||||||
required_versioning = None
|
required_versioning = None
|
||||||
if versioning and versioning_status.get('Status') != "Enabled":
|
if versioning and versioning_status.get('Status') != "Enabled":
|
||||||
|
@ -171,7 +180,7 @@ def create_or_update_bucket(s3_client, module, location):
|
||||||
versioning_status = wait_versioning_is_applied(module, s3_client, name, required_versioning)
|
versioning_status = wait_versioning_is_applied(module, s3_client, name, required_versioning)
|
||||||
|
|
||||||
# This output format is there to ensure compatibility with previous versions of the module
|
# This output format is there to ensure compatibility with previous versions of the module
|
||||||
versioning_return_value = {
|
result['versioning'] = {
|
||||||
'Versioning': versioning_status.get('Status', 'Disabled'),
|
'Versioning': versioning_status.get('Status', 'Disabled'),
|
||||||
'MfaDelete': versioning_status.get('MFADelete', 'Disabled'),
|
'MfaDelete': versioning_status.get('MFADelete', 'Disabled'),
|
||||||
}
|
}
|
||||||
|
@ -179,9 +188,13 @@ def create_or_update_bucket(s3_client, module, location):
|
||||||
# Requester pays
|
# Requester pays
|
||||||
try:
|
try:
|
||||||
requester_pays_status = get_bucket_request_payment(s3_client, name)
|
requester_pays_status = get_bucket_request_payment(s3_client, name)
|
||||||
except (BotoCoreError, ClientError) as e:
|
except BotoCoreError as exp:
|
||||||
module.fail_json_aws(e, msg="Failed to get bucket request payment")
|
module.fail_json_aws(exp, msg="Failed to get bucket request payment")
|
||||||
|
except ClientError as exp:
|
||||||
|
if exp.response['Error']['Code'] != 'NotImplemented' or requester_pays is not None:
|
||||||
|
module.fail_json_aws(exp, msg="Failed to get bucket request payment")
|
||||||
|
else:
|
||||||
|
if requester_pays is not None:
|
||||||
payer = 'Requester' if requester_pays else 'BucketOwner'
|
payer = 'Requester' if requester_pays else 'BucketOwner'
|
||||||
if requester_pays_status != payer:
|
if requester_pays_status != payer:
|
||||||
put_bucket_request_payment(s3_client, name, payer)
|
put_bucket_request_payment(s3_client, name, payer)
|
||||||
|
@ -193,12 +206,17 @@ def create_or_update_bucket(s3_client, module, location):
|
||||||
requester_pays_status = wait_payer_is_applied(module, s3_client, name, payer, should_fail=True)
|
requester_pays_status = wait_payer_is_applied(module, s3_client, name, payer, should_fail=True)
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
|
result['requester_pays'] = requester_pays
|
||||||
|
|
||||||
# Policy
|
# Policy
|
||||||
try:
|
try:
|
||||||
current_policy = get_bucket_policy(s3_client, name)
|
current_policy = get_bucket_policy(s3_client, name)
|
||||||
except (ClientError, BotoCoreError) as e:
|
except BotoCoreError as exp:
|
||||||
module.fail_json_aws(e, msg="Failed to get bucket policy")
|
module.fail_json_aws(exp, msg="Failed to get bucket policy")
|
||||||
|
except ClientError as exp:
|
||||||
|
if exp.response['Error']['Code'] != 'NotImplemented' or policy is not None:
|
||||||
|
module.fail_json_aws(exp, msg="Failed to get bucket policy")
|
||||||
|
else:
|
||||||
if policy is not None:
|
if policy is not None:
|
||||||
if isinstance(policy, string_types):
|
if isinstance(policy, string_types):
|
||||||
policy = json.loads(policy)
|
policy = json.loads(policy)
|
||||||
|
@ -223,12 +241,17 @@ def create_or_update_bucket(s3_client, module, location):
|
||||||
current_policy = wait_policy_is_applied(module, s3_client, name, policy, should_fail=True)
|
current_policy = wait_policy_is_applied(module, s3_client, name, policy, should_fail=True)
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
|
result['policy'] = current_policy
|
||||||
|
|
||||||
# Tags
|
# Tags
|
||||||
try:
|
try:
|
||||||
current_tags_dict = get_current_bucket_tags_dict(s3_client, name)
|
current_tags_dict = get_current_bucket_tags_dict(s3_client, name)
|
||||||
except (ClientError, BotoCoreError) as e:
|
except BotoCoreError as exp:
|
||||||
module.fail_json_aws(e, msg="Failed to get bucket tags")
|
module.fail_json_aws(exp, msg="Failed to get bucket tags")
|
||||||
|
except ClientError as exp:
|
||||||
|
if exp.response['Error']['Code'] != 'NotImplemented' or tags is not None:
|
||||||
|
module.fail_json_aws(exp, msg="Failed to get bucket tags")
|
||||||
|
else:
|
||||||
if tags is not None:
|
if tags is not None:
|
||||||
# Tags are always returned as text
|
# Tags are always returned as text
|
||||||
tags = dict((to_text(k), to_text(v)) for k, v in tags.items())
|
tags = dict((to_text(k), to_text(v)) for k, v in tags.items())
|
||||||
|
@ -243,12 +266,12 @@ def create_or_update_bucket(s3_client, module, location):
|
||||||
delete_bucket_tagging(s3_client, name)
|
delete_bucket_tagging(s3_client, name)
|
||||||
except (BotoCoreError, ClientError) as e:
|
except (BotoCoreError, ClientError) as e:
|
||||||
module.fail_json_aws(e, msg="Failed to delete bucket tags")
|
module.fail_json_aws(e, msg="Failed to delete bucket tags")
|
||||||
wait_tags_are_applied(module, s3_client, name, tags)
|
current_tags_dict = wait_tags_are_applied(module, s3_client, name, tags)
|
||||||
current_tags_dict = tags
|
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
module.exit_json(changed=changed, name=name, versioning=versioning_return_value,
|
result['tags'] = current_tags_dict
|
||||||
requester_pays=requester_pays, policy=current_policy, tags=current_tags_dict)
|
|
||||||
|
module.exit_json(changed=changed, name=name, **result)
|
||||||
|
|
||||||
|
|
||||||
def bucket_exists(s3_client, bucket_name):
|
def bucket_exists(s3_client, bucket_name):
|
||||||
|
@ -399,7 +422,7 @@ def wait_tags_are_applied(module, s3_client, bucket_name, expected_tags_dict):
|
||||||
if current_tags_dict != expected_tags_dict:
|
if current_tags_dict != expected_tags_dict:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
else:
|
else:
|
||||||
return
|
return current_tags_dict
|
||||||
module.fail_json(msg="Bucket tags failed to apply in the expected time")
|
module.fail_json(msg="Bucket tags failed to apply in the expected time")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue