def export_to_json(args):
    ### Get App definition and Export to JSON file ###
    logger.info("Getting Applications list")
    if args.verbose:
        print("Getting Applications list ...")
    application_api = swagger_client.ApplicationsApi()
    params = dict(size=100)
    apps = application_api.list_applications(**params)
    for i in apps.results:
        app = application_api.get_application(i.entity_id)
        if format(app.name) == args.appname:
            logger.info(
                "Extracting Tiers definition for requested application")
            tierList = application_api.list_application_tiers(id=app.entity_id)
            tierList = tierList.to_dict()
            if args.verbose:
                print(
                    "Extracting Tiers definition for requested application " +
                    args.appname + ": ")
                print(format(tierList))
            logger.info(
                "Writing Tiers definition for requested application to output JSON file"
            )
            # Define JSON export file full path
            folder = args.export_folder
            if not args.export_folder.endswith("/"):
                folder = folder + "/"
            json_file_export = folder + args.appname.replace(
                " ", "-") + suffix_file_export_vrni
            with open(json_file_export, 'w') as json_file:
                if args.verbose:
                    print(
                        "Writing Tiers definition for requested application to output JSON file ..."
                    )
                json.dump(tierList, json_file)
Beispiel #2
0
def main(args):
    application_api = swagger_client.ApplicationsApi()

    if args.application_backup_action == 'save':
        all_apps = []
        params = dict(size=10)
        while True:
            apps = application_api.list_applications(**params)
            for i in apps.results:
                app = application_api.get_application(i.entity_id)
                logger.info("Getting application '{}'".format(app.name))
                tiers = application_api.list_application_tiers(
                    id=app.entity_id)
                app_to_save = dict(name=app.name,
                                   no_of_tiers=len(tiers.results),
                                   tiers=tiers.to_dict())
                all_apps.append(app_to_save)
                time.sleep(
                    0.025
                )  # make sure we don't hit the vRNI throttle and start getting 429 errors
            if not apps.cursor:
                break
            params['cursor'] = apps.cursor

        logger.info("Storing Application/Tier info to {}".format(
            args.application_backup_yaml))
        with open(args.application_backup_yaml, 'w') as outfile:
            yaml.dump(all_apps, outfile, default_flow_style=False)

    if args.application_backup_action == 'restore':
        with open(args.application_backup_yaml, 'r') as outfile:
            all_apps = yaml.load(outfile)
        for app in all_apps:
            logger.info("Restoring app '{}'".format(app['name']))
            body = {"name": app['name'] + '-restored'}
            try:
                created_application = application_api.add_application(body)
                for tier in app['tiers']['results']:
                    tier.pop('application')
                    tier.pop('entity_id')
                    logger.info("\tRestoring tier '{}'".format(tier['name']))
                    application_api.add_tier(created_application.entity_id,
                                             tier)
                time.sleep(
                    0.025
                )  # make sure we don't hit the vRNI throttle and start getting 429 errors
            except ApiException as e:
                logger.error("{}".format(json.loads(e.body)))
def main(api_client, args):
    application_api = swagger_client.ApplicationsApi()
    if args.delete_apps:
        delete_application(application_api)
        delete_token(api_client)
        return

    reader = csv.DictReader(open("cmdb_ci_hardware.csv"))

    app_names = set()
    locations = collections.defaultdict(int)
    app_ips = collections.defaultdict(list)
    apps_tiers = collections.defaultdict(dict)
    total_rows = 0
    rows = 0
    for raw in reader:
        total_rows += 1
        raw['u_app_name'] = raw['u_app_name'].encode("utf-8")
        raw['u_app_name'] = raw['u_app_name'].lower()
        if raw['u_app_name']:
            add_tier_and_ip(apps_tiers, raw)
            app_names.add(raw['u_app_name'])
            rows += 1
            locations[raw['u_location']] += 1
            app_ips[raw['u_app_name']].append(raw["ip_address"])

    print("Total Apps discovered: {}".format(len(app_names)))
    print(app_names)
    print("Total rows: {}, Rows with apps: {}".format(total_rows, rows))
    print("Location wise distribution")
    print(json.dumps(locations, indent=4))
    for app, ips in app_ips.items():
        print("App Name: {:50} No of IPs: {}".format(app, len(ips)))
    print(json.dumps(app_ips, indent=4))

    app_no = 1
    for app_name, value in apps_tiers.items():
        vrni_app = create_application(application_api, app_name)
        logger.info("{} App Created: [{}]".format(app_no, app_name))
        create_tiers(app_name, application_api, vrni_app, apps_tiers[app_name])
        app_no += 1

    delete_token(api_client)
Beispiel #4
0
from __future__ import print_function
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint
# create an instance of the API class
api_instance = swagger_client.ApplicationsApi()
application_id = 'c80344c2-d7fc-41e1-adcc-dd33683a7f6b'  # str | The id of the application.

try:
    #
    api_instance.application_delete(application_id)
except ApiException as e:
    print("Exception when calling ApplicationsApi->application_delete: %s\n" %
          e)
def main():
    # Create application API client object
    application_api = swagger_client.ApplicationsApi()
    create_appication_and_tiers(application_api, "demo_app_1")