1# -------------------------------------------------------------------------------------------- 2# Copyright (c) Microsoft Corporation. All rights reserved. 3# Licensed under the MIT License. See License.txt in the project root for license information. 4# -------------------------------------------------------------------------------------------- 5 6from knack.util import CLIError 7from msrestazure.tools import is_valid_resource_id 8 9from ._utils import ( 10 validate_managed_registry, 11 get_registry_from_name_or_login_server 12) 13 14SOURCE_REGISTRY_NOT_FOUND = "The source container registry can not be found in the current subscription. " \ 15 "Please provide a valid address and/or credentials." 16IMPORT_NOT_SUPPORTED = "Imports are only supported for managed registries." 17SOURCE_NOT_FOUND = "Source cannot be found. " \ 18 "Please provide a valid image and source registry or a fully qualified source." 19LOGIN_SERVER_NOT_VALID = "Login server of the registry is not valid " \ 20 "because it is not a fully qualified domain name." 21CREDENTIALS_INVALID = "Authentication failed. Please provide password." 22 23 24def acr_import(cmd, 25 client, 26 registry_name, 27 source_image, 28 source_registry=None, 29 source_registry_username=None, 30 source_registry_password=None, 31 target_tags=None, 32 resource_group_name=None, 33 repository=None, 34 force=False): 35 36 if source_registry_username and not source_registry_password: 37 raise CLIError(CREDENTIALS_INVALID) 38 39 _, resource_group_name = validate_managed_registry( 40 cmd, registry_name, resource_group_name, IMPORT_NOT_SUPPORTED) 41 42 ImportImageParameters, ImportSource, ImportMode = cmd.get_models( 43 'ImportImageParameters', 'ImportSource', 'ImportMode') 44 45 if source_registry: 46 if is_valid_resource_id(source_registry): 47 source = ImportSource(resource_id=source_registry, source_image=source_image) 48 else: 49 registry = get_registry_from_name_or_login_server(cmd.cli_ctx, source_registry, source_registry) 50 if registry: 51 source = ImportSource(resource_id=registry.id, source_image=source_image) 52 else: 53 raise CLIError(SOURCE_REGISTRY_NOT_FOUND) 54 else: 55 slash = source_image.find('/') 56 if slash > 0: 57 registry_uri = source_image[:slash] 58 dot = registry_uri.find('.') 59 if dot > 0: 60 source_image = source_image[slash + 1:] 61 if source_registry_password: 62 ImportSourceCredentials = cmd.get_models('ImportSourceCredentials') 63 source = ImportSource(registry_uri=registry_uri, 64 source_image=source_image, 65 credentials=ImportSourceCredentials(password=source_registry_password, 66 username=source_registry_username)) 67 else: 68 registry = get_registry_from_name_or_login_server(cmd.cli_ctx, registry_uri) 69 if registry: 70 source = ImportSource(resource_id=registry.id, source_image=source_image) 71 else: 72 source = ImportSource(registry_uri=registry_uri, source_image=source_image) 73 else: 74 raise CLIError(LOGIN_SERVER_NOT_VALID) 75 else: 76 raise CLIError(SOURCE_NOT_FOUND) 77 78 if not target_tags and not repository: 79 index = source_image.find("@") 80 if index > 0: 81 target_tags = [source_image[:index]] 82 else: 83 target_tags = [source_image] 84 85 import_parameters = ImportImageParameters(source=source, 86 target_tags=target_tags, 87 untagged_target_repositories=repository, 88 mode=ImportMode.force.value if force else ImportMode.no_force.value) 89 return client.import_image( 90 resource_group_name=resource_group_name, 91 registry_name=registry_name, 92 parameters=import_parameters) 93