2 # -*- coding: utf-8 -*-
5 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
6 # This file is part of openmano
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact with: nfvlabs@tid.es
26 asyncio RO python client to interact with RO-server
36 from urllib
.parse
import quote
38 from copy
import deepcopy
40 __author__
= "Alfonso Tierno, Pablo Montes"
41 __date__
= "$09-Jan-2018 09:09:48$"
42 __version__
= "0.1.0-r470"
43 version_date
= "Jan 2018"
46 class ROClientException(Exception):
47 def __init__(self
, message
, http_code
=400):
48 self
.http_code
= http_code
49 Exception.__init
__(self
, message
)
50 """Common Exception for all openmano client exceptions"""
53 def remove_envelop(item
, indata
=None):
55 Obtain the useful data removing the envelop. It goes through the vnfd or nsd catalog and returns the
57 :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns'
58 :param indata: Content to be inspected
59 :return: the useful part of indata (a reference, not a new dictionay)
65 if clean_indata
.get('vnfd:vnfd-catalog'):
66 clean_indata
= clean_indata
['vnfd:vnfd-catalog']
67 elif clean_indata
.get('vnfd-catalog'):
68 clean_indata
= clean_indata
['vnfd-catalog']
69 if clean_indata
.get('vnfd'):
70 if not isinstance(clean_indata
['vnfd'], list) or len(clean_indata
['vnfd']) != 1:
71 raise ROClientException("'vnfd' must be a list only one element")
72 clean_indata
= clean_indata
['vnfd'][0]
74 if clean_indata
.get('nsd:nsd-catalog'):
75 clean_indata
= clean_indata
['nsd:nsd-catalog']
76 elif clean_indata
.get('nsd-catalog'):
77 clean_indata
= clean_indata
['nsd-catalog']
78 if clean_indata
.get('nsd'):
79 if not isinstance(clean_indata
['nsd'], list) or len(clean_indata
['nsd']) != 1:
80 raise ROClientException("'nsd' must be a list only one element")
81 clean_indata
= clean_indata
['nsd'][0]
83 if len(indata
) == 1 and "sdn_controller" in indata
:
84 clean_indata
= indata
["sdn_controller"]
85 elif item
== "tenant":
86 if len(indata
) == 1 and "tenant" in indata
:
87 clean_indata
= indata
["tenant"]
88 elif item
in ("vim", "vim_account", "datacenters"):
89 if len(indata
) == 1 and "datacenter" in indata
:
90 clean_indata
= indata
["datacenter"]
91 elif item
== "ns" or item
== "instances":
92 if len(indata
) == 1 and "instance" in indata
:
93 clean_indata
= indata
["instance"]
95 assert False, "remove_envelop with unknown item {}".format(item
)
101 headers_req
= {'Accept': 'application/yaml', 'content-type': 'application/yaml'}
102 client_to_RO
= {'tenant': 'tenants', 'vim': 'datacenters', 'vim_account': 'datacenters', 'sdn': 'sdn_controllers',
103 'vnfd': 'vnfs', 'nsd': 'scenarios',
105 mandatory_for_create
= {
106 'tenant': ("name", ),
107 'vnfd': ("name", "id", "connection-point", "vdu"),
108 'nsd': ("name", "id", "constituent-vnfd"),
109 'ns': ("name", "scenario", "datacenter"),
110 'vim': ("name", "vim_url"),
112 'sdn': ("name", "port", 'ip', 'dpid', 'type'),
117 def __init__(self
, loop
, endpoint_url
, **kwargs
):
119 self
.endpoint_url
= endpoint_url
121 self
.username
= kwargs
.get("username")
122 self
.password
= kwargs
.get("password")
123 self
.tenant_id_name
= kwargs
.get("tenant")
125 self
.datacenter_id_name
= kwargs
.get("datacenter")
126 self
.datacenter
= None
127 logger_name
= kwargs
.get('logger_name', 'ROClient')
128 self
.logger
= logging
.getLogger(logger_name
)
129 if kwargs
.get("loglevel"):
130 self
.logger
.setLevel(kwargs
["loglevel"])
132 requests
= kwargs
.get("TODO remove")
134 def __getitem__(self
, index
):
135 if index
== 'tenant':
136 return self
.tenant_id_name
137 elif index
== 'datacenter':
138 return self
.datacenter_id_name
139 elif index
== 'username':
141 elif index
== 'password':
143 elif index
== 'endpoint_url':
144 return self
.endpoint_url
146 raise KeyError("Invalid key '%s'" %str
(index
))
148 def __setitem__(self
,index
, value
):
149 if index
== 'tenant':
150 self
.tenant_id_name
= value
151 elif index
== 'datacenter' or index
== 'vim':
152 self
.datacenter_id_name
= value
153 elif index
== 'username':
154 self
.username
= value
155 elif index
== 'password':
156 self
.password
= value
157 elif index
== 'endpoint_url':
158 self
.endpoint_url
= value
160 raise KeyError("Invalid key '{}'".format(index
))
161 self
.tenant
= None # force to reload tenant with different credentials
162 self
.datacenter
= None # force to reload datacenter with different credentials
164 def _parse(self
, descriptor
, descriptor_format
, response
=False):
166 if descriptor_format
and descriptor_format
!= "json" and descriptor_format
!= "yaml":
167 raise ROClientException("'descriptor_format' must be a 'json' or 'yaml' text")
168 if descriptor_format
!= "json":
170 return yaml
.load(descriptor
)
171 except yaml
.YAMLError
as exc
:
173 if hasattr(exc
, 'problem_mark'):
174 mark
= exc
.problem_mark
175 error_pos
= " at line:{} column:{}s".format(mark
.line
+1, mark
.column
+1)
176 error_text
= "yaml format error" + error_pos
177 elif descriptor_format
!= "yaml":
179 return json
.loads(descriptor
)
180 except Exception as e
:
182 error_text
= "json format error" + str(e
)
185 raise ROClientException(error_text
)
186 raise ROClientException(error_text
)
188 def _parse_yaml(self
, descriptor
, response
=False):
190 return yaml
.load(descriptor
)
191 except yaml
.YAMLError
as exc
:
193 if hasattr(exc
, 'problem_mark'):
194 mark
= exc
.problem_mark
195 error_pos
= " at line:{} column:{}s".format(mark
.line
+1, mark
.column
+1)
196 error_text
= "yaml format error" + error_pos
198 raise ROClientException(error_text
)
199 raise ROClientException(error_text
)
202 def check_if_uuid(uuid_text
):
204 Check if text correspond to an uuid foramt
206 :return: True if it is an uuid False if not
211 except (ValueError, TypeError):
215 def _create_envelop(item
, indata
=None):
217 Returns a new dict that incledes indata with the expected envelop
218 :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns'
219 :param indata: Content to be enveloped
220 :return: a new dic with {<envelop>: {indata} } where envelop can be e.g. tenant, datacenter, ...
223 return {'vnfd-catalog': {'vnfd': [indata
]}}
225 return {'nsd-catalog': {'nsd': [indata
]}}
226 elif item
== "tenant":
227 return {'tenant': indata
}
228 elif item
in ("vim", "vim_account", "datacenter"):
229 return {'datacenter': indata
}
230 elif item
== "ns" or item
== "instances":
231 return {'instance': indata
}
233 assert False, "_create_envelop with unknown item {}".format(item
)
236 def update_descriptor(desc
, kwargs
):
237 desc
= deepcopy(desc
) # do not modify original descriptor
239 for k
, v
in kwargs
.items():
240 update_content
= desc
244 if kitem_old
is not None:
245 update_content
= update_content
[kitem_old
]
246 if isinstance(update_content
, dict):
248 elif isinstance(update_content
, list):
249 kitem_old
= int(kitem
)
251 raise ROClientException(
252 "Invalid query string '{}'. Descriptor is not a list nor dict at '{}'".format(k
, kitem
))
253 if v
== "__DELETE__":
254 del update_content
[kitem_old
]
256 update_content
[kitem_old
] = v
259 raise ROClientException(
260 "Invalid query string '{}'. Descriptor does not contain '{}'".format(k
, kitem_old
))
262 raise ROClientException("Invalid query string '{}'. Expected integer index list instead of '{}'".format(
265 raise ROClientException(
266 "Invalid query string '{}'. Index '{}' out of range".format(k
, kitem_old
))
269 def check_ns_status(ns_descriptor
):
271 Inspect RO instance descriptor and indicates the status
272 :param ns_descriptor: instance descriptor obtained with self.show("ns", )
273 :return: status, message: status can be BUILD,ACTIVE,ERROR, message is a text message
280 for net
in ns_descriptor
["nets"]:
282 if net
["status"] in ("ERROR", "VIM_ERROR"):
283 return "ERROR", net
["error_msg"]
284 elif net
["status"] == "ACTIVE":
286 for vnf
in ns_descriptor
["vnfs"]:
287 for vm
in vnf
["vms"]:
289 if vm
["status"] in ("ERROR", "VIM_ERROR"):
290 return "ERROR", vm
["error_msg"]
291 elif vm
["status"] == "ACTIVE":
294 if net_total
== net_done
and vm_total
== vm_done
:
295 return "ACTIVE", "VMs {}, networks: {}".format(vm_total
, net_total
)
297 return "BUILD", "VMs: {}/{}, networks: {}/{}".format(vm_done
, vm_total
, net_done
, net_total
)
300 def get_ns_vnf_ip(ns_descriptor
):
302 Get a dict with the IPs of every vnf.
303 :param ns_descriptor: instance descriptor obtained with self.show("ns", )
304 :return: dict iwth key member_vnf_index, value ip_address
307 for vnf
in ns_descriptor
["vnfs"]:
308 ns_ip
[str(vnf
["member_vnf_index"])] = vnf
["ip_address"]
310 # vnf[mgmt_access]: '{interface_id: cf3cbf00-385c-49b4-9a3f-b400b7b15dc6, vm_id: d0dd22a9-91ef-46f1-8e8f-8cf4b2d5b2d7}'
314 async def _get_item_uuid(self
, session
, item
, item_id_name
, all_tenants
=False):
317 elif all_tenants
is None:
321 await self
._get
_tenant
(session
)
322 tenant_text
= "/" + self
.tenant
325 url
= "{}{}/{}".format(self
.endpoint_url
, tenant_text
, item
)
326 if self
.check_if_uuid(item_id_name
):
327 item_id
= item_id_name
328 url
+= "/" + item_id_name
329 elif item_id_name
and item_id_name
.startswith("'") and item_id_name
.endswith("'"):
330 item_id_name
= item_id_name
[1:-1]
331 self
.logger
.debug("openmano GET %s", url
)
332 with aiohttp
.Timeout(self
.timeout_short
):
333 async with session
.get(url
, headers
=self
.headers_req
) as response
:
334 response_text
= await response
.read()
335 self
.logger
.debug("GET {} [{}] {}".format(url
, response
.status
, response_text
[:100]))
336 if response
.status
== 404: # NOT_FOUND
337 raise ROClientException("No {} found with id '{}'".format(item
[:-1], item_id_name
),
339 if response
.status
>= 300:
340 raise ROClientException(response_text
, http_code
=response
.status
)
341 content
= self
._parse
_yaml
(response_text
, response
=True)
346 assert isinstance(desc
, list), "_get_item_uuid get a non dict with a list inside {}".format(type(desc
))
349 if item_id_name
and i
["name"] != item_id_name
:
351 if uuid
: # found more than one
352 raise ROClientException(
353 "Found more than one {} with name '{}'. uuid must be used".format(item
, item_id_name
),
357 raise ROClientException("No {} found with name '{}'".format(item
[:-1], item_id_name
), http_code
=404)
360 async def _get_item(self
, session
, item
, item_id_name
, all_tenants
=False):
363 elif all_tenants
is None:
367 await self
._get
_tenant
(session
)
368 tenant_text
= "/" + self
.tenant
370 if self
.check_if_uuid(item_id_name
):
374 uuid
= await self
._get
_item
_uuid
(session
, item
, item_id_name
, all_tenants
)
376 url
= "{}{}/{}/{}".format(self
.endpoint_url
, tenant_text
, item
, uuid
)
377 self
.logger
.debug("GET %s", url
)
378 with aiohttp
.Timeout(self
.timeout_short
):
379 async with session
.get(url
, headers
=self
.headers_req
) as response
:
380 response_text
= await response
.read()
381 self
.logger
.debug("GET {} [{}] {}".format(url
, response
.status
, response_text
[:100]))
382 if response
.status
>= 300:
383 raise ROClientException(response_text
, http_code
=response
.status
)
385 return self
._parse
_yaml
(response_text
, response
=True)
387 async def _get_tenant(self
, session
):
389 self
.tenant
= await self
._get
_item
_uuid
(session
, "tenants", self
.tenant_id_name
, None)
392 async def _get_datacenter(self
, session
):
394 await self
._get
_tenant
(session
)
395 if not self
.datacenter
:
396 self
.datacenter
= await self
._get
_item
_uuid
(session
, "datacenters", self
.datacenter_id_name
, True)
397 return self
.datacenter
399 async def _create_item(self
, session
, item
, descriptor
, item_id_name
=None, action
=None, all_tenants
=False):
402 elif all_tenants
is None:
406 await self
._get
_tenant
(session
)
407 tenant_text
= "/" + self
.tenant
408 payload_req
= yaml
.safe_dump(descriptor
)
411 api_version_text
= ""
413 # assumes version v3 only
414 api_version_text
= "/v3"
416 elif item
== "scenarios":
417 # assumes version v3 only
418 api_version_text
= "/v3"
423 elif self
.check_if_uuid(item_id_name
):
424 uuid
= "/{}".format(item_id_name
)
427 uuid
= await self
._get
_item
_uuid
(session
, item
, item_id_name
, all_tenants
)
428 uuid
= "/{}".format(uuid
)
432 action
= "/".format(action
)
434 url
= "{}{apiver}{tenant}/{item}{id}{action}".format(self
.endpoint_url
, apiver
=api_version_text
, tenant
=tenant_text
,
435 item
=item
, id=uuid
, action
=action
)
436 self
.logger
.debug("openmano POST %s %s", url
, payload_req
)
437 with aiohttp
.Timeout(self
.timeout_large
):
438 async with session
.post(url
, headers
=self
.headers_req
, data
=payload_req
) as response
:
439 response_text
= await response
.read()
440 self
.logger
.debug("POST {} [{}] {}".format(url
, response
.status
, response_text
[:100]))
441 if response
.status
>= 300:
442 raise ROClientException(response_text
, http_code
=response
.status
)
444 return self
._parse
_yaml
(response_text
, response
=True)
446 async def _del_item(self
, session
, item
, item_id_name
, all_tenants
=False):
449 elif all_tenants
is None:
453 await self
._get
_tenant
(session
)
454 tenant_text
= "/" + self
.tenant
455 if not self
.check_if_uuid(item_id_name
):
457 _all_tenants
= all_tenants
458 if item
== "datacenters":
460 uuid
= await self
._get
_item
_uuid
(session
, item
, item_id_name
, all_tenants
=_all_tenants
)
464 url
= "{}{}/{}/{}".format(self
.endpoint_url
, tenant_text
, item
, uuid
)
465 self
.logger
.debug("DELETE %s", url
)
466 with aiohttp
.Timeout(self
.timeout_short
):
467 async with session
.delete(url
, headers
=self
.headers_req
) as response
:
468 response_text
= await response
.read()
469 self
.logger
.debug("DELETE {} [{}] {}".format(url
, response
.status
, response_text
[:100]))
470 if response
.status
>= 300:
471 raise ROClientException(response_text
, http_code
=response
.status
)
472 return self
._parse
_yaml
(response_text
, response
=True)
474 async def _list_item(self
, session
, item
, all_tenants
=False, filter_dict
=None):
477 elif all_tenants
is None:
481 await self
._get
_tenant
(session
)
482 tenant_text
= "/" + self
.tenant
484 url
= "{}{}/{}".format(self
.endpoint_url
, tenant_text
, item
)
487 for k
in filter_dict
:
488 url
+= separator
+ quote(str(k
)) + "=" + quote(str(filter_dict
[k
]))
490 self
.logger
.debug("openmano GET %s", url
)
491 with aiohttp
.Timeout(self
.timeout_short
):
492 async with session
.get(url
, headers
=self
.headers_req
) as response
:
493 response_text
= await response
.read()
494 self
.logger
.debug("GET {} [{}] {}".format(url
, response
.status
, response_text
[:100]))
495 if response
.status
>= 300:
496 raise ROClientException(response_text
, http_code
=response
.status
)
497 return self
._parse
_yaml
(response_text
, response
=True)
499 async def _edit_item(self
, session
, item
, item_id
, descriptor
, all_tenants
=False):
502 elif all_tenants
is None:
506 await self
._get
_tenant
(session
)
507 tenant_text
= "/" + self
.tenant
509 payload_req
= yaml
.safe_dump(descriptor
)
513 url
= "{}{}/{}/{}".format(self
.endpoint_url
, tenant_text
, item
, item_id
)
514 self
.logger
.debug("openmano PUT %s %s", url
, payload_req
)
515 with aiohttp
.Timeout(self
.timeout_large
):
516 async with session
.put(url
, headers
=self
.headers_req
, data
=payload_req
) as response
:
517 response_text
= await response
.read()
518 self
.logger
.debug("PUT {} [{}] {}".format(url
, response
.status
, response_text
[:100]))
519 if response
.status
>= 300:
520 raise ROClientException(response_text
, http_code
=response
.status
)
521 return self
._parse
_yaml
(response_text
, response
=True)
523 async def get_list(self
, item
, all_tenants
=False, filter_by
=None):
525 Obtain a list of items filtering by the specigy filter_by.
526 :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns'
527 :param all_tenants: True if not filtering by tenant. Only allowed for admin
528 :param filter_by: dictionary with filtering
529 :return: a list of dict. It can be empty. Raises ROClientException on Error,
532 if item
not in self
.client_to_RO
:
533 raise ROClientException("Invalid item {}".format(item
))
536 with aiohttp
.ClientSession(loop
=self
.loop
) as session
:
537 content
= await self
._list
_item
(session
, self
.client_to_RO
[item
], all_tenants
=all_tenants
,
538 filter_dict
=filter_by
)
539 if isinstance(content
, dict):
540 if len(content
) == 1:
541 for _
, v
in content
.items():
543 return content
.values()[0]
545 raise ROClientException("Output not a list neither dict with len equal 1", http_code
=500)
547 except aiohttp
.errors
.ClientOSError
as e
:
548 raise ROClientException(e
, http_code
=504)
550 async def show(self
, item
, item_id_name
=None, all_tenants
=False):
552 Obtain the information of an item from its id or name
553 :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns'
554 :param item_id_name: RO id or name of the item. Raise and exception if more than one found
555 :param all_tenants: True if not filtering by tenant. Only allowed for admin
556 :return: dictionary with the information or raises ROClientException on Error, NotFound, found several
559 if item
not in self
.client_to_RO
:
560 raise ROClientException("Invalid item {}".format(item
))
565 elif item
== 'vim_account':
568 with aiohttp
.ClientSession(loop
=self
.loop
) as session
:
569 content
= await self
._get
_item
(session
, self
.client_to_RO
[item
], item_id_name
, all_tenants
=all_tenants
)
570 return remove_envelop(item
, content
)
571 except aiohttp
.errors
.ClientOSError
as e
:
572 raise ROClientException(e
, http_code
=504)
574 async def delete(self
, item
, item_id_name
=None, all_tenants
=False):
576 Delete the information of an item from its id or name
577 :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns'
578 :param item_id_name: RO id or name of the item. Raise and exception if more than one found
579 :param all_tenants: True if not filtering by tenant. Only allowed for admin
580 :return: dictionary with the information or raises ROClientException on Error, NotFound, found several
583 if item
not in self
.client_to_RO
:
584 raise ROClientException("Invalid item {}".format(item
))
585 if item
== 'tenant' or item
== 'vim':
588 with aiohttp
.ClientSession(loop
=self
.loop
) as session
:
589 return await self
._del
_item
(session
, self
.client_to_RO
[item
], item_id_name
, all_tenants
=all_tenants
)
590 except aiohttp
.errors
.ClientOSError
as e
:
591 raise ROClientException(e
, http_code
=504)
593 async def edit(self
, item
, item_id_name
, descriptor
=None, descriptor_format
=None, **kwargs
):
595 :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns', 'vim'
596 :param descriptor: can be a dict, or a yaml/json text. Autodetect unless descriptor_format is provided
597 :param descriptor_format: Can be 'json' or 'yaml'
598 :param kwargs: Overrides descriptor with values as name, description, vim_url, vim_url_admin, vim_type
599 keys can be a dot separated list to specify elements inside dict
600 :return: dictionary with the information or raises ROClientException on Error
603 if isinstance(descriptor
, str):
604 descriptor
= self
._parse
(descriptor
, descriptor_format
)
610 if item
not in self
.client_to_RO
:
611 raise ROClientException("Invalid item {}".format(item
))
612 desc
= remove_envelop(item
, descriptor
)
614 # Override descriptor with kwargs
616 desc
= self
.update_descriptor(desc
, kwargs
)
618 if item
in ('tenant', 'vim'):
621 create_desc
= self
._create
_envelop
(item
, desc
)
623 with aiohttp
.ClientSession(loop
=self
.loop
) as session
:
624 _all_tenants
= all_tenants
627 item_id
= await self
._get
_item
_uuid
(session
, self
.client_to_RO
[item
], item_id_name
, all_tenants
=_all_tenants
)
628 # await self._get_tenant(session)
629 outdata
= await self
._edit
_item
(session
, self
.client_to_RO
[item
], item_id
, create_desc
, all_tenants
=all_tenants
)
630 return remove_envelop(item
, outdata
)
631 except aiohttp
.errors
.ClientOSError
as e
:
632 raise ROClientException(e
, http_code
=504)
634 async def create(self
, item
, descriptor
=None, descriptor_format
=None, **kwargs
):
636 Creates an item from its descriptor
637 :param item: can be 'tenant', 'vnfd', 'nsd', 'ns', 'vim', 'vim_account', 'sdn'
638 :param descriptor: can be a dict, or a yaml/json text. Autodetect unless descriptor_format is provided
639 :param descriptor_format: Can be 'json' or 'yaml'
640 :param kwargs: Overrides descriptor with values as name, description, vim_url, vim_url_admin, vim_type
641 keys can be a dot separated list to specify elements inside dict
642 :return: dictionary with the information or raises ROClientException on Error
645 if isinstance(descriptor
, str):
646 descriptor
= self
._parse
(descriptor
, descriptor_format
)
652 if item
not in self
.client_to_RO
:
653 raise ROClientException("Invalid item {}".format(item
))
654 desc
= remove_envelop(item
, descriptor
)
656 # Override descriptor with kwargs
658 desc
= self
.update_descriptor(desc
, kwargs
)
660 for mandatory
in self
.mandatory_for_create
[item
]:
661 if mandatory
not in desc
:
662 raise ROClientException("'{}' is mandatory parameter for {}".format(mandatory
, item
))
665 if item
in ('tenant', 'vim'):
668 create_desc
= self
._create
_envelop
(item
, desc
)
670 with aiohttp
.ClientSession(loop
=self
.loop
) as session
:
671 outdata
= await self
._create
_item
(session
, self
.client_to_RO
[item
], create_desc
,
672 all_tenants
=all_tenants
)
673 return remove_envelop(item
, outdata
)
674 except aiohttp
.errors
.ClientOSError
as e
:
675 raise ROClientException(e
, http_code
=504)
677 async def attach_datacenter(self
, datacenter
=None, descriptor
=None, descriptor_format
=None, **kwargs
):
679 if isinstance(descriptor
, str):
680 descriptor
= self
._parse
(descriptor
, descriptor_format
)
685 desc
= remove_envelop("vim", descriptor
)
688 # uuid = self._get_item_uuid(session, "datacenters", uuid_name, all_tenants=True)
689 # tenant_text = "/" + self._get_tenant()
691 desc
= self
.update_descriptor(desc
, kwargs
)
693 if not desc
.get("vim_tenant_name") and not desc
.get("vim_tenant_id"):
694 raise ROClientException("Wrong descriptor. At least vim_tenant_name or vim_tenant_id must be provided")
695 create_desc
= self
._create
_envelop
("vim", desc
)
696 payload_req
= yaml
.safe_dump(create_desc
)
697 with aiohttp
.ClientSession(loop
=self
.loop
) as session
:
699 item_id
= await self
._get
_item
_uuid
(session
, "datacenters", datacenter
, all_tenants
=True)
700 await self
._get
_tenant
(session
)
702 url
= "{}/{tenant}/datacenters/{datacenter}".format(self
.endpoint_url
, tenant
=self
.tenant
,
704 self
.logger
.debug("openmano POST %s %s", url
, payload_req
)
705 with aiohttp
.Timeout(self
.timeout_large
):
706 async with session
.post(url
, headers
=self
.headers_req
, data
=payload_req
) as response
:
707 response_text
= await response
.read()
708 self
.logger
.debug("POST {} [{}] {}".format(url
, response
.status
, response_text
[:100]))
709 if response
.status
>= 300:
710 raise ROClientException(response_text
, http_code
=response
.status
)
712 response_desc
= self
._parse
_yaml
(response_text
, response
=True)
713 desc
= remove_envelop("vim", response_desc
)
716 async def detach_datacenter(self
, datacenter
=None):
717 #TODO replace the code with delete_item(vim_account,...)
718 with aiohttp
.ClientSession(loop
=self
.loop
) as session
:
720 item_id
= await self
._get
_item
_uuid
(session
, "datacenters", datacenter
, all_tenants
=False)
721 tenant
= await self
._get
_tenant
(session
)
723 url
= "{}/{tenant}/datacenters/{datacenter}".format(self
.endpoint_url
, tenant
=tenant
,
725 self
.logger
.debug("openmano DELETE %s", url
)
726 with aiohttp
.Timeout(self
.timeout_large
):
727 async with session
.delete(url
, headers
=self
.headers_req
) as response
:
728 response_text
= await response
.read()
729 self
.logger
.debug("DELETE {} [{}] {}".format(url
, response
.status
, response_text
[:100]))
730 if response
.status
>= 300:
731 raise ROClientException(response_text
, http_code
=response
.status
)
733 response_desc
= self
._parse
_yaml
(response_text
, response
=True)
734 desc
= remove_envelop("vim", response_desc
)
738 # TODO convert to asyncio
742 def edit_datacenter(self
, uuid
=None, name
=None, descriptor
=None, descriptor_format
=None, all_tenants
=False, **kwargs
):
743 """Edit the parameters of a datacenter
744 Params: must supply a descriptor or/and a parameter to change
745 uuid or/and name. If only name is supplied, there must be only one or an exception is raised
746 descriptor: with format {'datacenter':{params to change info}}
747 must be a dictionary or a json/yaml text.
748 parameters to change can be supplyied by the descriptor or as parameters:
749 new_name: the datacenter name
750 vim_url: the datacenter URL
751 vim_url_admin: the datacenter URL for administrative issues
752 vim_type: the datacenter type, can be openstack or openvim.
753 public: boolean, available to other tenants
754 description: datacenter description
755 Return: Raises an exception on error, not found or found several
756 Obtain a dictionary with format {'datacenter':{new_datacenter_info}}
759 if isinstance(descriptor
, str):
760 descriptor
= self
.parse(descriptor
, descriptor_format
)
764 descriptor
={"datacenter": {}}
766 raise ROClientException("Missing descriptor")
768 if 'datacenter' not in descriptor
or len(descriptor
)!=1:
769 raise ROClientException("Descriptor must contain only one 'datacenter' field")
771 if param
=='new_name':
772 descriptor
['datacenter']['name'] = kwargs
[param
]
774 descriptor
['datacenter'][param
] = kwargs
[param
]
775 return self
._edit
_item
("datacenters", descriptor
, uuid
, name
, all_tenants
=None)
778 def edit_scenario(self
, uuid
=None, name
=None, descriptor
=None, descriptor_format
=None, all_tenants
=False, **kwargs
):
779 """Edit the parameters of a scenario
780 Params: must supply a descriptor or/and a parameters to change
781 uuid or/and name. If only name is supplied, there must be only one or an exception is raised
782 descriptor: with format {'scenario':{params to change info}}
783 must be a dictionary or a json/yaml text.
784 parameters to change can be supplyied by the descriptor or as parameters:
785 new_name: the scenario name
786 public: boolean, available to other tenants
787 description: scenario description
788 tenant_id. Propietary tenant
789 Return: Raises an exception on error, not found or found several
790 Obtain a dictionary with format {'scenario':{new_scenario_info}}
793 if isinstance(descriptor
, str):
794 descriptor
= self
.parse(descriptor
, descriptor_format
)
798 descriptor
={"scenario": {}}
800 raise ROClientException("Missing descriptor")
802 if 'scenario' not in descriptor
or len(descriptor
)>2:
803 raise ROClientException("Descriptor must contain only one 'scenario' field")
805 if param
=='new_name':
806 descriptor
['scenario']['name'] = kwargs
[param
]
808 descriptor
['scenario'][param
] = kwargs
[param
]
809 return self
._edit
_item
("scenarios", descriptor
, uuid
, name
, all_tenants
=None)
812 def vim_action(self
, action
, item
, uuid
=None, all_tenants
=False, **kwargs
):
813 """Perform an action over a vim
815 action: can be 'list', 'get'/'show', 'delete' or 'create'
816 item: can be 'tenants' or 'networks'
817 uuid: uuid of the tenant/net to show or to delete. Ignore otherwise
819 datacenter_name, datacenter_id: datacenters to act on, if missing uses classes store datacenter
820 descriptor, descriptor_format: descriptor needed on creation, can be a dict or a yaml/json str
821 must be a dictionary or a json/yaml text.
822 name: for created tenant/net Overwrite descriptor name if any
823 description: tenant descriptor. Overwrite descriptor description if any
825 Return: Raises an exception on error
826 Obtain a dictionary with format {'tenant':{new_tenant_info}}
828 if item
not in ("tenants", "networks", "images"):
829 raise ROClientException("Unknown value for item '{}', must be 'tenants', 'nets' or "
830 "images".format(str(item
)))
832 image_actions
= ['list','get','show','delete']
833 if item
== "images" and action
not in image_actions
:
834 raise ROClientException("Only available actions for item '{}' are {}\n"
835 "Requested action was '{}'".format(item
, ', '.join(image_actions
), action
))
839 tenant_text
= "/"+self
._get
_tenant
()
841 if "datacenter_id" in kwargs
or "datacenter_name" in kwargs
:
842 datacenter
= self
._get
_item
_uuid
(session
, "datacenters", kwargs
.get("datacenter"), all_tenants
=all_tenants
)
844 datacenter
= self
.get_datacenter(session
)
847 url
= "{}{}/vim/{}/{}".format(self
.endpoint_url
, tenant_text
, datacenter
, item
)
848 self
.logger
.debug("GET %s", url
)
849 mano_response
= requests
.get(url
, headers
=self
.headers_req
)
850 self
.logger
.debug("openmano response: %s", mano_response
.text
)
851 content
= self
._parse
_yaml
(mano_response
.text
, response
=True)
852 if mano_response
.status_code
==200:
855 raise ROClientException(str(content
), http_code
=mano_response
.status
)
856 elif action
=="get" or action
=="show":
857 url
= "{}{}/vim/{}/{}/{}".format(self
.endpoint_url
, tenant_text
, datacenter
, item
, uuid
)
858 self
.logger
.debug("GET %s", url
)
859 mano_response
= requests
.get(url
, headers
=self
.headers_req
)
860 self
.logger
.debug("openmano response: %s", mano_response
.text
)
861 content
= self
._parse
_yaml
(mano_response
.text
, response
=True)
862 if mano_response
.status_code
==200:
865 raise ROClientException(str(content
), http_code
=mano_response
.status
)
866 elif action
=="delete":
867 url
= "{}{}/vim/{}/{}/{}".format(self
.endpoint_url
, tenant_text
, datacenter
, item
, uuid
)
868 self
.logger
.debug("DELETE %s", url
)
869 mano_response
= requests
.delete(url
, headers
=self
.headers_req
)
870 self
.logger
.debug("openmano response: %s", mano_response
.text
)
871 content
= self
._parse
_yaml
(mano_response
.text
, response
=True)
872 if mano_response
.status_code
==200:
875 raise ROClientException(str(content
), http_code
=mano_response
.status
)
876 elif action
=="create":
877 if "descriptor" in kwargs
:
878 if isinstance(kwargs
["descriptor"], str):
879 descriptor
= self
._parse
(kwargs
["descriptor"], kwargs
.get("descriptor_format") )
881 descriptor
= kwargs
["descriptor"]
882 elif "name" in kwargs
:
883 descriptor
={item
[:-1]: {"name": kwargs
["name"]}}
885 raise ROClientException("Missing descriptor")
887 if item
[:-1] not in descriptor
or len(descriptor
)!=1:
888 raise ROClientException("Descriptor must contain only one 'tenant' field")
890 descriptor
[ item
[:-1] ]['name'] = kwargs
["name"]
891 if "description" in kwargs
:
892 descriptor
[ item
[:-1] ]['description'] = kwargs
["description"]
893 payload_req
= yaml
.safe_dump(descriptor
)
895 url
= "{}{}/vim/{}/{}".format(self
.endpoint_url
, tenant_text
, datacenter
, item
)
896 self
.logger
.debug("openmano POST %s %s", url
, payload_req
)
897 mano_response
= requests
.post(url
, headers
= self
.headers_req
, data
=payload_req
)
898 self
.logger
.debug("openmano response: %s", mano_response
.text
)
899 content
= self
._parse
_yaml
(mano_response
.text
, response
=True)
900 if mano_response
.status_code
==200:
903 raise ROClientException(str(content
), http_code
=mano_response
.status
)
905 raise ROClientException("Unknown value for action '{}".format(str(action
)))
908 if __name__
== '__main__':
909 RO_URL
= "http://localhost:9090/openmano"
910 TEST_TENANT
= "myTenant"
912 TEST_URL1
= "https://localhost:5000/v1"
913 TEST_TYPE1
= "openstack"
914 TEST_CONFIG1
= {"use_floating_ip": True}
916 TEST_URL2
= "https://localhost:5000/v2"
917 TEST_TYPE2
= "openvim"
918 TEST_CONFIG2
= {"config2": "config2", "config3": True}
920 streamformat
= "%(asctime)s %(name)s %(levelname)s: %(message)s"
921 logging
.basicConfig(format
=streamformat
)
922 logger
= logging
.getLogger("ROClient")
926 loop
= asyncio
.get_event_loop()
927 myClient
= ROClient(endpoint_url
=RO_URL
, loop
=loop
, loglevel
="DEBUG")
930 content
= loop
.run_until_complete(myClient
.get_list("tenant"))
931 print("tenants", content
)
932 content
= loop
.run_until_complete(myClient
.create("tenant", name
=TEST_TENANT
))
934 content
= loop
.run_until_complete(myClient
.show("tenant", TEST_TENANT
))
935 print("tenant", TEST_TENANT
, content
)
936 content
= loop
.run_until_complete(myClient
.edit("tenant", TEST_TENANT
, description
="another description"))
937 content
= loop
.run_until_complete(myClient
.show("tenant", TEST_TENANT
))
938 print("tenant edited", TEST_TENANT
, content
)
939 myClient
["tenant"] = TEST_TENANT
943 content
= loop
.run_until_complete(myClient
.create("vim", name
=TEST_VIM1
, type=TEST_TYPE1
, vim_url
=TEST_URL1
, config
=TEST_CONFIG1
))
945 content
= loop
.run_until_complete(myClient
.get_list("vim"))
946 print("vim", content
)
947 content
= loop
.run_until_complete(myClient
.show("vim", TEST_VIM1
))
948 print("vim", TEST_VIM1
, content
)
949 content
= loop
.run_until_complete(myClient
.edit("vim", TEST_VIM1
, description
="another description",
950 name
=TEST_VIM2
, type=TEST_TYPE2
, vim_url
=TEST_URL2
,
951 config
=TEST_CONFIG2
))
952 content
= loop
.run_until_complete(myClient
.show("vim", TEST_VIM2
))
953 print("vim edited", TEST_VIM2
, content
)
956 content
= loop
.run_until_complete(myClient
.attach_datacenter(TEST_VIM2
, vim_username
='user',
957 vim_password
='pass', vim_tenant_name
='vimtenant1', config
=TEST_CONFIG1
))
959 content
= loop
.run_until_complete(myClient
.get_list("vim_account"))
960 print("vim_account", content
)
961 content
= loop
.run_until_complete(myClient
.show("vim_account", TEST_VIM2
))
962 print("vim_account", TEST_VIM2
, content
)
963 content
= loop
.run_until_complete(myClient
.edit("vim_account", TEST_VIM2
, vim_username
='user2', vim_password
='pass2',
964 vim_tenant_name
="vimtenant2", config
=TEST_CONFIG2
))
965 content
= loop
.run_until_complete(myClient
.show("vim_account", TEST_VIM2
))
966 print("vim_account edited", TEST_VIM2
, content
)
968 myClient
["vim"] = TEST_VIM2
970 except Exception as e
:
971 logger
.error("Error {}".format(e
), exc_info
=True)
973 for item
in (("vim_account", TEST_VIM1
), ("vim", TEST_VIM1
),
974 ("vim_account", TEST_VIM2
), ("vim", TEST_VIM2
),
975 ("tenant", TEST_TENANT
)):
977 content
= loop
.run_until_complete(myClient
.delete(item
[0], item
[1]))
978 print("{} {} deleted; {}".format(item
[0], item
[1], content
))
979 except Exception as e
:
980 if e
.http_code
== 404:
981 print("{} {} not present or already deleted".format(item
[0], item
[1]))
983 logger
.error("Error {}".format(e
), exc_info
=True)