1 # Copyright 2017 Sandvine
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
.exceptions
import ClientException
22 from osmclient
.common
.exceptions
import NotFound
26 def __init__(self
, http
=None, ro_http
=None, client
=None):
28 self
._ro
_http
= ro_http
31 def _attach(self
, vim_name
, username
, secret
, vim_tenant_name
):
33 tenant
= self
._get
_ro
_tenant
()
35 raise ClientException("tenant {} not found".format(tenant_name
))
36 datacenter
= self
._get
_ro
_datacenter
(vim_name
)
37 if datacenter
is None:
38 raise Exception('datacenter {} not found'.format(vim_name
))
41 vim_account
['datacenter'] = {}
43 vim_account
['datacenter']['vim_username'] = username
44 vim_account
['datacenter']['vim_password'] = secret
45 vim_account
['datacenter']['vim_tenant_name'] = vim_tenant_name
46 return self
._ro
_http
.post_cmd('openmano/{}/datacenters/{}'
47 .format(tenant
['uuid'],
48 datacenter
['uuid']), vim_account
)
50 def _detach(self
, vim_name
):
51 return self
._ro
_http
.delete_cmd('openmano/{}/datacenters/{}'
52 .format('osm', vim_name
))
54 def create(self
, name
, vim_access
):
56 vim_account
['datacenter'] = {}
58 # currently assumes vim_acc
59 if ('vim-type' not in vim_access
or
60 'openstack' not in vim_access
['vim-type']):
61 raise Exception("only vim type openstack support")
63 vim_account
['datacenter']['name'] = name
64 vim_account
['datacenter']['type'] = vim_access
['vim-type']
65 vim_account
['datacenter']['vim_url'] = vim_access
['os-url']
66 vim_account
['datacenter']['vim_url_admin'] = vim_access
['os-url']
67 vim_account
['datacenter']['description'] = vim_access
['description']
70 vim_config
['use_floating_ip'] = False
72 if ('floating_ip_pool' in vim_access
and
73 vim_access
['floating_ip_pool'] is not None):
74 vim_config
['use_floating_ip'] = True
76 if 'keypair' in vim_access
and vim_access
['keypair'] is not None:
77 vim_config
['keypair'] = vim_access
['keypair']
79 vim_account
['datacenter']['config'] = vim_config
81 resp
= self
._ro
_http
.post_cmd('openmano/datacenters', vim_account
)
82 if resp
and 'error' in resp
:
83 raise ClientException("failed to create vim")
86 vim_access
['os-username'],
87 vim_access
['os-password'],
88 vim_access
['os-project-name'])
90 def delete(self
, vim_name
):
92 self
._detach
(vim_name
)
93 # detach. continue if error,
94 # it could be the datacenter is left without attachment
96 if 'result' not in self
._ro
_http
.delete_cmd('openmano/datacenters/{}'
98 raise ClientException("failed to delete vim {}".format(vim_name
))
101 resp
= self
._http
.get_cmd('v1/api/operational/datacenters')
102 if not resp
or 'rw-launchpad:datacenters' not in resp
:
105 datacenters
= resp
['rw-launchpad:datacenters']
107 vim_accounts
= list()
108 if 'ro-accounts' not in datacenters
:
111 tenant
= self
._get
_ro
_tenant
()
115 for roaccount
in datacenters
['ro-accounts']:
116 if 'datacenters' not in roaccount
:
118 for datacenter
in roaccount
['datacenters']:
119 vim_accounts
.append(self
._get
_ro
_datacenter
(datacenter
['name'],
123 def _get_ro_tenant(self
, name
='osm'):
124 resp
= self
._ro
_http
.get_cmd('openmano/tenants/{}'.format(name
))
129 if 'tenant' in resp
and 'uuid' in resp
['tenant']:
130 return resp
['tenant']
134 def _get_ro_datacenter(self
, name
, tenant_uuid
='any'):
135 resp
= self
._ro
_http
.get_cmd('openmano/{}/datacenters/{}'
136 .format(tenant_uuid
, name
))
138 raise NotFound("datacenter {} not found".format(name
))
140 if 'datacenter' in resp
and 'uuid' in resp
['datacenter']:
141 return resp
['datacenter']
143 raise NotFound("datacenter {} not found".format(name
))
146 tenant
= self
._get
_ro
_tenant
()
148 raise NotFound("no ro tenant found")
150 return self
._get
_ro
_datacenter
(name
, tenant
['uuid'])
152 def get_datacenter(self
, name
):
153 resp
= self
._http
.get_cmd('v1/api/operational/datacenters')
157 if not resp
or 'rw-launchpad:datacenters' not in resp
:
159 if 'ro-accounts' not in resp
['rw-launchpad:datacenters']:
161 for roaccount
in resp
['rw-launchpad:datacenters']['ro-accounts']:
162 if 'datacenters' not in roaccount
:
164 for datacenter
in roaccount
['datacenters']:
165 if datacenter
['name'] == name
:
169 def get_resource_orchestrator(self
):
170 resp
= self
._http
.get_cmd('v1/api/operational/resource-orchestrator')
171 if not resp
or 'rw-launchpad:resource-orchestrator' not in resp
:
173 return resp
['rw-launchpad:resource-orchestrator']