Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Check mode fixes for ec2_vpc_net module
Browse files Browse the repository at this point in the history
Returns VPC object information

Detects state change for VPC, DHCP options, and tags in check mode
  • Loading branch information
Shawn Siefkas committed Dec 4, 2015
1 parent a1ea218 commit 68ba008
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions cloud/amazon/ec2_vpc_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def update_vpc_tags(vpc, module, vpc_obj, tags, name):
try:
current_tags = dict((t.name, t.value) for t in vpc.get_all_tags(filters={'resource-id': vpc_obj.id}))
if cmp(tags, current_tags):
vpc.create_tags(vpc_obj.id, tags)
if not module.check_mode:
vpc.create_tags(vpc_obj.id, tags)
return True
else:
return False
Expand All @@ -158,7 +159,8 @@ def update_vpc_tags(vpc, module, vpc_obj, tags, name):
def update_dhcp_opts(connection, module, vpc_obj, dhcp_id):

if vpc_obj.dhcp_options_id != dhcp_id:
connection.associate_dhcp_options(dhcp_id, vpc_obj.id)
if not module.check_mode:
connection.associate_dhcp_options(dhcp_id, vpc_obj.id)
return True
else:
return False
Expand Down Expand Up @@ -194,6 +196,7 @@ def main():

module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True
)

if not HAS_BOTO:
Expand Down Expand Up @@ -231,7 +234,9 @@ def main():

if vpc_obj is None:
try:
vpc_obj = connection.create_vpc(cidr_block, instance_tenancy=tenancy)
if not module.check_mode:
vpc_obj = connection.create_vpc(cidr_block, instance_tenancy=tenancy)

changed = True
except BotoServerError, e:
module.fail_json(msg=e)
Expand All @@ -254,18 +259,20 @@ def main():
# which is needed in order to detect the current status of DNS options. For now we just update
# the attribute each time and is not used as a changed-factor.
try:
connection.modify_vpc_attribute(vpc_obj.id, enable_dns_support=dns_support)
connection.modify_vpc_attribute(vpc_obj.id, enable_dns_hostnames=dns_hostnames)
if not module.check_mode:
connection.modify_vpc_attribute(vpc_obj.id, enable_dns_support=dns_support)
connection.modify_vpc_attribute(vpc_obj.id, enable_dns_hostnames=dns_hostnames)
except BotoServerError, e:
e_msg=boto_exception(e)
module.fail_json(msg=e_msg)

# get the vpc obj again in case it has changed
try:
vpc_obj = connection.get_all_vpcs(vpc_obj.id)[0]
except BotoServerError, e:
e_msg=boto_exception(e)
module.fail_json(msg=e_msg)
if not module.check_mode:
# get the vpc obj again in case it has changed
try:
vpc_obj = connection.get_all_vpcs(vpc_obj.id)[0]
except BotoServerError, e:
e_msg=boto_exception(e)
module.fail_json(msg=e_msg)

module.exit_json(changed=changed, vpc=get_vpc_values(vpc_obj))

Expand All @@ -276,7 +283,8 @@ def main():

if vpc_obj is not None:
try:
connection.delete_vpc(vpc_obj.id)
if not module.check_mode:
connection.delete_vpc(vpc_obj.id)
vpc_obj = None
changed = True
except BotoServerError, e:
Expand Down

0 comments on commit 68ba008

Please sign in to comment.