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