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