bf10e5fd42e9be9f1f38932d2e6208965ef9d6d0
1 # Copyright 2018 Telefonica
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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
21 from osmclient
.common
import utils
22 from osmclient
.common
import wait
as WaitForStatus
23 from osmclient
.common
.exceptions
import ClientException
24 from osmclient
.common
.exceptions
import NotFound
30 def __init__(self
, http
=None, client
=None):
33 self
._apiName
= '/admin'
34 self
._apiVersion
= '/v1'
35 self
._apiResource
= '/wim_accounts'
36 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
37 self
._apiVersion
, self
._apiResource
)
39 def _wait(self
, id, deleteFlag
=False):
40 # Endpoint to get operation status
41 apiUrlStatus
= '{}{}{}'.format(self
._apiName
, self
._apiVersion
, '/wim_accounts')
42 # Wait for status for WIM instance creation/deletion
43 WaitForStatus
.wait_for_status(
46 WaitForStatus
.TIMEOUT_WIM_OPERATION
,
49 deleteFlag
=deleteFlag
)
51 def _get_id_for_wait(self
, name
):
52 # Returns id of name, or the id itself if given as argument
53 for wim
in self
.list():
54 if name
== wim
['uuid']:
56 for wim
in self
.list():
57 if name
== wim
['name']:
61 def create(self
, name
, wim_input
, wim_port_mapping
=None, wait
=False):
62 if 'wim_type' not in wim_input
:
63 raise Exception("wim type not provided")
65 wim_account
= wim_input
66 wim_account
["name"] = name
69 if 'config' in wim_input
and wim_input
['config'] is not None:
70 wim_config
= yaml
.safe_load(wim_input
['config'])
72 with
open(wim_port_mapping
, 'r') as f
:
73 wim_config
['wim_port_mapping'] = yaml
.safe_load(f
.read())
75 wim_account
['config'] = wim_config
76 #wim_account['config'] = json.dumps(wim_config)
78 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
79 postfields_dict
=wim_account
)
80 #print 'HTTP CODE: {}'.format(http_code)
81 #print 'RESP: {}'.format(resp)
82 if http_code
in (200, 201, 202, 204):
84 resp
= json
.loads(resp
)
85 if not resp
or 'id' not in resp
:
86 raise ClientException('unexpected response from server - {}'.format(
89 # Wait for status for WIM instance creation
90 self
._wait
(resp
.get('id'))
96 msg
= json
.loads(resp
)
99 raise ClientException("failed to create wim {} - {}".format(name
, msg
))
101 def update(self
, wim_name
, wim_account
, wim_port_mapping
=None, wait
=False):
102 wim
= self
.get(wim_name
)
103 wim_id_for_wait
= self
._get
_id
_for
_wait
(wim_name
)
105 if 'config' in wim_account
:
106 if wim_account
.get('config')=="" and (wim_port_mapping
):
107 raise ClientException("clearing config is incompatible with updating SDN info")
108 if wim_account
.get('config')=="":
111 wim_config
= yaml
.safe_load(wim_account
['config'])
113 with
open(wim_port_mapping
, 'r') as f
:
114 wim_config
['wim_port_mapping'] = yaml
.safe_load(f
.read())
115 wim_account
['config'] = wim_config
116 #wim_account['config'] = json.dumps(wim_config)
117 http_code
, resp
= self
._http
.put_cmd(endpoint
='{}/{}'.format(self
._apiBase
,wim
['_id']),
118 postfields_dict
=wim_account
)
119 #print 'HTTP CODE: {}'.format(http_code)
120 #print 'RESP: {}'.format(resp)
121 if http_code
in (200, 201, 202, 204):
123 # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
124 # Use the previously obtained id instead.
125 wait_id
= wim_id_for_wait
126 # Wait for status for WIM instance update
134 msg
= json
.loads(resp
)
137 raise ClientException("failed to update wim {} - {}".format(wim_name
, msg
))
139 def update_wim_account_dict(self
, wim_account
, wim_input
):
141 wim_account
['wim_type'] = wim_input
['wim_type']
142 wim_account
['description'] = wim_input
['description']
143 wim_account
['wim_url'] = wim_input
['url']
144 wim_account
['user'] = wim_input
.get('wim-username')
145 wim_account
['password'] = wim_input
.get('wim-password')
148 def get_id(self
, name
):
149 """Returns a WIM id from a WIM name
151 for wim
in self
.list():
152 if name
== wim
['name']:
154 raise NotFound("wim {} not found".format(name
))
156 def delete(self
, wim_name
, force
=False, wait
=False):
158 wim_id_for_wait
= self
._get
_id
_for
_wait
(wim_name
)
159 if not utils
.validate_uuid4(wim_name
):
160 wim_id
= self
.get_id(wim_name
)
163 querystring
= '?FORCE=True'
164 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
165 wim_id
, querystring
))
166 # print 'HTTP CODE: {}'.format(http_code)
167 # print 'RESP: {}'.format(resp)
168 # print 'WIM_ID: {}'.format(wim_id)
171 # 'resp' may be None.
172 # In that case, 'resp['id']' cannot be used, so use the previously obtained id instead
173 wait_id
= wim_id_for_wait
175 resp
= json
.loads(resp
)
176 wait_id
= resp
.get('id')
177 # Wait for status for WIM account deletion
178 self
._wait
(wait_id
, deleteFlag
=True)
180 print('Deletion in progress')
181 elif http_code
== 204:
187 msg
= json
.loads(resp
)
190 raise ClientException("failed to delete wim {} - {}".format(wim_name
, msg
))
192 def list(self
, filter=None):
193 """Returns a list of VIM accounts
197 filter_string
= '?{}'.format(filter)
198 resp
= self
._http
.get_cmd('{}{}'.format(self
._apiBase
,filter_string
))
202 for datacenter
in resp
:
203 wim_accounts
.append({"name": datacenter
['name'], "uuid": datacenter
['_id']
204 if '_id' in datacenter
else None})
208 """Returns a VIM account based on name or id
211 if not utils
.validate_uuid4(name
):
212 wim_id
= self
.get_id(name
)
213 resp
= self
._http
.get_cmd('{}/{}'.format(self
._apiBase
,wim_id
))
214 if not resp
or '_id' not in resp
:
215 raise ClientException('failed to get wim info: '.format(
219 raise NotFound("wim {} not found".format(name
))