Esempio n. 1
0
def get_resource(blueprint_id, deployment_id, tenant_name, resource_path):
    """
    Get resource from the manager file server with path relative to
    the deployment or blueprint denoted by ``deployment_id`` or
    ``blueprint_id``.

    An attempt will first be made for getting the resource from the deployment
    folder. If not found, an attempt will be made for getting the resource
    from the blueprint folder.

    :param blueprint_id: the blueprint id of the blueprint to download
                         the resource from
    :param deployment_id: the deployment id of the deployment to download the
                          resource from
    :param tenant_name: tenant name
    :param resource_path: path to resource relative to blueprint folder
    :returns: resource content
    """
    def _get_resource(base_url):
        try:
            return get_resource_from_manager(resource_path, base_url=base_url)
        except HttpException as e:
            if e.code != 404:
                raise
            return None

    resource = None
    if deployment_id is not None:
        relative_deployment_path = os.path.join(
            constants.FILE_SERVER_RESOURCES_FOLDER,
            constants.FILE_SERVER_DEPLOYMENTS_FOLDER, tenant_name,
            deployment_id)
        deployment_base_url = urljoin(utils.get_manager_file_server_url(),
                                      relative_deployment_path).replace(
                                          '\\', '/')
        resource = _get_resource(deployment_base_url)

    if resource is None:
        client = get_rest_client()
        blueprint = client.blueprints.get(blueprint_id)

        if blueprint['visibility'] == VisibilityState.GLOBAL:
            tenant_name = blueprint['tenant_name']

        relative_blueprint_path = os.path.join(
            constants.FILE_SERVER_RESOURCES_FOLDER,
            constants.FILE_SERVER_BLUEPRINTS_FOLDER, tenant_name, blueprint_id)
        blueprint_base_url = urljoin(utils.get_manager_file_server_url(),
                                     relative_blueprint_path).replace(
                                         '\\', '/')
        resource = _get_resource(blueprint_base_url)
        if resource is None:
            if deployment_id is None:
                url = blueprint_base_url
            else:
                url = ','.join([deployment_base_url, blueprint_base_url])
            raise HttpException(
                url, 404, 'Resource not found: {0}'.format(resource_path))
    return resource
def get_resource(resource_path, base_url=None):
    """
    Get resource from the manager file server.

    :param resource_path: path to resource on the file server
    :returns: resource content
    """
    if base_url is None:
        base_url = utils.get_manager_file_server_url()
    try:
        url = '{0}/{1}'.format(base_url, resource_path)
        response = urllib2.urlopen(url)
        return response.read()
    except urllib2.HTTPError as e:
        raise HttpException(e.url, e.code, e.msg)
Esempio n. 3
0
def get_resource(resource_path, base_url=None):
    """
    Get resource from the manager file server.

    :param resource_path: path to resource on the file server
    :returns: resource content
    """
    if base_url is None:
        base_url = utils.get_manager_file_server_url()
    try:
        url = "{0}/{1}".format(base_url, resource_path)
        response = urllib2.urlopen(url)
        return response.read()
    except urllib2.HTTPError as e:
        raise HttpException(e.url, e.code, e.msg)
Esempio n. 4
0
def get_resource_from_manager(resource_path, base_url=None):
    """
    Get resource from the manager file server.

    :param resource_path: path to resource on the file server
    :returns: resource content
    """
    if base_url is None:
        base_url = utils.get_manager_file_server_url()

    url = '{0}/{1}'.format(base_url, resource_path)
    if utils.is_verify_rest_certificate():
        verify = utils.get_local_rest_certificate() or True
    else:
        verify = False

    response = requests.get(url, verify=verify)
    if not response.ok:
        raise HttpException(url, response.status_code, response.reason)
    return response.content
Esempio n. 5
0
def get_resource_from_manager(resource_path, base_url=None):
    """
    Get resource from the manager file server.

    :param resource_path: path to resource on the file server
    :returns: resource content
    """
    if base_url is None:
        base_url = utils.get_manager_file_server_url()

    url = '{0}/{1}'.format(base_url, resource_path)
    verify = utils.get_local_rest_certificate()

    headers = {}
    try:
        headers[constants.CLOUDIFY_TOKEN_AUTHENTICATION_HEADER] = \
            ctx.rest_token
    except NotInContext:
        headers[constants.CLOUDIFY_TOKEN_AUTHENTICATION_HEADER] = \
            workflow_ctx.rest_token
    response = requests.get(url, verify=verify, headers=headers)
    if not response.ok:
        raise HttpException(url, response.status_code, response.reason)
    return response.content
def get_resource_from_manager(resource_path, base_url=None):
    """
    Get resource from the manager file server.

    :param resource_path: path to resource on the file server
    :returns: resource content
    """
    if base_url is None:
        base_url = utils.get_manager_file_server_url()

    url = '{0}/{1}'.format(base_url, resource_path)
    verify = utils.get_local_rest_certificate()

    headers = {}
    try:
        headers[constants.CLOUDIFY_TOKEN_AUTHENTICATION_HEADER] = \
            ctx.rest_token
    except NotInContext:
        headers[constants.CLOUDIFY_TOKEN_AUTHENTICATION_HEADER] = \
            workflow_ctx.rest_token
    response = requests.get(url, verify=verify, headers=headers)
    if not response.ok:
        raise HttpException(url, response.status_code, response.reason)
    return response.content
def get_resource(blueprint_id, deployment_id, tenant_name, resource_path):
    """
    Get resource from the manager file server with path relative to
    the deployment or blueprint denoted by ``deployment_id`` or
    ``blueprint_id``.

    An attempt will first be made for getting the resource from the deployment
    folder. If not found, an attempt will be made for getting the resource
    from the blueprint folder.

    :param blueprint_id: the blueprint id of the blueprint to download
                         the resource from
    :param deployment_id: the deployment id of the deployment to download the
                          resource from
    :param tenant_name: tenant name
    :param resource_path: path to resource relative to blueprint folder
    :returns: resource content
    """

    def _get_resource(base_url):
        try:
            return get_resource_from_manager(resource_path, base_url=base_url)
        except HttpException as e:
            if e.code != 404:
                raise
            return None

    resource = None
    if deployment_id is not None:
        relative_deployment_path = os.path.join(
            constants.FILE_SERVER_RESOURCES_FOLDER,
            constants.FILE_SERVER_DEPLOYMENTS_FOLDER,
            tenant_name,
            deployment_id
        )
        deployment_base_url = urljoin(
            utils.get_manager_file_server_url(),
            relative_deployment_path
        ).replace('\\', '/')
        resource = _get_resource(deployment_base_url)

    if resource is None:
        client = get_rest_client()
        blueprint = client.blueprints.get(blueprint_id)

        if blueprint['visibility'] == VisibilityState.GLOBAL:
            tenant_name = blueprint['tenant_name']

        relative_blueprint_path = os.path.join(
            constants.FILE_SERVER_RESOURCES_FOLDER,
            constants.FILE_SERVER_BLUEPRINTS_FOLDER,
            tenant_name,
            blueprint_id
        )
        blueprint_base_url = urljoin(
            utils.get_manager_file_server_url(),
            relative_blueprint_path
        ).replace('\\', '/')
        resource = _get_resource(blueprint_base_url)
        if resource is None:
            if deployment_id is None:
                url = blueprint_base_url
            else:
                url = ','.join([deployment_base_url, blueprint_base_url])
            raise HttpException(url, 404, 'Resource not found: {0}'
                                .format(resource_path))
    return resource