【问题标题】:How to use Azure-Python SDK `ResourcesMoveInfo` class in python如何在 python 中使用 Azure-Python SDK `ResourcesMoveInfo` 类
【发布时间】:2021-12-08 20:26:33
【问题描述】:

我遇到了这个 python 类 ResourcesMoveInfo,用于使用 Azure python SDK 将资源(Azure 图像)从一个订阅移动到另一个订阅。

但是当我像下面这样使用它时它失败了:

模式 1

来自https://buildmedia.readthedocs.org/media/pdf/azure-sdk-for-python/v1.0.3/azure-sdk-for-python.pdf的参考

用法:

metadata = azure.mgmt.resource.resourcemanagement.ResourcesMoveInfo(resources=rid,target_resource_group='/subscriptions/{0}/resourceGroups/{1}'.format(self.prod_subscription_id,self.resource_group))

错误:

AttributeError: module 'azure.mgmt.resource' has no attribute 'resourcemanagement'

模式 2

参考来自 - https://docs.microsoft.com/en-us/python/api/azure-mgmt-resource/azure.mgmt.resource.resources.v2019_07_01.models.resourcesmoveinfo?view=azure-python

用法:

metadata = azure.mgmt.resource.resources.v2020_06_01.models.ResourcesMoveInfo(resources=rid,target_resource_group='/subscriptions/{0}/resourceGroups/{1}'.format(self.prod_subscription_id,self.resource_group))

错误:

AttributeError: module 'azure.mgmt.resource.resources' has no attribute 'v2020_06_01'

对此要求/问题的任何帮助都会有所帮助。谢谢!

在这里添加代码sn-p:

import sys
import os
import time

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
import azure.mgmt.resource
#from azure.mgmt.resource.resources.v2020_06_01.models import ResourcesMoveInfo
from azure.identity import ClientSecretCredential
from cred_wrapper import CredentialWrapper


class Move():
    def __init__(self):

        self.nonprod_subscription_id = "abc"
        self.prod_subscription_id = "def"
        self.credential = ClientSecretCredential(
            client_id= os.environ["ARM_CLIENT_ID"],
            client_secret= os.environ["ARM_CLIENT_SECRET"],
            tenant_id= os.environ["ARM_TENANT_ID"]
            )
        #resource client for nonprod
        self.sp = CredentialWrapper(self.credential)
        self.resource_client = ResourceManagementClient(self.sp,self.nonprod_subscription_id)
        self.resource_group = "imgs-rg"


    def getresourceids(self):
        resource_ids = list(resource.id for resource in self.resource_client.resources.list_by_resource_group("{0}".format(self.resource_group)) if resource.id.find("latest")>=0)
        return resource_ids

    def getresourcenames(self):
        resource_names = list(resource.name for resource in self.resource_client.resources.list_by_resource_group("{0}".format(self.resource_group)) if resource.id.find("latest")>=0)
        return resource_names

    def deleteoldimages(self,name):
        #resource client id for prod
        rc = ResourceManagementClient(self.sp,self.prod_subscription_id)
        for resource in rc.resources.list_by_resource_group("{0}".format(self.resource_group)):
            if resource.name == name:
                #2019-12-01 is the latest api_version supported for deleting the resource type "image"
                rc.resources.begin_delete_by_id(resource.id,"2020-06-01")
                print("deleted {0}".format(resource.name))

    def moveimages(self):
        rnames = self.getresourcenames()
        for rname in rnames:
            print(rname)
            #self.deleteoldimages(rname)
        time.sleep(10)
        rids = self.getresourceids()
        rid = list(rids[0:])
        print(rid)
        metadata = azure.mgmt.resource.resources.v2020_06_01.models.ResourcesMoveInfo(resources=rid,target_resource_group='/subscriptions/{0}/resourceGroups/{1}'.format(self.prod_subscription_id,self.resource_group))
        #moving resources in the rid from nonprod subscription to prod subscription under the resource group avrc-imgs-rg
        if rid != []:
            print("moving {0}".format(rid))
            print(self.resource_client.resources.move_resources(source_resource_group_name="{0}".format(self.resource_group),parameters=metadata))
            #self.resource_client.resources.move_resources(source_resource_group_name="{0}".format(self.resource_group),resources=rid,target_resource_group='/subscriptions/{0}/resourceGroups/{1}'.format(self.prod_subscription_id,self.resource_group))
            #self.resource_client.resources.begin_move_resources(source_resource_group_name="{0}".format(self.resource_group),parameters=metadata)
            



if __name__ == "__main__":
    Move().moveimages()

【问题讨论】:

  • 您能否提供完整的错误跟踪来检查错误消息发生的路径。

标签: python azure azure-sdk-python


【解决方案1】:

根据您的输入,我们可以看到代码看起来不错。从您的错误消息来看,问题在于导入模块。

基本上,当我们导入一个模块时,很少有子模块会一起安装,很少有不会。这将取决于包的版本,要了解特定版本中涉及哪些模块,我们需要检查官方文档中的版本发布。

在您的情况下,似乎缺少一些资源模块,如果您可以看到整个错误跟踪,则在我们的本地将有一个包含站点包的路径。尝试找到该包及其子文件夹(Modules)并将它们与 Azure SDK for Python 下的 Resource 模块进行比较,您可以找到 here

在这种情况下,我们需要在我们的包下显式添加这些子模块。在您的情况下,您可以从我提供的 Git 链接简单地下载打包代码,并可以合并到您的本地文件夹中。

【讨论】:

    猜你喜欢
    • 2019-11-25
    • 2016-03-16
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多