BIG-IQ Tenant Catalog Test

Problem this snippet solves:

Add, list or delete a Tenant Catalog within BIG-IQ

Code :

#!/usr/bin/python
# 

import argparse
import json
import logging
import sys

import restapi;
import test_helper as helper

DEFAULT_BIGIQ_USER = "admin"
DEFAULT_BIGIQ_PASSWORD = "admin"
DEFAULT_BIGIQ_ACTION = "List"
DEFAULT_BIGIQ_CATALOG_SERVICE_NAME = "my-service-template"
DEFAULT_BIGIQ_IAPP_NAME = "f5.http"
DEFAULT_BIGIQ_CONNECTOR_NAME = "My-Tenant-Connector"

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])

#Retrieve connector link to include it into the tenant to be created
def retrieve_connector_link(list_connectors, connector_name):
    for i in range (1, len(list_connectors)):
        connector_name_ = list_connectors[i]['items'][0]['name']
        if (connector_name == connector_name_):
                return(list_connectors[i]['items'][0]['selfLink'])

##
# Runs the BIG-IQ cloud API test suite.
def run_bigiq_tests(bigiq_address,
                    action=DEFAULT_BIGIQ_ACTION,
    catalog_service_name=DEFAULT_BIGIQ_CATALOG_SERVICE_NAME,
    iapp_name=DEFAULT_BIGIQ_IAPP_NAME,
    connector_name=DEFAULT_BIGIQ_CONNECTOR_NAME,
    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"):
    # Preliminary clean-up, remove possible stale state.
        result = cloud_api.delete_service_template(catalog_service_name)    
validate_result(result, verbose)
        LOG.info("BIG-IQ: Catalog Service successfully deleted.")

    if (action == "List"):
# BIG-IQ: get provider service template.
    LOG.info("BIG-IQ: Retrieving provider service template...")
    result = cloud_api.get_service_templates()
    validate_result(result, True)
LOG.info("BIG-IQ: Provider service templates successfully retrieved.")

    if (action == "Create"):
# BIG-IQ: create provider service template.
    LOG.info("BIG-IQ: Creating provider service template...")
    list_connectors = cloud_api.get_local_connectors()
        local_connector_link = retrieve_connector_link(list_connectors, connector_name)
print (local_connector_link)
service_template_example =  cloud_api.get_service_template_sample(iapp_name)
service_template = service_template_example[1]
service_template['templateName'] = catalog_service_name
service_template_properties_ = helper.load_json_file_as_object("./bigiq_service_template_cloud_reference_property.json")
service_template_properties_['provider'] = local_connector_link
service_template_properties = [service_template_properties_]
service_template_json = helper.modify_json(json.dumps(service_template),
                                               'properties',
                                               service_template_properties)
  print (service_template_json)
result = cloud_api.create_service_template(service_template_json)
validate_result(result, verbose)
    LOG.info("BIG-IQ: Provider service template 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('--catalog-service-name', default=DEFAULT_BIGIQ_CATALOG_SERVICE_NAME)
    parser.add_argument('--iapp-name', default=DEFAULT_BIGIQ_IAPP_NAME)
    parser.add_argument('--connector-name', default=DEFAULT_BIGIQ_CONNECTOR_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,
    catalog_service_name=args.catalog_service_name,
    connector_name=args.connector_name,
    iapp_name=args.iapp_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()
Published Mar 09, 2015
Version 1.0
No CommentsBe the first to comment