Coder Social home page Coder Social logo

pynsxv's Introduction

VMware has ended active development of this project, this repository will no longer be updated.

PyNSXv

PyNSXv is a high level python based library that exposes ready to use work-flows and a CLI tool that can be used to control VMware NSX for vSphere

PyNSXv can be used in two different ways, as a library by importing the files in the /library subdirectory into your code, or as a CLI tool by executing pynsxvon the command line after installation. To install PyNSXv you can use PIP on your system

Table of Contents

[Using PyNSXv as a CLI Tool](#Using PyNSXv as a CLI Tool)
[Using PyNSXv as a Python Library](#Using PyNSXv as a Python Library)
Dependencies
[Installing PyNSXv](#Installing PyNSXv)
Contributing
License

#Using PyNSXv as a CLI Tool

After you installed PyNSXv, the first thing you have to do is to create your a ini file that contains the host names and credentials of your vCenter and NSX Manager.

If you have multiple NSX environments, you can create multiple ini files and reference to them using the -i command line option. By default PyNSXv will look for a file called nsx.ini in the path your are running the pynsxv command in.

The ini file has the following format:

# [nsxraml]
# nsxraml_file = <>
# Uncomment the above section and add the path to the raml spec you want to use instead of the bundled version

[nsxv]
nsx_manager = <nsx_manager_IP>
nsx_username = admin
nsx_password = <nsx_manager_password>

[vcenter]
vcenter = <VC_IP_or_Hostname>
vcenter_user = [email protected]
vcenter_passwd = <vc_password>

[defaults]
transport_zone = <transport_zone_name>
datacenter_name = <vcenter datacenter name>
edge_datastore = <datastore name to deploy edges in>
edge_cluster = <vcenter cluster for edge gateways>

After placing the nsx.ini file in you path, you can run pynsxv from your shell or cmd prompt. On Linux and Mac simply use pynsxv followed by the subcommand. On Windows you will need to type pynsxv.exe followed by the subcommand:

▶ pynsxv -h
usage: pynsxv [-h] [-i INI] [-v] [-d] {lswitch,dlr,esg,dhcp,dfw,usage} ...

PyNSXv Command Line Client for NSX for vSphere

positional arguments:
  {lswitch,dlr,esg,dhcp,dfw,usage}
    lswitch             Functions for logical switches
    dlr                 Functions for distributed logical routers
    esg                 Functions for edge services gateways
    dhcp                Functions for Edge DHCP
    dfw                 Functions for distributed firewall
    usage               Functions to retrieve NSX-v usage statistics

optional arguments:
  -h, --help            show this help message and exit
  -i INI, --ini INI     nsx configuration file
  -v, --verbose         increase output verbosity
  -d, --debug           print low level debug of http transactions

Here is an example output using the lswitch subcommand:

▶ pynsxv lswitch list
+---------------------+----------------+
| LS name             | LS ID          |
|---------------------+----------------|
| edge_ls             | virtualwire-63 |
| dlr_ls              | virtualwire-64 |
+---------------------+----------------+

Each subcommand has its own set of subcommands, as well as arguments.
You can see what is available by using -h after the first subcommand:

▶ pynsxv lswitch -h
usage: cli.py lswitch [-h] [-t TRANSPORT_ZONE] [-n NAME] command

Functions for logical switches

positional arguments:
  command
                            create: create a new logical switch
                            read:   return the virtual wire id of a logical switch
                            delete: delete a logical switch"
                            list:   return a list of all logical switches


optional arguments:
  -h, --help            show this help message and exit
  -t TRANSPORT_ZONE, --transport_zone TRANSPORT_ZONE
                        nsx transport zone
  -n NAME, --name NAME  logical switch name, needed for create, read and delete

You can also use the -v switch of the main pynsxv command to switch to a json formated output for use with shell scripts

#Using PyNSXv as a Python Library

When using PyNSXv as a Python library you can either download the individual modules in /library of this repo and import them in your code, or you can install PyNSXv and import the modules from your python path for pip installed modules.

To use the modules you will also need to have at least the nsxramlclient installed (https://github.com/vmware/nsxramlclient).
You will also need a copy of the latest RAML API Spec of NSX for vSphere (https://github.com/vmware/nsxraml).

Import the module you want to use, as well as the NsxClient Class of the nsxramlclient.
Then instantiate a session object of the NsxClient Class, and pass this session object to the function you want to use out of the module imported from PyNSXv. Here's an example:

▶ ipython
Python 2.7.11 (default, Jun 17 2016, 20:01:51)
Type "copyright", "credits" or "license" for more information.

IPython 4.2.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from pynsxv.library.nsx_logical_switch import logical_switch_list,logical_switch_create

In [2]: from nsxramlclient.client import NsxClient

In [3]: nsxraml_file = '/raml/nsxraml/nsxvapi.raml'
In [4]: nsxmanager = 'nsxmanager.invalid.org'
In [5]: nsx_username = 'admin'
In [6]: nsx_password = 'vmware'

In [7]: client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password)

In [8]: lswitch_list = logical_switch_list(client_session)

In [9]: lswitch_list
Out[9]:
([('k8s-dlr-plr-transit', 'virtualwire-39'),
  ('k8s-minion-1', 'virtualwire-40'),
  ('k8s-minion-2', 'virtualwire-41'),
  ('vic-external', 'virtualwire-51'),
  ('vic-container-net1', 'virtualwire-52'),
  ('edge_ls', 'virtualwire-63'),
  ('dlr_ls', 'virtualwire-64')],
 [{'clientHandle': None,
   'controlPlaneMode': 'HYBRID_MODE',
   'ctrlLsUuid': 'e0c72e20-f39e-41ba-adb4-f0b191521c0c',
   'description': None,
   'extendedAttributes': None,
   'guestVlanAllowed': 'false',
   'isUniversal': 'false',

   .... Output truncated ....

In [10]: new_lswitch = logical_switch_create(client_session, 'TZ1', 'new_lswitch_name')

In [11]: new_lswitch
Out[12]: ('virtualwire-65', '/api/2.0/vdn/virtualwires/virtualwire-65')

All module function have inline documentation available to guide you through the needed parameters:

In [1]: help(logical_switch_list)

logical_switch_list(client_session)
    This function returns all logical switches found in NSX
    :param client_session: An instance of an NsxClient Session
    :return: returns a tuple, the first item is a list of tuples with item 0 containing the LS Name as string
             and item 1 containing the LS id as string. The second item contains a list of dictionaries containing
             all logical switch details

#Dependencies PyNSXv has the following dependencies:

Please check the installation instructions of these projects if you run into installation issues

#Installing PyNSXv

PyNSXv can be installed using pip:

pip install pynsxv

[Installing PyNSXv on Ubuntu](#Installing PyNSXv on Ubuntu)
[Installing PyNSXv on a MAC](#Installing PyNSXv on a MAC)
[Installing PyNSXv on Windows](#Installing PyNSXv on Windows)
[Various caveats](#Various caveats)

#Installing PyNSXv on Ubuntu

First update/upgrade using apt and install the python-openssl, libxml and libxslt dependencies of the nsxramclient using apt. Please also consult the Github page of the nsxramclient and pyvmomi in case you run into issues during the installation of these dependencies.

apt-get update && apt-get upgrade -y
apt-get install libssl-dev libffi-dev libxml2-dev libxslt-dev python-dev zlib1g-dev python-pip -y

After this you can simply install PyNSXv using pip:

sudo pip install pynsxv

#Installing PyNSXv on a MAC

Make sure to install XCODE and its Command Line utilities: https://itunes.apple.com/app/xcode/id497799835?mt=12

xcode-select --install

If not yet present on your system, install python pip. The instructions on how to do so can be found here: https://pip.pypa.io/en/latest/installing/#install-or-upgrade-pip

curl https://bootstrap.pypa.io/get-pip.py > get-pip.py
sudo python get-pip.py

If you don't have it yet, install homebrew on your system: http://brew.sh

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Now install some of the xml formating dependencies needed by PyNSXv and the underlying nsxramlclient

brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force

Finaly we are ready to install PyNSXv using pip:

sudo pip install pynsxv

NOTE: OSX 10.11 El Capitan introduces problems due to an outdated dependency six. Due to OSX's new System Integrity Protection (SIP) pip cannot remove the outdated version and install the needed one: http://apple.stackexchange.com/questions/209572/how-to-use-pip-after-the-os-x-el-capitan-upgrade/210021#210021 http://stackoverflow.com/questions/31900008/oserror-errno-1-operation-not-permitted-when-installing-scrapy-in-osx-10-11

If you run into this error:

OSError: [Errno 1] Operation not permitted: '/tmp/pip-nIfswi-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

You should install a new non-bundled python version:

brew install python

sudo pip uninstall pynsxv
sudo pip uninstall six
sudo pip uninstall PyYAML
sudo pip uninstall pyraml-parser
sudo pip uninstall nsxramlclient

pip install pyopenssl
pip install pynsxv

#Installing PyNSXv on Windows

Install python 2.x on your Windows (https://www.python.org/downloads/). Note: Attention do NOT install python 3.x.

Add python installation folder in the Windows path to be able to run the pynsxv CLI from any folder (Under "Control Panel – Environment Variables", edit the System Variable "Path", and add the "Python27\Scripts" folder).

Install of pynsxv.

C:\>pip install pynsxv

You can find more details in the /docs folder Windows Installation Details

#Various caveats

Should you see this warning message: UserWarning: /home/vagrant/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).

You can solve this by changing the permissions of the .python-eggs folder:

chmod g-wx,o-wx ~/.python-eggs

#Contributing

Everyone is more than welcome to contribute to PyNSXv. If you come up with any interesting additional subcommand, workflow, bugfix that you would like to share, you can simply send us a pull request. Should you be interested in helping us coding missing functionality, you can see what we are tracking as enhancements in the Github Issue tracker of this repository. Before sending us your pull request, please make sure that you pull the latest code from the devel branch, and base your additions from this branch. Also note that you might be requested to sign a Contributor License Agreement before we can merge your code. This happens automatically when you submit your first pull request.

If you don't want to code, we still very much welcome any help with testing and requests for additional functionality. Please don't hesitate to contact us and open tickets in the Github Issue tracker if you need help using PyNSXv.

#License

Licensed under the X11 (MIT)license (the “License”) set forth below; you may not use this file except in compliance with the License. 

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.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pynsxv's People

Contributors

alaquiante avatar avoltmer avatar dananourie avatar ddesmidt avatar dstefka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pynsxv's Issues

DFW Section prompt

Creating a new DFW section will allow -sname prompt me with the allowed values when a value is incorrect.

-stype does not and causes a traceback.

Command works still though the output is not nice.

aburke@Dreamspike ~/D/p/pynsxv> pynsxv dfw create_section -sname pynsxv -stype dog
Section Type Unknown - Allowed values are L2/L3/L3R -- Aborting
Traceback (most recent call last):
  File "/usr/local/bin/pynsxv", line 9, in <module>
    load_entry_point('PyNSXv==0.4', 'console_scripts', 'pynsxv')()
  File "build/bdist.macosx-10.11-x86_64/egg/pynsxv/cli.py", line 62, in main
  File "build/bdist.macosx-10.11-x86_64/egg/pynsxv/library/nsx_dfw.py", line 1625, in _dfw_main
  File "build/bdist.macosx-10.11-x86_64/egg/pynsxv/library/nsx_dfw.py", line 1443, in _dfw_section_create_print
TypeError: 'NoneType' object is not iterable

The command will still work


aburke@Dreamspike ~/D/p/pynsxv> pynsxv dfw create_section -sname pynsxv -stype L3
+------------------------+------+--------+
| Name                   |   ID | Type   |
|------------------------+------+--------|
| pynsxv                 | 1005 | LAYER3 |
| Bookstore              | 1004 | LAYER3 |
| Default Section Layer3 | 1003 | LAYER3 |
+------------------------+------+--------+

Is this project dead?

Can anyone comment on the status of this project? This is the only work I have seen for DFW via python...

Add shell script examples

Add examples on how to create lswitches, esg'd, dlr's using shell and windows CMD scripts using the CLI functionality

Add SSL VPN Support

Enable or disable SSL VPN
Get SSL VPN details
Apply server settings
Apply server settings
Add private network
Add private network
Query private network
Query private network
Delete private network
Delete private network
Apply all private networks
Upload RSA config file
Apply Authentication Configuration
Query Authentication Configuration
Add IP pool
Modify IP pool
Get IP pool
Delete IP pool
Modify IP pool
Gets all IP pools
Deletes all IP pools
Add a user
Modify user
Delete user
Query user
Delete all user
Apply all users
Add installation package
Modify installation package
Query installation package
Delete installation package
Query all installation package
Delete all installation packages
Apply installation packages

Add ESG code to tune Configuration

New in 6.2.3, there's an API to tune the edge configuration:

/api/4.0/edgePublish/tuningConfiguration

It gives access to the following Configs

<lockUpdatesOnEdge>false</lockUpdatesOnEdge>
<aggregatePublishing>false</aggregatePublishing>
<edgeVMHealthCheckIntervalInMin>0</edgeVMHealthCheckIntervalInMin>
<healthCheckCommandTimeoutInMs>120000</healthCheckCommandTimeoutInMs>
<maxParallelVixCallsForHealthCheck>25</maxParallelVixCallsForHealthCheck>
<publishingTimeoutInMs>1200000</publishingTimeoutInMs>
0 0 0

generate vCenter content object only when needed

Right now the vCenter Content Object gets created each time the CLI is invoked, no matter if the subsequently executed function needs it or not. This slows down the response unnecessarily.

We should implement a check like the one done in the lswitch module for all modules we have:

    if args.command in ['list_pgs', 'list_vms']:
        vccontent = connect_to_vc(config.get('vcenter', 'vcenter'), config.get('vcenter', 'vcenter_user'),
                                  config.get('vcenter', 'vcenter_passwd'))
    else:
        vccontent = None

Basic Edge Services Gateway Functionality

This new feature is about creation of Edge Gateways and their Base Configuration.

edge_create Create 1 Edge with "admin_pwd" + "Cluster" + "DataStore" + "uplink interface/IP/subnet" + "dgw" + "FW status"
edge_read Find Edge ID from Edge Name
edge_delete Delete Edge
edge_list Display all Edges

edge_interface_create Add internal interface in an existing Edge
edge_interface_read Get internal interface id
edge_interface_delete Delete internal interface in an existing Edge
edge_interface_list Display all interfaces of an existing Edge

edge_secondaryip_create Add secondary IP in an existing interface
edge_secondaryip_delete Delete secondary IP in an existing interface

edge_dgw_create Configure default gw of an Edge
edge_dgw_delete Delete default gw of an Edge
edge_dgw_list Display default gw of an Edge

edge_static_create Configure static route of an Edge
edge_static_delete Delete static route of an Edge
edge_static_list Display static route of an Edge

edge_ssh_enable ssh on Edge
edge_edgesize Change edge size

Distributed Firewall functionality

Implement Distributed Firewall functionality with the following capabilities:

  • Create 1 Section with no rule client_session, section_name
  • Find Section ID from Section Name client_session, section_name
  • Delete 1 Section client_session, section_id
  • Display all Sections client_session
  • Display one Section client_session, section_id
  • Add 1 Rule in a section "client_session, section_id, rule_name,
    source_type, source_value, dest_type, dest_value,
    service_proto, service_port, action,
    apply_to_type, apply_to_value"
  • Find rule ID from Section ID client_session, rule_name, section_id
  • Delete 1 Rule client_session, rule_id
  • Display all Rules client_session
  • Display one Rule client_session, rule_id
  • Add 1 source in an existing rule "client_session, rule_id,
    source_type, source_value"
  • Add 1 dest in an existing rule "client_session, rule_id,
    dest_type, dest_value"
  • Add 1 service in an existing rule "client_session, rule_id,
    service_proto, service_port"
  • Add 1 apply_to in an existing rule "client_session, rule_id,
    apply_to_type, apply_to_value"
  • Delete 1 source in an existing rule "client_session, rule_id,
    source_type, source_value"
  • Delete 1 dest in an existing rule "client_session, rule_id,
    dest_type, dest_value"
  • Delete 1 service in an existing rule "client_session, rule_id,
    service_proto, service_port"
  • Delete 1 apply_to in an existing rule "client_session, rule_id,
    apply_to_type, apply_to_value"
  • Move 1 rule above another rule "client_session, rule_id_to_move,
    rule_id_base" <<------ STANDBY ( POTENTIALLY SHIFTED POST GA )
  • Move 1 rule below another rule "client_session, rule_id_to_move,
    rule_id_base" <<------ STANDBY ( POTENTIALLY SHIFTED POST GA )

account for duplicate names in all read and delete functions

For logical Switches, and a number of items in the DHCP and LB module, duplicate names are possible in NSXv.
Today the code will always only work with the first item found with the specified name.

We need to standardize on:

  1. Allow the use of names for all delete and read functions
  2. Throw an error when more then one object with the same name is found saying that one needs to also specify the object Id to make sure we delete the right object
  3. Allow the user to optionally specify the Object-Id with the name

Harmonize commands DLR / ESG

make the DLR and ESG cli subcommands follow the same semantics _

e.g. set_dgw to set the default gateway instead of dgw_set

Security Tags

  • Create new security tags
  • Delete existing security tags
    • Check the tag is not in use first
  • List all tags
    • List membership
  • Apply security tags to VMs

Basic Load Balancing Functionality

Basic Edge LB functionality using "single port UDP/TCP/HTTP VIP" with "persistence sce_ip/cookie_insert" with "ICMP/UDP/ICMP/HTTP GET url monitor"

Advanced Load Balancing

Advanced Load Balancing on Edge Gw including SSL termination, AppRules, advanced healthchecks

Module needed to backup/restore Edge Configuration

Once the base modules are done, we have customer asks for modules to backup DLR and ESG configuration.

There is an ask for backup/restore functionality for Edges in general. This is hard to achieve as multiple API calls need to be done to backup certain portions of the Edge configuration. Some manipulation of object IDs is also required. Details to Backup/Restore to same VC are captured in the attached doc.

Import_Export_Edge_Config_Unofficial.pdf

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.