restructure osmclient
[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
27 def __init__(self,http=None,ro_http=None,client=None):
28 self._client=client
29 self._ro_http=ro_http
30 self._http=http
31
32 def _attach(self,vim_name,username,secret,vim_tenant_name):
33 tenant_name='osm'
34 tenant=self._get_ro_tenant()
35 if tenant is None:
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))
40
41 vim_account={}
42 vim_account['datacenter']={}
43
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)
48
49 def _detach(self,vim_name):
50 return self._ro_http.delete_cmd('openmano/{}/datacenters/{}'.format('osm',vim_name))
51
52 def create(self, name, vim_access):
53 vim_account={}
54 vim_account['datacenter']={}
55
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")
59
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']
65
66 vim_config = {}
67 vim_config['use_floating_ip'] = False
68
69 if 'floating_ip_pool' in vim_access and vim_access['floating_ip_pool'] is not None:
70 vim_config['use_floating_ip'] = True
71
72 if 'keypair' in vim_access and vim_access['keypair'] is not None:
73 vim_config['keypair'] = vim_access['keypair']
74
75 vim_account['datacenter']['config'] = vim_config
76
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")
80 else:
81 self._attach(name,vim_access['os-username'],vim_access['os-password'],vim_access['os-project-name'])
82
83 def delete(self,vim_name):
84 # first detach
85 resp=self._detach(vim_name)
86 # detach. continue if error, it could be the datacenter is left without attachment
87
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))
90
91 def list(self):
92 resp=self._http.get_cmd('v1/api/operational/datacenters')
93 if not resp or 'rw-launchpad:datacenters' not in resp:
94 return list()
95
96 datacenters=resp['rw-launchpad:datacenters']
97
98 vim_accounts = list()
99 if 'ro-accounts' not in datacenters:
100 return vim_accounts
101
102 tenant=self._get_ro_tenant()
103 if tenant is None:
104 return vim_accounts
105
106 for roaccount in datacenters['ro-accounts']:
107 if 'datacenters' not in roaccount:
108 continue
109 for datacenter in roaccount['datacenters']:
110 vim_accounts.append(self._get_ro_datacenter(datacenter['name'],tenant['uuid']))
111 return vim_accounts
112
113 def _get_ro_tenant(self,name='osm'):
114 resp=self._ro_http.get_cmd('openmano/tenants/{}'.format(name))
115
116 if not resp:
117 return None
118
119 if 'tenant' in resp and 'uuid' in resp['tenant']:
120 return resp['tenant']
121 else:
122 return None
123
124 def _get_ro_datacenter(self,name,tenant_uuid='any'):
125 resp=self._ro_http.get_cmd('openmano/{}/datacenters/{}'.format(tenant_uuid,name))
126 if not resp:
127 raise NotFound("datacenter {} not found".format(name))
128
129 if 'datacenter' in resp and 'uuid' in resp['datacenter']:
130 return resp['datacenter']
131 else:
132 raise NotFound("datacenter {} not found".format(name))
133
134 def get(self,name):
135 tenant=self._get_ro_tenant()
136 if tenant is None:
137 raise NotFound("no ro tenant found")
138
139 return self._get_ro_datacenter(name,tenant['uuid'])
140
141 def get_datacenter(self,name):
142 resp=self._http.get_cmd('v1/api/operational/datacenters')
143 if not resp:
144 return None
145 datacenters=resp['rw-launchpad:datacenters']
146
147 if not resp or 'rw-launchpad:datacenters' not in resp:
148 return None
149 if 'ro-accounts' not in resp['rw-launchpad:datacenters']:
150 return None
151 for roaccount in resp['rw-launchpad:datacenters']['ro-accounts']:
152 if not 'datacenters' in roaccount:
153 continue
154 for datacenter in roaccount['datacenters']:
155 if datacenter['name'] == name:
156 return datacenter
157 return None
158
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:
162 return None
163 return resp['rw-launchpad:resource-orchestrator']