简体   繁体   中英

Create a vm or cloud service on azure using libcloud?

I'm having a problem with libcloud/drivers/azure.py when I create a VM or a cloud service, I receive always a 400 bad request without a body. Could someone help me out of this please?

Here is my code:

# connectHandler use my pem file to create a connection with the server, return a AzureNodeDriver objet
conn = _connectHandler.api_connect(compute_ressource)
try:
    result = conn.ex_create_cloud_service(name= "testCloudServiceZT200", location= "Central US")
except Exception as e:
    logger.error(e)
else:
    return result

and here is what I got in return: <LibcloudError in <libcloud.compute.drivers.azure.AzureNodeDriver object at 0x7fceaeb457d0> 'Message: Bad Request, Body: , Status code: 400'>

Could someone please tell me why this error, and maybe give me some examples of azure.py, it will be very grateful. thx!!

The Azure driver seems very new, since it's not even included in the latest release available via PyPI. I had the same problem, used ipdb to look at the request and XML that is created by the Azure driver. At first I thought I found a number of problems, but after looking at the source and not only the output of the debugger it boils down to a simple fix.

The following curl request works for me:

curl -v -E {PATH_TO_PEM_FILE} -H 'x-ms-version: 2015-04-01' -H 'Content-Type: application/xml' -d '<?xml version="1.0" encoding="utf-8"?><CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><ServiceName>test-cloudservice-libcloud-azure-driver</ServiceName><Label>dGVzdC1jbG91ZHNlcnZpY2UtbGliY2xvdWQtYXp1cmUtZHJpdmVyCg==</Label><Location>West Europe</Location></CreateHostedService>' https://management.core.windows.net/{SUBSCRIPTION_ID}/services/hostedservices

But the XML that is generated by the Azure driver contains encoding='utf8' , which doesn't work. It has to be utf-8 . If you replace line 2710 in the current head of the github repo (e105433e941262e03eb3bb922db47dabdd8efa75) with result = ensure_string(ET.tostring(doc, encoding='utf-8')) , it works, encoding='utf8' was the culprit, at least for me.

I have opened a pull request, hope it solves the issue for you as well: https://github.com/apache/libcloud/pull/538

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM