def add_template_output(cmd, client, resource_group_name, image_template_name, gallery_name=None, location=None, # pylint: disable=line-too-long, unused-argument output_name=None, is_vhd=None, tags=None, gallery_image_definition=None, gallery_replication_regions=None, managed_image=None, managed_image_location=None): # pylint: disable=line-too-long, unused-argument _require_defer(cmd) from azure.mgmt.imagebuilder.models import ( ImageTemplateManagedImageDistributor, ImageTemplateVhdDistributor, ImageTemplateSharedImageDistributor) existing_image_template = cached_get( cmd, client.virtual_machine_image_templates.get, resource_group_name=resource_group_name, image_template_name=image_template_name) distributor = None if managed_image: parsed = parse_resource_id(managed_image) distributor = ImageTemplateManagedImageDistributor( run_output_name=output_name or parsed['name'], image_id=managed_image, location=managed_image_location or location) elif gallery_image_definition: parsed = parse_resource_id(gallery_image_definition) distributor = ImageTemplateSharedImageDistributor( run_output_name=output_name or parsed['child_name_1'], gallery_image_id=gallery_image_definition, replication_regions=gallery_replication_regions or [location]) elif is_vhd: distributor = ImageTemplateVhdDistributor(run_output_name=output_name) if distributor: distributor.artifact_tags = tags or {} if existing_image_template.distribute is None: existing_image_template.distribute = [] else: for existing_distributor in existing_image_template.distribute: if existing_distributor.run_output_name == distributor.run_output_name: raise CLIError( "Output with output name {} already exists in image template {}." .format(distributor.run_output_name.lower(), image_template_name)) existing_image_template.distribute.append(distributor) return cached_put( cmd, client.virtual_machine_image_templates.begin_create_or_update, parameters=existing_image_template, # pylint: disable=line-too-long resource_group_name=resource_group_name, image_template_name=image_template_name)
def create_image_template( # pylint: disable=too-many-locals cmd, client, resource_group_name, image_template_name, location=None, source_dict=None, scripts_list=None, destinations_lists=None, build_timeout=None, tags=None, source=None, scripts=None, checksum=None, managed_image_destinations=None, # pylint: disable=unused-argument shared_image_destinations=None, no_wait=False): # pylint: disable=unused-argument, too-many-locals from azure.mgmt.imagebuilder.models import (ImageTemplate, ImageTemplateSharedImageVersionSource, ImageTemplatePlatformImageSource, ImageTemplateIsoSource, ImageTemplateManagedImageSource, # pylint: disable=line-too-long ImageTemplateShellCustomizer, ImageTemplatePowerShellCustomizer, ImageTemplateManagedImageDistributor, ImageTemplateSharedImageDistributor) # pylint: disable=line-too-long template_source, template_scripts, template_destinations = None, [], [] # create image template source settings if source_dict['type'] == _SourceType.PLATFORM_IMAGE: template_source = ImageTemplatePlatformImageSource(**source_dict) elif source_dict['type'] == _SourceType.ISO_URI: template_source = ImageTemplateIsoSource(**source_dict) elif source_dict['type'] == _SourceType.MANAGED_IMAGE: template_source = ImageTemplateManagedImageSource(**source_dict) elif source_dict['type'] == _SourceType.SIG_VERSION: template_source = ImageTemplateSharedImageVersionSource(**source_dict) # create image template customizer settings # Script structure can be found in _parse_script's function definition for script in scripts_list: script.pop("is_url") script["script_uri"] = script.pop("script") if script["type"] == ScriptType.SHELL: template_scripts.append(ImageTemplateShellCustomizer(**script)) elif script["type"] == ScriptType.POWERSHELL: template_scripts.append(ImageTemplatePowerShellCustomizer(**script)) else: # Should never happen logger.debug("Script %s has type %s", script["script"], script["type"]) raise CLIError("Script {} has an invalid type.".format(script["script"])) # create image template distribution / destination settings for dest_type, rid, loc_info in destinations_lists: parsed = parse_resource_id(rid) if dest_type == _DestType.MANAGED_IMAGE: template_destinations.append(ImageTemplateManagedImageDistributor( image_id=rid, location=loc_info, run_output_name=parsed['name'])) elif dest_type == _DestType.SHARED_IMAGE_GALLERY: template_destinations.append(ImageTemplateSharedImageDistributor( gallery_image_id=rid, replication_regions=loc_info, run_output_name=parsed['child_name_1'])) else: logger.info("No applicable destination found for destination %s", str(tuple([dest_type, rid, loc_info]))) image_template = ImageTemplate(source=template_source, customize=template_scripts, distribute=template_destinations, location=location, build_timeout_in_minutes=build_timeout, tags=(tags or {})) return cached_put(cmd, client.virtual_machine_image_templates.create_or_update, parameters=image_template, resource_group_name=resource_group_name, image_template_name=image_template_name)
def create_image_template( # pylint: disable=too-many-locals, too-many-branches, too-many-statements, unused-argument cmd, client, resource_group_name, image_template_name, location=None, source_dict=None, scripts_list=None, destinations_lists=None, build_timeout=None, tags=None, source=None, scripts=None, checksum=None, managed_image_destinations=None, shared_image_destinations=None, no_wait=False, image_template=None, identity=None, vm_size=None, os_disk_size=None, vnet=None, subnet=None, proxy_vm_size=None, build_vm_identities=None): from azure.mgmt.imagebuilder.models import ( ImageTemplate, ImageTemplateSharedImageVersionSource, ImageTemplatePlatformImageSource, ImageTemplateManagedImageSource, ImageTemplateShellCustomizer, ImageTemplatePowerShellCustomizer, ImageTemplateManagedImageDistributor, ImageTemplateSharedImageDistributor, ImageTemplateIdentity, ComponentsVrq145SchemasImagetemplateidentityPropertiesUserassignedidentitiesAdditionalproperties, # pylint: disable=line-too-long ImageTemplateVmProfile, VirtualNetworkConfig) if image_template is not None: logger.warning( 'You are using --image-template. All other parameters will be ignored.' ) if os.path.exists(image_template): # Local file with open(image_template) as f: content = f.read() else: # It should be an URL msg = '\nusage error: --image-template is not a correct local path or URL' try: r = requests.get(image_template) except Exception: raise CLIError(traceback.format_exc() + msg) if r.status_code != 200: raise CLIError(traceback.format_exc() + msg) content = r.content try: obj = json.loads(content) except json.JSONDecodeError: raise CLIError( traceback.format_exc() + '\nusage error: Content of --image-template is not a valid JSON string' ) content = {} if 'properties' in obj: content = obj['properties'] if 'location' in obj: content['location'] = obj['location'] if 'tags' in obj: content['tags'] = obj['tags'] if 'identity' in obj: content['identity'] = obj['identity'] return client.virtual_machine_image_templates.begin_create_or_update( parameters=content, resource_group_name=resource_group_name, image_template_name=image_template_name) template_source, template_scripts, template_destinations = None, [], [] # create image template source settings if source_dict['type'] == _SourceType.PLATFORM_IMAGE: template_source = ImageTemplatePlatformImageSource(**source_dict) elif source_dict['type'] == _SourceType.ISO_URI: # It was supported before but is removed in the current service version. raise CLIError('usage error: Source type ISO URI is not supported.') elif source_dict['type'] == _SourceType.MANAGED_IMAGE: template_source = ImageTemplateManagedImageSource(**source_dict) elif source_dict['type'] == _SourceType.SIG_VERSION: template_source = ImageTemplateSharedImageVersionSource(**source_dict) # create image template customizer settings # Script structure can be found in _parse_script's function definition for script in scripts_list: script.pop("is_url") script["script_uri"] = script.pop("script") if script["type"] == ScriptType.SHELL: template_scripts.append(ImageTemplateShellCustomizer(**script)) elif script["type"] == ScriptType.POWERSHELL: template_scripts.append( ImageTemplatePowerShellCustomizer(**script)) else: # Should never happen logger.debug("Script %s has type %s", script["script"], script["type"]) raise CLIError("Script {} has an invalid type.".format( script["script"])) # create image template distribution / destination settings for dest_type, rid, loc_info in destinations_lists: parsed = parse_resource_id(rid) if dest_type == _DestType.MANAGED_IMAGE: template_destinations.append( ImageTemplateManagedImageDistributor( image_id=rid, location=loc_info, run_output_name=parsed['name'])) elif dest_type == _DestType.SHARED_IMAGE_GALLERY: template_destinations.append( ImageTemplateSharedImageDistributor( gallery_image_id=rid, replication_regions=loc_info, run_output_name=parsed['child_name_1'])) else: logger.info("No applicable destination found for destination %s", str(tuple([dest_type, rid, loc_info]))) # Identity identity_body = None if identity is not None: subscription_id = get_subscription_id(cmd.cli_ctx) user_assigned_identities = {} for ide in identity: if not is_valid_resource_id(ide): ide = resource_id(subscription=subscription_id, resource_group=resource_group_name, namespace='Microsoft.ManagedIdentity', type='userAssignedIdentities', name=ide) user_assigned_identities[ide] = ComponentsVrq145SchemasImagetemplateidentityPropertiesUserassignedidentitiesAdditionalproperties() # pylint: disable=line-too-long identity_body = ImageTemplateIdentity( type='UserAssigned', user_assigned_identities=user_assigned_identities) # VM profile vnet_config = None if vnet or subnet: if not is_valid_resource_id(subnet): subscription_id = get_subscription_id(cmd.cli_ctx) subnet = resource_id(subscription=subscription_id, resource_group=resource_group_name, namespace='Microsoft.Network', type='virtualNetworks', name=vnet, child_type_1='subnets', child_name_1=subnet) vnet_config = VirtualNetworkConfig(subnet_id=subnet) if proxy_vm_size is not None: if subnet is not None: vnet_config = VirtualNetworkConfig(subnet_id=subnet, proxy_vm_size=proxy_vm_size) else: raise RequiredArgumentMissingError( 'Usage error: --proxy-vm-size is only configurable when --subnet is specified.' ) vm_profile = ImageTemplateVmProfile(vm_size=vm_size, os_disk_size_gb=os_disk_size, user_assigned_identities=build_vm_identities, vnet_config=vnet_config) # pylint: disable=line-too-long image_template = ImageTemplate(source=template_source, customize=template_scripts, distribute=template_destinations, location=location, build_timeout_in_minutes=build_timeout, tags=(tags or {}), identity=identity_body, vm_profile=vm_profile) return cached_put( cmd, client.virtual_machine_image_templates.begin_create_or_update, parameters=image_template, resource_group_name=resource_group_name, image_template_name=image_template_name)
def create_image_template( # pylint: disable=too-many-locals, too-many-branches, too-many-statements cmd, client, resource_group_name, image_template_name, location=None, source_dict=None, scripts_list=None, destinations_lists=None, build_timeout=None, tags=None, source=None, scripts=None, checksum=None, managed_image_destinations=None, # pylint: disable=unused-argument shared_image_destinations=None, no_wait=False, image_template=None): # pylint: disable=unused-argument, too-many-locals from azure.mgmt.imagebuilder.models import (ImageTemplate, ImageTemplateSharedImageVersionSource, ImageTemplatePlatformImageSource, ImageTemplateIsoSource, ImageTemplateManagedImageSource, # pylint: disable=line-too-long ImageTemplateShellCustomizer, ImageTemplatePowerShellCustomizer, ImageTemplateManagedImageDistributor, ImageTemplateSharedImageDistributor) # pylint: disable=line-too-long if image_template is not None: if os.path.exists(image_template): # Local file with open(image_template) as f: content = f.read() else: # It should be an URL msg = '\nusage error: --image-template is not a correct local path or URL' try: r = requests.get(image_template) except Exception: raise CLIError(traceback.format_exc() + msg) if r.status_code != 200: raise CLIError(traceback.format_exc() + msg) content = r.content try: obj = json.loads(content) except json.JSONDecodeError: raise CLIError( traceback.format_exc() + '\nusage error: Content of --image-template is not a valid JSON string' ) content = {} if 'properties' in obj: content = obj['properties'] if 'location' in obj: content['location'] = obj['location'] if 'tags' in obj: content['tags'] = obj['tags'] return client.virtual_machine_image_templates.create_or_update( parameters=content, resource_group_name=resource_group_name, image_template_name=image_template_name) template_source, template_scripts, template_destinations = None, [], [] # create image template source settings if source_dict['type'] == _SourceType.PLATFORM_IMAGE: template_source = ImageTemplatePlatformImageSource(**source_dict) elif source_dict['type'] == _SourceType.ISO_URI: template_source = ImageTemplateIsoSource(**source_dict) elif source_dict['type'] == _SourceType.MANAGED_IMAGE: template_source = ImageTemplateManagedImageSource(**source_dict) elif source_dict['type'] == _SourceType.SIG_VERSION: template_source = ImageTemplateSharedImageVersionSource(**source_dict) # create image template customizer settings # Script structure can be found in _parse_script's function definition for script in scripts_list: script.pop("is_url") script["script_uri"] = script.pop("script") if script["type"] == ScriptType.SHELL: template_scripts.append(ImageTemplateShellCustomizer(**script)) elif script["type"] == ScriptType.POWERSHELL: template_scripts.append( ImageTemplatePowerShellCustomizer(**script)) else: # Should never happen logger.debug("Script %s has type %s", script["script"], script["type"]) raise CLIError("Script {} has an invalid type.".format( script["script"])) # create image template distribution / destination settings for dest_type, rid, loc_info in destinations_lists: parsed = parse_resource_id(rid) if dest_type == _DestType.MANAGED_IMAGE: template_destinations.append( ImageTemplateManagedImageDistributor( image_id=rid, location=loc_info, run_output_name=parsed['name'])) elif dest_type == _DestType.SHARED_IMAGE_GALLERY: template_destinations.append( ImageTemplateSharedImageDistributor( gallery_image_id=rid, replication_regions=loc_info, run_output_name=parsed['child_name_1'])) else: logger.info("No applicable destination found for destination %s", str(tuple([dest_type, rid, loc_info]))) image_template = ImageTemplate(source=template_source, customize=template_scripts, distribute=template_destinations, location=location, build_timeout_in_minutes=build_timeout, tags=(tags or {})) return cached_put(cmd, client.virtual_machine_image_templates.create_or_update, parameters=image_template, resource_group_name=resource_group_name, image_template_name=image_template_name)