Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_capacityreservationgroup_info.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_info
14
15 version_added: "2.4.0"
16
17 short_description: Get or list the capacity reservation group
18
19 description:
20 - Get or list the capacity reservation group.
21
22 options:
23 resource_group:
24 description:
25 - Name of the resource group.
26 type: str
27 name:
28 description:
29 - Name of the capacity reservation group.
30 type: str
31 tags:
32 description:
33 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
34 type: list
35 elements: str
36
37 extends_documentation_fragment:
38 - azure.azcollection.azure
39
40 author:
41 - xuzhang3 (@xuzhang3)
42 - Fred-sun (@Fred-sun)
43
44 '''
45
46 EXAMPLES = '''
47 - name: Get facts of the capacity reservation group by name
48 azure_rm_capacityreservationgroup_info:
49 resource_group: myResourceGroup
50 name: mycapacityreservationgroup
51
52 - name: List facts of the capacity reservation group by resource group
53 azure_rm_capacityreservationgroup_info:
54 resource_group: myResourceGroup
55
56 - name: List facts of the capacity reservation group by subscription and filter by tags
57 azure_rm_capacityreservationgroup_info:
58 tags:
59 - testing
60 - foo:bar
61 '''
62
63 RETURN = '''
64 capacity_reservation_group:
65 description:
66 - Current state of the Capacity Reservation Group.
67 returned: always
68 type: complex
69 contains:
70 id:
71 description:
72 - Resource ID.
73 returned: always
74 type: str
75 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Compute/capacityReservationGroups/testname01"
76 location:
77 description:
78 - The Geo-location where the resource lives.
79 returned: always
80 type: str
81 sample: eastus
82 name:
83 description:
84 - Resource name.
85 returned: always
86 type: str
87 sample: testname01
88 resource_group:
89 description:
90 - Name of resource group.
91 type: str
92 returned: always
93 sample: myResourceGroup
94 tags:
95 description:
96 - Resource tags.
97 returned: always
98 type: dict
99 sample: { 'key': 'value' }
100 zones:
101 description:
102 - A list of all capacity reservation resource ids that belong to capacity reservation group.
103 returned: always
104 type: list
105 sample: ['1', '2']
106 type:
107 description:
108 - The resource type.
109 type: str
110 returned: always
111 sample: "Microsoft.Compute/capacityReservationGroups/capacityReservations"
112 '''
113
114 try:
115 from azure.core.exceptions import HttpResponseError
116 except Exception:
117 # This is handled in azure_rm_common
118 pass
119
120 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
121
122
123 class AzureRMCapacityReservationGroupInfo(AzureRMModuleBase):
124
125 def __init__(self):
126
127 self.module_arg_spec = dict(
128 resource_group=dict(type='str'),
129 name=dict(type='str'),
130 tags=dict(type='list', elements='str')
131 )
132
133 self.results = dict(
134 changed=False,
135 capacity_reservation_groups=[]
136 )
137
138 self.resource_group = None
139 self.name = None
140 self.tags = None
141
142 super(AzureRMCapacityReservationGroupInfo, self).__init__(self.module_arg_spec,
143 supports_check_mode=True,
144 supports_tags=False,
145 facts_module=True)
146
147 def exec_module(self, **kwargs):
148 for key in self.module_arg_spec:
149 setattr(self, key, kwargs[key])
150
151 if self.name and self.resource_group:
152 response = [self.get_by_name()]
153 elif self.resource_group:
154 response = self.list_by_resourcegroup()
155 else:
156 response = self.list_all()
157
158 for item in response:
159 if item is not None and self.has_tags(item.tags, self.tags):
160 self.results['capacity_reservation_groups'].append(self.to_dict(item))
161
162 return self.results
163
164 def get_by_name(self):
165 response = None
166 try:
167 response = self.compute_client.capacity_reservation_groups.get(self.resource_group, self.name)
168
169 except HttpResponseError as exec:
170 self.log("Failed to retrieves information about a capacity reservation group, Exception as {0}".format(exec))
171
172 return response
173
174 def list_by_resourcegroup(self):
175 response = None
176 try:
177 response = self.compute_client.capacity_reservation_groups.list_by_resource_group(self.resource_group)
178 except HttpResponseError as exec:
179 self.log("Faild to list ssh public keys by resource group, exception as {0}".format(exec))
180 return response
181
182 def list_all(self):
183 response = None
184 try:
185 response = self.compute_client.capacity_reservation_groups.list_by_subscription()
186 except HttpResponseError as exc:
187 self.fail("Failed to list all items - {0}".format(str(exc)))
188
189 return response
190
191 def to_dict(self, body):
192 return dict(
193 id=body.id,
194 resource_group=self.resource_group,
195 name=body.name,
196 location=body.location,
197 tags=body.tags,
198 type=body.type,
199 zones=body.zones,
200 )
201
202
203 def main():
204 AzureRMCapacityReservationGroupInfo()
205
206
207 if __name__ == '__main__':
208 main()