From e26c422590219f865d958a8537b8f6c0973ac703 Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Thu, 17 May 2018 15:02:43 +0200 Subject: [PATCH] vnf-list and vnf-show for sol005 client Change-Id: Ic0c63ebc425788da562987f7ad1416cc625aeeb3 Signed-off-by: garciadeblas --- osmclient/scripts/osm.py | 12 ++++--- osmclient/sol005/client.py | 4 +-- osmclient/sol005/vnf.py | 72 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 osmclient/sol005/vnf.py diff --git a/osmclient/scripts/osm.py b/osmclient/scripts/osm.py index 2e65121..76daaa4 100755 --- a/osmclient/scripts/osm.py +++ b/osmclient/scripts/osm.py @@ -217,12 +217,16 @@ def vnfd_list2(ctx, filter): @cli.command(name='vnf-list') +@click.option('--ns', default=None, help='NS instance id or name to restrict the VNF list') @click.pass_context -def vnf_list(ctx): +def vnf_list(ctx, ns): ''' list all VNF instances''' try: - check_client_version(ctx.obj, ctx.command.name, 'v1') - resp = ctx.obj.vnf.list() + if ns: + check_client_version(ctx.obj, '--ns') + resp = ctx.obj.vnf.list(ns) + else: + resp = ctx.obj.vnf.list() except ClientException as inst: print(inst.message) exit(1) @@ -409,7 +413,7 @@ def vnf_show(ctx, name, literal, filter): NAME: name or ID of the VNF instance ''' try: - check_client_version(ctx.obj, ctx.command.name, 'v1') + check_client_version(ctx.obj, ctx.command.name) resp = ctx.obj.vnf.get(name) except ClientException as inst: print(inst.message) diff --git a/osmclient/sol005/client.py b/osmclient/sol005/client.py index b8627b9..ca8f151 100644 --- a/osmclient/sol005/client.py +++ b/osmclient/sol005/client.py @@ -18,11 +18,11 @@ OSM SOL005 client API """ -#from osmclient.v1 import vnf #from osmclient.v1 import vca from osmclient.sol005 import vnfd from osmclient.sol005 import nsd from osmclient.sol005 import ns +from osmclient.sol005 import vnf from osmclient.sol005 import vim from osmclient.sol005 import package from osmclient.sol005 import http @@ -85,8 +85,8 @@ class Client(object): self.ns = ns.Ns(self._http_client, client=self) self.vim = vim.Vim(self._http_client, client=self) self.sdnc = sdncontroller.SdnController(self._http_client, client=self) + self.vnf = vnf.Vnf(self._http_client, client=self) ''' - self.vnf = vnf.Vnf(http_client, client=self, **kwargs) self.vca = vca.Vca(http_client, client=self, **kwargs) self.utils = utils.Utils(http_client, **kwargs) ''' diff --git a/osmclient/sol005/vnf.py b/osmclient/sol005/vnf.py new file mode 100644 index 0000000..0a1e44a --- /dev/null +++ b/osmclient/sol005/vnf.py @@ -0,0 +1,72 @@ +# 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 vnf API handling +""" + +from osmclient.common import utils +from osmclient.common.exceptions import NotFound + + +class Vnf(object): + + def __init__(self, http=None, client=None): + self._http = http + self._client = client + self._apiName = '/nslcm' + self._apiVersion = '/v1' + self._apiResource = '/vnfrs' + self._apiBase = '{}{}{}'.format(self._apiName, + self._apiVersion, self._apiResource) + + def list(self, ns=None): + """Returns a list of VNF instances + """ + filter_string = '' + if ns: + ns_instance = self._client.ns.get(ns) + filter_string = '?nsr-id-ref={}'.format(ns_instance['_id']) + resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + if resp: + return resp + return list() + + def get(self, name): + """Returns a VNF instance based on name or id + """ + if utils.validate_uuid4(name): + for vnf in self.list(): + if vnf == vnf['_id']: + return vnf + else: + for vnf in self.list(): + if name == vnf['name']: + return vnf + raise NotFound("vnf {} not found".format(name)) + + def get_individual(self, name): + vnf_id = name + if not utils.validate_uuid4(name): + for vnf in self.list(): + if name == vnf['name']: + vnf_id = vnf['_id'] + break + resp = self._http.get_cmd('{}/{}'.format(self._apiBase, vnf_id)) + if resp: + return resp + raise NotFound("vnf {} not found".format(name)) + -- 2.17.1