changed/added license heading. Moved some json files to yaml
[osm/openvim.git] / charm / openvim / layer-openvim / tests / openvim.py
1 ##
2 # Copyright 2016
3 # This file is part of openvim
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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
15 # under the License.
16 #
17 ##
18
19 import requests
20 import json
21
22 class Connection(object):
23 def __init__(self, base_url):
24 self.base_url = base_url
25
26 def set_active_tenant(self, tenant):
27 self.tenant_id = tenant["id"]
28
29 def get_tenants(self):
30 return self._http_get("tenants")["tenants"]
31
32 def get_hosts(self):
33 return self._http_get("hosts")["hosts"]
34
35 def get_networks(self):
36 return self._http_get("networks")["networks"]
37
38 def get_images(self):
39 return self._http_get(self.tenant_id + "/images")["images"]
40
41 def get_flavors(self):
42 return self._http_get(self.tenant_id + "/flavors")["flavors"]
43
44 def create_server(self, name, description, image, flavor, networks):
45 request_data = {"server": {
46 "name": name,
47 "description": description,
48 "imageRef": image["id"],
49 "flavorRef": flavor["id"],
50 "networks": [
51 {"name": n["name"], "uuid": n["id"]}
52 for n in networks
53 ]
54 }}
55
56 path = self.tenant_id + "/servers"
57 return self._http_post(path, request_data)
58
59 def _http_get(self, path):
60 response = requests.get(self.base_url + path)
61 assert response.status_code == 200
62 return response.json()
63
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()
70
71 def connect(host, port=9080):
72 base_url = "http://%s:%s/openvim/" % (host, port)
73 return Connection(base_url)