return http_code,data tuple in DELETE operations for sol005 client
[osm/osmclient.git] / osmclient / sol005 / vim.py
1 # Copyright 2018 Telefonica
2 #
3 # All Rights Reserved.
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 OSM vim API handling
19 """
20
21 from osmclient.common import utils
22 from osmclient.common.exceptions import ClientException
23 from osmclient.common.exceptions import NotFound
24 import yaml
25
26
27 class Vim(object):
28 def __init__(self, http=None, client=None):
29 self._http = http
30 self._client = client
31 self._apiName = '/admin'
32 self._apiVersion = '/v1'
33 self._apiResource = '/vims'
34 self._apiBase = '{}{}{}'.format(self._apiName,
35 self._apiVersion, self._apiResource)
36 def create(self, name, vim_access):
37 if 'vim-type' not in vim_access:
38 #'openstack' not in vim_access['vim-type']):
39 raise Exception("vim type not provided")
40
41 vim_account = {}
42 vim_config = {'hello': 'hello'}
43 vim_account['name'] = name
44 vim_account = self.update_vim_account_dict(vim_account, vim_access)
45
46 vim_config = {}
47 if 'config' in vim_access and vim_access['config'] is not None:
48 vim_config = yaml.load(vim_access['config'])
49
50 vim_account['config'] = vim_config
51
52 resp = self._http.post_cmd(endpoint=self._apiBase,
53 postfields_dict=vim_account)
54 if not resp or 'id' not in resp:
55 raise ClientException('failed to create vim {}: {}'.format(
56 name, resp))
57 else:
58 print resp['id']
59
60 def update(self, vim_name, vim_account):
61 vim = self.get(vim_name)
62 resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']),
63 postfields_dict=vim_account)
64 if not resp or '_id' not in resp:
65 raise ClientException('failed to update vim: '.format(
66 resp))
67 else:
68 print resp['_id']
69
70 def update_vim_account_dict(self, vim_account, vim_access):
71 vim_account['vim_type'] = vim_access['vim-type']
72 vim_account['description'] = vim_access['description']
73 vim_account['vim_url'] = vim_access['vim-url']
74 vim_account['vim_user'] = vim_access['vim-username']
75 vim_account['vim_password'] = vim_access['vim-password']
76 vim_account['vim_tenant_name'] = vim_access['vim-tenant-name']
77 return vim_account
78
79 def get_id(self, name):
80 """Returns a VIM id from a VIM name
81 """
82 for vim in self.list():
83 if name == vim['name']:
84 return vim['uuid']
85 raise NotFound("vim {} not found".format(name))
86
87 def delete(self, vim_name):
88 vim_id = vim_name
89 if not utils.validate_uuid4(vim_name):
90 vim_id = self.get_id(vim_name)
91 http_code, resp = self._http.delete_cmd('{}/{}'.format(self._apiBase,vim_id))
92 #print 'RESP: {}'.format(resp)
93 if http_code == 202:
94 print 'Deletion in progress'
95 elif http_code == 204:
96 print 'Deleted'
97 else:
98 raise ClientException("failed to delete vim {} - {}".format(vim_name, resp))
99
100 def list(self, filter=None):
101 """Returns a list of VIM accounts
102 """
103 filter_string = ''
104 if filter:
105 filter_string = '?{}'.format(filter)
106 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
107 if not resp:
108 return list()
109 vim_accounts = []
110 for datacenter in resp:
111 vim_accounts.append({"name": datacenter['name'], "uuid": datacenter['_id']
112 if '_id' in datacenter else None})
113 return vim_accounts
114
115 def get(self, name):
116 """Returns a VIM account based on name or id
117 """
118 vim_id = name
119 if not utils.validate_uuid4(name):
120 vim_id = self.get_id(name)
121 resp = self._http.get_cmd('{}/{}'.format(self._apiBase,vim_id))
122 if not resp or '_id' not in resp:
123 raise ClientException('failed to get vim info: '.format(
124 resp))
125 else:
126 return resp
127 raise NotFound("vim {} not found".format(name))
128