Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New module: manage Amazon CloudFront origin access identities (cloud/amazon/cloudfront_origin_access_identity) #24568

Closed
wants to merge 51 commits into from

Conversation

wilvk
Copy link
Contributor

@wilvk wilvk commented May 12, 2017

SUMMARY

As advised by @ryansb in PR #24292 this comprises the origin access identity component of the cloudfront module additions.

ISSUE TYPE
  • New Module Pull Request
COMPONENT NAME

lib/ansible/modules/cloud/amazon/cloudfront_origin_access_identity.py

ANSIBLE VERSION
  config file =
  configured module search path = [u'/Users/willvk/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/willvk/Source/gits/ansibles/ansible/lib/ansible
  executable location = /Users/willvk/Source/gits/ansibles/ansible/bin/ansible
  python version = 2.7.10 (default, Feb  6 2017, 23:53:20) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)]

ADDITIONAL INFORMATION

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 aws cloud community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. new_module This PR includes a new module. new_plugin This PR includes a new plugin. labels May 13, 2017
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label May 15, 2017
argument_spec.update(dict(
state=dict(choices=['present', 'updated', 'absent'], default='present'),
origin_access_identity_id=dict(required=True, default=None, type='str'),
caller_reference=dict(required=False, default=None, type='str'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter isn't documented.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added to doc


from ansible.module_utils.ec2 import get_aws_connection_info, ec2_argument_spec
from ansible.module_utils.ec2 import boto3_conn, HAS_BOTO3
from ansible.modules.cloud.amazon.cloudfront_distribution import CloudFrontHelpers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To share code between modules, please use module_utils instead of importing other modules directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

def delete_origin_access_identity(self, origin_access_identity_id, e_tag):
try:
func = partial(self.client.delete_cloud_front_origin_access_identity,
Id=origin_access_identity_id, IfMatch=e_tag)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't ever going to be paginated. docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no - removed

'CallerReference': caller_reference,
'Comment': comment
})
return self.paginated_response(func)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create/update/delete calls aren't paginated in boto3. Are you using this pagination function for other error handling as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed paginated - was only using to pop ResponseMetadata which have added back

'Comment': comment
},
Id=origin_access_identity_id, IfMatch=e_tag)
return self.paginated_response(func)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. c:module_utils/ and removed community_review In order to be merged, this PR must follow the community review workflow. labels May 16, 2017
# - cloudfront_origin_access_identity


class CloudFrontHelpers:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really doesn't need to be an object - I'd prefer to have from module_utils.cloudfront import helpers to instantiating a class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

pass


class CloudFrontOriginAccessIdentityServiceManager:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be class CloudFrontOriginAccessIdentityServiceManager(object): to use new-style 2.7 classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@alikins alikins changed the title cloudfront_origin_access_identity.py New module: Add cloudfront_origin_access_identity (cloud/amazon/cloudfront_origin_access_identity) May 22, 2017
@alikins alikins changed the title New module: Add cloudfront_origin_access_identity (cloud/amazon/cloudfront_origin_access_identity) New module: manage Amazon CloudFront origin access identities (cloud/amazon/cloudfront_origin_access_identity) Jun 5, 2017
@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:community This issue/PR relates to code supported by the Ansible community. labels Jun 23, 2017
@ansibot ansibot added the stale_review Updates were made after the last review and the last review is more than 7 days old. label Jul 3, 2017
@ansibot
Copy link
Contributor

ansibot commented Jul 18, 2017

@willthames
Copy link
Contributor

With cloudfront_distribution I updated it to just create an origin access identity when needed - do you think there's a need for a separate management of OAI?

@ansibot ansibot added the support:core This issue/PR relates to code supported by the Ansible Engineering Team. label Nov 20, 2017
@wilvk
Copy link
Contributor Author

wilvk commented Nov 20, 2017

@willthames probably not unless there is a compelling use case for it.

return dictionary


def snake_dict_to_pascal_dict(snake_dict):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snake_dict_to_camel_dict now takes the parameter capitalize_first and camel_dict_to_snake_dict takes reversible so those can be used instead these pascal functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is no longer required as it is now in ansible/module_utils/ec2.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i might just open a new PR for this and only keep the origin_access_identity file as the rebase is going to be a nightmare

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, whatever is easiest. I'm sorry about the rebase nightmare.

@@ -238,8 +238,8 @@
pass # will be caught by imported HAS_BOTO3


class CloudFrontServiceManager:
"""Handles CloudFront Services"""
class CloudFrontFactsServiceManager:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a new style class CloudFrontFactsServiceManager(object)

#!/usr/bin/python
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CloudFrontFactsServiceManager)
import ansible.module_utils.cloudfront as helpers
from ansible.module_utils.ec2 import camel_dict_to_snake_dict
from ansible.module_utils.basic import AnsibleModule
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if AnsibleAWSModule was used instead of AnsibleModule because the exception handling is better (and cloudfront_distribution is using it). To import: from ansible.module_utils.aws.core import AnsibleAWSModule

Instead of using e.response and str(e) yourself, you can call

except (ClientError, BotoCoreError) as e:
    module.fail_json_aws(e, msg="Unable to <thing that failed>")

and it formats the exception by using e.message if it exists or str(e) and uses e.response only if it exists.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using AnsibleAWSModule also means you can remove importing HAS_BOTO3 and the HAS_BOTO3 check, as the module checks it.

self.client = boto3_conn(
self.module, conn_type='client', resource=resource,
region=region, endpoint=ec2_url, **aws_connect_kwargs)
except botocore.exceptions.NoRegionError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boto3_conn() now handles NoRegionError and ClientError so you can remove that here.

'CallerReference': caller_reference,
'Comment': comment
})
except botocore.exceptions.ClientError as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BotoCoreError should be caught here as well and all other places catching ClientError https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/amazon/GUIDELINES.md#boto3-1

BotoCoreError does not have a .response, so using AnsibleAWSModule will cut down on exception handling logic as I suggested above.

argument_spec.update(dict(
state=dict(choices=['present', 'absent'], default='present'),
origin_access_identity_id=dict(required=False, default=None,
type='str'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type='str' is the default so you can remove that. default=None and required=False are also defaults that can be removed.

else:
result = service_mgr.create_origin_access_identity(
caller_reference, comment)
changed = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this module is not idempotent. Running the same task twice with state: present or state: absent will show changed every time.

origin_access_identity_id, e_tag)
changed = True

if result:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just do result.pop('ResponseMetadata', None) and then call module.exit_json with result

@s-hertel
Copy link
Contributor

@wilvk These requested changes should also be applied to #24567.

@wilvk wilvk force-pushed the cloudfront_origin_access_identity branch from 11540fc to 743c116 Compare January 20, 2018 07:23
@ansibot ansibot removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_review Updates were made after the last review and the last review is more than 7 days old. labels Jan 20, 2018
@wilvk wilvk force-pushed the cloudfront_origin_access_identity branch from 1d82a78 to 0dac57c Compare January 27, 2018 23:53
@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html ci_verified Changes made in this PR are causing tests to fail. and removed ci_verified Changes made in this PR are causing tests to fail. labels Jan 27, 2018
@wilvk
Copy link
Contributor Author

wilvk commented Jan 31, 2018

Closing as have migrated changes to new PR #35540

@wilvk wilvk closed this Jan 31, 2018
@wilvk wilvk deleted the cloudfront_origin_access_identity branch January 31, 2018 11:27
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 aws c:module_utils/ ci_verified Changes made in this PR are causing tests to fail. cloud module_utils/ module This issue/PR relates to a module. needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. new_module This PR includes a new module. new_plugin This PR includes a new plugin. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants