cancel
Showing results for 
Search instead for 
Did you mean: 
Login & Join the DevCentral Connects Group to watch the Recorded LiveStream (May 12) on Basic iControl Security - show notes included.

Problem this snippet solves:

Add, list or delete an application service 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"
DEFAULT_BIGIQ_TENANT_NAME = "My-Tenant"
DEFAULT_BIGIQ_APPLICATION_SERVICE_NAME = "my-tenant-service" 
DEFAULT_BIGIQ_CATALOG_SERVICE_NAME = "my-service-template"
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:
        LOG.info(result[1])

##
# Runs the BIG-IQ cloud API test suite.
def run_bigiq_tests(bigiq_address,
                    action=DEFAULT_BIGIQ_ACTION,
    tenant_name=DEFAULT_BIGIQ_TENANT_NAME,
    catalog_service_name=DEFAULT_BIGIQ_CATALOG_SERVICE_NAME, 
    application_service_name=DEFAULT_BIGIQ_APPLICATION_SERVICE_NAME,
    bigiq_user=DEFAULT_BIGIQ_USER,
                    bigiq_password=DEFAULT_BIGIQ_PASSWORD,
    demo_mode=False,
                    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"):
    result = cloud_api.delete_tenant_service(tenant_name, application_service_name)
validate_result(result,verbose)
LOG.info("BIG-IQ: Application Service successfully deleted.")

    if (action == "List"):
# BIG-IQ: get tenant service.
        LOG.info("BIG-IQ: Retrieving tenant service...")
        result = cloud_api.get_tenant_service(tenant_name, application_service_name)
        validate_result(result, True)
        LOG.info("BIG-IQ: Tenant service successfully retrieved.")
 
    if (action == "Create"):
    # BIG-IQ; create tenant service.
    LOG.info("BIG-IQ: Creating tenant service...")
    result = cloud_api.create_tenant_service(tenant_name, load_json_file("./bigiq_create_tenant_service.json"))
    validate_result(result, verbose)
    LOG.info("BIG-IQ: Tenant service 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('--tenant-name', default=DEFAULT_BIGIQ_TENANT_NAME)
    parser.add_argument('--catalog-service-name', default=DEFAULT_BIGIQ_CATALOG_SERVICE_NAME)
    parser.add_argument('--application-service-name', default=DEFAULT_BIGIQ_APPLICATION_SERVICE_NAME)
    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)
 
    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,
    tenant_name=args.tenant_name,
    catalog_service_name=args.catalog_service_name,
    application_service_name=args.application_service_name,
    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()
Version history
Last update:
‎09-Mar-2015 15:14
Updated by:
Contributors