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

Start using ClientRequestTokens in event lists #31997

Merged
merged 6 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 30 additions & 11 deletions lib/ansible/modules/cloud/amazon/cloudformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@

import json
import time
import uuid
import traceback
from hashlib import sha1

Expand All @@ -237,15 +238,25 @@
# import a class, otherwise we'll use a fully qualified path
from ansible.module_utils.ec2 import AWSRetry, boto_exception
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_bytes
from ansible.module_utils._text import to_bytes, to_native


def get_stack_events(cfn, stack_name):
def get_stack_events(cfn, stack_name, token_filter=None):
'''This event data was never correct, it worked as a side effect. So the v2.3 format is different.'''
ret = {'events':[], 'log':[]}

try:
events = cfn.describe_stack_events(StackName=stack_name)
pg = cfn.get_paginator(
'describe_stack_events'
).paginate(
StackName=stack_name
)
if token_filter is not None:
events = list(pg.search(
"StackEvents[?ClientRequestToken == '{0}']".format(token_filter)
))
else:
events = list(pg)
except (botocore.exceptions.ValidationError, botocore.exceptions.ClientError) as err:
error_msg = boto_exception(err)
if 'does not exist' in error_msg:
Expand All @@ -255,7 +266,7 @@ def get_stack_events(cfn, stack_name):
ret['log'].append('Unknown error: ' + str(error_msg))
return ret

for e in events.get('StackEvents', []):
for e in events:
eventline = 'StackEvent {ResourceType} {LogicalResourceId} {ResourceStatus}'.format(**e)
ret['events'].append(eventline)

Expand All @@ -281,7 +292,7 @@ def create_stack(module, stack_params, cfn):

try:
cfn.create_stack(**stack_params)
result = stack_operation(cfn, stack_params['StackName'], 'CREATE')
result = stack_operation(cfn, stack_params['StackName'], 'CREATE', stack_params['ClientRequestToken'])
except Exception as err:
error_msg = boto_exception(err)
module.fail_json(msg="Failed to create stack {0}: {1}.".format(stack_params.get('StackName'), error_msg), exception=traceback.format_exc())
Expand All @@ -300,6 +311,10 @@ def create_changeset(module, stack_params, cfn):
module.fail_json(msg="Either 'template' or 'template_url' is required.")
if module.params['changeset_name'] is not None:
stack_params['ChangeSetName'] = module.params['changeset_name']

# changesets don't accept ClientRequestToken parameters
stack_params.pop('ClientRequestToken', None)

try:
changeset_name = build_changeset_name(stack_params)
stack_params['ChangeSetName'] = changeset_name
Expand Down Expand Up @@ -336,7 +351,7 @@ def update_stack(module, stack_params, cfn):
# don't need to be updated.
try:
cfn.update_stack(**stack_params)
result = stack_operation(cfn, stack_params['StackName'], 'UPDATE')
result = stack_operation(cfn, stack_params['StackName'], 'UPDATE', stack_params['ClientRequestToken'])
except Exception as err:
error_msg = boto_exception(err)
if 'No updates are to be performed.' in error_msg:
Expand Down Expand Up @@ -368,7 +383,7 @@ def boto_supports_termination_protection(cfn):
return hasattr(cfn, "update_termination_protection")


def stack_operation(cfn, stack_name, operation):
def stack_operation(cfn, stack_name, operation, op_token=None):
'''gets the status of a stack while it is created/updated/deleted'''
existed = []
while True:
Expand All @@ -379,15 +394,15 @@ def stack_operation(cfn, stack_name, operation):
# If the stack previously existed, and now can't be found then it's
# been deleted successfully.
if 'yes' in existed or operation == 'DELETE': # stacks may delete fast, look in a few ways.
ret = get_stack_events(cfn, stack_name)
ret = get_stack_events(cfn, stack_name, op_token)
ret.update({'changed': True, 'output': 'Stack Deleted'})
return ret
else:
return {'changed': True, 'failed': True, 'output': 'Stack Not Found', 'exception': traceback.format_exc()}
ret = get_stack_events(cfn, stack_name)
ret = get_stack_events(cfn, stack_name, op_token)
if not stack:
if 'yes' in existed or operation == 'DELETE': # stacks may delete fast, look in a few ways.
ret = get_stack_events(cfn, stack_name)
ret = get_stack_events(cfn, stack_name, op_token)
ret.update({'changed': True, 'output': 'Stack Deleted'})
return ret
else:
Expand Down Expand Up @@ -430,6 +445,9 @@ def build_changeset_name(stack_params):
def check_mode_changeset(module, stack_params, cfn):
"""Create a change set, describe it and delete it before returning check mode outputs."""
stack_params['ChangeSetName'] = build_changeset_name(stack_params)
# changesets don't accept ClientRequestToken parameters
stack_params.pop('ClientRequestToken', None)

try:
change_set = cfn.create_change_set(**stack_params)
for i in range(60): # total time 5 min
Expand Down Expand Up @@ -506,6 +524,7 @@ def main():
# collect the parameters that are passed to boto3. Keeps us from having so many scalars floating around.
stack_params = {
'Capabilities': ['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM'],
'ClientRequestToken': to_native(uuid.uuid4()),
}
state = module.params['state']
stack_params['StackName'] = module.params['stack_name']
Expand Down Expand Up @@ -610,7 +629,7 @@ def main():
result = {'changed': False, 'output': 'Stack not found.'}
else:
cfn.delete_stack(StackName=stack_params['StackName'])
result = stack_operation(cfn, stack_params['StackName'], 'DELETE')
result = stack_operation(cfn, stack_params['StackName'], 'DELETE', stack_params['ClientRequestToken'])
except Exception as err:
module.fail_json(msg=boto_exception(err), exception=traceback.format_exc())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"status_code": 200,
"data": {
"StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/ansible-test-basic-yaml/b5ca09e0-0f1b-11e7-8ebb-503aca41a035",
"StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/ansible-test-basic-yaml/04023cd0-b5d0-11e7-86b8-503ac93168c5",
"ResponseMetadata": {
"RetryAttempts": 0,
"HTTPStatusCode": 200,
"RequestId": "b5c37a4c-0f1b-11e7-8ccd-097b5e15ba0c",
"RequestId": "03fbfc36-b5d0-11e7-ae09-550cfe4b2358",
"HTTPHeaders": {
"x-amzn-requestid": "b5c37a4c-0f1b-11e7-8ccd-097b5e15ba0c",
"date": "Wed, 22 Mar 2017 16:22:14 GMT",
"x-amzn-requestid": "03fbfc36-b5d0-11e7-ae09-550cfe4b2358",
"date": "Fri, 20 Oct 2017 19:51:07 GMT",
"content-length": "393",
"content-type": "text/xml"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"ResponseMetadata": {
"RetryAttempts": 0,
"HTTPStatusCode": 200,
"RequestId": "d86831b6-0f1b-11e7-8ccd-097b5e15ba0c",
"RequestId": "170d1e02-b5d0-11e7-ae09-550cfe4b2358",
"HTTPHeaders": {
"x-amzn-requestid": "d86831b6-0f1b-11e7-8ccd-097b5e15ba0c",
"date": "Wed, 22 Mar 2017 16:23:12 GMT",
"x-amzn-requestid": "170d1e02-b5d0-11e7-ae09-550cfe4b2358",
"date": "Fri, 20 Oct 2017 19:51:39 GMT",
"content-length": "212",
"content-type": "text/xml"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@
"data": {
"StackEvents": [
{
"StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/ansible-test-basic-yaml/b5ca09e0-0f1b-11e7-8ebb-503aca41a035",
"EventId": "b5caf440-0f1b-11e7-8ebb-503aca41a035",
"StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/ansible-test-basic-yaml/04023cd0-b5d0-11e7-86b8-503ac93168c5",
"EventId": "04032730-b5d0-11e7-86b8-503ac93168c5",
"ResourceStatus": "CREATE_IN_PROGRESS",
"ResourceType": "AWS::CloudFormation::Stack",
"Timestamp": {
"hour": 16,
"hour": 19,
"__class__": "datetime",
"month": 3,
"second": 14,
"microsecond": 662000,
"month": 10,
"second": 8,
"microsecond": 324000,
"year": 2017,
"day": 22,
"minute": 22
"day": 20,
"minute": 51
},
"ResourceStatusReason": "User Initiated",
"StackName": "ansible-test-basic-yaml",
"PhysicalResourceId": "arn:aws:cloudformation:us-west-2:123456789012:stack/ansible-test-basic-yaml/b5ca09e0-0f1b-11e7-8ebb-503aca41a035",
"PhysicalResourceId": "arn:aws:cloudformation:us-west-2:123456789012:stack/ansible-test-basic-yaml/04023cd0-b5d0-11e7-86b8-503ac93168c5",
"ClientRequestToken": "3faf3fb5-b289-41fc-b940-44151828f6cf",
"LogicalResourceId": "ansible-test-basic-yaml"
}
],
"ResponseMetadata": {
"RetryAttempts": 0,
"HTTPStatusCode": 200,
"RequestId": "b601e2e5-0f1b-11e7-8ccd-097b5e15ba0c",
"RequestId": "043d4a05-b5d0-11e7-ae09-550cfe4b2358",
"HTTPHeaders": {
"x-amzn-requestid": "b601e2e5-0f1b-11e7-8ccd-097b5e15ba0c",
"date": "Wed, 22 Mar 2017 16:22:14 GMT",
"content-length": "1097",
"x-amzn-requestid": "043d4a05-b5d0-11e7-ae09-550cfe4b2358",
"date": "Fri, 20 Oct 2017 19:51:08 GMT",
"content-length": "1183",
"content-type": "text/xml"
}
}
Expand Down

This file was deleted.