Пример #1
0
def validate_template(model_storage, resource_storage, plugin_manager, logger):
    """
    Validates a TOSCA template
    """
    body = request.json

    # Check body
    if "service_template_path" in body:
        service_template_path = body["service_template_path"]
    else:
        return "request body missing service_template_path", 501
    if "service_template_filename" in body:
        service_template_filename = body["service_template_filename"]
    else:
        service_template_filename = "service-template.yaml"

    service_template_path = service_template_utils.get(
        service_template_path, service_template_filename)

    core = Core(model_storage, resource_storage, plugin_manager)
    try:
        context = core.validate_service_template(service_template_path)
    except ParsingError as e:
        return e.message, 400

    logger.info('Service template {} validated'.format(service_template_path))
    return "", 200
Пример #2
0
def delete_service(service_id, model_storage, resource_storage, plugin_manager,
                   logger):
    """
    Deletes the specified servi e
    """
    service = model_storage.service.get(service_id)
    core = Core(model_storage, resource_storage, plugin_manager)
    core.delete_service(service_id, force=True)
    return "service {}  deleted".format(service.id), 200
Пример #3
0
def install_template_private(service_template_name, model_storage,
                             resource_storage, plugin_manager, logger):
    service_template_path = None
    service_template_filename = "service-template.yaml"
    fileSp = request.files['service_template']._file
    fileType = "unknown"
    if "multipart/form-data" in request.headers['Content-Type']:
        if request.headers["file-type"] == 'csar':
            fileType = '.csar'
        elif request.headers["file-type"] == 'yaml':
            fileType = '.yaml'
        else:
            logger.error("Unknown file-type in request header")
            return "Unknown file-type in request header", 400
        f = tempfile.NamedTemporaryFile(prefix="ariatmp_",
                                        dir="/tmp/aria",
                                        suffix=fileType,
                                        delete=False)
        f.write(fileSp.getvalue())
        f.seek(fileSp.tell(), 0)
        service_template_path = f.name
    else:
        logger.error("Unknown Content-type in request header")
        return "Unknown Content-Type in request header", 400
    fileSp.close()

    if "template_filename" in request.headers:
        service_template_filename = request.headers["template_filename"]

    file_path = service_template_utils.get(service_template_path,
                                           service_template_filename)

    core = Core(model_storage, resource_storage, plugin_manager)
    logger.info("service-template file {}".format(file_path))
    logger.info("service-template {}".format(service_template_path))

    try:
        service_template_id = \
            core.create_service_template(file_path,
                                         os.path.dirname(file_path),
                                         service_template_name)
    except storage_exceptions.StorageError as e:
        logger.error("storage exception")
        utils.check_overriding_storage_exceptions(e, 'service template',
                                                  service_template_name)
        return e.message, 500
    except Exception as e:
        logger.error("catchall exception")
        return e.message, 500
    finally:
        os.remove(service_template_path)
        os.system("rm -rf " + os.path.dirname(file_path))

    return "service template installed", 200, service_template_id
Пример #4
0
def create_service(template_id, service_name, model_storage, resource_storage,
                   plugin_manager, logger):
    """
    Creates a service from the specified service template
    """
    body = request.json
    inputs = {}
    if 'inputs' in body:
        inputs = body['inputs']
    core = Core(model_storage, resource_storage, plugin_manager)
    service = core.create_service(template_id, inputs, service_name)

    logger.info("service {} created".format(service.name))
    return "service {} created".format(service.name), 200
Пример #5
0
def create_service(template_id, service_name, model_storage, resource_storage,
                   plugin_manager, logger):
    """
    Creates a service from the specified service template
    """
    body = request.json or {}
    if "multipart/form-data" in request.headers['Content-Type']:
        body = json.load(request.files['input_values']._file)
    inputs = {}
    if 'inputs' in body:
        inputs = body['inputs']
    core = Core(model_storage, resource_storage, plugin_manager)
    service = core.create_service(template_id, inputs, service_name)

    logger.info("service {} created".format(service.name))
    return "service {} created".format(service.name), 200, service
Пример #6
0
def delete_template(template_id, model_storage, resource_storage,
                    plugin_manager, logger):
    """
    Deletes a template from Aria storage
    """

    logger.info('Deleting service template {}'.format(template_id))
    core = Core(model_storage, resource_storage, plugin_manager)
    try:
        core.delete_service_template(template_id)
    except DependentServicesError as e:
        logger.error("dependent services error")
        return e.message, 400
    except Exception as e:
        logger.error("failed")
        return "Failed to delete template", 500

    logger.info('Service template {} deleted'.format(template_id))
    return "", 200
Пример #7
0
def install_template(template_name, model_storage, resource_storage,
                     plugin_manager, logger):
    """
    installs a template in Aria storage
    """
    body = request.json

    # Check body
    if "service_template_path" in body:
        service_template_path = body["service_template_path"]
    else:
        return "request body missing service_template_path", 501

    if "service_template_filename" in body:
        service_template_filename = body["service_template_filename"]
    else:
        service_template_filename = "service-template.yaml"

    service_template_path = service_template_utils.get(
        service_template_path, service_template_filename)

    core = Core(model_storage, resource_storage, plugin_manager)

    try:
        core.create_service_template(service_template_path,
                                     os.path.dirname(service_template_path),
                                     template_name)
    except storage_exceptions.StorageError as e:
        logger.error("storage exception")
        utils.check_overriding_storage_exceptions(e, 'service template',
                                                  template_name)
        return e.message, 500
    except Exception as e:
        logger.error("catchall exception")
        return e.message, 500

    return "service template installed", 200
Пример #8
0
 def core(self):
     if not self._core:
         self._core = Core(model_storage=self.model_storage,
                           resource_storage=self.resource_storage,
                           plugin_manager=self.plugin_manager)
     return self._core
Пример #9
0
def install_template(template_name, model_storage, resource_storage,
                     plugin_manager, logger):
    """
    installs a template in Aria storage

    3 modes possible:

      1. PUT JSON body which points to a CSAR URL.  Content-type must be
         application/json. PUT data is a JSON object/map with the following
         keys.:
          * service_template_path (required): URL to CSAR
          * service_template_filename (optional): service template file.

      2. PUT with service template file body.  Content-type must be
         text/plain.

      3. PUT with binary CSAR body.  Content-type must be application/zip.
         Optional query string arg "template_filename" can indicate the
         service template filename in the CSAR.  Defaults to
         "service-template.yaml".
    """

    service_template_path = None
    service_template_filename = "service-template.yaml"
    rtype = "unknown"
    if request.is_json:
        rtype = "json"
    elif request.headers['Content-Type'] == "application/zip":
        rtype = "zip"
        suffix = ".csar"
    elif request.headers['Content-Type'] == "text/plain":
        rtype = "yaml"
        suffix = ".yaml"
    elif request.headers['Content-Type'] == "multipart/form-data":
        rtype = "multipart"

    if rtype == "zip" or rtype == "yaml":
        with tempfile.NamedTemporaryFile(prefix="ariatmp_",
                                         suffix=suffix,
                                         delete=False) as f:
            f.write(request.data)
            service_template_path = f.name
        if request.headers['Content-Type'] == "application/zip":
            if "template_filename" in request.args:
                service_template_filename = request.args["template_filename"]

    elif rtype == "json":

        body = request.json or {}

        # Check body
        if "service_template_path" in body:
            service_template_path = body["service_template_path"]
        else:
            return "request body missing service_template_path", 501

        if "service_template_filename" in body:
            service_template_filename = body["service_template_filename"]
        else:
            service_template_filename = "service-template.yaml"

    else:
        return "Unrecognized content type", 400

    service_template_file_path = service_template_utils.get(
        service_template_path, service_template_filename)

    core = Core(model_storage, resource_storage, plugin_manager)

    try:
        core.create_service_template(service_template_file_path,
                                     os.path.dirname(service_template_path),
                                     template_name)
    except storage_exceptions.StorageError as e:
        logger.error("storage exception")
        utils.check_overriding_storage_exceptions(e, 'service template',
                                                  template_name)
        return e.message, 500
    except Exception as e:
        logger.error("catchall exception")
        return e.message, 500
    finally:
        # cleanup
        if rtype == "zip" or rtype == "yaml":
            os.remove(service_template_path)
        if rtype == "zip":
            shutil.rmtree(os.path.dirname(service_template_file_path))

    return "service template installed", 200