Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_org.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {'metadata_version': '1.1',
11 'status': ['preview'],
12 'supported_by': 'community'}
13
14 DOCUMENTATION = r'''
15 ---
16 module: ucs_org
17
18 short_description: Manages UCS Organizations for UCS Manager
19
20 description:
21 - Manages UCS Organizations for UCS Manager.
22
23 extends_documentation_fragment: cisco.ucs.ucs
24
25 options:
26 state:
27 description:
28 - If C(absent), will remove organization.
29 - If C(present), will create or update organization.
30 choices: [absent, present]
31 default: present
32 type: str
33
34 org_name:
35 description:
36 - The name of the organization.
37 - Enter up to 16 characters.
38 - "You can use any characters or spaces except the following:"
39 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
40 aliases: [ name ]
41 type: str
42
43 parent_org_path:
44 description:
45 - A forward slash / separated hierarchical path from the root organization to the parent of the organization to be added or updated.
46 - UCS Manager supports a hierarchical structure of organizations up to five levels deep not including the root organization.
47 - For example the parent_org_path for an organization named level5 could be root/level1/level2/level3/level4
48 default: root
49 type: str
50
51 description:
52 description:
53 - A user-defined description of the organization.
54 - Enter up to 256 characters.
55 - "You can use any characters or spaces except the following:"
56 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
57 aliases: [ descr ]
58 type: str
59
60 delegate_to:
61 description:
62 - Where the module will be run
63 default: localhost
64 type: str
65
66 requirements:
67 - ucsmsdk
68
69 author:
70 - David Soper (@dsoper2)
71 - John McDonough (@movinalot)
72 - CiscoUcs (@CiscoUcs)
73 '''
74
75 EXAMPLES = r'''
76 - name: Add UCS Organization
77 cisco.ucs.ucs_org:
78 hostname: "{{ ucs_hostname }}"
79 username: "{{ ucs_username }}"
80 password: "{{ ucs_password }}"
81 org_name: test
82 description: testing org
83 state: present
84 delegate_to: localhost
85
86 - name: Update UCS Organization
87 cisco.ucs.ucs_org:
88 hostname: "{{ ucs_hostname }}"
89 username: "{{ ucs_username }}"
90 password: "{{ ucs_password }}"
91 org_name: test
92 description: Testing org
93 state: present
94 delegate_to: localhost
95
96 - name: Add UCS Organization
97 cisco.ucs.ucs_org:
98 hostname: "{{ ucs_hostname }}"
99 username: "{{ ucs_username }}"
100 password: "{{ ucs_password }}"
101 org_name: level1
102 parent_org_path: root
103 description: level1 org
104 state: present
105 delegate_to: localhost
106
107 - name: Add UCS Organization
108 cisco.ucs.ucs_org:
109 hostname: "{{ ucs_hostname }}"
110 username: "{{ ucs_username }}"
111 password: "{{ ucs_password }}"
112 org_name: level2
113 parent_org_path: root/level1
114 description: level2 org
115 state: present
116
117 - name: Add UCS Organization
118 cisco.ucs.ucs_org:
119 hostname: "{{ ucs_hostname }}"
120 username: "{{ ucs_username }}"
121 password: "{{ ucs_password }}"
122 org_name: level3
123 parent_org_path: root/level1/level2
124 description: level3 org
125 state: present
126
127 - name: Remove UCS Organization
128 cisco.ucs.ucs_org:
129 hostname: "{{ ucs_hostname }}"
130 username: "{{ ucs_username }}"
131 password: "{{ ucs_password }}"
132 org_name: level2
133 parent_org_path: root/level1
134 state: absent
135 '''
136
137 RETURN = r'''
138 #
139 '''
140
141 from ansible.module_utils.basic import AnsibleModule
142 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
143
144
145 def main():
146 argument_spec = ucs_argument_spec.copy()
147 argument_spec.update(
148 org_name=dict(type='str', aliases=['name']),
149 parent_org_path=dict(type='str', default='root'),
150 description=dict(type='str', aliases=['descr']),
151 state=dict(type='str', default='present', choices=['present', 'absent']),
152 delegate_to=dict(type='str', default='localhost'),
153 )
154
155 module = AnsibleModule(
156 argument_spec,
157 supports_check_mode=True,
158 required_if=[
159 ['state', 'present', ['org_name']],
160 ],
161 )
162
163 # UCSModule verifies ucsmsdk is present and exits on failure.
164 # Imports are below for UCS object creation.
165 ucs = UCSModule(module)
166 from ucsmsdk.mometa.org.OrgOrg import OrgOrg
167
168 err = False
169 changed = False
170 requested_state = module.params['state']
171
172 kwargs = dict()
173
174 if module.params['description'] is not None:
175 kwargs['descr'] = module.params['description']
176
177 try:
178 parent_org_dn = 'org-' + module.params['parent_org_path'].replace('/', '/org-')
179 dn = parent_org_dn + '/org-' + module.params['org_name']
180
181 mo = ucs.login_handle.query_dn(dn)
182
183 # Determine state change
184 if mo:
185 # Object exists, if it should exist has anything changed?
186 if requested_state == 'present':
187 # Do some or all Object properties not match, that is a change
188 if not mo.check_prop_match(**kwargs):
189 changed = True
190
191 # Object does not exist but should, that is a change
192 else:
193 if requested_state == 'present':
194 changed = True
195
196 # Object exists but should not, that is a change
197 if mo and requested_state == 'absent':
198 changed = True
199
200 # Apply state if not check_mode
201 if changed and not module.check_mode:
202 if requested_state == 'absent':
203 ucs.login_handle.remove_mo(mo)
204 else:
205 kwargs['parent_mo_or_dn'] = parent_org_dn
206 kwargs['name'] = module.params['org_name']
207 if module.params['description'] is not None:
208 kwargs['descr'] = module.params['description']
209
210 mo = OrgOrg(**kwargs)
211 ucs.login_handle.add_mo(mo, modify_present=True)
212 ucs.login_handle.commit()
213
214 except Exception as e:
215 err = True
216 ucs.result['msg'] = "setup error: %s " % str(e)
217
218 ucs.result['changed'] = changed
219 if err:
220 module.fail_json(**ucs.result)
221
222 module.exit_json(**ucs.result)
223
224
225 if __name__ == '__main__':
226 main()