3 # This file is part of openvim
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
22 class Connection(object):
23 def __init__(self
, base_url
):
24 self
.base_url
= base_url
26 def set_active_tenant(self
, tenant
):
27 self
.tenant_id
= tenant
["id"]
29 def get_tenants(self
):
30 return self
._http
_get
("tenants")["tenants"]
33 return self
._http
_get
("hosts")["hosts"]
35 def get_networks(self
):
36 return self
._http
_get
("networks")["networks"]
39 return self
._http
_get
(self
.tenant_id
+ "/images")["images"]
41 def get_flavors(self
):
42 return self
._http
_get
(self
.tenant_id
+ "/flavors")["flavors"]
44 def create_server(self
, name
, description
, image
, flavor
, networks
):
45 request_data
= {"server": {
47 "description": description
,
48 "imageRef": image
["id"],
49 "flavorRef": flavor
["id"],
51 {"name": n
["name"], "uuid": n
["id"]}
56 path
= self
.tenant_id
+ "/servers"
57 return self
._http
_post
(path
, request_data
)
59 def _http_get(self
, path
):
60 response
= requests
.get(self
.base_url
+ path
)
61 assert response
.status_code
== 200
62 return response
.json()
64 def _http_post(self
, path
, request_data
):
65 data
= json
.dumps(request_data
)
66 headers
= {"content-type": "application/json"}
67 response
= requests
.post(self
.base_url
+ path
, data
=data
, headers
=headers
)
68 assert response
.status_code
== 200
69 return response
.json()
71 def connect(host
, port
=9080):
72 base_url
= "http://%s:%s/openvim/" % (host
, port
)
73 return Connection(base_url
)