Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_capacityreservationgroup.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_capacityreservationgroup
14 version_added: "2.4.0"
15 short_description: Manage Capacity Reservation Group
16 description:
17 - Create, update or delete the Capacity Reservation Group.
18 options:
19 resource_group:
20 description:
21 - Name of resource group.
22 required: true
23 type: str
24 location:
25 description:
26 - Valid Azure location. Defaults to location of the resource group.
27 type: str
28 name:
29 description:
30 - The name of the Capacity Reservation Group.
31 required: true
32 type: str
33 zones:
34 description:
35 - Availability Zones to use for this capacity reservation group.
36 - The zones can be assigned only during creation.
37 - If not provided, the group supports only regional resources in the region.
38 - If provided, enforces each capacity reservation in the group to be in one of the zones.
39 type: list
40 elements: str
41 choices:
42 - '1'
43 - '2'
44 - '3'
45 state:
46 description:
47 - State of the Capacity Reservation Group. Use C(present) to create or update and C(absent) to delete.
48 default: present
49 type: str
50 choices:
51 - absent
52 - present
53
54 extends_documentation_fragment:
55 - azure.azcollection.azure
56 - azure.azcollection.azure_tags
57
58 author:
59 - xuzhang3 (@xuzhang3)
60 - Fred-sun (@Fred-sun)
61
62 '''
63
64 EXAMPLES = '''
65 - name: Create a Capacity Reservation Group
66 azure_rm_capacityreservationgroup:
67 resource_group: myResourceGroup
68 name: testname01
69 zones:
70 - 1
71 - 2
72 tags:
73 key: value
74
75 - name: Delete the Capacity Reservation Group
76 azure_rm_capacityreservationgroup:
77 resource_group: myResourceGroup
78 name: testname01
79 state: absent
80 '''
81 RETURN = '''
82 state:
83 description:
84 - Current state of the Capacity Reservation Group.
85 returned: always
86 type: complex
87 contains:
88 id:
89 description:
90 - Resource ID.
91 returned: always
92 type: str
93 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Compute/capacityReservationGroups/testname01"
94 location:
95 description:
96 - The Geo-location where the resource lives.
97 returned: always
98 type: str
99 sample: eastus
100 name:
101 description:
102 - Resource name.
103 returned: always
104 type: str
105 sample: testname01
106 resource_group:
107 description:
108 - Name of resource group.
109 type: str
110 returned: always
111 sample: myResourceGroup
112 tags:
113 description:
114 - Resource tags.
115 returned: always
116 type: dict
117 sample: { 'key': 'value' }
118 zones:
119 description:
120 - A list of all capacity reservation resource ids that belong to capacity reservation group.
121 returned: always
122 type: list
123 sample: ['1', '2']
124 '''
125
126 try:
127 from azure.core.exceptions import ResourceNotFoundError
128 except ImportError:
129 # This is handled in azure_rm_common
130 pass
131
132 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
133
134
135 class AzureRMCapacityReservationGroup(AzureRMModuleBase):
136
137 def __init__(self):
138
139 self.module_arg_spec = dict(
140 resource_group=dict(type='str', required=True),
141 name=dict(type='str', required=True),
142 state=dict(type='str', default='present', choices=['present', 'absent']),
143 location=dict(type='str'),
144 zones=dict(type='list', elements='str', choices=['1', '2', '3']),
145 )
146
147 self.resource_group = None
148 self.name = None
149 self.state = None
150 self.location = None
151 self.zones = None
152
153 self.body = dict()
154
155 self.results = dict(
156 changed=False,
157 state=dict()
158 )
159
160 super(AzureRMCapacityReservationGroup, self).__init__(self.module_arg_spec,
161 supports_tags=True,
162 supports_check_mode=True)
163
164 def exec_module(self, **kwargs):
165
166 for key in list(self.module_arg_spec.keys()) + ['tags']:
167 setattr(self, key, kwargs[key])
168 for key in ['tags', 'zones']:
169 self.body[key] = kwargs[key]
170
171 old_response = self.get_by_name()
172
173 changed = False
174 results = None
175
176 if old_response is not None:
177 if self.state == 'present':
178 update_tags, self.body['tags'] = self.update_tags(old_response['tags'])
179 if self.location is not None and self.location.lower() != old_response.get('location').lower():
180 self.fail('The parameters location not support to udpate')
181 elif self.body.get('zones') is not None and not all(key in old_response['zones'] for key in self.body[key]):
182 self.fail('The parameters zones not support to udpate')
183 elif update_tags:
184 changed = True
185 if not self.check_mode:
186 results = self.update_capacity_reservation_groups(dict(tags=self.body['tags']))
187 else:
188 results = old_response
189 else:
190 changed = True
191 if not self.check_mode:
192 results = self.delete_capacity_reservation_groups()
193 else:
194 resource_group = self.get_resource_group(self.resource_group)
195 if self.location is None:
196 # Set default location
197 self.body['location'] = resource_group.location
198
199 if self.state == 'present':
200 changed = True
201 if not self.check_mode:
202 results = self.create_capacity_reservation_groups(self.body)
203 else:
204 changed = False
205 self.log("The Capacity Reservation Group is not exists")
206
207 self.results['changed'] = changed
208 self.results['state'] = results
209
210 return self.results
211
212 def get_by_name(self):
213 response = None
214 try:
215 response = self.compute_client.capacity_reservation_groups.get(self.resource_group, self.name)
216
217 except ResourceNotFoundError as exec:
218 self.log("Failed to get ssh public keys, Exception as {0}".format(exec))
219
220 return self.to_dict(response)
221
222 def create_capacity_reservation_groups(self, body):
223 response = None
224 try:
225 response = self.to_dict(self.compute_client.capacity_reservation_groups.create_or_update(self.resource_group, self.name, body))
226 except Exception as exc:
227 self.fail("Error creating Capacity Reservation Group {0} - {1}".format(self.name, str(exc)))
228
229 return self.to_dict(response)
230
231 def update_capacity_reservation_groups(self, body):
232 response = None
233 try:
234 response = self.compute_client.capacity_reservation_groups.update(self.resource_group, self.name, body)
235 except Exception as exc:
236 self.fail("Error updating Capacity Reservation Group {0} - {1}".format(self.name, str(exc)))
237 return self.to_dict(response)
238
239 def delete_capacity_reservation_groups(self):
240 try:
241 self.compute_client.capacity_reservation_groups.delete(self.resource_group, self.name)
242 except Exception as exc:
243 if self.get_by_name() is not None:
244 self.fail("Error deleting Capacity Reservation Group {0} - {1}".format(self.name, str(exc)))
245
246 def to_dict(self, body):
247 if body is None:
248 results = None
249 elif isinstance(body, dict):
250 results = dict(
251 resource_group=self.resource_group,
252 id=body.get('id'),
253 name=body.get('name'),
254 location=body.get('location'),
255 zones=body.get('zones'),
256 tags=body.get('tags'))
257 else:
258 results = dict(
259 resource_group=self.resource_group,
260 id=body.id,
261 name=body.name,
262 location=body.location,
263 zones=body.zones,
264 tags=body.tags)
265 return results
266
267
268 def main():
269 AzureRMCapacityReservationGroup()
270
271
272 if __name__ == '__main__':
273 main()