Esempio n. 1
0
def import_apim_api(client,
                    resource_group_name,
                    service_name,
                    path,
                    specification_format,
                    description=None,
                    subscription_key_header_name=None,
                    subscription_key_query_param_name=None,
                    api_id=None,
                    api_revision=None,
                    api_version=None,
                    api_version_set_id=None,
                    display_name=None,
                    service_url=None,
                    protocols=None,
                    specification_path=None,
                    specification_url=None,
                    api_type=None,
                    subscription_required=None,
                    soap_api_type=None,
                    wsdl_endpoint_name=None,
                    wsdl_service_name=None,
                    no_wait=False):
    """Import a new API"""
    cms = client.api

    # api_type: Type of API. Possible values include: 'http', 'soap'
    # possible parameter format is 'wadl-xml', 'wadl-link-json', 'swagger-json', 'swagger-link-json', 'wsdl', 'wsdl-link', 'openapi', 'openapi+json', 'openapi-link'
    # possible parameter specificationFormat is 'Wadl', 'Swagger', 'OpenApi', 'OpenApiJson', 'Wsdl'

    resource = ApiCreateOrUpdateParameter(path=path)

    if api_revision is not None and api_id is not None:
        api_id = api_id + ";rev=" + api_revision
    elif api_id is None:
        api_id = uuid.uuid4().hex

    if specification_path is not None and specification_url is None:
        api_file = open(specification_path, 'r')
        content_value = api_file.read()
        resource.value = content_value
    elif specification_url is not None and specification_path is None:
        resource.value = specification_url
    elif specification_path is not None and specification_url is not None:
        raise CLIError(
            "Can't specify specification-url and specification-path at the same time."
        )
    else:
        raise CLIError(
            "Please either specify specification-url or specification-path.")

    FORMAT_MAPPINGS = {
        ImportFormat.Wadl.value: {
            # specification_path is not none
            True: ContentFormat.wadl_xml.value,
            # specification_url is not none
            False: ContentFormat.wadl_link_json.value
        },
        ImportFormat.Swagger.value: {
            True: ContentFormat.swagger_json.value,
            False: ContentFormat.swagger_link_json.value
        },
        ImportFormat.OpenApi.value: {
            True: ContentFormat.openapi.value,
            False: ContentFormat.openapi_link.value
        },
        ImportFormat.OpenApiJson.value: {
            True: ContentFormat.openapijson.value,
            False: ContentFormat.openapi_link.value
        },
        ImportFormat.Wsdl.value: {
            True: ContentFormat.wsdl.value,
            False: ContentFormat.wsdl_link.value
        }
    }

    if specification_format in FORMAT_MAPPINGS:
        resource.format = FORMAT_MAPPINGS[specification_format][
            specification_path is not None]
    else:
        raise CLIError("Please provide valid value for specificationFormat: " +
                       specification_format)

    resource.protocols = protocols
    resource.service_url = service_url
    resource.display_name = display_name
    resource.description = description
    resource.subscription_required = subscription_required
    resource.subscription_key_parameter_names = get_subscription_key_parameter_names(
        subscription_key_query_param_name, subscription_key_header_name)
    resource.api_version = api_version
    resource.api_version_set_id = _get_vs_fullpath(api_version_set_id)

    if specification_format == ImportFormat.Wsdl.value:
        if api_type == ApiType.http.value:
            soap_api_type = SoapApiType.soap_to_rest.value
        else:
            soap_api_type = SoapApiType.soap_pass_through.value

        resource.soap_api_type = soap_api_type

        if wsdl_service_name is not None and wsdl_endpoint_name is not None:
            resource.wsdl_selector = ApiCreateOrUpdatePropertiesWsdlSelector(
                wsdl_service_name=wsdl_service_name,
                wsdl_endpoint_name=wsdl_endpoint_name)

    return sdk_no_wait(no_wait,
                       cms.create_or_update,
                       resource_group_name=resource_group_name,
                       service_name=service_name,
                       api_id=api_id,
                       parameters=resource)
Esempio n. 2
0
def apim_api_import(
        client, resource_group_name, service_name, path, specification_format, description=None,
        subscription_key_header_name=None, subscription_key_query_param_name=None, api_id=None, api_revision=None,
        api_version=None, api_version_set_id=None, display_name=None, service_url=None, protocols=None,
        specification_path=None, specification_url=None, api_type=None, subscription_required=None, soap_api_type=None,
        wsdl_endpoint_name=None, wsdl_service_name=None, no_wait=False):
    """Import a new API"""
    cms = client.api

    # api_type: Type of API. Possible values include: 'http', 'soap'
    # possible parameter format is 'wadl-xml', 'wadl-link-json', 'swagger-json', 'swagger-link-json',
    #   'wsdl', 'wsdl-link', 'openapi', 'openapi+json', 'openapi-link'
    # possible parameter specificationFormat is 'Wadl', 'Swagger', 'OpenApi', 'OpenApiJson', 'Wsdl'

    parameters = ApiCreateOrUpdateParameter(
        path=path,
        protocols=protocols,
        service_url=service_url,
        display_name=display_name,
        description=description,
        subscription_required=subscription_required,
        subscription_key_parameter_names=_get_subscription_key_parameter_names(
            subscription_key_query_param_name,
            subscription_key_header_name),
        api_version=api_version,
        api_version_set_id=_get_vs_fullpath(api_version_set_id)
    )

    if api_revision is not None and api_id is not None:
        api_id = api_id + ";rev=" + api_revision
    if api_revision is not None and api_id is None:
        api_id = uuid.uuid4().hex + ";rev=" + api_revision
    elif api_id is None:
        api_id = uuid.uuid4().hex

    if specification_path is not None and specification_url is None:
        api_file = open(specification_path, 'r')
        content_value = api_file.read()
        parameters.value = content_value
    elif specification_url is not None and specification_path is None:
        parameters.value = specification_url
    elif specification_path is not None and specification_url is not None:
        raise MutuallyExclusiveArgumentError(
            "Can't specify specification-url and specification-path at the same time.")
    else:
        raise RequiredArgumentMissingError(
            "Please either specify specification-url or specification-path.")

    FORMAT_MAPPINGS = {
        ImportFormat.Wadl.value: {
            # specification_path is not none
            True: ContentFormat.WADL_XML.value,
            # specification_url is not none
            False: ContentFormat.WADL_LINK_JSON.value
        },
        ImportFormat.Swagger.value: {
            True: ContentFormat.SWAGGER_JSON.value,
            False: ContentFormat.SWAGGER_LINK_JSON.value
        },
        ImportFormat.OpenApi.value: {
            True: ContentFormat.OPENAPI.value,
            False: ContentFormat.OPENAPI_LINK.value
        },
        ImportFormat.OpenApiJson.value: {
            True: ContentFormat.OPENAPI_JSON.value,
            False: ContentFormat.OPENAPI_JSON_LINK.value
        },
        ImportFormat.Wsdl.value: {
            True: ContentFormat.WSDL.value,
            False: ContentFormat.WSDL_LINK.value
        }
    }

    if specification_format in FORMAT_MAPPINGS:
        parameters.format = FORMAT_MAPPINGS[specification_format][specification_path is not None]
    else:
        raise InvalidArgumentValueError(
            "SpecificationFormat: " + specification_format + "is not supported.")

    if specification_format == ImportFormat.Wsdl.value:
        if api_type == ApiType.http.value:
            soap_api_type = SoapApiType.soap_to_rest.value
        else:
            soap_api_type = SoapApiType.soap_pass_through.value

        parameters.soap_api_type = soap_api_type

        if wsdl_service_name is not None and wsdl_endpoint_name is not None:
            parameters.wsdl_selector = ApiCreateOrUpdatePropertiesWsdlSelector(
                wsdl_service_name=wsdl_service_name,
                wsdl_endpoint_name=wsdl_endpoint_name
            )

    return sdk_no_wait(
        no_wait,
        cms.begin_create_or_update,
        resource_group_name=resource_group_name,
        service_name=service_name,
        api_id=api_id,
        parameters=parameters)