Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_guest_custom_attribute_defs.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Ansible Project
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = r'''
13 ---
14 module: vmware_guest_custom_attribute_defs
15 short_description: Manage custom attributes definitions for virtual machine from VMware
16 description:
17 - This module can be used to add and remove custom attributes definitions for the given virtual machine from VMware.
18 author:
19 - Jimmy Conner (@cigamit)
20 - Abhijeet Kasurde (@Akasurde)
21 options:
22 attribute_key:
23 description:
24 - Name of the custom attribute definition.
25 - This is required parameter, if O(state=present) or O(state=absent).
26 required: false
27 type: str
28 state:
29 description:
30 - Manage definition of custom attributes.
31 - If set to V(present) and definition not present, then custom attribute definition is created.
32 - If set to V(present) and definition is present, then no action taken.
33 - If set to V(absent) and definition is present, then custom attribute definition is removed.
34 - If set to V(absent) and definition is absent, then no action taken.
35 default: 'present'
36 choices: ['present', 'absent']
37 type: str
38 extends_documentation_fragment:
39 - vmware.vmware.base_options
40
41 '''
42
43 EXAMPLES = r'''
44 - name: Add VMware Attribute Definition
45 community.vmware.vmware_guest_custom_attribute_defs:
46 hostname: "{{ vcenter_hostname }}"
47 username: "{{ vcenter_username }}"
48 password: "{{ vcenter_password }}"
49 state: present
50 attribute_key: custom_attr_def_1
51 delegate_to: localhost
52 register: defs
53
54 - name: Remove VMware Attribute Definition
55 community.vmware.vmware_guest_custom_attribute_defs:
56 hostname: "{{ vcenter_hostname }}"
57 username: "{{ vcenter_username }}"
58 password: "{{ vcenter_password }}"
59 state: absent
60 attribute_key: custom_attr_def_1
61 delegate_to: localhost
62 register: defs
63 '''
64
65 RETURN = r'''
66 custom_attribute_defs:
67 description: list of all current attribute definitions
68 returned: always
69 type: list
70 sample: ["sample_5", "sample_4"]
71 '''
72
73 from ansible.module_utils.basic import AnsibleModule
74 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
75 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
76
77 try:
78 from pyVmomi import vim
79 except ImportError:
80 pass
81
82
83 class VmAttributeDefManager(PyVmomi):
84 def __init__(self, module):
85 super(VmAttributeDefManager, self).__init__(module)
86
87 def remove_custom_def(self, field):
88 changed = False
89 f = dict()
90 for x in self.custom_field_mgr:
91 if x.name == field and x.managedObjectType == vim.VirtualMachine:
92 changed = True
93 if not self.module.check_mode:
94 self.content.customFieldsManager.RemoveCustomFieldDef(key=x.key)
95 break
96 f[x.name] = (x.key, x.managedObjectType)
97 return {'changed': changed, 'failed': False, 'custom_attribute_defs': list(f.keys())}
98
99 def add_custom_def(self, field):
100 changed = False
101 found = False
102 f = dict()
103 for x in self.custom_field_mgr:
104 if x.name == field:
105 found = True
106 f[x.name] = (x.key, x.managedObjectType)
107
108 if not found:
109 changed = True
110 if not self.module.check_mode:
111 new_field = self.content.customFieldsManager.AddFieldDefinition(name=field, moType=vim.VirtualMachine)
112 f[new_field.name] = (new_field.key, new_field.type)
113 return {'changed': changed, 'failed': False, 'custom_attribute_defs': list(f.keys())}
114
115
116 def main():
117 argument_spec = base_argument_spec()
118 argument_spec.update(
119 attribute_key=dict(type='str', no_log=False),
120 state=dict(type='str', default='present', choices=['absent', 'present']),
121 )
122 module = AnsibleModule(
123 argument_spec=argument_spec,
124 supports_check_mode=True,
125 required_if=[
126 ['state', 'present', ['attribute_key']],
127 ['state', 'absent', ['attribute_key']],
128 ]
129 )
130
131 pyv = VmAttributeDefManager(module)
132 results = dict(changed=False, custom_attribute_defs=list())
133 if module.params['state'] == "present":
134 results = pyv.add_custom_def(module.params['attribute_key'])
135 elif module.params['state'] == "absent":
136 results = pyv.remove_custom_def(module.params['attribute_key'])
137
138 module.exit_json(**results)
139
140
141 if __name__ == '__main__':
142 main()