def return_sgs(): # Return a list of all the SGs installed in NSX Managers to_return = list() nsxmanager = config.get('nsxv', 'nsx_manager') nsxuser = config.get('nsxv', 'nsx_username') nsxpass = config.get('nsxv', 'nsx_password') nsxramlfile_dir = resource_filename(__name__, 'api_spec') nsxramlfile = '{}/nsxvapi.raml'.format(nsxramlfile_dir) # Too lazy...just hard code it for now nsxramlfile = '/usr/local/lib/python2.7/dist-packages/pynsxv/library/api_spec/nsxvapi.raml' #Connect to NSX Manager nsx_session = NsxClient(nsxramlfile, nsxmanager, nsxuser, nsxpass, debug=False) sgs = nsx_session.read('secGroupScope', uri_parameters={'scopeId': 'globalroot-0'}) # sgs = get_secgroups(nsx_session, 'globalroot-0') sgs_list = sgs.items()[1][1]['list']['securitygroup'] # print sgs_list for i, val in enumerate(sgs_list): # print i,val # print val['name'] to_return.append(val['name']) return to_return
from tests.config import * from nsxramlclient.client import NsxClient import time client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password, debug=True) ipset_dict = client_session.extract_resource_body_schema('ipsetCreate', 'create') ipset_dict['ipset']['name'] = 'Test' ipset_dict['ipset']['value'] = '192.168.1.0/24' ipset_dict['ipset']['inheritanceAllowed'] = 'True' newipset_return = client_session.create('ipsetCreate', uri_parameters={'scopeMoref': 'globalroot-0'}, request_body_dict=ipset_dict) newipset = dict(client_session.read('ipset', uri_parameters={'ipsetId': newipset_return['objectId']})['body']) newipset['ipset']['value'] = '10.0.0.0/16' newipset['ipset']['inheritanceAllowed'] = 'False' time.sleep(10) client_session.update('ipset', uri_parameters={'ipsetId': newipset_return['objectId']}, request_body_dict=newipset) client_session.read('ipsetList', uri_parameters={'scopeMoref': 'globalroot-0'}) time.sleep(10) client_session.delete('ipset', uri_parameters={'ipsetId': newipset_return['objectId']})
# IN THE SOFTWARE. __author__ = 'yfauser' from tests.config import * from nsxramlclient.client import NsxClient import time TRANSPORT_ZONE = 'TZ' client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password, debug=False) # find the objectId of the Scope with the name of the Transport Zone vdn_scopes = client_session.read('vdnScopes', 'read')['body'] vdn_scope_dict_list = [scope_dict for scope_dict in vdn_scopes['vdnScopes'].items()] vdn_scope = [scope[1]['objectId'] for scope in vdn_scope_dict_list if scope[1]['name'] == TRANSPORT_ZONE][0] # get a template dict for the lswitch create lswitch_create_dict = client_session.extract_resource_body_schema('logicalSwitches', 'create') client_session.view_body_dict(lswitch_create_dict) # fill the details for the new lswitch in the body dict lswitch_create_dict['virtualWireCreateSpec']['controlPlaneMode'] = 'UNICAST_MODE' lswitch_create_dict['virtualWireCreateSpec']['name'] = 'TestLogicalSwitch1' lswitch_create_dict['virtualWireCreateSpec']['tenantId'] = 'Tenant1' # create new lswitch new_ls = client_session.create('logicalSwitches', uri_parameters={'scopeId': vdn_scope}, request_body_dict=lswitch_create_dict)
TRANSPORT_ZONE = 'TZ1' client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password, debug=True, fail_mode='raise') lswitch_id1 = 'virtualwire-237' lswitch_id2 = 'virtualwire-999' lswitch_id3 = 'blaar' # find the objectId of the Scope with the name of the Transport Zone vdn_scopes = client_session.read('vdnScopes', 'read')['body'] vdn_scope_dict_list = [ scope_dict for scope_dict in vdn_scopes['vdnScopes'].items() ] vdn_scope = [ scope[1]['objectId'] for scope in vdn_scope_dict_list if scope[1]['name'] == TRANSPORT_ZONE ][0] # Read the properties of the logical switch new_ls_props = client_session.read( 'logicalSwitch', uri_parameters={'virtualWireID': lswitch_id1}) client_session.view_response(new_ls_props) # Read the property of an not existing virtual wire ID try:
from tests.config import * from nsxramlclient.client import NsxClient from nsxramlclient.exceptions import NsxError __author__ = 'yfauser' TRANSPORT_ZONE = 'TZ1' client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password, debug=True, fail_mode='raise') lswitch_id1 = 'virtualwire-237' lswitch_id2 = 'virtualwire-999' lswitch_id3 = 'blaar' # find the objectId of the Scope with the name of the Transport Zone vdn_scopes = client_session.read('vdnScopes', 'read')['body'] vdn_scope_dict_list = [scope_dict for scope_dict in vdn_scopes['vdnScopes'].items()] vdn_scope = [scope[1]['objectId'] for scope in vdn_scope_dict_list if scope[1]['name'] == TRANSPORT_ZONE][0] # Read the properties of the logical switch new_ls_props = client_session.read('logicalSwitch', uri_parameters={'virtualWireID': lswitch_id1}) client_session.view_response(new_ls_props) # Read the property of an not existing virtual wire ID try: new_ls_props = client_session.read('logicalSwitch', uri_parameters={'virtualWireID': lswitch_id2}) client_session.view_response(new_ls_props) except NsxError as e: print "### caught exception !!!!" print e.status, e.msg
class NSX(): ''' NSX Class: Following functionality are implemented. 1. Create and get a hardware gateway. 2. Create and get a hardware binding. 3. Create and get a logical switch. ''' def __init__(self, **kwargs): self._nsxraml_file = kwargs.pop('raml_file') self._nsx_password = kwargs.pop('password') self._nsx_username = kwargs.pop('username') self._nsxmanager = kwargs.pop('ip') self._client_session = NsxClient(self._nsxraml_file, self._nsxmanager, self._nsx_username, self._nsx_password) self.logger = logging.getLogger(__name__) self._cert = "" self._bfd = True self._name = "" def add_hwdevice_cert(self, **kwargs): self._cert = str(kwargs.pop('cert')) self._bfd = kwargs.pop('bfd', True) self._name = str(kwargs.pop('name')) self.logger.info("Creating Hardware Device: %s", self._name) hw_dict = self._client_session.extract_resource_body_example('vdnHardwareGateway', 'create') hw_dict['hardwareGatewaySpec']['name'] = self._name hw_dict['hardwareGatewaySpec']['certificate'] = self._cert hw_dict['hardwareGatewaySpec']['bfdEnabled'] = self._bfd hw = self._client_session.create('vdnHardwareGateway', request_body_dict=hw_dict) if 200 in hw.values(): return True return False def get_hwdevice(self): self.logger.info("Reading Hardware Gateway Certificates") hw = self._client_session.read('vdnHardwareGateway') if 200 in hw.values(): return hw['body']['list'] else: return False def get_hwgateway_biding(self, **kwargs): lswitch_name = str(kwargs.pop('lswitch_name')) self.logger.info("Reading Hardware Bidings for the logical switch id: %r", lswitch_name) objid = self.get_lswitch_objectId(lswitch_name=lswitch_name) if objid: hw_bindings = self._client_session.read('vdnHardwareBinding', uri_parameters={'virtualWireID': objid}) if 200 in hw_bindings.values(): return hw_bindings['body']['list'] else: return False else: self.logger.info("Logical Switch: %r entry not found", lswitch_name) return False def get_hwdevice_objectId(self, **kwargs): hwdevice_name = str(kwargs.pop('hwdevice_name')) hwdevice = self.get_hwdevice() objid = None hw_list = hwdevice['hardwareGateway'] if isinstance(hw_list, dict): if hw_list['name'] == hwdevice_name: objid = hw_list['objectId'] else: for i in hw_list: if i['name'] == hwdevice_name: objid = i['objectId'] break return objid def get_logicalswitch(self): self.logger.info("Reading list of Logical Switches") lswitch = self._client_session.read('logicalSwitchesGlobal') if 200 in lswitch.values(): return lswitch['body']['virtualWires'] else: return False def create_logicalswitch(self, **kwargs): lswitch_name = str(kwargs.pop('name')) zone = str(kwargs.pop('zone', 'TZ1')) controlplan_mode = str(kwargs.pop('controlplan_mode', 'UNICAST_MODE')) tenant = str(kwargs.pop('tenant', 'vsphere.local')) self.logger.info("Crating Logical Switch: %r", lswitch_name) # find the objectId of the Scope with the name of the Transport Zone vdn_scopes = self._client_session.read('vdnScopes', 'read')['body'] vdn_scope_dict_list = [scope_dict for scope_dict in vdn_scopes['vdnScopes'].items()] vdn_scope = [scope[1]['objectId'] for scope in vdn_scope_dict_list if scope[1]['name'] == zone][0] # get a template dict for the lswitch create lswitch_create_dict = self._client_session.extract_resource_body_example('logicalSwitches', 'create') # self._client_session.view_body_dict(lswitch_create_dict) # fill the details for the new lswitch in the body dict lswitch_create_dict['virtualWireCreateSpec']['controlPlaneMode'] = controlplan_mode lswitch_create_dict['virtualWireCreateSpec']['name'] = lswitch_name lswitch_create_dict['virtualWireCreateSpec']['tenantId'] = tenant # create new lswitch new_ls = self._client_session.create('logicalSwitches', uri_parameters={'scopeId': vdn_scope}, request_body_dict=lswitch_create_dict) # self._client_session.view_response(new_ls) if 200 in new_ls.values(): return True else: return False def get_lswitch_objectId(self, **kwargs): lswitch_name = str(kwargs.pop('lswitch_name')) lswitch = self.get_logicalswitch() objid = None sw_list = lswitch['dataPage']['virtualWire'] if isinstance(sw_list, dict): if sw_list['name'] == lswitch_name: objid = sw_list['objectId'] else: for i in sw_list: if i['name'] == lswitch_name: objid = i['objectId'] break return objid def create_hardwarebinding(self, **kwargs): lswitch_name = str(kwargs.pop('lswitch_name')) vlan = str(kwargs.pop('vlan')) port_name = str(kwargs.pop('port_name')) switch_name = str(kwargs.pop('switch_name')) hardware_gateway_name = str(kwargs.pop('hardware_gateway_name')) self.logger.info("Creating Hardware bindings for the logical switch: %r", lswitch_name) hwdevice_id = self.get_hwdevice_objectId(hwdevice_name=hardware_gateway_name) if not hwdevice_id: self.logger.info("Hardware device entry: %r entry not found", hardware_gateway_name) return False objectId = self.get_lswitch_objectId(lswitch_name=lswitch_name) if objectId: hw_dict = self._client_session.extract_resource_body_example('vdnHardwareBinding', 'create') hw_dict['hardwareGatewayBinding']['hardwareGatewayId'] = hwdevice_id hw_dict['hardwareGatewayBinding']['portName'] = port_name hw_dict['hardwareGatewayBinding']['vlan'] = vlan hw_dict['hardwareGatewayBinding']['switchName'] = switch_name hw = self._client_session.create('vdnHardwareBinding', uri_parameters={'virtualWireID': objectId}, request_body_dict=hw_dict) if 200 in hw.values(): return True else: return False else: self.logger.info("Logical Switch: %r entry not found", lswitch_name) return False
debug=True) macset_dict = client_session.extract_resource_body_schema( 'macsetScopeCreate', 'create') macset_dict['macset']['name'] = 'test0' # CREATE macset on scope newmacset_return = client_session.create( 'macsetScopeCreate', uri_parameters={'scopeId': 'globalroot-0'}, request_body_dict=macset_dict) # READ macset by ID newmacset = dict( client_session.read( 'macset', uri_parameters={'macsetId': newmacset_return['objectId']})['body']) # UPDATE macset by ID newmacset['macset']['name'] = 'test1' newmacset['macset']['revision'] = '1' newmacset['macset']['value'] = '44:55:66:77:88:99' time.sleep(10) client_session.update( 'macset', uri_parameters={'macsetId': newmacset_return['objectId']}, request_body_dict=newmacset) # READ macsets on scope
# # 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. __author__ = 'yfauser' from nsxramlclient.client import NsxClient from config import * client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password, debug=False) # Get specific scope Information response_scope1 = client_session.read('vdnScope', uri_parameters={'scopeId': 'vdnscope-1'}) client_session.view_response(response_scope1) # Get all scopes response_all_scopes = client_session.read('vdnScopes') client_session.view_response(response_all_scopes) # Add a scope create_body = client_session.extract_resource_body_schema('vdnScopes', 'create') client_session.view_body_dict(create_body) create_body['vdnScope']['clusters']['cluster']['cluster'] #create_response = client_session.create('vdnScopes', request_body_dict=create_body) #TODO: Complete the tests for Scopes
__author__ = 'yfauser' from tests.config import * from nsxramlclient.client import NsxClient import time client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password, debug=False) # Change the fabric UDP Port to different values (VXLAN standard value) update_udp_port_response = client_session.update( 'vdnConfigUDPUpdate', uri_parameters={'portNumber': '4789'}) client_session.view_response(update_udp_port_response) time.sleep(5) # Get the configured UDP Port for VXLAN in the Fabric upd_port_response = client_session.read('vdnConfigUDP') client_session.view_response(upd_port_response) time.sleep(5) # change the UDP Port back to the NSX Default update_udp_port_response = client_session.update( 'vdnConfigUDPUpdate', uri_parameters={'portNumber': '8472'}) client_session.view_response(update_udp_port_response)
from creds import * from nsxramlclient.client import NsxClient import pprint pp = pprint.PrettyPrinter(indent=1) nsxraml_file = 'nsxraml/nsxvapiv614.raml' client_session = NsxClient(nsxraml_file, nsx_manager, nsx_username, nsx_password) vdn_scopes = client_session.read('vdnScopes', 'read') pp.pprint(vdn_scopes['body'])
# IN THE SOFTWARE. __author__ = 'yfauser' from tests.config import * from nsxramlclient.client import NsxClient import time TRANSPORT_ZONE = 'TZ1' client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password, debug=True) # find the objectId of the Scope with the name of the Transport Zone vdn_scopes = client_session.read('vdnScopes', 'read')['body'] vdn_scope_dict_list = [scope_dict for scope_dict in vdn_scopes['vdnScopes'].items()] vdn_scope = [scope[1]['objectId'] for scope in vdn_scope_dict_list if scope[1]['name'] == TRANSPORT_ZONE][0] # get a template dict for the lswitch create lswitch_create_dict = client_session.extract_resource_body_example('logicalSwitches', 'create') client_session.view_body_dict(lswitch_create_dict) # fill the details for the new lswitch in the body dict lswitch_create_dict['virtualWireCreateSpec']['controlPlaneMode'] = 'UNICAST_MODE' lswitch_create_dict['virtualWireCreateSpec']['name'] = 'TestLogicalSwitch1' lswitch_create_dict['virtualWireCreateSpec']['tenantId'] = 'Tenant1' # create new lswitch new_ls = client_session.create('logicalSwitches', uri_parameters={'scopeId': vdn_scope}, request_body_dict=lswitch_create_dict)
from tests.config import * from nsxramlclient.client import NsxClient import time client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password, debug=True) macset_dict = client_session.extract_resource_body_example('macsetScopeCreate', 'create') macset_dict['macset']['name'] = 'test0' # CREATE macset on scope newmacset_return = client_session.create('macsetScopeCreate', uri_parameters={'scopeId': 'globalroot-0'}, request_body_dict=macset_dict) # READ macset by ID newmacset = dict(client_session.read('macset', uri_parameters={'macsetId': newmacset_return['objectId']})['body']) # UPDATE macset by ID newmacset['macset']['name'] = 'test1' newmacset['macset']['revision'] = '1' newmacset['macset']['value'] = '44:55:66:77:88:99' time.sleep(10) client_session.update('macset', uri_parameters={'macsetId': newmacset_return['objectId']}, request_body_dict=newmacset) # READ macsets on scope client_session.read('macsetScopeRead', uri_parameters={'scopeId': 'globalroot-0'}) time.sleep(10)
# Copyright © 2015 VMware, Inc. All Rights Reserved. # # 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. __author__ = 'yfauser' from tests.config import * from nsxramlclient.client import NsxClient client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password, debug=True) print client_session.read('nwfabricFeatures') print client_session.read('nwfabricStatus', query_parameters_dict={'resource': 'domain-c1632'})
# coding=utf-8 # # Copyright © 2015 VMware, Inc. All Rights Reserved. # # 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. __author__ = 'yfauser' from tests.config import * from nsxramlclient.client import NsxClient client_session = NsxClient(nsxraml_file, nsxmanager, nsx_username, nsx_password, debug=True) print client_session.read('nwfabricFeatures') print client_session.read('nwfabricStatus', query_parameters_dict={'resource': 'domain-c1632'})