在DevTestLabs中使用python Azure SDK创建自定义映像

时间:2017-09-26 16:39:54

标签: python azure azure-devtest-labs

我正在尝试从我上传到开发实验室的VHD创建自定义图像。

我使用以下代码执行此操作:

from azure.mgmt.storage import StorageManagementClient
....
credentials = ServicePrincipalCredentials( client_id = '##', tenant = '##', secret = "##")
resource_client = DevTestLabsClient(credentials, subscriptID)

....
custom_image_properties = CustomImagePropertiesCustom(CustomImageOsType.windows, config.CustomImage.Name, True)
custom_image = CustomImage(vhd = custom_image_properties)
resource_client.custom_images.create_or_update(rgName,labName, imageName, custom_image)

它引发了以下错误: 无法解析名为ImageName的URI,其值为' ## customImageName ##'。

让我知道我做错了什么?我在哪里可以进入API中的VHD路径。我找不到任何有道路的论据!

2 个答案:

答案 0 :(得分:0)

  

它抛出以下错误:无法解析名为ImageName的URI   值为' ## customImageName ##'。

根据错误消息,似乎imagename值是URI。

图片名称应为字符串。

create_or_update(resource_group_name, lab_name, name, custom_image, custom_headers=None, raw=False, **operation_config)


Parameters: 
resource_group_name (str) – The name of the resource group.
lab_name (str) – The name of the lab.
name (str) – The name of the custom image.

更多信息请参阅此link

顺便说一下,为了更有效地解决这个问题,请你发布整个脚本:)

答案 1 :(得分:0)

我尝试使用您提供的代码创建custom image

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.devtestlabs import DevTestLabsClient
from azure.mgmt.devtestlabs.models.custom_image_properties_custom import CustomImagePropertiesCustom
from azure.mgmt.devtestlabs.models.custom_image import CustomImage
from azure.mgmt.devtestlabs.models.dev_test_labs_client_enums import CustomImageOsType

client_id = <your client id>
tenant = <your tenant id>
secret = <your secret id>
subscriptID = <your subcript id>
imageName='jaygong.vhd'
name=<your custom image name as you want>
rgName = <your resource name>
labName = <your lab name>

credentials = ServicePrincipalCredentials(client_id=client_id, tenant=tenant , secret=secret)
resource_client = DevTestLabsClient(credentials, subscriptID)
custom_image_properties = CustomImagePropertiesCustom(CustomImageOsType.windows, imageName, True)
custom_image = CustomImage(vhd = custom_image_properties)
resource_client.custom_images.create_or_update(rgName,labName, name, custom_image)
然后我再现你的问题。

E:\Python27\python.exe E:/PythonWorkSpace/CreateVM/Create.py
Traceback (most recent call last):
  File "E:/PythonWorkSpace/CreateVM/Create.py", line 19, in <module>
    resource_client.custom_images.create_or_update(rgName,labName, imageName, custom_image)
  File "E:\Python27\lib\site-packages\azure\mgmt\devtestlabs\operations\custom_images_operations.py", line 293, in create_or_update
    get_long_running_status, long_running_operation_timeout)
  File "E:\Python27\lib\site-packages\msrestazure\azure_operation.py", line 350, in __init__
    raise CloudError(self._response)
msrestazure.azure_exceptions.CloudError: Azure Error: InvalidUrlProvided
Message: Failed to parse URI named ImageName with value of 'aaa'.

Process finished with exit code 1

经过研究,我发现上面的imageName参数不仅仅是您的VHD名称,它应该是您的存储名称中complete url文件的VHD。 它看起来像:

https://<your storage account>.blob.core.windows.net/<your container name>/<your vhd file name>

我更改了imageName的值,然后成功创建了自定义图片。

enter image description here

希望它对你有所帮助。任何关心,请随时让我知道。