【Azure Compute Gallery】使用 Python 代码从 Azure Compute Gallery 复制 Image‐Version - LuBu0505/My-Code GitHub Wiki

问题描述

Azure Compute Gallery 可以帮助围绕 Azure 资源(例如映像和应用程序)生成结构和组织,并且支持全局复制。 image.png

如果想通过Python代码实现 Image-Version从一个Azure Compute Gallery复制到另一个中,如何实现呢?

问题解答

示例Python代码:

import os
from msrestazure.azure_cloud import AZURE_CHINA_CLOUD as CLOUD
from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.compute.models import GalleryImageVersion, GalleryImageVersionStorageProfile, GalleryImageVersionPublishingProfile, GalleryArtifactVersionSource, TargetRegion

credential = DefaultAzureCredential(authority=CLOUD.endpoints.active_directory)
SUBSCRIPTION_ID = "{subscriptionId}"

client = ComputeManagementClient(credential, subscription_id=SUBSCRIPTION_ID,
       base_url = CLOUD.endpoints.resource_manager,
       credential_scopes = [CLOUD.endpoints.resource_manager + "/.default"]
   )

//设置源 Image Version的Resource ID ...
source = GalleryArtifactVersionSource(id=" /subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/galleries/{galleryName}/images/{imageDefinitionName}/versions/{versionName}")
storageprofile = GalleryImageVersionStorageProfile(source=source)
//设置目标region
targetregion = TargetRegion(name="{region}")
publishingprofile = GalleryImageVersionPublishingProfile(target_regions=[targetregion], replica_count=1, exclude_from_latest=True)

imageversion = GalleryImageVersion(location="{region}", publishing_profile=publishingprofile, storage_profile=storageprofile)

image_creation = client.gallery_image_versions.begin_create_or_update(resource_group_name
="{resourceGroup}", gallery_name="{galleryName}", gallery_image_name="{imageDefinitionName}", gallery_image_version_name="{versionName}", gallery_image_version=imageversion)
// 创建目标 Image Version
image_creation.wait()

注意: {subscriptionId} , {galleryName},{resourceGroup},{region},{imageDefinitionName},{versionName} 均需要替换为对应的真实值。

参考资料

Python对Gallery Images Version操作的Python class GalleryImageVersionsOperations :  https://learn.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2021_07_01.operations.galleryimageversionsoperations?view=azure-python

使用ComputeManagementClient class的gallery_image_version属性来初始化 : https://learn.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2021_07_01.computemanagementclient?view=azure-python

/END/

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!