I am attempting to use python-ovh to simply create an instance but it is giving me trouble. The API documentation actually seems to put out invalid Python code (at least for Python3…) and I’m not sure what I’m doing wrong. I believe I’m passing correct JSON but the API doesn’t like it. I am having a really hard time finding example code through searches also.
My code:
self.inst = {}
self.inst['flavorId'] = self.flavorid
self.inst['imageId'] = self.imageid
self.inst['name'] = self.name
self.inst['region'] = self.region
self.inst['monthlyBilling'] = False
try:
self.instance = client.post("/cloud/project/" + self.servicename + "/instance",
json.dumps(self.inst, separators=(",",":"))
)
except ovh.APIError as e:
print("JSON: " + json.dumps(self.inst, separators=(",",":")))
print("Ooops, failed to create instance:", e)
Debug output:
JSON: {“flavorId”:“14c5fa3f-fdad-45c4-9cd1-14dd99c341ee”,“imageId”:“92bee304-a24f-4db5-9896-864da799f905”,“name”:“ovhcloud-test-1”,“region”:“BHS5”,“monthlyBilling”:false}
Error output:
Ooops, failed to create instance: Missing parameter(s): flavorId, name, region \nOVH-Query-ID: CA.ext-3.6101d265.2209.d74765fb-6227-4105-a24b-c46c74f3e508\n"
the API docs tell me to do this:
result = client.post(’/cloud/project/xxxxxx/instance’,
=’{“flavorId”:“14c5fa3f-fdad-45c4-9cd1-14dd99c34”,“imageId”:“92bee304-a24f-4db5-9896-864da799f905”,“monthlyBilling”:false,“name”:“testinstance”,“region”:“BHS5”,“userData”:“testdata”}’, // Request Body (type: cloud.ProjectInstanceCreation)
)
But that gives syntax errors and doesn’t really work with variable substitution either.
Can anyone help tell me what I’m doing wrong?
In the client.post()
parameters, you have to explicitly give each parameters. The Python client already manage to conversion to JSON, so you don't have to manage the json.dumps()
by yourself.
Here is a working example of calling OVH API through the Python client to create a cloud instance:
#!/usr/bin/env python
import ovh
client = ovh.Client(
endpoint='ovh-eu',
application_key='my_app_key',
application_secret='my_secret_key',
consumer_key='my_consumer_key'
)
project_id = 'my_cloud_project_id'
try:
instance = client.post(
'/cloud/project/' + project_id + '/instance',
flavorId='d145323c-2fe7-4084-98d8-f65c54bbbaf4',
name='my_instance_name',
region='GRA5',
imageId='a125424e-3d5c-4276-a8ad-adf852ce1771',
monthlyBilling=False
)
except ovh.APIError as e:
print('ERROR: ')
print(e)
If you want to give a Python dictionary as parameters (as you tried in your example), it is possible like this:
instance_creation_params = {
'flavorId': 'd145323c-2fe7-4084-98d8-f65c54bbbaf4',
'name': 'my_instance_name',
'region':'GRA5',
'imageId': 'a125424e-3d5c-4276-a8ad-adf852ce1771',
'monthlyBilling': False
}
try:
instance = client.post(
'/cloud/project/' + project_id + '/instance',
**instance_creation_params
)
except ovh.APIError as e:
print('ERROR: ')
print(e)