Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2016 Matt Davis, <[email protected]>
4 # Chris Houseknecht, <[email protected]>
5 #
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = '''
13 ---
14 module: azure_rm_resourcegroup_info
15
16 version_added: "0.1.2"
17
18 short_description: Get resource group facts
19
20 description:
21 - Get facts for a specific resource group or all resource groups.
22
23 options:
24 name:
25 description:
26 - Limit results to a specific resource group.
27 type: str
28 tags:
29 description:
30 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
31 type: list
32 elements: str
33 list_resources:
34 description:
35 - List all resources under the resource group.
36 - Note this will cost network overhead for each resource group. Suggest use this when I(name) set.
37 type: bool
38
39
40 extends_documentation_fragment:
41 - azure.azcollection.azure
42
43 author:
44 - Chris Houseknecht (@chouseknecht)
45 - Matt Davis (@nitzmahone)
46
47 '''
48
49 EXAMPLES = '''
50 - name: Get facts for one resource group
51 azure_rm_resourcegroup_info:
52 name: myResourceGroup
53
54 - name: Get facts for all resource groups
55 azure_rm_resourcegroup_info:
56
57 - name: Get facts by tags
58 azure_rm_resourcegroup_info:
59 tags:
60 - testing
61 - foo:bar
62
63 - name: Get facts for one resource group including resources it contains
64 azure_rm_resourcegroup_info:
65 name: myResourceGroup
66 list_resources: true
67 '''
68 RETURN = '''
69 resourcegroups:
70 description:
71 - List of resource group dicts.
72 returned: always
73 type: list
74 contains:
75 id:
76 description:
77 - Resource id.
78 returned: always
79 type: str
80 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup"
81 name:
82 description:
83 - Resource group name.
84 returned: always
85 type: str
86 sample: foo
87 tags:
88 description:
89 - Tags assigned to resource group.
90 returned: always
91 type: dict
92 sample: { "tag": "value" }
93 resources:
94 description:
95 - List of resources under the resource group.
96 returned: when I(list_resources=yes).
97 type: list
98 contains:
99 id:
100 description:
101 - Resource id.
102 returned: always
103 type: str
104 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMa
105 chines/myVirtualMachine"
106 name:
107 description:
108 - Resource name.
109 returned: always
110 type: str
111 sample: myVirtualMachine
112 location:
113 description:
114 - Resource region.
115 returned: always
116 type: str
117 sample: eastus
118 type:
119 description:
120 - Resource type.
121 returned: always
122 type: str
123 sample: "Microsoft.Compute/virtualMachines"
124 tags:
125 description:
126 - Tags to assign to the managed disk.
127 returned: always
128 type: dict
129 sample: { "tag": "value" }
130 '''
131
132 try:
133 from azure.core.exceptions import ResourceNotFoundError
134 except Exception:
135 # This is handled in azure_rm_common
136 pass
137
138 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
139
140
141 AZURE_OBJECT_CLASS = 'ResourceGroup'
142
143
144 class AzureRMResourceGroupInfo(AzureRMModuleBase):
145
146 def __init__(self):
147
148 self.module_arg_spec = dict(
149 name=dict(type='str'),
150 tags=dict(type='list', elements='str'),
151 list_resources=dict(type='bool')
152 )
153
154 self.results = dict(
155 changed=False,
156 resourcegroups=[]
157 )
158
159 self.name = None
160 self.tags = None
161 self.list_resources = None
162
163 super(AzureRMResourceGroupInfo, self).__init__(self.module_arg_spec,
164 supports_check_mode=True,
165 supports_tags=False,
166 facts_module=True)
167
168 def exec_module(self, **kwargs):
169
170 for key in self.module_arg_spec:
171 setattr(self, key, kwargs[key])
172
173 if self.name:
174 result = self.get_item()
175 else:
176 result = self.list_items()
177
178 if self.list_resources:
179 for item in result:
180 item['resources'] = self.list_by_rg(item['name'])
181
182 self.results['resourcegroups'] = result
183
184 return self.results
185
186 def get_item(self):
187 self.log('Get properties for {0}'.format(self.name))
188 item = None
189 result = []
190
191 try:
192 item = self.rm_client.resource_groups.get(self.name)
193 except ResourceNotFoundError:
194 pass
195
196 if item and self.has_tags(item.tags, self.tags):
197 result = [self.serialize_obj(item, AZURE_OBJECT_CLASS)]
198
199 return result
200
201 def list_items(self):
202 self.log('List all items')
203 try:
204 response = self.rm_client.resource_groups.list()
205 except Exception as exc:
206 self.fail("Failed to list all items - {0}".format(str(exc)))
207
208 results = []
209 for item in response:
210 if self.has_tags(item.tags, self.tags):
211 results.append(self.serialize_obj(item, AZURE_OBJECT_CLASS))
212 return results
213
214 def list_by_rg(self, name):
215 self.log('List resources under resource group')
216 results = []
217 try:
218 response = self.rm_client.resources.list_by_resource_group(name)
219 while True:
220 results.append(response.next().as_dict())
221 except StopIteration:
222 pass
223 except Exception as exc:
224 self.fail('Error when listing resources under resource group {0}: {1}'.format(name, exc.message or str(exc)))
225 return results
226
227
228 def main():
229 AzureRMResourceGroupInfo()
230
231
232 if __name__ == '__main__':
233 main()