9d103028a6af8fafbc60dc2d705eedf7763a8a9f
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
.exceptions
import ClientException
23 from osmclient
.common
.exceptions
import NotFound
29 def __init__(self
, http
=None, client
=None):
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
, sdn_controller
, sdn_port_mapping
):
38 if 'vim-type' not in vim_access
:
39 #'openstack' not in vim_access['vim-type']):
40 raise Exception("vim type not provided")
43 vim_account
['name'] = name
44 vim_account
= self
.update_vim_account_dict(vim_account
, vim_access
)
47 if 'config' in vim_access
and vim_access
['config'] is not None:
48 vim_config
= json
.loads(vim_access
['config'])
50 sdnc
= self
._client
.sdnc
.get(sdn_controller
)
51 vim_config
['sdn-controller'] = sdnc
['_id']
53 with
open(sdn_port_mapping
, 'r') as f
:
54 vim_config
['sdn-port-mapping'] = yaml
.safe_load(f
.read())
56 vim_account
['config'] = vim_config
57 #vim_account['config'] = json.dumps(vim_config)
59 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
60 postfields_dict
=vim_account
)
61 #print 'HTTP CODE: {}'.format(http_code)
62 #print 'RESP: {}'.format(resp)
64 resp
= json
.loads(resp
)
65 if not resp
or 'id' not in resp
:
66 raise ClientException('failed to create vim {}: {}'.format(
71 def update(self
, vim_name
, vim_account
, sdn_controller
, sdn_port_mapping
):
72 vim
= self
.get(vim_name
)
75 if 'config' in vim_account
:
76 if config
=="" and (sdncontroller
or sdn_port_mapping
):
77 raise ClientException("clearing config is incompatible with updating SDN info")
81 vim_config
= json
.loads(vim_account
['config'])
83 sdnc
= self
._client
.sdnc
.get(sdn_controller
)
84 vim_config
['sdn-controller'] = sdnc
['_id']
86 with
open(sdn_port_mapping
, 'r') as f
:
87 vim_config
['sdn-port-mapping'] = yaml
.safe_load(f
.read())
88 vim_account
['config'] = vim_config
89 #vim_account['config'] = json.dumps(vim_config)
90 http_code
, resp
= self
._http
.put_cmd(endpoint
='{}/{}'.format(self
._apiBase
,vim
['_id']),
91 postfields_dict
=vim_account
)
92 #print 'HTTP CODE: {}'.format(http_code)
93 #print 'RESP: {}'.format(resp)
95 resp
= json
.loads(resp
)
96 if not resp
or 'id' not in resp
:
97 raise ClientException('failed to update vim: '.format(resp
))
101 def update_vim_account_dict(self
, vim_account
, vim_access
):
102 vim_account
['vim_type'] = vim_access
['vim-type']
103 vim_account
['description'] = vim_access
['description']
104 vim_account
['vim_url'] = vim_access
['vim-url']
105 vim_account
['vim_user'] = vim_access
['vim-username']
106 vim_account
['vim_password'] = vim_access
['vim-password']
107 vim_account
['vim_tenant_name'] = vim_access
['vim-tenant-name']
110 def get_id(self
, name
):
111 """Returns a VIM id from a VIM name
113 for vim
in self
.list():
114 if name
== vim
['name']:
116 raise NotFound("vim {} not found".format(name
))
118 def delete(self
, vim_name
, force
=False):
120 if not utils
.validate_uuid4(vim_name
):
121 vim_id
= self
.get_id(vim_name
)
124 querystring
= '?FORCE=True'
125 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
126 vim_id
, querystring
))
128 resp
= json
.loads(resp
)
129 #print 'RESP: {}'.format(resp)
131 print 'Deletion in progress'
132 elif http_code
== 204:
135 raise ClientException("failed to delete vim {} - {}".format(vim_name
, resp
))
137 def list(self
, filter=None):
138 """Returns a list of VIM accounts
142 filter_string
= '?{}'.format(filter)
143 resp
= self
._http
.get_cmd('{}{}'.format(self
._apiBase
,filter_string
))
147 for datacenter
in resp
:
148 vim_accounts
.append({"name": datacenter
['name'], "uuid": datacenter
['_id']
149 if '_id' in datacenter
else None})
153 """Returns a VIM account based on name or id
156 if not utils
.validate_uuid4(name
):
157 vim_id
= self
.get_id(name
)
158 resp
= self
._http
.get_cmd('{}/{}'.format(self
._apiBase
,vim_id
))
159 if not resp
or '_id' not in resp
:
160 raise ClientException('failed to get vim info: '.format(
164 raise NotFound("vim {} not found".format(name
))