From 8160019e32e0c1009d7a98cf965b9bb9bf10f664 Mon Sep 17 00:00:00 2001 From: Mike Marchetti Date: Wed, 19 Apr 2017 12:42:34 -0400 Subject: [PATCH] initial commit Signed-off-by: Mike Marchetti --- LICENSE | 201 ++++++++++++++++ Makefile | 2 + README.md | 76 ++++++ osmclient/__init__.py | 0 osmclient/common/OsmAPI.py | 436 ++++++++++++++++++++++++++++++++++ osmclient/common/__init__.py | 0 osmclient/scripts/__init__.py | 0 osmclient/scripts/osm.py | 191 +++++++++++++++ setup.py | 17 ++ stdeb.cfg | 2 + 10 files changed, 925 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 osmclient/__init__.py create mode 100644 osmclient/common/OsmAPI.py create mode 100644 osmclient/common/__init__.py create mode 100644 osmclient/scripts/__init__.py create mode 100755 osmclient/scripts/osm.py create mode 100644 setup.py create mode 100644 stdeb.cfg diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8dada3e --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + 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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1a2e448 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +package: + @python setup.py --command-packages=stdeb.command bdist_deb > /dev/null 2>&1 diff --git a/README.md b/README.md new file mode 100644 index 0000000..1df94df --- /dev/null +++ b/README.md @@ -0,0 +1,76 @@ +# python-osmclient +A python client for osm orchestration + +# Installation + +## Install dependencies +```bash +sudo apt-get install python-dev libcurl4-gnutls-dev python-pip libgnutls-dev python-prettytable   +sudo pip install pycurl +``` + +## Install python-osmclient + sudo pip install git+https://github.com/mfmarche/python-osmclient + +# Setup +Set the OSM_HOSTNAME variable to the host of the osm server. + +Example +```bash +localhost$ export OSM_HOSTNAME=:8008 +``` + +# Examples + +## upload vnfd +```bash +localhost$ osm upload-package ubuntu_xenial_vnf.tar.gz +{'transaction_id': 'ec12af77-1b91-4c84-b233-60f2c2c16d14'} +localhost$ osm vnfd-list ++--------------------+--------------------+ +| vnfd name | id | ++--------------------+--------------------+ +| ubuntu_xenial_vnfd | ubuntu_xenial_vnfd | ++--------------------+--------------------+ +``` + +## upload nsd +```bash +localhost$ osm upload-package ubuntu_xenial_ns.tar.gz +{'transaction_id': 'b560c9cb-43e1-49ef-a2da-af7aab24ce9d'} +localhost$ osm nsd-list ++-------------------+-------------------+ +| nsd name | id | ++-------------------+-------------------+ +| ubuntu_xenial_nsd | ubuntu_xenial_nsd | ++-------------------+-------------------+ +``` +## vim-list + +```bash +localhost$ osm vim-list ++-------------+-----------------+--------------------------------------+ +| ro-account | datacenter name | uuid | ++-------------+-----------------+--------------------------------------+ +| osmopenmano | openstack-site | 2ea04690-0e4a-11e7-89bc-00163e59ff0c | ++-------------+-----------------+--------------------------------------+ +``` + + +## instantiate ns +```bash +localhost$ osm ns-create ubuntu_xenial_nsd testns openstack-site +{'success': ''} +localhost$ osm ns-list ++------------------+--------------------------------------+-------------------+--------------------+---------------+ +| ns instance name | id | catalog name | operational status | config status | ++------------------+--------------------------------------+-------------------+--------------------+---------------+ +| testns | 6b0d2906-13d4-11e7-aa01-b8ac6f7d0c77 | ubuntu_xenial_nsd | running | configured | ++------------------+--------------------------------------+-------------------+--------------------+---------------+ +``` + +# Bash Completion +python-osmclient uses [click](http://click.pocoo.org/5/). You can setup bash completion by putting this in your .bashrc: + + eval "$(_OSM_COMPLETE=source osm)" + diff --git a/osmclient/__init__.py b/osmclient/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/osmclient/common/OsmAPI.py b/osmclient/common/OsmAPI.py new file mode 100644 index 0000000..f85e872 --- /dev/null +++ b/osmclient/common/OsmAPI.py @@ -0,0 +1,436 @@ +from io import BytesIO +import pycurl +import json +import pprint +import uuid +from prettytable import PrettyTable +import time + +class OsmAPI(): + def __init__(self,host,upload_port=8443): + if host is None: + raise Exception('missing host specifier') + + self._user='admin' + self._password='admin' + self._host=host + self._upload_port=upload_port + self._url = 'https://{}/'.format(self._host) + self._upload_url = 'https://{}:{}/'.format(self._host.split(':')[0],self._upload_port) + + def get_curl_cmd(self,url): + curl_cmd = pycurl.Curl() + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.URL, self._url + url ) + curl_cmd.setopt(pycurl.SSL_VERIFYPEER,0) + curl_cmd.setopt(pycurl.SSL_VERIFYHOST,0) + curl_cmd.setopt(pycurl.USERPWD, '{}:{}'.format(self._user,self._password)) + curl_cmd.setopt(pycurl.HTTPHEADER, ['Accept: application/vnd.yand.data+json','Content-Type": "application/vnd.yang.data+json']) + return curl_cmd + + def get_curl_upload_cmd(self,filename): + curl_cmd = pycurl.Curl() + curl_cmd.setopt(pycurl.HTTPPOST,[(('package',(pycurl.FORM_FILE,filename)))]) + curl_cmd.setopt(pycurl.URL, self._upload_url + 'composer/upload?api_server=https://localhost&upload_server=https://' + self._host.split(':')[0]) + curl_cmd.setopt(pycurl.SSL_VERIFYPEER,0) + curl_cmd.setopt(pycurl.SSL_VERIFYHOST,0) + curl_cmd.setopt(pycurl.USERPWD, '{}:{}'.format(self._user,self._password)) + return curl_cmd + + def get_ns_opdata(self,id): + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/operational/ns-instance-opdata/nsr/{}?deep'.format(id)) + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + if data.getvalue(): + return json.loads(data.getvalue().decode()) + return None + + def get_ns_catalog(self): + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/running/nsd-catalog/nsd') + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + if data.getvalue(): + resp = json.loads(data.getvalue().decode()) + return resp + return {'nsd:nsd': []} + + def get_config_agents(self): + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/config/config-agent') + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + if data.getvalue(): + resp = json.loads(data.getvalue().decode()) + if 'rw-config-agent:config-agent' in resp: + return resp['rw-config-agent:config-agent']['account'] + return list() + + def delete_config_agent(self,name): + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/config/config-agent/account/'+name) + curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE") + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + pprint.pprint(resp) + + def add_config_agent(self,name,account_type,server,user,secret): + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/config/config-agent') + curl_cmd.setopt(pycurl.POST,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + + postdata={} + postdata['account'] = list() + + account={} + account['name'] = name + account['account-type'] = account_type + account['juju'] = {} + account['juju']['user'] = user + account['juju']['secret'] = secret + account['juju']['ip-address'] = server + postdata['account'].append(account) + + jsondata=json.dumps(postdata) + curl_cmd.setopt(pycurl.POSTFIELDS,jsondata) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + pprint.pprint(resp) + + def get_ns_instance_list(self): + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/running/ns-instance-config') + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + return resp['nsr:ns-instance-config'] + + def get_vnf_catalog(self): + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/running/vnfd-catalog/vnfd') + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + if data.getvalue(): + resp = json.loads(data.getvalue().decode()) + return resp + return {'vnfd:vnfd': []} + + def get_vcs_info(self): + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/operational/vcs/info') + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + if data.getvalue(): + resp = json.loads(data.getvalue().decode()) + return resp['rw-base:info']['components']['component_info'] + return list() + + def get_vnfr_catalog(self): + data = BytesIO() + curl_cmd=self.get_curl_cmd('v1/api/operational/vnfr-catalog/vnfr') + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + if data.getvalue(): + resp = json.loads(data.getvalue().decode()) + return resp + return None + + def get_vnf(self,vnf_name): + vnfs=self.get_vnfr_catalog() + for vnf in vnfs['vnfr:vnfr']: + if vnf_name == vnf['name']: + return vnf + return None + + def get_vnf_monitoring(self,vnf_name): + vnf=self.get_vnf(vnf_name) + if vnf is not None: + if 'monitoring-param' in vnf: + return vnf['monitoring-param'] + return None + + def get_ns_monitoring(self,ns_name): + ns=self.get_ns(ns_name) + if ns is None: + raise Exception('cannot find ns {}'.format(ns_name)) + + vnfs=self.get_vnfr_catalog() + if vnfs is None: + return None + mon_list={} + for vnf in vnfs['vnfr:vnfr']: + if ns['id'] == vnf['nsr-id-ref']: + if 'monitoring-param' in vnf: + mon_list[vnf['name']] = vnf['monitoring-param'] + + return mon_list + + def list_key_pair(self): + data = BytesIO() + curl_cmd=self.get_curl_cmd('v1/api/config/key-pair?deep') + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + if 'nsr:key-pair' in resp: + return resp['nsr:key-pair'] + return list() + + def list_ns_catalog(self): + resp = self.get_ns_catalog() + table=PrettyTable(['nsd name','id']) + for ns in resp['nsd:nsd']: + table.add_row([ns['name'],ns['id']]) + table.align='l' + print(table) + + def list_ns_instance(self): + resp = self.get_ns_instance_list() + table=PrettyTable(['ns instance name','id','catalog name','operational status','config status']) + if 'nsr' in resp: + for ns in resp['nsr']: + nsopdata=self.get_ns_opdata(ns['id']) + nsr=nsopdata['nsr:nsr'] + table.add_row([nsr['name-ref'],nsr['ns-instance-config-ref'],nsr['nsd-name-ref'],nsr['operational-status'],nsr['config-status']]) + table.align='l' + print(table) + + def get_nsd(self,name): + resp = self.get_ns_catalog() + for ns in resp['nsd:nsd']: + if name == ns['name']: + return ns + return None + + def get_vnfd(self,name): + resp = self.get_vnf_catalog() + for vnf in resp['vnfd:vnfd']: + if name == vnf['name']: + return vnf + return None + + def get_ns(self,name): + resp=self.get_ns_instance_list() + for ns in resp['nsr']: + if name == ns['name']: + return ns + return None + + def instantiate_ns(self,nsd_name,nsr_name,account,vim_network_prefix=None,ssh_keys=None,description='default description',admin_status='ENABLED'): + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/config/ns-instance-config/nsr') + curl_cmd.setopt(pycurl.POST,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + + postdata={} + postdata['nsr'] = list() + nsr={} + nsr['id']=str(uuid.uuid1()) + + nsd=self.get_nsd(nsd_name) + if nsd is None: + raise Exception('cannot find nsd {}'.format(nsd_name)) + + datacenter=self.get_datacenter(account) + if datacenter is None: + raise Exception('cannot find datacenter account {}'.format(account)) + + nsr['nsd']=nsd + nsr['name']=nsr_name + nsr['short-name']=nsr_name + nsr['description']=description + nsr['admin-status']=admin_status + nsr['om-datacenter']=datacenter['uuid'] + + if ssh_keys is not None: + # ssh_keys is comma separate list + ssh_keys_format=[] + for key in ssh_keys.split(','): + ssh_keys_format.append({'key-pair-ref':key}) + + nsr['ssh-authorized-key']=ssh_keys_format + + if vim_network_prefix is not None: + for index,vld in enumerate(nsr['nsd']['vld']): + network_name = vld['name'] + nsr['nsd']['vld'][index]['vim-network-name'] = '{}-{}'.format(vim_network_prefix,network_name) + + postdata['nsr'].append(nsr) + jsondata=json.dumps(postdata) + curl_cmd.setopt(pycurl.POSTFIELDS,jsondata) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + pprint.pprint(resp) + + def delete_nsd(self,nsd_name): + nsd=self.get_nsd(nsd_name) + if nsd is None: + raise Exception('cannot find nsd {}'.format(nsd_name)) + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/running/nsd-catalog/nsd/'+nsd['id']) + curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE") + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + pprint.pprint(resp) + + def delete_vnfd(self,vnfd_name): + vnfd=self.get_vnfd(vnfd_name) + if vnfd is None: + raise Exception('cannot find vnfd {}'.format(vnfd_name)) + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/running/vnfd-catalog/vnfd/'+vnfd['id']) + curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE") + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + pprint.pprint(resp) + + def terminate_ns(self,ns_name): + ns=self.get_ns(ns_name) + if ns is None: + raise Exception('cannot find ns {}'.format(ns_name)) + + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/config/ns-instance-config/nsr/'+ns['id']) + curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE") + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + pprint.pprint(resp) + + def upload_package(self,filename): + data = BytesIO() + curl_cmd=self.get_curl_upload_cmd(filename) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + pprint.pprint(resp) + + def add_vim_account(self,name,user_name,secret,auth_url,tenant,mgmt_network,float_ip_pool,account_type='openstack'): + data = BytesIO() + curl_cmd=self.get_curl_cmd('api/config/cloud') + curl_cmd.setopt(pycurl.POST,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + vim_account={} + vim_account['account']={} + + vim_account['account']['name'] = name + vim_account['account']['account-type'] = account_type + vim_account['account'][account_type] = {} + vim_account['account'][account_type]['key'] = user_name + vim_account['account'][account_type]['secret'] = secret + vim_account['account'][account_type]['auth_url'] = auth_url + vim_account['account'][account_type]['tenant'] = tenant + vim_account['account'][account_type]['mgmt-network'] = mgmt_network + vim_account['account'][account_type]['floating-ip-pool'] = float_ip_pool + + jsondata=json.dumps(vim_account) + curl_cmd.setopt(pycurl.POSTFIELDS,jsondata) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + pprint.pprint(resp) + + def list_vim_accounts(self): + data = BytesIO() + curl_cmd=self.get_curl_cmd('v1/api/operational/datacenters') + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + datacenters=resp['rw-launchpad:datacenters'] + if 'ro-accounts' in datacenters: + return datacenters['ro-accounts'] + return list() + + def get_datacenter(self,name): + data = BytesIO() + curl_cmd=self.get_curl_cmd('v1/api/operational/datacenters') + curl_cmd.setopt(pycurl.HTTPGET,1) + curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) + curl_cmd.perform() + curl_cmd.close() + resp = json.loads(data.getvalue().decode()) + datacenters=resp['rw-launchpad:datacenters'] + if 'ro-accounts' in datacenters: + for roaccount in datacenters['ro-accounts']: + if not 'datacenters' in roaccount: + continue + for datacenter in roaccount['datacenters']: + if datacenter['name'] == name: + return datacenter + return None + + def show_ns(self,ns_name): + resp = self.get_ns_instance_list() + table=PrettyTable(['attribute','value']) + + if 'nsr' in resp: + for ns in resp['nsr']: + if ns_name == ns['name']: + # dump ns config + for k,v in ns.items(): + table.add_row([k,json.dumps(v,indent=2)]) + + nsopdata=self.get_ns_opdata(ns['id']) + nsr_optdata=nsopdata['nsr:nsr'] + for k,v in nsr_optdata.items(): + table.add_row([k,json.dumps(v,indent=2)]) + table.align='l' + print(table) + + def show_ns_scaling(self,ns_name): + resp = self.get_ns_instance_list() + + table=PrettyTable(['instance-id','operational status','create-time','vnfr ids']) + + if 'nsr' in resp: + for ns in resp['nsr']: + if ns_name == ns['name']: + nsopdata=self.get_ns_opdata(ns['id']) + scaling_records=nsopdata['nsr:nsr']['scaling-group-record'] + for record in scaling_records: + if 'instance' in record: + instances=record['instance'] + for inst in instances: + table.add_row([inst['instance-id'],inst['op-status'],time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(inst['create-time'])),inst['vnfrs']]) + table.align='l' + print(table) + + def list_vnfr(self): + return self.get_vnfr_catalog() + + def list_vnf_catalog(self): + resp = self.get_vnf_catalog() + table=PrettyTable(['vnfd name','id']) + for ns in resp['vnfd:vnfd']: + table.add_row([ns['name'],ns['id']]) + table.align='l' + print(table) diff --git a/osmclient/common/__init__.py b/osmclient/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/osmclient/scripts/__init__.py b/osmclient/scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/osmclient/scripts/osm.py b/osmclient/scripts/osm.py new file mode 100755 index 0000000..d74580c --- /dev/null +++ b/osmclient/scripts/osm.py @@ -0,0 +1,191 @@ +import click +from osmclient.common import OsmAPI +from prettytable import PrettyTable +import pprint +import textwrap + +@click.group() +@click.option('--hostname',default=None,envvar='OSM_HOSTNAME',help='hostname of server. Also can set OSM_HOSTNAME in environment') +@click.pass_context +def cli(ctx,hostname): + if hostname is None: + print("either hostname option or OSM_HOSTNAME environment variable needs to be specified") + exit(1) + ctx.obj=OsmAPI.OsmAPI(hostname) + +@cli.command(name='ns-list') +@click.pass_context +def ns_list(ctx): + ctx.obj.list_ns_instance() + +@cli.command(name='nsd-list') +@click.pass_context +def nsd_list(ctx): + ctx.obj.list_ns_catalog() + +@cli.command(name='vnfd-list') +@click.pass_context +def vnfd_list(ctx): + ctx.obj.list_vnf_catalog() + +@cli.command(name='vnf-list') +@click.pass_context +def vnf_list(ctx): + resp=ctx.obj.list_vnfr() + table=PrettyTable(['vnf name','id','operational status','config Status','mgmt interface','nsr id']) + if resp is not None: + for vnfr in resp['vnfr:vnfr']: + if not 'mgmt-interface' in vnfr: + vnfr['mgmt-interface'] = {} + vnfr['mgmt-interface']['ip-address'] = None + table.add_row([vnfr['name'],vnfr['id'],vnfr['operational-status'],vnfr['config-status'],vnfr['mgmt-interface']['ip-address'],vnfr['nsr-id-ref']]) + table.align='l' + print(table) + +@cli.command(name='vnf-monitoring-show') +@click.argument('vnf_name') +@click.pass_context +def vnf_monitoring_show(ctx,vnf_name): + resp=ctx.obj.get_vnf_monitoring(vnf_name) + table=PrettyTable(['vnf name','monitoring name','value','units']) + if resp is not None: + for monitor in resp: + table.add_row([vnf_name,monitor['name'],monitor['value-integer'],monitor['units']]) + table.align='l' + print(table) + +@cli.command(name='ns-monitoring-show') +@click.argument('ns_name') +@click.pass_context +def ns_monitoring_show(ctx,ns_name): + resp=ctx.obj.get_ns_monitoring(ns_name) + table=PrettyTable(['vnf name','monitoring name','value','units']) + if resp is not None: + for key,val in resp.items(): + for monitor in val: + table.add_row([key,monitor['name'],monitor['value-integer'],monitor['units']]) + table.align='l' + print(table) + +@cli.command(name='ns-create') +@click.argument('nsd_name') +@click.argument('ns_name') +@click.argument('vim_account') +@click.option('--admin_status',default='ENABLED',help='administration status') +@click.option('--ssh_keys',default=None,help='comma separated list of keys to inject to vnfs') +@click.option('--vim_network_prefix',default=None,help='vim network name prefix') +@click.pass_context +def ns_create(ctx,nsd_name,ns_name,vim_account,admin_status,ssh_keys,vim_network_prefix): + ctx.obj.instantiate_ns(nsd_name,ns_name,vim_network_prefix=vim_network_prefix,ssh_keys=ssh_keys,account=vim_account) + +@cli.command(name='ns-delete') +@click.argument('ns_name') +@click.pass_context +def ns_delete(ctx,ns_name): + ctx.obj.terminate_ns(ns_name) + +''' +@cli.command(name='keypair-list') +@click.pass_context +def keypair_list(ctx): + resp=ctx.obj.list_key_pair() + table=PrettyTable(['key Name','key']) + for kp in resp: + table.add_row([kp['name'],kp['key']]) + table.align='l' + print(table) +''' + +@cli.command(name='upload-package') +@click.argument('filename') +@click.pass_context +def upload_package(ctx,filename): + ctx.obj.upload_package(filename) + +@cli.command(name='ns-show') +@click.argument('ns_name') +@click.pass_context +def ns_show(ctx,ns_name): + ctx.obj.show_ns(ns_name) + +@cli.command(name='ns-scaling-show') +@click.argument('ns_name') +@click.pass_context +def show_ns_scaling(ctx,ns_name): + ctx.obj.show_ns_scaling(ns_name) + +@cli.command(name='nsd-delete') +@click.argument('nsd_name') +@click.pass_context +def nsd_delete(ctx,nsd_name): + ctx.obj.delete_nsd(nsd_name) + +@cli.command(name='vnfd-delete') +@click.argument('vnfd_name') +@click.pass_context +def nsd_delete(ctx,vnfd_name): + ctx.obj.delete_vnfd(vnfd_name) + +@cli.command(name='config-agent-list') +@click.pass_context +def config_agent_list(ctx): + table=PrettyTable(['name','account-type','details']) + for account in ctx.obj.get_config_agents(): + table.add_row([account['name'],account['account-type'],account['juju']]) + table.align='l' + print(table) + +@cli.command(name='config-agent-delete') +@click.argument('name') +@click.pass_context +def config_agent_delete(ctx,name): + ctx.obj.delete_config_agent(name) + +@cli.command(name='config-agent-add') +@click.argument('name') +@click.argument('account_type') +@click.argument('server') +@click.argument('user') +@click.argument('secret') +@click.pass_context +def config_agent_add(ctx,name,account_type,server,user,secret): + ctx.obj.add_config_agent(name,account_type,server,user,secret) + +''' +@cli.command(name='vim-create') +@click.argument('name') +@click.argument('user') +@click.argument('password') +@click.argument('auth_url') +@click.argument('tenant') +@click.argument('mgmt_network') +@click.argument('floating_ip_pool') +@click.option('--account_type',default='openstack') +@click.pass_context +def vim_create(ctx,name,user,password,auth_url,tenant,mgmt_network,floating_ip_pool,account_type): + ctx.obj.add_vim_account(name,user,password,auth_url,tenant,mgmt_network,floating_ip_pool,account_type) +''' + +@cli.command(name='vim-list') +@click.pass_context +def vim_list(ctx): + resp=ctx.obj.list_vim_accounts() + table=PrettyTable(['ro-account','datacenter name','uuid']) + for roaccount in resp: + for datacenter in roaccount['datacenters']: + table.add_row([roaccount['name'],datacenter['name'],datacenter['uuid']]) + table.align='l' + print(table) + +@cli.command(name='vcs-list') +@click.pass_context +def vcs_list(ctx): + resp=ctx.obj.get_vcs_info() + table=PrettyTable(['component name','state']) + for component in resp: + table.add_row([component['component_name'],component['state']]) + table.align='l' + print(table) + +if __name__ == '__main__': + cli() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..13b1771 --- /dev/null +++ b/setup.py @@ -0,0 +1,17 @@ +from setuptools import setup,find_packages + +setup( + name='osmclient', + version='0.1', + author='Mike Marchetti', + author_email='mmarchetti@sandvine.com', + packages=find_packages(), + include_package_data=True, + install_requires=[ + 'Click','prettytable' + ], + entry_points=''' + [console_scripts] + osm=osmclient.scripts.osm:cli + ''', +) diff --git a/stdeb.cfg b/stdeb.cfg new file mode 100644 index 0000000..eadf0fa --- /dev/null +++ b/stdeb.cfg @@ -0,0 +1,2 @@ +[DEFAULT] +Depends: python-setuptools, python-pycurl, python-click, python-prettytable -- 2.25.1