Add port call of vimdriver
[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 from lcm.pub.utils.restcall import req_by_msb
16 from .exceptions import VimException
17
18 VIM_DRIVER_BASE_URL = "openoapi/vimdriver/v1"
19
20 def call(vim_id, res, method, data=''):
21     url = "%/%s/%s" % (VIM_DRIVER_BASE_URL, vim_id, res)
22     ret = req_by_msb(url, method, data)
23     if ret[0] > 0:
24         raise VimException(ret[1], ret[2])
25     return json.JSONDecoder().decode(ret[1]) if ret[1] else {}
26
27 def create_image(vim_id, data):
28     return call(vim_id, "images", "POST", data)
29
30 def delete_image(vim_id, image_id):
31     return call(vim_id, "images/%s" % image_id, "DELETE")
32     
33 def get_image(vim_id, image_id):
34     return call(vim_id, "images/%s" % image_id, "GET")
35     
36 def list_image(vim_id):
37     return call(vim_id, "images", "GET")
38
39 def create_network(vim_id, data):
40     return call(vim_id, "networks", "POST", data)
41
42 def delete_network(vim_id, network_id):
43     return call(vim_id, "networks/%s" % network_id, "DELETE")
44     
45 def get_network(vim_id, network_id):
46     return call(vim_id, "networks/%s" % network_id, "GET")
47     
48 def list_network(vim_id):
49     return call(vim_id, "networks", "GET")
50     
51 def create_subnet(vim_id, data):
52     return call(vim_id, "subnets", "POST", data)
53
54 def delete_subnet(vim_id, subnet_id):
55     return call(vim_id, "subnets/%s" % subnet_id, "DELETE")
56     
57 def get_subnet(vim_id, subnet_id):
58     return call(vim_id, "subnets/%s" % subnet_id, "GET")
59     
60 def list_subnet(vim_id):
61     return call(vim_id, "subnets", "GET")
62
63 def create_port(vim_id, data):
64     return call(vim_id, "ports", "POST", data)
65
66 def delete_port(vim_id, port_id):
67     return call(vim_id, "ports/%s" % port_id, "DELETE")
68     
69 def get_port(vim_id, port_id):
70     return call(vim_id, "ports/%s" % port_id, "GET")
71     
72 def list_port(vim_id):
73     return call(vim_id, "ports", "GET")
74     
75 def create_flavor(vim_id, data):
76     return call(vim_id, "flavors", "POST", data)
77
78 def delete_flavor(vim_id, flavor_id):
79     return call(vim_id, "flavors/%s" % flavor_id, "DELETE")
80     
81 def get_flavor(vim_id, flavor_id):
82     return call(vim_id, "flavors/%s" % flavor_id, "GET")
83     
84 def list_flavor(vim_id):
85     return call(vim_id, "flavors", "GET")