Skip to content

Commit

Permalink
Add vpc key to returned VPC data on create
Browse files Browse the repository at this point in the history
  • Loading branch information
mamercad committed Jul 10, 2022
1 parent 7c12b8b commit 4fd2482
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/276-vpc-inconsistent-data-return.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- digital_ocean_vpc: add C(vpc) key to returned VPC data on create (https://github.com/ansible-collections/community.digitalocean/issues/276).
42 changes: 27 additions & 15 deletions plugins/modules/digital_ocean_vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@
returned: success
type: dict
sample:
created_at: '2021-06-17T11:43:12.12121565Z'
default: false
description: ''
id: a3b72d97-192f-4984-9d71-08a5faf2e0c7
ip_range: 10.116.16.0/20
name: testvpc1
region: nyc1
urn: do:vpc:a3b72d97-192f-4984-9d71-08a5faf2e0c7
msg: Created VPC myvpc1 in nyc1
vpc:
created_at: '2021-06-17T11:43:12.12121565Z'
default: false
description: ''
id: a3b72d97-192f-4984-9d71-08a5faf2e0c7
ip_range: 10.116.16.0/20
name: testvpc1
region: nyc1
urn: do:vpc:a3b72d97-192f-4984-9d71-08a5faf2e0c7
"""


Expand Down Expand Up @@ -173,12 +175,16 @@ def create(self):
json = response.json
if response.status_code != 200:
self.module.fail_json(
msg="Failed to update VPC {0}: {1}".format(
self.name, json["message"]
msg="Failed to update VPC {0} in {1}: {2}".format(
self.name, self.region, json["message"]
)
)
else:
self.module.exit_json(changed=False, data=json)
self.module.exit_json(
changed=False,
data=json,
msg="Updated VPC {0} in {1}".format(self.name, self.region),
)
else:
self.module.fail_json(
changed=False, msg="Unexpected error, please file a bug"
Expand All @@ -198,11 +204,17 @@ def create(self):
status = response.status_code
json = response.json
if status == 201:
self.module.exit_json(changed=True, data=json["vpc"])
self.module.exit_json(
changed=True,
data=json,
msg="Created VPC {0} in {1}".format(self.name, self.region),
)
else:
self.module.fail_json(
changed=False,
msg="Failed to create VPC: {0}".format(json["message"]),
msg="Failed to create VPC {0} in {1}: {2}".format(
self.name, self.region, json["message"]
),
)

def delete(self):
Expand All @@ -211,7 +223,7 @@ def delete(self):

vpc = self.get_by_name()
if vpc is None:
self.module.fail_json(msg="Unable to find VPC {0}".format(self.name))
self.module.fail_json(msg="Unable to find VPC {0} in {1}".format(self.name, self.region))
else:
vpc_id = vpc.get("id", None)
if vpc_id is not None:
Expand All @@ -221,7 +233,7 @@ def delete(self):
if status == 204:
self.module.exit_json(
changed=True,
msg="Deleted VPC {0} ({1})".format(self.name, vpc_id),
msg="Deleted VPC {0} in {1} ({2})".format(self.name, self.region, vpc_id),
)
else:
json = response.json
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/targets/digital_ocean_vpc/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
that:
- result.changed
- result.data is defined
- result.data != {}
- result.data.name == vpc_name
- result.data.vpc is defined
- result.data.vpc != {}
- result.data.vpc.name == vpc_name

- name: Set a fact for the VPC ID
ansible.builtin.set_fact:
vpc_id: "{{ result.data.id }}"
vpc_id: "{{ result.data.vpc.id }}"

- name: Fetch VPC members
community.digitalocean.digital_ocean_vpc_info:
Expand Down

0 comments on commit 4fd2482

Please sign in to comment.