def main(): session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % ("<NSX MANAGER IP>", 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context("<USERNAME>", "<PASSWORD>") connector.set_security_context(security_context) urllib3.disable_warnings() tz_list = [] tz_svc = TransportZones(stub_config) tz_list = tz_svc.list() r = tz_list.results start_row = 1 for i in r: tz = i.convert_to(TransportZone) sheet1.write(start_row, 0, tz.display_name) sheet1.write(start_row, 1, tz.description) sheet1.write(start_row, 2, tz.id) sheet1.write(start_row, 3, tz.resource_type) sheet1.write(start_row, 4, tz.host_switch_id) sheet1.write(start_row, 5, tz.host_switch_mode) sheet1.write(start_row, 6, tz.host_switch_name) sheet1.write(start_row, 7, tz.is_default) sheet1.write(start_row, 8, tz.nested_nsx) sheet1.write(start_row, 9, tz.transport_type) sheet1.write(start_row, 10,tz.uplink_teaming_policy_names) start_row += 1 ls_wkbk.save('Transport Zones.xls')
def main(): # Read the command-line arguments. The args object will contain # 4 properties, args.nsx_host, args.tcp_port, args.user, and # args.password. args = getargs.getargs() # Create a session using the requests library. For more information on # requests, see http://docs.python-requests.org/en/master/ session = requests.session() # If your NSX API server is using its default self-signed certificate, # you will need the following line, otherwise the python ssl layer # will reject the server certificate. THIS IS UNSAFE and you should # normally verify the server certificate. session.verify = False # Set up the API connector and security context nsx_url = 'https://%s:%s' % (args.nsx_host, args.tcp_port) connector = connect.get_requests_connector( session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( args.user, args.password) connector.set_security_context(security_context) # Now any API calls we make should authenticate to NSX using # HTTP Basic Authentication. Let's get a list of all Transport Zones. transportzones_svc = TransportZones(stub_config) tzs = transportzones_svc.list() # Create a pretty printer to make the output look nice. pp = PrettyPrinter() pp.pprint(tzs)
def SheetSegments(auth_list,WORKBOOK,TN_WS,NSX_Config = {}): NSX_Config['Segments'] = [] Dict_LS = {} # NSX Connection SessionNSX = ConnectNSX(auth_list) stub_config = StubConfigurationFactory.new_std_configuration(SessionNSX[1]) ls_svc = LogicalSwitches(stub_config) ls_list = ls_svc.list() nb = len(ls_list.results) tz_svc = TransportZones(stub_config) tz_list = tz_svc.list() XLS_Lines = [] TN_HEADER_ROW = ('Segments', 'VNI', 'VLAN', 'Transport Zone Name', 'Transport Zone Type', 'Replication Mode', 'Admin State') if ls_list.result_count > 0: while True: for segment in ls_list.results: TZ_NAME = "" TZ_Type = "" for tz in tz_list.results: if segment.transport_zone_id == tz.id: TZ_NAME = tz.display_name TZ_Type = tz.transport_type Dict_LS['segment_name'] = segment.display_name Dict_LS['vni'] = segment.vni Dict_LS['vlan'] = segment.vlan Dict_LS['tz_name'] = TZ_NAME Dict_LS['tz_type'] = TZ_Type Dict_LS['replication_mode'] = segment.replication_mode Dict_LS['status'] = segment.admin_state NSX_Config['Segments'].append(Dict_LS) XLS_Lines.append([segment.display_name, segment.vni, segment.vlan, TZ_NAME, TZ_Type, segment.replication_mode,segment.admin_state]) if ls_list.cursor is None: break else: print(" --> more than " + str(nb) + " results for " + style.RED + "Segments" + style.NORMAL + " - please wait") ls_list = LogicalSwitches(stub_config).list(cursor =ls_list.cursor ) nb = len(ls_list.results) + nb else: XLS_Lines.append(["no Segments", "", "", "", "", "",""]) if GetOutputFormat() == 'CSV': CSV = WORKBOOK FillSheetCSV(CSV,TN_HEADER_ROW,XLS_Lines) elif GetOutputFormat() == 'JSON': JSON = WORKBOOK FillSheetJSON(JSON, NSX_Config) elif GetOutputFormat() == 'YAML': YAML = WORKBOOK FillSheetYAML(YAML, NSX_Config) else: FillSheet(WORKBOOK,TN_WS.title,TN_HEADER_ROW,XLS_Lines,"0072BA") ConditionnalFormat(TN_WS, 'G2:G' + str(len(XLS_Lines) + 1), 'UP')
def listTransportZones(module, stub_config): tz_list = [] try: tz_svc = TransportZones(stub_config) tz_list = tz_svc.list() except Error as ex: api_error = ex.date.convert_to(ApiError) module.fail_json(msg='API Error listing Transport Zones: %s'%(api_error.error_message)) return tz_list
def getTransportZoneEndPoint(module, stub_config): tz_endpoints = [] transportzones_svc = TransportZones(stub_config) try: tzs = transportzones_svc.list() except Error as ex: api_error = ex.data.convert_to(ApiError) module.exit_json(changed=False, message="Error listing Transport Zones: "%(api_error)) for tz_name in module.params['transport_zone_endpoints']: for vs in tzs.results: fn = vs.convert_to(TransportZone) if fn.display_name == tz_name: ep=TransportZoneEndPoint(transport_zone_id=fn.id) tz_endpoints.append(ep) return tz_endpoints
def main(): session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % ("<NSX-T MANAGER FQDN>", 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( "<USERNAME>", "<PASSWORD>") connector.set_security_context(security_context) urllib3.disable_warnings() ls_list = [] ls_svc = LogicalSwitches(stub_config) ls_list = ls_svc.list() # print(ls_list) tz_list = [] tz_svc = TransportZones(stub_config) tz_list = tz_svc.list() start_row = 1 for vs in ls_list.results: ls = vs.convert_to(LogicalSwitch) sheet1.write(start_row, 0, ls.display_name) sheet1.write(start_row, 1, ls.vni, style_alignleft) sheet1.write(start_row, 2, ls.vlan, style_alignleft) sheet1.write(start_row, 4, ls.transport_zone_id) sheet1.write(start_row, 6, ls.replication_mode) sheet1.write(start_row, 7, ls.admin_state) # print(ls.resource_type) newlist = [] for i in range(len(ls.tags)): newlist.append(ls.tags[i].tag) sheet1.write(start_row, 8, (str(newlist[0]))) if len(newlist) > 1: sheet1.write(start_row, 9, (str(newlist.pop()))) x = len(tz_list.results) for i in range(0, x): if ls.transport_zone_id == tz_list.results[i].id: sheet1.write(start_row, 3, tz_list.results[i].display_name) sheet1.write(start_row, 5, tz_list.results[i].transport_type) start_row += 1 ls_wkbk.save('Logical Switches.xls')
def main(): # Read the command-line arguments. arg_parser = argparse.ArgumentParser() arg_parser.add_argument('-n', '--nsx_host', type=str, required=True, help='NSX host to connect to') arg_parser.add_argument('-t', '--tcp_port', type=int, default=443, help='TCP port for NSX server') arg_parser.add_argument('-c', '--client_certificate', type=str, required=True, help='Name of PEM file containing client ' 'certificate and private key') args = arg_parser.parse_args() # Create a session using the requests library. For more information on # requests, see http://docs.python-requests.org/en/master/ session = requests.session() # If your NSX API server is using its default self-signed certificate, # you will need the following line, otherwise the python ssl layer # will reject the server certificate. THIS IS UNSAFE and you should # normally verify the server certificate. session.verify = False # Configure the requests library to supply a client certificate session.cert = args.client_certificate # Set up the API connector nsx_url = 'https://%s:%s' % (args.nsx_host, args.tcp_port) connector = connect.get_requests_connector( session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) # Now any API calls we make should authenticate to NSX using # the client certificate. Let's get a list of all Transport Zones. transportzones_svc = TransportZones(stub_config) tzs = transportzones_svc.list() # Create a pretty printer to make the output look nice. pp = PrettyPrinter() pp.pprint(tzs)
def main(): args = getargs.getargs() print("----debug-----1") nsx_host="192.168.55.37" user = "******" password = "******" tcp_port = 443 # stub_config = auth.get_session_auth_stub_config(args.user, args.password, # args.nsx_host, # args.tcp_port) stub_config = auth.get_session_auth_stub_config(user, password, nsx_host, tcp_port) # Create a pretty printer to make the output look nice. pp = PrettyPrinter() # Create the service we'll need. transportzones_svc = TransportZones(stub_config) # First, list all transport zones. If your NSX installation has # just been installed, this should return an empty list. tzs = transportzones_svc.list() print("Initial list of transport zones - %d zones" % tzs.result_count) pp.pprint(tzs) # Create a transport zone. new_tz = TransportZone( transport_type=TransportZone.TRANSPORT_TYPE_OVERLAY, display_name="My transport zone", description="Transport zone for basic create/read/update/delete demo", host_switch_name="hostswitch1" ) result_tz = transportzones_svc.create(new_tz) print("Transport zone created. id is %s" % result_tz.id) # Save the id, which uniquely identifies the resource we created. tz_id = result_tz.id # Read that transport zone. read_tz = transportzones_svc.get(tz_id) print("Re-read the transport zone") pp.pprint(read_tz) # List all transport zones again. The newly created transport # zone will be in the list. tzs = transportzones_svc.list() print("Updated list of transport zones - %d zones" % tzs.result_count) pp.pprint(tzs) print("You can now examine the list of transport zones in the") print("NSX manager if you wish. Press enter to continue.") sys.stdin.readline() # Update the transport zone. read_tz.description = "Updated description for transport zone" updated_tz = transportzones_svc.update(tz_id, read_tz) print("After updating description. Note that the revision property is " "automatically updated.") pp.pprint(updated_tz) # Update the transport zone again. # # Note that NSX insists that clients always operate on up-to-date # data. To enforce this, every resource in NSX has a "revision" # property that is automatically maintained by NSX and is # incremented each time the resource is updated. If a client # submits an update operation, and the revision property in the # payload provided by the client does not match the revision # stored on the server, another update must have happened since # the client last read the resource, and the client's copy is # therefore stale. In this case, the server will return a 412 # Precondition Failed error. This is intended to prevent clients # from clobbering each other's updates. To recover from this # error, the client must re-read the resource, apply any desired # updates, and perform another update operation. updated_tz.description = "Updated description again for transport zone" updated_tz = transportzones_svc.update(tz_id, updated_tz) print("After updating description again.") pp.pprint(updated_tz) # Delete the transport zone. transportzones_svc.delete(tz_id) print("After deleting transport zone") # Now if we try to read the transport zone, we should get a # 404 Not Found error. This example also shows how you can # check for and handle specific errors from the NSX API. try: read_tz = transportzones_svc.get(tz_id) except NotFound: print("Transport zone is gone, as expected")