Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create server raise Client Error 400? #37

Open
DravenJohnson opened this issue Apr 8, 2021 · 2 comments
Open

Create server raise Client Error 400? #37

DravenJohnson opened this issue Apr 8, 2021 · 2 comments

Comments

@DravenJohnson
Copy link

DravenJohnson commented Apr 8, 2021

Using following:

compute_api = ComputeAPI(auth_token=secret_key, region='pl-waw-1')

compute_api.query().servers.post({'project': org_id, "name": 'test', "commercial_type": "DEV1-M"})

Got:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/draven/.local/lib/python2.7/site-packages/slumber/__init__.py", line 167, in post
    resp = self._request("POST", data=data, files=files, params=kwargs)
  File "scaleway/scaleway/apis/__init__.py", line 87, in _request
    return super(SlumberResource, self)._request(*args, **kwargs)
  File "/home/draven/.local/lib/python2.7/site-packages/slumber/__init__.py", line 101, in _request
    raise exception_class("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content)
slumber.exceptions.HttpClientError: Client Error 400: https://api-pl-waw.scaleway.com/instance/v1/zones/pl-waw-1/servers/

Since there is no documents about how to use POST / Create so based on other sample such as this I assume the above code is correct.

But Error 400 ?

@brmzkw
Copy link
Contributor

brmzkw commented Apr 9, 2021

You can catch the exception to display the error and figure out what is going on.

import json

from scaleway.apis import ComputeAPI
from slumber.exceptions import HttpClientError

try:
    res = compute_api.query().servers.post({'project': org_id, "name": 'test', "commercial_type": "DEV1-M"})
except HttpClientError as exc:
    print(json.dumps(exc.response.json(), indent=2))
{
  "type": "invalid_request_error",
  "message": "Validation Error",
  "fields": {
    "image": [
      "required key not provided"
    ],
    "volumes": [
      "required key not provided"
    ]
  }
}

To create a server, you need to specify an image ("ubuntu" for example), or a list of volumes. Let's try to find the image to specify:

res = compute_api.query().images.get()
image = next(image for image in res['images'] if 'Ubuntu 20.04' in image['name'])
print(json.dumps(image, indent=2))

This retrieves the first image containing the name "Ubuntu 20.04". Feel free to print (res) to list all the images and get the one you prefer.

{
  "id": "7af873db-4999-4147-a176-59f6f22833fd",
  "name": "Ubuntu 20.04 Focal Fossa",
  "organization": "51b656e3-4865-41e8-adbc-0c45bdd780db",
  "project": "51b656e3-4865-41e8-adbc-0c45bdd780db",
  "root_volume": {
    "id": "671a345f-32a7-4d25-98db-6ceae7f68c33",
    "name": "ubuntu_20.04_focal_fossa:volume-0",
    "volume_type": "l_ssd",
    "size": 10000000000
  },
  "extra_volumes": {},
  "public": true,
  "arch": "x86_64",
  "creation_date": "2021-03-25T11:32:45.773914+00:00",
  "modification_date": "2021-03-25T11:32:45.773914+00:00",
  "default_bootscript": null,
  "from_server": null,
  "state": "available",
  "zone": "pl-waw-1"
}

You can now create the server with:

res = compute_api.query().servers.post({
    "project": org_id,
    "name": 'test',
    "commercial_type": "DEV1-M",
    "image": "7af873db-4999-4147-a176-59f6f22833fd"
})
server_id = res['server']['id']

On creation the server is not started automatically. To poweron the server, you need another API query:

compute_api.query().servers(server_id).action.post({
    'action': 'poweron'
})

@DravenJohnson
Copy link
Author

Great, thank you for helping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants