From 88f9b44b6fcf1f9be8ee84f787a79a6ef1cd507c Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Mon, 14 Jan 2019 18:17:15 +0100 Subject: [PATCH] Support of WIM accounts in osmclient Change-Id: I3395f6dc78d8060d7ae3aa3cbbef7ca9fad8713a Signed-off-by: garciadeblas --- osmclient/scripts/osm.py | 161 ++++++++++++++++++++++++++++++++++ osmclient/sol005/client.py | 2 + osmclient/sol005/wim.py | 174 +++++++++++++++++++++++++++++++++++++ 3 files changed, 337 insertions(+) create mode 100644 osmclient/sol005/wim.py diff --git a/osmclient/scripts/osm.py b/osmclient/scripts/osm.py index 0a1f7c3..2c151ff 100755 --- a/osmclient/scripts/osm.py +++ b/osmclient/scripts/osm.py @@ -1802,6 +1802,167 @@ def vim_show(ctx, name): print(table) +#################### +# WIM operations +#################### + +@cli.command(name='wim-create') +@click.option('--name', + prompt=True, + help='Name for the WIM account') +@click.option('--user', + help='WIM username') +@click.option('--password', + help='WIM password') +@click.option('--url', + prompt=True, + help='WIM url') +# @click.option('--tenant', +# help='wIM tenant name') +@click.option('--config', + default=None, + help='WIM specific config parameters') +@click.option('--wim_type', + help='WIM type') +@click.option('--description', + default='no description', + help='human readable description') +@click.option('--wim_port_mapping', default=None, help="File describing the port mapping between DC edge (datacenters, switches, ports) and WAN edge (WAN service endpoint id and info)") +@click.pass_context +def wim_create(ctx, + name, + user, + password, + url, + # tenant, + config, + wim_type, + description, + wim_port_mapping): + '''creates a new WIM account + ''' + try: + check_client_version(ctx.obj, ctx.command.name) + # if sdn_controller: + # check_client_version(ctx.obj, '--sdn_controller') + # if sdn_port_mapping: + # check_client_version(ctx.obj, '--sdn_port_mapping') + wim = {} + if user: wim['user'] = user + if password: wim['password'] = password + if url: wim['wim_url'] = url + # if tenant: wim['tenant'] = tenant + wim['wim_type'] = wim_type + if description: wim['description'] = description + if config: wim['config'] = config + ctx.obj.wim.create(name, wim, wim_port_mapping) + except ClientException as inst: + print((inst.message)) + exit(1) + + +@cli.command(name='wim-update', short_help='updates a WIM account') +@click.argument('name') +@click.option('--newname', help='New name for the WIM account') +@click.option('--user', help='WIM username') +@click.option('--password', help='WIM password') +@click.option('--url', help='WIM url') +@click.option('--config', help='WIM specific config parameters') +@click.option('--wim_type', help='WIM type') +@click.option('--description', help='human readable description') +@click.option('--wim_port_mapping', default=None, help="File describing the port mapping between DC edge (datacenters, switches, ports) and WAN edge (WAN service endpoint id and info)") +@click.pass_context +def wim_update(ctx, + name, + newname, + user, + password, + url, + config, + wim_type, + description, + wim_port_mapping): + '''updates a WIM account + + NAME: name or ID of the WIM account + ''' + try: + check_client_version(ctx.obj, ctx.command.name) + wim = {} + if newname: wim['name'] = newname + if user: wim['user'] = user + if password: wim['password'] = password + if url: wim['url'] = url + # if tenant: wim['tenant'] = tenant + if wim_type: wim['wim_type'] = wim_type + if description: wim['description'] = description + if config: wim['config'] = config + ctx.obj.wim.update(name, wim, wim_port_mapping) + except ClientException as inst: + print((inst.message)) + exit(1) + + +@cli.command(name='wim-delete') +@click.argument('name') +@click.option('--force', is_flag=True, help='forces the deletion bypassing pre-conditions') +@click.pass_context +def wim_delete(ctx, name, force): + '''deletes a WIM account + + NAME: name or ID of the WIM account to be deleted + ''' + try: + check_client_version(ctx.obj, ctx.command.name) + ctx.obj.wim.delete(name, force) + except ClientException as inst: + print((inst.message)) + exit(1) + + +@cli.command(name='wim-list') +@click.option('--filter', default=None, + help='restricts the list to the WIM accounts matching the filter') +@click.pass_context +def wim_list(ctx, filter): + '''list all WIM accounts''' + try: + check_client_version(ctx.obj, ctx.command.name) + resp = ctx.obj.wim.list(filter) + table = PrettyTable(['wim name', 'uuid']) + for wim in resp: + table.add_row([wim['name'], wim['uuid']]) + table.align = 'l' + print(table) + except ClientException as inst: + print((inst.message)) + exit(1) + + +@cli.command(name='wim-show') +@click.argument('name') +@click.pass_context +def wim_show(ctx, name): + '''shows the details of a WIM account + + NAME: name or ID of the WIM account + ''' + try: + check_client_version(ctx.obj, ctx.command.name) + resp = ctx.obj.wim.get(name) + if 'password' in resp: + resp['wim_password']='********' + except ClientException as inst: + print((inst.message)) + exit(1) + + table = PrettyTable(['key', 'attribute']) + for k, v in list(resp.items()): + table.add_row([k, json.dumps(v, indent=2)]) + table.align = 'l' + print(table) + + #################### # SDN controller operations #################### diff --git a/osmclient/sol005/client.py b/osmclient/sol005/client.py index c70be23..32e8fb9 100644 --- a/osmclient/sol005/client.py +++ b/osmclient/sol005/client.py @@ -26,6 +26,7 @@ from osmclient.sol005 import nsi from osmclient.sol005 import ns from osmclient.sol005 import vnf from osmclient.sol005 import vim +from osmclient.sol005 import wim from osmclient.sol005 import package from osmclient.sol005 import http from osmclient.sol005 import sdncontroller @@ -83,6 +84,7 @@ class Client(object): self.ns = ns.Ns(self._http_client, client=self) self.nsi = nsi.Nsi(self._http_client, client=self) self.vim = vim.Vim(self._http_client, client=self) + self.wim = wim.Wim(self._http_client, client=self) self.sdnc = sdncontroller.SdnController(self._http_client, client=self) self.vnf = vnf.Vnf(self._http_client, client=self) self.project = projectmodule.Project(self._http_client, client=self) diff --git a/osmclient/sol005/wim.py b/osmclient/sol005/wim.py new file mode 100644 index 0000000..d2721cd --- /dev/null +++ b/osmclient/sol005/wim.py @@ -0,0 +1,174 @@ +# Copyright 2018 Telefonica +# +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +OSM wim API handling +""" + +from osmclient.common import utils +from osmclient.common.exceptions import ClientException +from osmclient.common.exceptions import NotFound +import yaml +import json + + +class Wim(object): + def __init__(self, http=None, client=None): + self._http = http + self._client = client + self._apiName = '/admin' + self._apiVersion = '/v1' + self._apiResource = '/wim_accounts' + self._apiBase = '{}{}{}'.format(self._apiName, + self._apiVersion, self._apiResource) + def create(self, name, wim_input, wim_port_mapping=None): + if 'wim_type' not in wim_input: + raise Exception("wim type not provided") + + wim_account = wim_input + wim_account["name"] = name + + wim_config = {} + if 'config' in wim_input and wim_input['config'] is not None: + wim_config = yaml.safe_load(wim_input['config']) + if wim_port_mapping: + with open(wim_port_mapping, 'r') as f: + wim_config['wim_port_mapping'] = yaml.safe_load(f.read()) + if wim_config: + wim_account['config'] = wim_config + #wim_account['config'] = json.dumps(wim_config) + + http_code, resp = self._http.post_cmd(endpoint=self._apiBase, + postfields_dict=wim_account) + #print 'HTTP CODE: {}'.format(http_code) + #print 'RESP: {}'.format(resp) + if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {}'.format( + resp)) + print(resp['id']) + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to create wim {} - {}".format(name, msg)) + + def update(self, wim_name, wim_account, wim_port_mapping=None): + wim = self.get(wim_name) + + wim_config = {} + if 'config' in wim_account: + if wim_account.get('config')=="" and (wim_port_mapping): + raise ClientException("clearing config is incompatible with updating SDN info") + if wim_account.get('config')=="": + wim_config = None + else: + wim_config = yaml.safe_load(wim_account['config']) + if wim_port_mapping: + with open(wim_port_mapping, 'r') as f: + wim_config['wim_port_mapping'] = yaml.safe_load(f.read()) + wim_account['config'] = wim_config + #wim_account['config'] = json.dumps(wim_config) + http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,wim['_id']), + postfields_dict=wim_account) + #print 'HTTP CODE: {}'.format(http_code) + #print 'RESP: {}'.format(resp) + if http_code in (200, 201, 202, 204): + pass + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to update wim {} - {}".format(wim_name, msg)) + + def update_wim_account_dict(self, wim_account, wim_input): + print (wim_input) + wim_account['wim_type'] = wim_input['wim_type'] + wim_account['description'] = wim_input['description'] + wim_account['wim_url'] = wim_input['url'] + wim_account['user'] = wim_input.get('wim-username') + wim_account['password'] = wim_input.get('wim-password') + return wim_account + + def get_id(self, name): + """Returns a VIM id from a VIM name + """ + for wim in self.list(): + if name == wim['name']: + return wim['uuid'] + raise NotFound("wim {} not found".format(name)) + + def delete(self, wim_name, force=False): + wim_id = wim_name + if not utils.validate_uuid4(wim_name): + wim_id = self.get_id(wim_name) + querystring = '' + if force: + querystring = '?FORCE=True' + http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase, + wim_id, querystring)) + #print 'HTTP CODE: {}'.format(http_code) + #print 'RESP: {}'.format(resp) + if http_code == 202: + print('Deletion in progress') + elif http_code == 204: + print('Deleted') + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to delete wim {} - {}".format(wim_name, msg)) + + def list(self, filter=None): + """Returns a list of VIM accounts + """ + filter_string = '' + if filter: + filter_string = '?{}'.format(filter) + resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + if not resp: + return list() + wim_accounts = [] + for datacenter in resp: + wim_accounts.append({"name": datacenter['name'], "uuid": datacenter['_id'] + if '_id' in datacenter else None}) + return wim_accounts + + def get(self, name): + """Returns a VIM account based on name or id + """ + wim_id = name + if not utils.validate_uuid4(name): + wim_id = self.get_id(name) + resp = self._http.get_cmd('{}/{}'.format(self._apiBase,wim_id)) + if not resp or '_id' not in resp: + raise ClientException('failed to get wim info: '.format( + resp)) + else: + return resp + raise NotFound("wim {} not found".format(name)) + -- 2.17.1