update Makefile, pep8, scaling
[osm/osmclient.git] / osmclient / v1 / vim.py
1 # Copyright 2017 Sandvine
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.exceptions import ClientException
22 from osmclient.common.exceptions import NotFound
23
24
25 class Vim(object):
26 def __init__(self, http=None, ro_http=None, client=None):
27 self._client = client
28 self._ro_http = ro_http
29 self._http = http
30
31 def _attach(self, vim_name, username, secret, vim_tenant_name):
32 tenant_name = 'osm'
33 tenant = self._get_ro_tenant()
34 if tenant is None:
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))
39
40 vim_account = {}
41 vim_account['datacenter'] = {}
42
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)
49
50 def _detach(self, vim_name):
51 return self._ro_http.delete_cmd('openmano/{}/datacenters/{}'
52 .format('osm', vim_name))
53
54 def create(self, name, vim_access):
55 vim_account = {}
56 vim_account['datacenter'] = {}
57
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")
62
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']
68
69 vim_config = {}
70 vim_config['use_floating_ip'] = False
71
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
75
76 if 'keypair' in vim_access and vim_access['keypair'] is not None:
77 vim_config['keypair'] = vim_access['keypair']
78
79 vim_account['datacenter']['config'] = vim_config
80
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")
84 else:
85 self._attach(name,
86 vim_access['os-username'],
87 vim_access['os-password'],
88 vim_access['os-project-name'])
89
90 def delete(self, vim_name):
91 # first detach
92 self._detach(vim_name)
93 # detach. continue if error,
94 # it could be the datacenter is left without attachment
95
96 if 'result' not in self._ro_http.delete_cmd('openmano/datacenters/{}'
97 .format(vim_name)):
98 raise ClientException("failed to delete vim {}".format(vim_name))
99
100 def list(self):
101 resp = self._http.get_cmd('v1/api/operational/datacenters')
102 if not resp or 'rw-launchpad:datacenters' not in resp:
103 return list()
104
105 datacenters = resp['rw-launchpad:datacenters']
106
107 vim_accounts = list()
108 if 'ro-accounts' not in datacenters:
109 return vim_accounts
110
111 tenant = self._get_ro_tenant()
112 if tenant is None:
113 return vim_accounts
114
115 for roaccount in datacenters['ro-accounts']:
116 if 'datacenters' not in roaccount:
117 continue
118 for datacenter in roaccount['datacenters']:
119 vim_accounts.append(self._get_ro_datacenter(datacenter['name'],
120 tenant['uuid']))
121 return vim_accounts
122
123 def _get_ro_tenant(self, name='osm'):
124 resp = self._ro_http.get_cmd('openmano/tenants/{}'.format(name))
125
126 if not resp:
127 return None
128
129 if 'tenant' in resp and 'uuid' in resp['tenant']:
130 return resp['tenant']
131 else:
132 return None
133
134 def _get_ro_datacenter(self, name, tenant_uuid='any'):
135 resp = self._ro_http.get_cmd('openmano/{}/datacenters/{}'
136 .format(tenant_uuid, name))
137 if not resp:
138 raise NotFound("datacenter {} not found".format(name))
139
140 if 'datacenter' in resp and 'uuid' in resp['datacenter']:
141 return resp['datacenter']
142 else:
143 raise NotFound("datacenter {} not found".format(name))
144
145 def get(self, name):
146 tenant = self._get_ro_tenant()
147 if tenant is None:
148 raise NotFound("no ro tenant found")
149
150 return self._get_ro_datacenter(name, tenant['uuid'])
151
152 def get_datacenter(self, name):
153 resp = self._http.get_cmd('v1/api/operational/datacenters')
154 if not resp:
155 return None
156
157 if not resp or 'rw-launchpad:datacenters' not in resp:
158 return None
159 if 'ro-accounts' not in resp['rw-launchpad:datacenters']:
160 return None
161 for roaccount in resp['rw-launchpad:datacenters']['ro-accounts']:
162 if 'datacenters' not in roaccount:
163 continue
164 for datacenter in roaccount['datacenters']:
165 if datacenter['name'] == name:
166 return datacenter
167 return None
168
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:
172 return None
173 return resp['rw-launchpad:resource-orchestrator']