From 01cb6ea0013f96a569aa29ff783108610dc64594 Mon Sep 17 00:00:00 2001 From: Mario Lenz Date: Wed, 30 Sep 2020 18:09:42 +0200 Subject: [PATCH 1/4] Fix issue when trying to set a VM custom attribute when there are custom attributes with the same name for other object types --- changelogs/fragments/412-vmware_guest_custom_attributes.yml | 2 ++ plugins/modules/vmware_guest_custom_attributes.py | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/412-vmware_guest_custom_attributes.yml diff --git a/changelogs/fragments/412-vmware_guest_custom_attributes.yml b/changelogs/fragments/412-vmware_guest_custom_attributes.yml new file mode 100644 index 000000000..8303647fa --- /dev/null +++ b/changelogs/fragments/412-vmware_guest_custom_attributes.yml @@ -0,0 +1,2 @@ +minor_changes: + - vmware_guest_custom_attributes - Fixed issue when trying to set a VM custom attribute when there are custom attributes with the same name for other object types (https://github.com/ansible-collections/community.vmware/issues/412) diff --git a/plugins/modules/vmware_guest_custom_attributes.py b/plugins/modules/vmware_guest_custom_attributes.py index ab4542851..d07aa9ba7 100644 --- a/plugins/modules/vmware_guest_custom_attributes.py +++ b/plugins/modules/vmware_guest_custom_attributes.py @@ -199,8 +199,10 @@ def set_custom_field(self, vm, user_fields): def check_exists(self, field): for x in self.custom_field_mgr: - if x.name == field: - return x + # The custom attribute should be either global (managedObjectType == None) or VM specific + if x.managedObjectType is None or x.managedObjectType == vim.VirtualMachine: + if x.name == field: + return x return False From 6c754afdc10a3ca3563b9b20b1bc0a10c032251b Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 1 Oct 2020 11:12:33 +0530 Subject: [PATCH 2/4] Added missing full stop. --- changelogs/fragments/412-vmware_guest_custom_attributes.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/412-vmware_guest_custom_attributes.yml b/changelogs/fragments/412-vmware_guest_custom_attributes.yml index 8303647fa..454db48c5 100644 --- a/changelogs/fragments/412-vmware_guest_custom_attributes.yml +++ b/changelogs/fragments/412-vmware_guest_custom_attributes.yml @@ -1,2 +1,2 @@ minor_changes: - - vmware_guest_custom_attributes - Fixed issue when trying to set a VM custom attribute when there are custom attributes with the same name for other object types (https://github.com/ansible-collections/community.vmware/issues/412) + - vmware_guest_custom_attributes - Fixed issue when trying to set a VM custom attribute when there are custom attributes with the same name for other object types (https://github.com/ansible-collections/community.vmware/issues/412). From d32dfa55772edd9a7d9cded9b0696d11edd60ae9 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 1 Oct 2020 12:07:35 +0530 Subject: [PATCH 3/4] Combine conditions --- plugins/modules/vmware_guest_custom_attributes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/vmware_guest_custom_attributes.py b/plugins/modules/vmware_guest_custom_attributes.py index d07aa9ba7..4f1c3612f 100644 --- a/plugins/modules/vmware_guest_custom_attributes.py +++ b/plugins/modules/vmware_guest_custom_attributes.py @@ -200,7 +200,7 @@ def set_custom_field(self, vm, user_fields): def check_exists(self, field): for x in self.custom_field_mgr: # The custom attribute should be either global (managedObjectType == None) or VM specific - if x.managedObjectType is None or x.managedObjectType == vim.VirtualMachine: + if x.managedObjectType in (None, vim.VirtualMachine): if x.name == field: return x return False From a750f0d2db357bb54748cd010a1cd13e06eb9b21 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 1 Oct 2020 12:09:16 +0530 Subject: [PATCH 4/4] Optimize condition --- plugins/modules/vmware_guest_custom_attributes.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugins/modules/vmware_guest_custom_attributes.py b/plugins/modules/vmware_guest_custom_attributes.py index 4f1c3612f..d624db2b3 100644 --- a/plugins/modules/vmware_guest_custom_attributes.py +++ b/plugins/modules/vmware_guest_custom_attributes.py @@ -200,9 +200,8 @@ def set_custom_field(self, vm, user_fields): def check_exists(self, field): for x in self.custom_field_mgr: # The custom attribute should be either global (managedObjectType == None) or VM specific - if x.managedObjectType in (None, vim.VirtualMachine): - if x.name == field: - return x + if x.managedObjectType in (None, vim.VirtualMachine) and x.name == field: + return x return False