Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_chassis_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': 'community'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: intersight_chassis_profile
16 short_description: Manage Chassis Profiles in Cisco Intersight
17 description:
18 - Create, update, and delete Chassis Profiles on Cisco Intersight.
19 - For more information see L(Cisco Intersight,https://intersight.com/apidocs/chassis/Profile/post/).
20 - This module does not manage deployment of the chassis profile.
21 extends_documentation_fragment: intersight
22 options:
23 state:
24 description:
25 - If C(present), will verify the resource is present and will create if needed.
26 - If C(absent), will verify the resource is absent and will delete if needed.
27 type: str
28 choices: [present, absent]
29 default: present
30 organization:
31 description:
32 - The name of the Organization this resource is assigned to.
33 - Profiles and Policies that are created within a Custom Organization are applicable only to devices in the same Organization.
34 type: str
35 default: default
36 name:
37 description:
38 - The name assigned to the Chassis Profile.
39 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
40 type: str
41 required: true
42 description:
43 description:
44 - The user-defined description of the Chassis Profile.
45 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
46 type: str
47 aliases: [descr]
48 tags:
49 description:
50 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
51 type: list
52 elements: dict
53 assigned_chassis_name:
54 description:
55 - The name of the chassis to assign to this profile.
56 - The module resolves the chassis name to a MOID via the C(/equipment/Chasses) API endpoint.
57 - If not provided, the profile will be created without a chassis assignment.
58 type: str
59 imc_access_policy:
60 description:
61 - Name of IMC Access Policy to associate with this profile.
62 type: str
63 power_policy:
64 description:
65 - Name of Power Policy to associate with this profile.
66 type: str
67 snmp_policy:
68 description:
69 - Name of SNMP Policy to associate with this profile.
70 type: str
71 thermal_policy:
72 description:
73 - Name of Thermal Policy to associate with this profile.
74 type: str
75 author:
76 - Ron Gershburg (@rgershbu)
77 '''
78
79 EXAMPLES = r'''
80 - name: Create a basic Chassis Profile
81 cisco.intersight.intersight_chassis_profile:
82 api_private_key: "{{ api_private_key }}"
83 api_key_id: "{{ api_key_id }}"
84 organization: "default"
85 name: "Chassis-Profile-01"
86 description: "Basic chassis profile"
87 tags:
88 - Key: "Environment"
89 Value: "Production"
90 state: present
91
92 - name: Create a Chassis Profile with chassis assignment and policies
93 cisco.intersight.intersight_chassis_profile:
94 api_private_key: "{{ api_private_key }}"
95 api_key_id: "{{ api_key_id }}"
96 organization: "default"
97 name: "Chassis-Profile-02"
98 description: "Chassis profile with assignment and policies"
99 assigned_chassis_name: "FOX2534P0GG"
100 imc_access_policy: "IMC-Access-Prod"
101 power_policy: "Power-Grid-Redundancy"
102 snmp_policy: "SNMP-Monitoring"
103 thermal_policy: "Thermal-Balanced"
104 state: present
105
106 - name: Delete a Chassis Profile
107 cisco.intersight.intersight_chassis_profile:
108 api_private_key: "{{ api_private_key }}"
109 api_key_id: "{{ api_key_id }}"
110 name: "Chassis-Profile-01"
111 state: absent
112 '''
113
114 RETURN = r'''
115 api_response:
116 description: The API response output returned by the specified resource.
117 returned: always
118 type: dict
119 sample:
120 "api_response": {
121 "Name": "Chassis-Profile-02",
122 "ObjectType": "chassis.Profile",
123 "AssignedChassis": {
124 "Moid": "67e3ebbd61767530012e5a93",
125 "ObjectType": "equipment.Chassis"
126 },
127 "PolicyBucket": [
128 {
129 "Moid": "69b2af9c6275723101974d23",
130 "ObjectType": "access.Policy"
131 },
132 {
133 "Moid": "69b2af9c6275723101974d24",
134 "ObjectType": "power.Policy"
135 }
136 ],
137 "Tags": [
138 {
139 "Key": "Environment",
140 "Value": "Production"
141 }
142 ]
143 }
144 '''
145
146
147 from ansible.module_utils.basic import AnsibleModule
148 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import (
149 IntersightModule,
150 intersight_argument_spec,
151 resolve_policy_bucket,
152 sync_policy_bucket,
153 )
154
155
156 POLICY_MAPPING = {
157 'imc_access_policy': {'resource_path': '/access/Policies', 'object_type': 'access.Policy'},
158 'power_policy': {'resource_path': '/power/Policies', 'object_type': 'power.Policy'},
159 'snmp_policy': {'resource_path': '/snmp/Policies', 'object_type': 'snmp.Policy'},
160 'thermal_policy': {'resource_path': '/thermal/Policies', 'object_type': 'thermal.Policy'},
161 }
162
163
164 def resolve_assigned_chassis(intersight, chassis_name):
165 """Resolve a chassis name to its MOID via the equipment/Chasses endpoint."""
166 chassis_moid = intersight.get_moid_by_name(
167 resource_path='/equipment/Chasses',
168 resource_name=chassis_name,
169 )
170 if not chassis_moid:
171 intersight.module.fail_json(
172 msg=f"Chassis '{chassis_name}' not found. Verify the chassis name via GET /equipment/Chasses."
173 )
174 return chassis_moid
175
176
177 def unassign_chassis_if_needed(intersight, resource_path, profile_name, organization_moid):
178 """Unassign the chassis from a profile before deletion.
179
180 The Intersight API forbids deleting a chassis profile while a chassis is
181 associated with it. This function checks for an existing assignment and
182 PATCHes AssignedChassis to null when one is found.
183 """
184 filter_str = f"Name eq '{profile_name}' and Organization.Moid eq '{organization_moid}'"
185 intersight.get_resource(
186 resource_path=resource_path,
187 query_params={'$filter': filter_str},
188 )
189
190 profile = intersight.result['api_response']
191 if not profile.get('Moid'):
192 return
193
194 assigned = profile.get('AssignedChassis')
195 if assigned and assigned.get('Moid'):
196 intersight.configure_resource(
197 moid=profile['Moid'],
198 resource_path=resource_path,
199 body={'AssignedChassis': None},
200 query_params={},
201 )
202
203
204 def main():
205 argument_spec = intersight_argument_spec.copy()
206 argument_spec.update(
207 state=dict(type='str', choices=['present', 'absent'], default='present'),
208 organization=dict(type='str', default='default'),
209 name=dict(type='str', required=True),
210 description=dict(type='str', aliases=['descr']),
211 tags=dict(type='list', elements='dict'),
212 assigned_chassis_name=dict(type='str'),
213 imc_access_policy=dict(type='str'),
214 power_policy=dict(type='str'),
215 snmp_policy=dict(type='str'),
216 thermal_policy=dict(type='str'),
217 )
218
219 module = AnsibleModule(
220 argument_spec,
221 supports_check_mode=True,
222 )
223
224 intersight = IntersightModule(module)
225 intersight.result['api_response'] = {}
226 intersight.result['trace_id'] = ''
227
228 resource_path = '/chassis/Profiles'
229
230 intersight.api_body = {
231 'Organization': {
232 'Name': intersight.module.params['organization'],
233 },
234 'Name': intersight.module.params['name'],
235 }
236
237 if intersight.module.params['state'] == 'present':
238 intersight.set_tags_and_description()
239
240 chassis_name = intersight.module.params.get('assigned_chassis_name')
241 if chassis_name:
242 chassis_moid = resolve_assigned_chassis(intersight, chassis_name)
243 intersight.api_body['AssignedChassis'] = {
244 'Moid': chassis_moid,
245 'ObjectType': 'equipment.Chassis',
246 }
247
248 if intersight.module.params['state'] == 'absent':
249 organization_moid = intersight.get_moid_by_name(
250 resource_path='/organization/Organizations',
251 resource_name=intersight.module.params['organization'],
252 )
253 if organization_moid:
254 unassign_chassis_if_needed(intersight, resource_path, intersight.module.params['name'], organization_moid)
255
256 intersight.configure_policy_or_profile(resource_path=resource_path)
257 profile_response = intersight.result['api_response']
258 profile_moid = profile_response.get('Moid') if profile_response else None
259
260 if profile_moid and intersight.module.params['state'] == 'present':
261 organization_name = intersight.module.params['organization']
262 desired_bucket = resolve_policy_bucket(intersight, organization_name, POLICY_MAPPING)
263 current_bucket = profile_response.get('PolicyBucket') or []
264 bucket_path = f'/chassis/Profiles/{profile_moid}/PolicyBucket'
265 sync_policy_bucket(intersight, bucket_path, desired_bucket, current_bucket)
266
267 module.exit_json(**intersight.result)
268
269
270 if __name__ == '__main__':
271 main()