Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2018 Zim Kalinowski, <[email protected]>
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_containerregistry_info
14 version_added: "0.1.2"
15 short_description: Get Azure Container Registry facts
16 description:
17 - Get facts for Container Registry.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group to which the container registry belongs.
23 type: str
24 name:
25 description:
26 - The name of the container registry.
27 type: str
28 retrieve_credentials:
29 description:
30 - Retrieve credentials for container registry.
31 type: bool
32 default: no
33 tags:
34 description:
35 - List of tags to be matched.
36 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
37 type: list
38 elements: str
39
40 extends_documentation_fragment:
41 - azure.azcollection.azure
42
43 author:
44 - Zim Kalinowski (@zikalino)
45
46 '''
47
48 EXAMPLES = '''
49 - name: Get instance of Registry
50 azure_rm_containerregistry_info:
51 resource_group: myResourceGroup
52 name: myRegistry
53
54 - name: List instances of Registry
55 azure_rm_containerregistry_info:
56 resource_group: myResourceGroup
57 tags:
58 - key
59 - key:value
60 '''
61
62 RETURN = '''
63 registries:
64 description:
65 - A list of dictionaries containing facts for registries.
66 returned: always
67 type: complex
68 contains:
69 id:
70 description:
71 - The resource ID.
72 returned: always
73 type: str
74 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ContainerRegistry/registr
75 ies/myRegistry"
76 name:
77 description:
78 - The name of the resource.
79 returned: always
80 type: str
81 sample: myRegistry
82 location:
83 description:
84 - The location of the resource. This cannot be changed after the resource is created.
85 returned: always
86 type: str
87 sample: westus
88 admin_user_enabled:
89 description:
90 - Is admin user enabled.
91 returned: always
92 type: bool
93 sample: yes
94 sku:
95 description:
96 - The SKU name of the container registry.
97 returned: always
98 type: str
99 sample: Premium
100 provisioning_state:
101 description:
102 - Provisioning state of the container registry.
103 returned: always
104 type: str
105 sample: Succeeded
106 login_server:
107 description:
108 - Login server for the registry.
109 returned: always
110 type: str
111 sample: acrd08521b.azurecr.io
112 credentials:
113 description:
114 - Credentials, fields will be empty if admin user is not enabled for ACR.
115 returned: when C(retrieve_credentials) is set and C(admin_user_enabled) is set on ACR
116 type: complex
117 contains:
118 username:
119 description:
120 - The user name for container registry.
121 returned: when registry exists and C(admin_user_enabled) is set
122 type: str
123 sample: zim
124 password:
125 description:
126 - password value.
127 returned: when registry exists and C(admin_user_enabled) is set
128 type: str
129 sample: pass1value
130 password2:
131 description:
132 - password2 value.
133 returned: when registry exists and C(admin_user_enabled) is set
134 type: str
135 sample: pass2value
136 identity:
137 description:
138 - Identity for the Server.
139 type: complex
140 returned: when available
141 contains:
142 type:
143 description:
144 - Type of the managed identity
145 returned: always
146 sample: UserAssigned
147 type: str
148 user_assigned_identities:
149 description:
150 - User Assigned Managed Identities and its options
151 returned: always
152 type: complex
153 contains:
154 id:
155 description:
156 - Dict of the user assigned identities IDs associated to the Resource
157 returned: always
158 type: dict
159 elements: dict
160 tags:
161 description:
162 - Tags assigned to the resource. Dictionary of string:string pairs.
163 type: dict
164 sample: { "tag1": "abc" }
165 '''
166
167 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
168
169
170 class AzureRMContainerRegistryInfo(AzureRMModuleBase):
171 def __init__(self):
172 # define user inputs into argument
173 self.module_arg_spec = dict(
174 resource_group=dict(
175 type='str',
176 ),
177 name=dict(
178 type='str'
179 ),
180 retrieve_credentials=dict(
181 type='bool',
182 default=False
183 ),
184 tags=dict(
185 type='list',
186 elements='str'
187 )
188 )
189 # store the results of the module operation
190 self.results = dict(
191 changed=False
192 )
193 self.resource_group = None
194 self.name = None
195 self.retrieve_credentials = False
196 self.tags = None
197
198 super(AzureRMContainerRegistryInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
199
200 def exec_module(self, **kwargs):
201
202 for key in list(self.module_arg_spec) + ['tags']:
203 setattr(self, key, kwargs[key])
204
205 if self.name:
206 self.results['registries'] = self.get()
207 elif self.resource_group:
208 self.results['registries'] = self.list_by_resource_group()
209 else:
210 self.results['registries'] = self.list_all()
211
212 return self.results
213
214 def get(self):
215 response = None
216 results = []
217 try:
218 response = self.containerregistry_client.registries.get(resource_group_name=self.resource_group,
219 registry_name=self.name)
220 self.log("Response : {0}".format(response))
221 except Exception as e:
222 self.log("Could not get facts for Registries: {0}".format(str(e)))
223
224 if response is not None:
225 if self.has_tags(response.tags, self.tags):
226 results.append(self.format_item(response))
227
228 return results
229
230 def list_all(self):
231 response = None
232 results = []
233 try:
234 response = self.containerregistry_client.registries.list()
235 self.log("Response : {0}".format(response))
236 except Exception as e:
237 self.fail("Could not get facts for Registries: {0}".format(str(e)))
238
239 if response is not None:
240 for item in response:
241 if self.has_tags(item.tags, self.tags):
242 results.append(self.format_item(item))
243 return results
244
245 def list_by_resource_group(self):
246 response = None
247 results = []
248 try:
249 response = self.containerregistry_client.registries.list_by_resource_group(resource_group_name=self.resource_group)
250 self.log("Response : {0}".format(response))
251 except Exception as e:
252 self.fail("Could not get facts for Registries: {0}".format(str(e)))
253
254 if response is not None:
255 for item in response:
256 if self.has_tags(item.tags, self.tags):
257 results.append(self.format_item(item))
258 return results
259
260 def format_item(self, item):
261 d = item.as_dict()
262 resource_group = d['id'].split('resourceGroups/')[1].split('/')[0]
263 name = d['name']
264 credentials = {}
265 admin_user_enabled = d['admin_user_enabled']
266
267 if self.retrieve_credentials and admin_user_enabled:
268 credentials = self.containerregistry_client.registries.list_credentials(resource_group_name=resource_group, registry_name=name).as_dict()
269 for index in range(len(credentials['passwords'])):
270 password = credentials['passwords'][index]
271 if password['name'] == 'password':
272 credentials['password'] = password['value']
273 elif password['name'] == 'password2':
274 credentials['password2'] = password['value']
275 credentials.pop('passwords')
276
277 d = {
278 'resource_group': resource_group,
279 'name': d['name'],
280 'location': d['location'],
281 'admin_user_enabled': admin_user_enabled,
282 'sku': d['sku']['tier'].lower(),
283 'provisioning_state': d['provisioning_state'],
284 'login_server': d['login_server'],
285 'id': d['id'],
286 'tags': d.get('tags', None),
287 'credentials': credentials,
288 'identity': d.get('identity', None)
289 }
290 return d
291
292
293 def main():
294 AzureRMContainerRegistryInfo()
295
296
297 if __name__ == '__main__':
298 main()