Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_san_connectivity.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 __metaclass__ = type
8
9 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'certified'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: ucs_san_connectivity
16 short_description: Configures SAN Connectivity Policies on Cisco UCS Manager
17 description:
18 - Configures SAN Connectivity Policies on Cisco UCS Manager.
19 extends_documentation_fragment: cisco.ucs.ucs
20 options:
21 state:
22 description:
23 - If C(present), will verify SAN Connectivity Policies are present and will create if needed.
24 - If C(absent), will verify SAN Connectivity Policies are absent and will delete if needed.
25 choices: [present, absent]
26 default: present
27 type: str
28 name:
29 description:
30 - The name of the SAN Connectivity Policy.
31 - This name can be between 1 and 16 alphanumeric characters.
32 - "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)."
33 - You cannot change this name after the policy is created.
34 type: str
35 description:
36 description:
37 - A description of the policy.
38 - Cisco recommends including information about where and when to use the policy.
39 - Enter up to 256 characters.
40 - "You can use any characters or spaces except the following:"
41 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
42 aliases: [ descr ]
43 type: str
44 wwnn_pool:
45 description:
46 - Name of the WWNN pool to use for WWNN assignment.
47 default: default
48 type: str
49 vhba_list:
50 description:
51 - List of vHBAs used by the SAN Connectivity Policy.
52 - vHBAs used by the SAN Connectivity Policy must be created from a vHBA template.
53 type: list
54 elements: dict
55 suboptions:
56 name:
57 description:
58 - The name of the virtual HBA (required).
59 type: str
60 vhba_template:
61 description:
62 - The name of the virtual HBA template (required).
63 type: str
64 adapter_policy:
65 description:
66 - The name of the Fibre Channel adapter policy.
67 - A user defined policy can be used, or one of the system defined policies (default, Linux, Solaris, VMware, Windows, WindowsBoot)
68 default: default
69 type: str
70 order:
71 description:
72 - String specifying the vHBA assignment order (e.g., '1', '2').
73 default: unspecified
74 type: str
75 org_dn:
76 description:
77 - Org dn (distinguished name)
78 default: org-root
79 type: str
80 requirements:
81 - ucsmsdk
82 author:
83 - David Soper (@dsoper2)
84 - John McDonough (@movinalot)
85 - CiscoUcs (@CiscoUcs)
86 '''
87
88 EXAMPLES = r'''
89 - name: Configure SAN Connectivity Policy
90 cisco.ucs.ucs_san_connectivity:
91 hostname: 172.16.143.150
92 username: admin
93 password: password
94 name: Cntr-FC-Boot
95 wwnn_pool: WWNN-Pool
96 vhba_list:
97 - name: Fabric-A
98 vhba_template: vHBA-Template-A
99 adapter_policy: Linux
100 - name: Fabric-B
101 vhba_template: vHBA-Template-B
102 adapter_policy: Linux
103
104 - name: Remove SAN Connectivity Policy
105 cisco.ucs.ucs_san_connectivity:
106 hostname: 172.16.143.150
107 username: admin
108 password: password
109 name: Cntr-FC-Boot
110 state: absent
111 '''
112
113 RETURN = r'''
114 #
115 '''
116
117 from ansible.module_utils.basic import AnsibleModule
118 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
119
120
121 def main():
122 vhba_list = dict(
123 name=dict(type='str'),
124 vhba_template=dict(type='str'),
125 adapter_policy=dict(type='str', default='default'),
126 order=dict(type='str', default='unspecified'),
127 )
128 argument_spec = ucs_argument_spec.copy()
129 argument_spec.update(
130 org_dn=dict(type='str', default='org-root'),
131 name=dict(type='str'),
132 description=dict(type='str', aliases=['descr']),
133 wwnn_pool=dict(type='str', default='default'),
134 vhba_list=dict(type='list', elements='dict', options=vhba_list),
135 state=dict(type='str', default='present', choices=['present', 'absent']),
136 )
137
138 module = AnsibleModule(
139 argument_spec,
140 supports_check_mode=True,
141 )
142 ucs = UCSModule(module)
143
144 err = False
145
146 from ucsmsdk.mometa.vnic.VnicSanConnPolicy import VnicSanConnPolicy
147 from ucsmsdk.mometa.vnic.VnicFcNode import VnicFcNode
148 from ucsmsdk.mometa.vnic.VnicFc import VnicFc
149 from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf
150
151 changed = False
152 try:
153 san_connectivity_list = [module.params]
154 for san_connectivity in san_connectivity_list:
155 mo_exists = False
156 props_match = False
157 # set default params. Done here to set values for lists which can't be done in the argument_spec
158 if not san_connectivity.get('wwnn_pool'):
159 san_connectivity['wwnn_pool'] = 'default'
160 if san_connectivity.get('vhba_list'):
161 for vhba in san_connectivity['vhba_list']:
162 if not vhba.get('adapter_policy'):
163 vhba['adapter_policy'] = ''
164 if not vhba.get('order'):
165 vhba['order'] = 'unspecified'
166 # dn is <org_dn>/san-conn-pol-<name>
167 dn = module.params['org_dn'] + '/san-conn-pol-' + san_connectivity['name']
168
169 mo = ucs.login_handle.query_dn(dn)
170 if mo:
171 mo_exists = True
172 # check top-level mo props
173 kwargs = dict(descr=san_connectivity['description'])
174 if (mo.check_prop_match(**kwargs)):
175 # top-level props match, check next level mo/props
176 # vnicFcNode object
177 child_dn = dn + '/fc-node'
178 mo_1 = ucs.login_handle.query_dn(child_dn)
179 if mo_1:
180 kwargs = dict(ident_pool_name=san_connectivity['wwnn_pool'])
181 if (mo_1.check_prop_match(**kwargs)):
182 if not san_connectivity.get('vhba_list'):
183 props_match = True
184 else:
185 # check vnicFc props
186 for vhba in san_connectivity['vhba_list']:
187 child_dn = dn + '/fc-' + vhba['name']
188 mo_2 = ucs.login_handle.query_dn(child_dn)
189 kwargs = {}
190 kwargs['adaptor_profile_name'] = vhba['adapter_policy']
191 kwargs['order'] = vhba['order']
192 kwargs['nw_templ_name'] = vhba['vhba_template']
193 if (mo_2.check_prop_match(**kwargs)):
194 props_match = True
195
196 if module.params['state'] == 'absent':
197 # mo must exist but all properties do not have to match
198 if mo_exists:
199 if not module.check_mode:
200 ucs.login_handle.remove_mo(mo)
201 ucs.login_handle.commit()
202 changed = True
203 else:
204 if not props_match:
205 if not module.check_mode:
206 # create if mo does not already exist
207 mo = VnicSanConnPolicy(
208 parent_mo_or_dn=module.params['org_dn'],
209 name=san_connectivity['name'],
210 descr=san_connectivity['description'],
211 )
212 mo_1 = VnicFcNode(
213 parent_mo_or_dn=mo,
214 ident_pool_name=san_connectivity['wwnn_pool'],
215 addr='pool-derived',
216 )
217 if san_connectivity.get('vhba_list'):
218 for vhba in san_connectivity['vhba_list']:
219 mo_2 = VnicFc(
220 parent_mo_or_dn=mo,
221 name=vhba['name'],
222 adaptor_profile_name=vhba['adapter_policy'],
223 nw_templ_name=vhba['vhba_template'],
224 order=vhba['order'],
225 )
226 mo_2_1 = VnicFcIf(
227 parent_mo_or_dn=mo_2,
228 name='default',
229 )
230
231 ucs.login_handle.add_mo(mo, True)
232 ucs.login_handle.commit()
233 changed = True
234
235 except Exception as e:
236 err = True
237 ucs.result['msg'] = "setup error: %s " % str(e)
238
239 ucs.result['changed'] = changed
240 if err:
241 module.fail_json(**ucs.result)
242 module.exit_json(**ucs.result)
243
244
245 if __name__ == '__main__':
246 main()