Add vm 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 ######################################################################
28
29 def create_image(vim_id, data):
30     return call(vim_id, "images", "POST", data)
31
32 def delete_image(vim_id, image_id):
33     return call(vim_id, "images/%s" % image_id, "DELETE")
34     
35 def get_image(vim_id, image_id):
36     return call(vim_id, "images/%s" % image_id, "GET")
37     
38 def list_image(vim_id):
39     return call(vim_id, "images", "GET")
40
41 ######################################################################
42
43 def create_network(vim_id, data):
44     return call(vim_id, "networks", "POST", data)
45
46 def delete_network(vim_id, network_id):
47     return call(vim_id, "networks/%s" % network_id, "DELETE")
48     
49 def get_network(vim_id, network_id):
50     return call(vim_id, "networks/%s" % network_id, "GET")
51     
52 def list_network(vim_id):
53     return call(vim_id, "networks", "GET")
54
55 ######################################################################
56
57 def create_subnet(vim_id, data):
58     return call(vim_id, "subnets", "POST", data)
59
60 def delete_subnet(vim_id, subnet_id):
61     return call(vim_id, "subnets/%s" % subnet_id, "DELETE")
62     
63 def get_subnet(vim_id, subnet_id):
64     return call(vim_id, "subnets/%s" % subnet_id, "GET")
65     
66 def list_subnet(vim_id):
67     return call(vim_id, "subnets", "GET")
68
69 ######################################################################
70
71 def create_port(vim_id, data):
72     return call(vim_id, "ports", "POST", data)
73
74 def delete_port(vim_id, port_id):
75     return call(vim_id, "ports/%s" % port_id, "DELETE")
76     
77 def get_port(vim_id, port_id):
78     return call(vim_id, "ports/%s" % port_id, "GET")
79     
80 def list_port(vim_id):
81     return call(vim_id, "ports", "GET")
82
83 ######################################################################
84
85 def create_flavor(vim_id, data):
86     return call(vim_id, "flavors", "POST", data)
87
88 def delete_flavor(vim_id, flavor_id):
89     return call(vim_id, "flavors/%s" % flavor_id, "DELETE")
90     
91 def get_flavor(vim_id, flavor_id):
92     return call(vim_id, "flavors/%s" % flavor_id, "GET")
93     
94 def list_flavor(vim_id):
95     return call(vim_id, "flavors", "GET")
96
97 ######################################################################
98
99 def create_vm(vim_id, data):
100     return call(vim_id, "vms", "POST", data)
101
102 def delete_vm(vim_id, vm_id):
103     return call(vim_id, "vms/%s" % vm_id, "DELETE")
104     
105 def get_vm(vim_id, vm_id):
106     return call(vim_id, "vms/%s" % vm_id, "GET")
107     
108 def list_vm(vim_id):
109     return call(vim_id, "vms", "GET")
110
111 ######################################################################
112
113 def create_volume(vim_id, data):
114     return call(vim_id, "volumes", "POST", data)
115
116 def delete_volume(vim_id, volume_id):
117     return call(vim_id, "volumes/%s" % volume_id, "DELETE")
118     
119 def get_volume(vim_id, volume_id):
120     return call(vim_id, "volumes/%s" % volume_id, "GET")
121     
122 def list_volume(vim_id):
123     return call(vim_id, "volumes", "GET")