79701f858cb908c4c5706612085a3a46d4ff80b4
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
27 def __init__(self
,http
=None,ro_http
=None,client
=None):
32 def _attach(self
,vim_name
,username
,secret
,vim_tenant_name
):
34 tenant
=self
._get
_ro
_tenant
()
36 raise ClientException("tenant {} not found".format(tenant_name
))
37 datacenter
= self
._get
_ro
_datacenter
(vim_name
)
38 if datacenter
is None:
39 raise Exception('datacenter {} not found'.format(vim_name
))
42 vim_account
['datacenter']={}
44 vim_account
['datacenter']['vim_username'] = username
45 vim_account
['datacenter']['vim_password'] = secret
46 vim_account
['datacenter']['vim_tenant_name'] = vim_tenant_name
47 return self
._ro
_http
.post_cmd('openmano/{}/datacenters/{}'.format(tenant
['uuid'],datacenter
['uuid']),vim_account
)
49 def _detach(self
,vim_name
):
50 return self
._ro
_http
.delete_cmd('openmano/{}/datacenters/{}'.format('osm',vim_name
))
52 def create(self
, name
, vim_access
):
54 vim_account
['datacenter']={}
56 # currently assumes vim_acc
57 if 'vim-type' not in vim_access
or 'openstack' not in vim_access
['vim-type']:
58 raise Exception("only vim type openstack support")
60 vim_account
['datacenter']['name'] = name
61 vim_account
['datacenter']['type'] = vim_access
['vim-type']
62 vim_account
['datacenter']['vim_url'] = vim_access
['os-url']
63 vim_account
['datacenter']['vim_url_admin'] = vim_access
['os-url']
64 vim_account
['datacenter']['description'] = vim_access
['description']
67 vim_config
['use_floating_ip'] = False
69 if 'floating_ip_pool' in vim_access
and vim_access
['floating_ip_pool'] is not None:
70 vim_config
['use_floating_ip'] = True
72 if 'keypair' in vim_access
and vim_access
['keypair'] is not None:
73 vim_config
['keypair'] = vim_access
['keypair']
75 vim_account
['datacenter']['config'] = vim_config
77 resp
= self
._ro
_http
.post_cmd('openmano/datacenters',vim_account
)
78 if resp
and 'error' in resp
:
79 raise ClientException("failed to create vim")
81 self
._attach
(name
,vim_access
['os-username'],vim_access
['os-password'],vim_access
['os-project-name'])
83 def delete(self
,vim_name
):
85 resp
=self
._detach
(vim_name
)
86 # detach. continue if error, it could be the datacenter is left without attachment
88 if 'result' not in self
._ro
_http
.delete_cmd('openmano/datacenters/{}'.format(vim_name
)):
89 raise ClientException("failed to delete vim {}".format(vim_name
))
92 resp
=self
._http
.get_cmd('v1/api/operational/datacenters')
93 if not resp
or 'rw-launchpad:datacenters' not in resp
:
96 datacenters
=resp
['rw-launchpad:datacenters']
99 if 'ro-accounts' not in datacenters
:
102 tenant
=self
._get
_ro
_tenant
()
106 for roaccount
in datacenters
['ro-accounts']:
107 if 'datacenters' not in roaccount
:
109 for datacenter
in roaccount
['datacenters']:
110 vim_accounts
.append(self
._get
_ro
_datacenter
(datacenter
['name'],tenant
['uuid']))
113 def _get_ro_tenant(self
,name
='osm'):
114 resp
=self
._ro
_http
.get_cmd('openmano/tenants/{}'.format(name
))
119 if 'tenant' in resp
and 'uuid' in resp
['tenant']:
120 return resp
['tenant']
124 def _get_ro_datacenter(self
,name
,tenant_uuid
='any'):
125 resp
=self
._ro
_http
.get_cmd('openmano/{}/datacenters/{}'.format(tenant_uuid
,name
))
127 raise NotFound("datacenter {} not found".format(name
))
129 if 'datacenter' in resp
and 'uuid' in resp
['datacenter']:
130 return resp
['datacenter']
132 raise NotFound("datacenter {} not found".format(name
))
135 tenant
=self
._get
_ro
_tenant
()
137 raise NotFound("no ro tenant found")
139 return self
._get
_ro
_datacenter
(name
,tenant
['uuid'])
141 def get_datacenter(self
,name
):
142 resp
=self
._http
.get_cmd('v1/api/operational/datacenters')
145 datacenters
=resp
['rw-launchpad:datacenters']
147 if not resp
or 'rw-launchpad:datacenters' not in resp
:
149 if 'ro-accounts' not in resp
['rw-launchpad:datacenters']:
151 for roaccount
in resp
['rw-launchpad:datacenters']['ro-accounts']:
152 if not 'datacenters' in roaccount
:
154 for datacenter
in roaccount
['datacenters']:
155 if datacenter
['name'] == name
:
159 def get_resource_orchestrator(self
):
160 resp
=self
._http
.get_cmd('v1/api/operational/resource-orchestrator')
161 if not resp
or 'rw-launchpad:resource-orchestrator' not in resp
:
163 return resp
['rw-launchpad:resource-orchestrator']