BIG-IQ Device Test
Problem this snippet solves:
Add, list or delete a device within BIG-IQ
Code :
#!/usr/bin/python # import argparse import json import logging import sys import restapi; DEFAULT_BIGIQ_USER = "admin" DEFAULT_BIGIQ_PASSWORD = "admin" DEFAULT_BIGIQ_ACTION = "List" #This will be used to retrieve the ID of the device we want to delete DEFAULT_BIGIP_HOSTNAME = "your.test.lab" LOG = logging.getLogger(__name__) ## # Loads a JSON file. def load_json_file(name): with file(name) as f: json_data = json.load(f) if json_data: json_data = json.dumps(json_data) return json_data # Prints a line separator (for easy viewing). def print_separator(): print("-" * 80) ## # Validates the HTTP/REST result. def validate_result(result, verbose=False): # Any result beyond HTTP 400 is considered an error. if ((result == None) or (result[0] >= restapi.HTTP_400)): sys.exit(1) if verbose: for i in range (1, len(result)): LOG.info(result[i]) def retrieve_device_id(list_devices, hostname): for i in range (1, len(list_devices)): hostname_ = list_devices[i]['items'][0]['hostname'] if (hostname == hostname_): print(list_devices[i]['items'][0]['id']) return(list_devices[i]['items'][0]['id']) ## # Runs the BIG-IQ cloud API test suite. def run_bigiq_tests(bigiq_address, action=DEFAULT_BIGIQ_ACTION, hostname=DEFAULT_BIGIP_HOSTNAME, bigiq_user=DEFAULT_BIGIQ_USER, bigiq_password=DEFAULT_BIGIQ_PASSWORD, verbose=False): LOG.info("Running BIG-IQ Tenant tests...") # Initialize cloud API tester. LOG.info("BIG-IQ: Initializing cloud API shim...") cloud_api = restapi.RestApi(host=bigiq_address, user=bigiq_user, password=bigiq_password) LOG.info("BIG-IQ: Cloud API shim successfully initialized.") if (action == "Delete"): LOG.info("BIG-IQ: Delete Device...") list_devices = cloud_api.get_devices() device_id = retrieve_device_id(list_devices, hostname) result = cloud_api.delete_device(device_id) validate_result(result, verbose) LOG.info("BIG-IQ: Device successfully deleted.") if (action == "List"): # BIG-IQ: query devices list LOG.info("BIG-IQ: Querying list of devices...") result = cloud_api.get_devices() validate_result(result, True) LOG.info("BIG-IQ: Devices list successfully retrieved.") if (action == "Create"): # BIG-IQ: add device. LOG.info("BIG-IQ: Adding Device...") result = cloud_api.create_device(load_json_file("./bigiq_create_device.json")) validate_result(result, verbose) LOG.info("BIG-IQ: Device successfully created.") # Initializes logging. def init_logging(level=logging.INFO): log_format = '%(asctime)-15s: %(funcName)s(): %(message)s' logging.basicConfig(format=log_format, level=level) ## # Main entry point def main(): parser = argparse.ArgumentParser(description="Runs various REST-API functional tests") parser.add_argument('-v', '--verbose', action='store_true', default=False) parser.add_argument('--bigiq-address', required=True) parser.add_argument('--bigiq-user', default=DEFAULT_BIGIQ_USER) parser.add_argument('--bigiq-password', default=DEFAULT_BIGIQ_PASSWORD) parser.add_argument('--action', default=DEFAULT_BIGIQ_ACTION) parser.add_argument('--hostname', default=DEFAULT_BIGIP_HOSTNAME) args = parser.parse_args() log_level = logging.INFO if (args.verbose): log_level = logging.DEBUG init_logging(log_level) # BIG-IQ tests. run_bigiq_tests(bigiq_address=args.bigiq_address, action=args.action, hostname=args.hostname, bigiq_user=args.bigiq_user, bigiq_password=args.bigiq_password, verbose=args.verbose) LOG.info("OK. Done testing.") ## # Main entry point launcher. if __name__ == '__main__': main()
Published Mar 09, 2015
Version 1.0CodeCentral_194
Cirrus
Joined May 05, 2019
CodeCentral_194
Cirrus
Joined May 05, 2019
- Paolo_Arcagni_9Historic F5 AccountWhere can I find the restapi python package?
- Paolo_Arcagni_9Historic F5 AccountWhere can I find the restapi python package?