From 4fd2482b598a3b9ff9ac0f1c6db471d4dd284a2f Mon Sep 17 00:00:00 2001 From: Mark Mercado Date: Sun, 10 Jul 2022 13:05:47 -0400 Subject: [PATCH] Add `vpc` key to returned VPC data on create --- .../276-vpc-inconsistent-data-return.yml | 2 + plugins/modules/digital_ocean_vpc.py | 42 ++++++++++++------- .../targets/digital_ocean_vpc/tasks/main.yml | 7 ++-- 3 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 changelogs/fragments/276-vpc-inconsistent-data-return.yml diff --git a/changelogs/fragments/276-vpc-inconsistent-data-return.yml b/changelogs/fragments/276-vpc-inconsistent-data-return.yml new file mode 100644 index 00000000..434e4a15 --- /dev/null +++ b/changelogs/fragments/276-vpc-inconsistent-data-return.yml @@ -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). diff --git a/plugins/modules/digital_ocean_vpc.py b/plugins/modules/digital_ocean_vpc.py index eb8a3085..a6e25246 100644 --- a/plugins/modules/digital_ocean_vpc.py +++ b/plugins/modules/digital_ocean_vpc.py @@ -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 """ @@ -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" @@ -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): @@ -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: @@ -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 diff --git a/tests/integration/targets/digital_ocean_vpc/tasks/main.yml b/tests/integration/targets/digital_ocean_vpc/tasks/main.yml index a3a22f50..eed068a6 100644 --- a/tests/integration/targets/digital_ocean_vpc/tasks/main.yml +++ b/tests/integration/targets/digital_ocean_vpc/tasks/main.yml @@ -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: