コード例 #1
0
def upsert(powerapps_rp, settings, client_secret, is_update, overwrite_settings):
    """
    Method for create/update operation
    """

    # Make sure the required files exist
    ensure_file_exists(
        file=settings.api_properties,
        file_type='API Properties')
    ensure_file_exists(
        file=settings.api_definition,
        file_type='API Definition')

    # Open the property file
    with open(settings.api_properties, 'r') as file:
        property_definition = json.load(file)

    # Get the property object
    properties = property_definition[_PROPERTIES]

    # Add secret
    token_property = properties.get(_CONNECTION_PARAMETERS, {}).get(_TOKEN, None)
    if token_property:
        oauth_settings = token_property.get(_OAUTH_SETTINGS, None)
        if oauth_settings and client_secret:
            oauth_settings[_CLIENT_SECRET] = client_secret
        elif oauth_settings and not client_secret and not is_update:
            raise CLIError('Please provide OAuth2 client secret using the --secret argument.')

    # Load swagger definition
    with open(settings.api_definition, 'r') as file:
        openapi_definition = json.load(file)

    # Append swagger
    properties[_OPEN_API_DEFINITION] = openapi_definition

    # Add backend service
    backend_service_url = _create_backendservice_url(openapi_definition)
    properties[_BACKEND_SERVICE] = {_SERVICE_URL: backend_service_url}

    # Append the environment id
    properties[_ENVIRONMENT] = {_NAME: settings.environment}

    # Add displayName only when creating a new connector
    if is_update is not True:
        properties[_DISPLAY_NAME] = openapi_definition[_INFO][_TITLE]

    # Add description
    properties[_DESCRIPTION] = openapi_definition[_INFO][_DESCRIPTION]

    # Validate Open API Definition
    powerapps_rp.validate_connector(
        payload=openapi_definition,
        enable_certification_rules=False)

    # Get the shared access signature
    response = powerapps_rp.generate_resource_storage(settings.environment)
    sas_url = response[_SHARED_ACCESS_SIGNATURE]

    # Upload the icon
    if settings.icon and os.path.exists(settings.icon):
        icon_uri = upload_icon(
            sas_url=sas_url,
            file_path=settings.icon)
        properties[_ICON_URI] = icon_uri

    # Update or create the connector
    if is_update is True:
        api_registration = powerapps_rp.update_connector(
            environment=settings.environment,
            connector_id=settings.connector_id,
            payload=property_definition)
        connector_id = settings.connector_id
    else:
        api_registration = powerapps_rp.create_connector(
            environment=settings.environment,
            payload=property_definition)
        connector_id = json.loads(api_registration)[_NAME]

        # Save the settings
        settings.connector_id = connector_id
        write_settings(settings, overwrite_settings)

    return connector_id
コード例 #2
0
def download(powerapps_rp, settings, destination, overwrite):
    """
    Download operation.
    """
    # Prepare folders
    directory = _prepare_directory(destination=destination,
                                   connector_id=settings.connector_id)

    # Check if files could be overwritten
    if not overwrite:
        overwrite = _ensure_overwrite(settings)

    api_registration = powerapps_rp.get_connector(
        environment=settings.environment, connector_id=settings.connector_id)

    if _PROPERTIES not in api_registration:
        raise CLIError(
            'Properties not present in the api registration information.')

    api_properties = api_registration[_PROPERTIES]

    # Save the settings
    write_settings(settings, overwrite)

    # Property whitelist
    property_keys_whitelist = [
        _CONNECTION_PARAMETERS, _ICON_BRAND_COLOR, _CAPABILITIES,
        _POLICY_TEMPLATE_INSTANCES
    ]

    # Remove the keys that aren't present in the property JSON
    properties_present = list(
        filter(lambda prop: prop in api_properties, property_keys_whitelist))

    # Only output the white listed properties that are present in the property JSON
    api_properties_selected = {_PROPERTIES: {}}
    api_properties_selected[_PROPERTIES] = {
        prop: api_properties[prop]
        for prop in properties_present
    }

    # Write the api properties
    api_prop = format_json(api_properties_selected)
    open(file=settings.api_properties, mode='w').write(api_prop)

    # Write the open api definition,
    # either from swagger URL when available or from swagger property.
    if _API_DEFINITIONS in api_properties and _ORIGINAL_SWAGGER_URL in api_properties[
            _API_DEFINITIONS]:
        original_swagger_url = api_properties[_API_DEFINITIONS][
            _ORIGINAL_SWAGGER_URL]
        response = requests.get(original_swagger_url, allow_redirects=True)
        response_string = response.content.decode('utf-8-sig')
        swagger = format_json(json.loads(response_string))
        open(file=settings.api_definition, mode='w').write(swagger)

    # Write the icon
    if _ICON_URI in api_properties:
        icon_url = api_properties[_ICON_URI]
        response = requests.get(icon_url, allow_redirects=True)
        open(file=settings.icon, mode='wb').write(response.content)

    return directory