Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_storage_profile.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_storage_profile
16 short_description: Configures storage profiles on Cisco UCS Manager
17 description:
18 - Configures storage profiles on Cisco UCS Manager.
19 extends_documentation_fragment: cisco.ucs.ucs
20 options:
21 state:
22 description:
23 - If C(present), will verify that the storage profile is present and will create if needed.
24 - If C(absent), will verify that the storage profile is absent and will delete if needed.
25 choices: [ absent, present ]
26 default: present
27 type: str
28 name:
29 description:
30 - The name of the storage profile.
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 profile is created.
34 required: yes
35 type: str
36 description:
37 description:
38 - The user-defined description of the storage profile.
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 local_luns:
45 description:
46 - List of Local LUNs used by the storage profile.
47 type: list
48 elements: dict
49 suboptions:
50 name:
51 description:
52 - The name of the local LUN.
53 required: yes
54 type: str
55 size:
56 description:
57 - Size of this LUN in GB.
58 - The size can range from 1 to 10240 GB.
59 default: '1'
60 type: str
61 auto_deploy:
62 description:
63 - Whether the local LUN should be automatically deployed or not.
64 choices: [ auto-deploy, no-auto-deploy ]
65 default: auto-deploy
66 type: str
67 expand_to_avail:
68 description:
69 - Specifies that this LUN can be expanded to use the entire available disk group.
70 - For each service profile, only one LUN can use this option.
71 - Expand To Available option is not supported for already deployed LUN.
72 choices: ['no', 'yes']
73 default: 'no'
74 type: str
75 fractional_size:
76 description:
77 - Fractional size of this LUN in MB.
78 default: '0'
79 type: str
80 disk_policy_name:
81 description:
82 - The disk group configuration policy to be applied to this local LUN.
83 type: str
84 state:
85 description:
86 - If C(present), will verify local LUN is present on profile.
87 If C(absent), will verify local LUN is absent on profile.
88 choices: [ absent, present ]
89 default: present
90 type: str
91 org_dn:
92 description:
93 - The distinguished name (dn) of the organization where the resource is assigned.
94 default: org-root
95 type: str
96 requirements:
97 - ucsmsdk
98 author:
99 - Sindhu Sudhir (@sisudhir)
100 - David Soper (@dsoper2)
101 - John McDonough (@movinalot)
102 - CiscoUcs (@CiscoUcs)
103 '''
104
105 EXAMPLES = r'''
106 - name: Configure Storage Profile
107 cisco.ucs.ucs_storage_profile:
108 hostname: 172.16.143.150
109 username: admin
110 password: password
111 name: DEE-StgProf
112 local_luns:
113 - name: Boot-LUN
114 size: '60'
115 disk_policy_name: DEE-DG
116 - name: Data-LUN
117 size: '200'
118 disk_policy_name: DEE-DG
119
120 - name: Remove Storage Profile
121 cisco.ucs.ucs_storage_profile:
122 hostname: 172.16.143.150
123 username: admin
124 password: password
125 name: DEE-StgProf
126 state: absent
127
128 - name: Remove Local LUN from Storage Profile
129 cisco.ucs.ucs_storage_profile:
130 hostname: 172.16.143.150
131 username: admin
132 password: password
133 name: DEE-StgProf
134 local_luns:
135 - name: Data-LUN
136 state: absent
137 '''
138
139 RETURN = r'''
140 #
141 '''
142
143 from ansible.module_utils.basic import AnsibleModule
144 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
145
146
147 def main():
148 local_lun = dict(
149 name=dict(type='str', required=True),
150 state=dict(type='str', default='present', choices=['present', 'absent']),
151 size=dict(type='str', default='1'),
152 auto_deploy=dict(type='str', default='auto-deploy', choices=['auto-deploy', 'no-auto-deploy']),
153 expand_to_avail=dict(type='str', default='no', choices=['no', 'yes']),
154 fractional_size=dict(type='str', default='0'),
155 disk_policy_name=dict(type='str'),
156 )
157 argument_spec = ucs_argument_spec.copy()
158 argument_spec.update(
159 org_dn=dict(type='str', default='org-root'),
160 name=dict(type='str', required=True),
161 description=dict(type='str', aliases=['descr']),
162 local_luns=dict(type='list', elements='dict', options=local_lun),
163 state=dict(type='str', default='present', choices=['present', 'absent']),
164 )
165
166 module = AnsibleModule(
167 argument_spec,
168 supports_check_mode=True,
169 )
170 ucs = UCSModule(module)
171
172 err = False
173
174 # UCSModule creation above verifies ucsmsdk is present and exits on failure. Additional imports are done below.
175 from ucsmsdk.mometa.lstorage.LstorageProfile import LstorageProfile
176 from ucsmsdk.mometa.lstorage.LstorageDasScsiLun import LstorageDasScsiLun
177
178 ucs.result['changed'] = False
179 try:
180 mo_exists = False
181 props_match = False
182 # dn is <org_dn>/profile-<name>
183 dn = module.params['org_dn'] + '/profile-' + module.params['name']
184
185 mo = ucs.login_handle.query_dn(dn)
186 if mo:
187 mo_exists = True
188
189 if module.params['state'] == 'absent':
190 # mo must exist but all properties do not have to match
191 if mo_exists:
192 if not module.check_mode:
193 ucs.login_handle.remove_mo(mo)
194 ucs.login_handle.commit()
195 ucs.result['changed'] = True
196 else:
197 if mo_exists:
198 # check top-level mo props
199 kwargs = dict(descr=module.params['description'])
200 if mo.check_prop_match(**kwargs):
201 # top-level props match, check next level mo/props
202 if not module.params.get('local_luns'):
203 props_match = True
204 else:
205 # check local lun props
206 for lun in module.params['local_luns']:
207 child_dn = dn + '/das-scsi-lun-' + lun['name']
208 mo_1 = ucs.login_handle.query_dn(child_dn)
209 if lun['state'] == 'absent':
210 if mo_1:
211 props_match = False
212 break
213 else:
214 if mo_1:
215 kwargs = dict(size=str(lun['size']))
216 kwargs['auto_deploy'] = lun['auto_deploy']
217 kwargs['expand_to_avail'] = lun['expand_to_avail']
218 kwargs['fractional_size'] = str(lun['fractional_size'])
219 kwargs['local_disk_policy_name'] = lun['disk_policy_name']
220 if mo_1.check_prop_match(**kwargs):
221 props_match = True
222 else:
223 props_match = False
224 break
225
226 if not props_match:
227 if not module.check_mode:
228 # create if mo does not already exist
229 mo = LstorageProfile(
230 parent_mo_or_dn=module.params['org_dn'],
231 name=module.params['name'],
232 descr=module.params['description'],
233 )
234
235 if module.params.get('local_luns'):
236 for lun in module.params['local_luns']:
237 if lun['state'] == 'absent':
238 child_dn = dn + '/das-scsi-lun-' + lun['name']
239 mo_1 = ucs.login_handle.query_dn(child_dn)
240 ucs.login_handle.remove_mo(mo_1)
241 else:
242 mo_1 = LstorageDasScsiLun(
243 parent_mo_or_dn=mo,
244 name=lun['name'],
245 size=str(lun['size']),
246 auto_deploy=lun['auto_deploy'],
247 expand_to_avail=lun['expand_to_avail'],
248 fractional_size=str(lun['fractional_size']),
249 local_disk_policy_name=lun['disk_policy_name'],
250 )
251
252 ucs.login_handle.add_mo(mo, True)
253 ucs.login_handle.commit()
254 ucs.result['changed'] = True
255
256 except Exception as e:
257 err = True
258 ucs.result['msg'] = "setup error: %s " % str(e)
259
260 if err:
261 module.fail_json(**ucs.result)
262 module.exit_json(**ucs.result)
263
264
265 if __name__ == '__main__':
266 main()