15229ed61ad09a5e1f1912ec5fab977094aa4aa2
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / vimapi / api.py
1 # Copyright 2017 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import json
16
17 from lcm.pub.utils.restcall import req_by_msb
18 from .exceptions import VimException
19
20 VIM_DRIVER_BASE_URL = "openoapi/vimdriver/v1"
21
22 def call(vim_id, res, method, data=''):
23     if data and not isinstance(a, (str, unicode)):
24         data = json.JSONEncoder().encode(data)
25     url = "%/%s/%s" % (VIM_DRIVER_BASE_URL, vim_id, res)
26     ret = req_by_msb(url, method, data)
27     if ret[0] > 0:
28         raise VimException(ret[1], ret[2])
29     return json.JSONDecoder().decode(ret[1]) if ret[1] else {}
30
31 ######################################################################
32
33 def create_image(vim_id, data):
34     return call(vim_id, "images", "POST", data)
35
36 def delete_image(vim_id, image_id):
37     return call(vim_id, "images/%s" % image_id, "DELETE")
38     
39 def get_image(vim_id, image_id):
40     return call(vim_id, "images/%s" % image_id, "GET")
41     
42 def list_image(vim_id):
43     return call(vim_id, "images", "GET")
44
45 ######################################################################
46
47 def create_network(vim_id, data):
48     return call(vim_id, "networks", "POST", data)
49
50 def delete_network(vim_id, network_id):
51     return call(vim_id, "networks/%s" % network_id, "DELETE")
52     
53 def get_network(vim_id, network_id):
54     return call(vim_id, "networks/%s" % network_id, "GET")
55     
56 def list_network(vim_id):
57     return call(vim_id, "networks", "GET")
58
59 ######################################################################
60
61 def create_subnet(vim_id, data):
62     return call(vim_id, "subnets", "POST", data)
63
64 def delete_subnet(vim_id, subnet_id):
65     return call(vim_id, "subnets/%s" % subnet_id, "DELETE")
66     
67 def get_subnet(vim_id, subnet_id):
68     return call(vim_id, "subnets/%s" % subnet_id, "GET")
69     
70 def list_subnet(vim_id):
71     return call(vim_id, "subnets", "GET")
72
73 ######################################################################
74
75 def create_port(vim_id, data):
76     return call(vim_id, "ports", "POST", data)
77
78 def delete_port(vim_id, port_id):
79     return call(vim_id, "ports/%s" % port_id, "DELETE")
80     
81 def get_port(vim_id, port_id):
82     return call(vim_id, "ports/%s" % port_id, "GET")
83     
84 def list_port(vim_id):
85     return call(vim_id, "ports", "GET")
86
87 ######################################################################
88
89 def create_flavor(vim_id, data):
90     return call(vim_id, "flavors", "POST", data)
91
92 def delete_flavor(vim_id, flavor_id):
93     return call(vim_id, "flavors/%s" % flavor_id, "DELETE")
94     
95 def get_flavor(vim_id, flavor_id):
96     return call(vim_id, "flavors/%s" % flavor_id, "GET")
97     
98 def list_flavor(vim_id):
99     return call(vim_id, "flavors", "GET")
100
101 ######################################################################
102
103 def create_vm(vim_id, data):
104     return call(vim_id, "vms", "POST", data)
105
106 def delete_vm(vim_id, vm_id):
107     return call(vim_id, "vms/%s" % vm_id, "DELETE")
108     
109 def get_vm(vim_id, vm_id):
110     return call(vim_id, "vms/%s" % vm_id, "GET")
111     
112 def list_vm(vim_id):
113     return call(vim_id, "vms", "GET")
114
115 ######################################################################
116
117 def create_volume(vim_id, data):
118     return call(vim_id, "volumes", "POST", data)
119
120 def delete_volume(vim_id, volume_id):
121     return call(vim_id, "volumes/%s" % volume_id, "DELETE")
122     
123 def get_volume(vim_id, volume_id):
124     return call(vim_id, "volumes/%s" % volume_id, "GET")
125     
126 def list_volume(vim_id):
127     return call(vim_id, "volumes", "GET")