コード例 #1
0
def setupTenantKeys():
    """
    This function will get and set API creation and subscribe tokens for each tenant
    :return: True if successful. False otherwise
    """
    global tenant_config_details

    # set id, secret, tokens for each tenant
    for tenant in tenant_config_details:
        tenant_data = tenant_config_details.get(tenant)

        # get client id and client secret
        client_id, client_secret = request_methods.getIDSecret(gateway_protocol, gateway_host, gateway_servelet_port_https, token_registration_endpoint, tenant_data['admin_username'], tenant_data['admin_b64'])
        
        if client_id == None or client_secret == None:
            logger.error("Fetching client id, client secret unsuccessful for tenant: {}. Aborting task...".format(tenant))
            return False
        logger.info("Successfully fetched client id, client secret for tenant: {}".format(tenant))

        concat_value = client_id + ":" + client_secret
        b64_encoded = base64.b64encode(concat_value.encode('utf-8')).decode('utf-8')
        tenant_config_details[tenant]['b64_keySecret'] = b64_encoded

        # get API creation access token
        access_token_create = request_methods.getAccessToken(gateway_protocol, gateway_host, nio_pt_transport_port, token_endpoint, b64_encoded, 'apim:api_create apim:api_view', tenant_data['admin_username'], tenant_data['admin_password'])[0]

        if access_token_create == None:
            logger.error("Getting API creation access token failed for tenant: {}!. Aborting task...".format(tenant))
            return False
        logger.info("Successfully received API creation access token for tenant: {}".format(tenant))
        tenant_config_details[tenant]['create_token'] = access_token_create

        # get API subscribe access token
        access_token_subs = request_methods.getAccessToken(gateway_protocol, gateway_host, nio_pt_transport_port, token_endpoint, b64_encoded, 'apim:subscribe apim:api_view', tenant_data['admin_username'], tenant_data['admin_password'])[0]

        if access_token_subs == None:
            logger.error("Getting subscription access token failed for tenant: {}!. Aborting task...".format(tenant))
            return False
        logger.info("Successfully received subscribe access token for tenant: {}".format(tenant))
        tenant_config_details[tenant]['subscribe_token'] = access_token_subs

    return True
コード例 #2
0
def createApplicationsAndSubscribe():
    """
    This function will create all applications and subscribe to APIs
    :return: None
    """
    created_count = 0
    app_api_sub = ""

    # get id and secret
    client_id, client_secret = request_methods.getIDSecret(
        gateway_protocol, gateway_host, gateway_servelet_port_https,
        token_registration_endpoint, admin_username, admin_b64)

    if client_id == None or client_secret == None:
        logger.error(
            "Fetching client id, client secret unsuccessful!. Aborting task..."
        )
        return
    logger.info("Successfully fetched client id, client secret")

    concat_value = client_id + ":" + client_secret
    b64_encoded = base64.b64encode(
        concat_value.encode('utf-8')).decode('utf-8')

    # get subscriber access token
    access_token_subs = request_methods.getAccessToken(
        gateway_protocol, gateway_host, nio_pt_transport_port, token_endpoint,
        b64_encoded, 'apim:subscribe apim:api_view', admin_username,
        admin_password)[0]

    if access_token_subs == None:
        logger.error(
            "Getting subscription access token failed!. Aborting task...")
        return
    logger.info("Successfully received subscribe access token")

    # read all app data from apim.yaml file
    with open(abs_path + '/../../../../config/apim.yaml', 'r') as config_file:
        apim_config = yaml.load(config_file, Loader=yaml.FullLoader)

    apps = apim_config['apps']
    logger.info("Application data read from apim.yaml")

    # clear application key, secret file
    with open(abs_path + '/../../data/scenario/api_invoke_key_secret.csv',
              'w') as file:
        file.write('')

    # clear previous stored app id's
    with open(abs_path + '/../../data/runtime_data/app_ids.csv', 'w') as f:
        f.write('')

    # iterate the procedure for each application in the config file
    for app in apps:
        app_name = app['name']
        api_subs = app['api_subscriptions'].split(',')
        app_api_sub += app_name + ": "

        # create new Application
        app_id = request_methods.createApplication(
            gateway_protocol, gateway_host, gateway_servelet_port_https,
            store_application_endpoint, access_token_subs, app_name,
            app['description'], app_throttling_tier)

        if not app_id:
            logger.error(
                "App creation Failed!. App name: {}. Retrying...".format(
                    app_name))
            app_id = request_methods.createApplication(
                gateway_protocol, gateway_host, gateway_servelet_port_https,
                store_application_endpoint, access_token_subs, app_name,
                app['description'], app_throttling_tier)
            if not app_id:
                logger.error(
                    "App creation Failed!. App name: {}".format(app_name))
            else:
                logger.info(
                    "Application created Successfully. App name: {}, App ID: {}"
                    .format(app_name, app_id))
                created_count += 1
        else:
            logger.info(
                "Application created Successfully. App name: {}, App ID: {}".
                format(app_name, app_id))
            created_count += 1

        #subscribe for each API
        sub_count = 0
        for api in api_subs:
            ret_val = request_methods.subscribe(gateway_protocol, gateway_host,
                                                gateway_servelet_port_https,
                                                store_subs_endpoint,
                                                access_token_subs,
                                                subscription_tier,
                                                api_ids.get(api), app_id)

            if not ret_val:
                logger.error(
                    "API subscription Failed!. App name: {}, API name: {}. Retrying..."
                    .format(app_name, api))
                ret_val = request_methods.subscribe(
                    gateway_protocol, gateway_host,
                    gateway_servelet_port_https,
                    store_subs_endpoint, access_token_subs, subscription_tier,
                    api_ids.get(api), app_id)
                if not ret_val:
                    logger.error(
                        "API subscription Failed!. App name: {}, API name: {}".
                        format(app_name, api))
                else:
                    logger.info(
                        "API subscription successful. App name: {}, API name: {}"
                        .format(app_name, api))
                    sub_count += 1
            else:
                logger.info(
                    "API subscription successful. App name: {}, API name: {}".
                    format(app_name, api))
                sub_count += 1
        app_api_sub += str(sub_count) + " "

        # generate production key
        keygen_endpoint = str(store_application_endpoint
                              ) + '/generate-keys?applicationId=' + str(app_id)
        key, secret = request_methods.genProductionKey(
            gateway_protocol, gateway_host, gateway_servelet_port_https,
            keygen_endpoint, access_token_subs, token_validity_period)

        if key == None:
            logger.error(
                "App key generation Failed!. App name: {}. Retrying...".format(
                    app_name))
            key, secret = request_methods.genProductionKey(
                gateway_protocol, gateway_host, gateway_servelet_port_https,
                keygen_endpoint, access_token_subs, token_validity_period)
            if key == None:
                logger.error("App key generation Failed!. App name: {}".format(
                    app_name))
                app_api_sub += "(key: false), "
            else:
                logger.info(
                    "App key generation successful. App name: {}".format(
                        app_name))
                app_api_sub += "(key: true), "
        else:
            logger.info(
                "App key generation successful. App name: {}".format(app_name))
            app_api_sub += "(key: true), "

        # write key secret to api_invoke_key_secret.csv file
        concat_value = key + ":" + secret
        b64_encoded = base64.b64encode(
            concat_value.encode('utf-8')).decode('utf-8')

        with open(abs_path + '/../../data/scenario/api_invoke_key_secret.csv',
                  'a+') as file:
            file.write(app_name + ',' + b64_encoded + '\n')

    out_txt = "Application creation process completed. Total {} Apps created. Following subscriptions happen:\n{}".format(
        str(created_count), app_api_sub)
    logger.info(out_txt)
コード例 #3
0
def createAndPublishAPIs():
    """
    This function will create and publish all APIs
    :return: None
    """
    global api_ids, swagger_definitions
    created_count = 0
    published_count = 0

    # get id and secret
    client_id, client_secret = request_methods.getIDSecret(
        gateway_protocol, gateway_host, gateway_servelet_port_https,
        token_registration_endpoint, admin_username, admin_b64)

    if client_id == None or client_secret == None:
        logger.error(
            "Fetching client id, client secret unsuccessful!. Aborting task..."
        )
        return
    logger.info("Successfully fetched client id, client secret")

    concat_value = client_id + ":" + client_secret
    b64_encoded = base64.b64encode(
        concat_value.encode('utf-8')).decode('utf-8')

    # get access token to create APIs
    access_token_create = request_methods.getAccessToken(
        gateway_protocol, gateway_host, nio_pt_transport_port, token_endpoint,
        b64_encoded, 'apim:api_create apim:api_view', admin_username,
        admin_password)[0]

    if access_token_create == None:
        logger.error(
            "Getting API creation access token failed!. Aborting task...")
        return
    logger.info("Successfully received API creation access token")

    # get access token to publish
    access_token_publish = request_methods.getAccessToken(
        gateway_protocol, gateway_host, nio_pt_transport_port, token_endpoint,
        b64_encoded, 'apim:api_publish', admin_username, admin_password)[0]
    if access_token_publish == None:
        logger.error(
            "Getting API publishing access token failed!. Aborting task...")
        return
    logger.info("Successfully received API publishing access token")

    # read all API data from api_details.yaml file
    with open(abs_path + '/../../../../config/api_details.yaml',
              'r') as config_file:
        api_config = yaml.load(config_file, Loader=yaml.FullLoader)

    apis = api_config['apis']
    logger.info("API data read from api_details.yaml")

    # clear previous stored api id's
    with open(abs_path + '/../../data/runtime_data/api_ids.csv', 'w') as f:
        f.write('')

    # iterate the procedure for each API in the config file
    for api in apis:
        api_name = api['name']

        # create new API
        api_id = request_methods.createAPI(
            gateway_protocol, gateway_host, gateway_servelet_port_https,
            publisher_api_endpoint, access_token_create, api_name,
            api['description'], api['context'], api['version'],
            swagger_definitions.get(api_name.lower()), api['tags'],
            api_throttling_tier, api_visibility, production_endpoint,
            sandbox_endpoint, admin_username)

        if not api_id:
            logger.error(
                "API creation Failed!. API name: {}. Retrying...".format(
                    api_name))
            api_id = request_methods.createAPI(
                gateway_protocol, gateway_host, gateway_servelet_port_https,
                publisher_api_endpoint, access_token_create, api_name,
                api['description'], api['context'], api['version'],
                swagger_definitions.get(api_name.lower()), api['tags'],
                api_throttling_tier, api_visibility, production_endpoint,
                sandbox_endpoint, admin_username)
            if not api_id:
                logger.error(
                    "API creation Failed!. API name: {}".format(api_name))
            else:
                logger.info(
                    "API created Successfully. API name: {}, API ID: {}".
                    format(api_name, api_id))
                api_ids[api_name] = api_id
                created_count += 1
        else:
            logger.info(
                "API created Successfully. API name: {}, API ID: {}".format(
                    api_name, api_id))
            api_ids[api_name] = api_id
            created_count += 1

        # publish the API
        ret_val = request_methods.publishAPI(gateway_protocol, gateway_host,
                                             gateway_servelet_port_https,
                                             publisher_api_endpoint,
                                             access_token_publish, api_id)

        if not ret_val:
            logger.error(
                "API publishing Failed!. API name: {}. Retrying...".format(
                    api_name))
            ret_val = request_methods.publishAPI(gateway_protocol,
                                                 gateway_host,
                                                 gateway_servelet_port_https,
                                                 publisher_api_endpoint,
                                                 access_token_publish, api_id)
            if not ret_val:
                logger.error(
                    "API publishing Failed!. API name: {}.".format(api_name))
            else:
                logger.info(
                    "API published Successfully. API name: {}, API ID: {}".
                    format(api_name, api_id))
                published_count += 1
        else:
            logger.info(
                "API published Successfully. API name: {}, API ID: {}".format(
                    api_name, api_id))
            published_count += 1

    out_txt = "API creation process completed. Total {} APIs created. Total {} APIs published".format(
        str(created_count), str(published_count))
    logger.info(out_txt)
コード例 #4
0
def removeApplications():
    """
    This function will remove all created applications from API Manager
    :return: None
    """

    remove_count = 0

    # get id and secret
    client_id, client_secret = request_methods.getIDSecret(
        gateway_protocol, gateway_host, gateway_servelet_port_https,
        token_registration_endpoint, admin_username, admin_b64)

    if client_id == None or client_secret == None:
        logger.error(
            "Fetching client id, client secret unsuccessful!. Aborting task..."
        )
        return
    logger.info("Successfully fetched client id, client secret")

    concat_value = client_id + ":" + client_secret
    b64_encoded = base64.b64encode(
        concat_value.encode('utf-8')).decode('utf-8')

    # get access token
    access_token = request_methods.getAccessToken(
        gateway_protocol, gateway_host, nio_pt_transport_port, token_endpoint,
        b64_encoded, 'apim:subscribe apim:api_view', admin_username,
        admin_password)[0]

    if access_token == None:
        logger.error("Getting access token failed!. Aborting task...")
        return
    logger.info("Successfully received access token")

    # iterate for each application
    with open(abs_path + '/../../data/runtime_data/app_ids.csv', 'r') as f:
        reader = csv.reader(f)

        for app_id in reader:
            deleted = request_methods.deleteAppAPI(
                gateway_protocol, gateway_host, gateway_servelet_port_https,
                store_application_endpoint, access_token, app_id[0])

            if not deleted:
                logger.error(
                    "Application removing Failed!. App id: {}. Retrying...".
                    format(app_id[0]))
                deleted = request_methods.deleteAppAPI(
                    gateway_protocol, gateway_host,
                    gateway_servelet_port_https, store_application_endpoint,
                    access_token, app_id[0])
                if not deleted:
                    logger.error(
                        "Application removing Failed!. App id: {}".format(
                            app_id[0]))
                else:
                    logger.info(
                        "Application removed successfully!. App id: {}".format(
                            app_id[0]))
                    remove_count += 1
            else:
                logger.info(
                    "Application removed successfully!. App id: {}".format(
                        app_id[0]))
                remove_count += 1

        logger.info(
            "Application deletion process completed. Total {} applications removed"
            .format(str(remove_count)))