2017-09-26 47 views
0

我想從我上傳到devtest實驗室的VHD創建自定義圖像。在DevTestLabs中使用python Azure SDK創建自定義圖像

我使用下面的代碼來做到這一點:

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) 

它拋出我下面的錯誤: 無法分析與價值URI名爲ImageName「## customImageName ##」。

讓我知道我在做什麼錯了?我在哪裏肯定要在API中輸入VHD的路徑。我無法找到任何採取路徑的論據!

+0

有沒有進展?我的回答能幫助你嗎? –

回答

0

它引發了以下錯誤:無法解析名爲ImageName 的值爲'## customImageName ##'的URI。

根據錯誤消息,似乎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


順便說一句,要解決此問題更高的效率,你可以請把你的整個腳本:)

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的名字,它應該是你的VHD文件存儲的名字的complete url。 它看起來像:

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

我成功地改變了imageName然後創建的自定義圖像的價值。

enter image description here

希望它能幫助you.Any關注,請隨時讓我kown。