Search in sources :

Example 1 with InvalidPathException

use of org.alien4cloud.tosca.editor.exception.InvalidPathException in project alien4cloud by alien4cloud.

the class AbstractUpdateFileProcessor method process.

@Override
public void process(Csar csar, Topology topology, T operation) {
    // archive content tree is actually a node that contains only the folder of the topology
    TreeNode root = EditionContextManager.get().getArchiveContentTree().getChildren().first();
    // walk the file path to insert an element
    TreeNode target = root;
    if (operation.getPath().endsWith("/")) {
        throw new InvalidPathException("Path <" + operation.getPath() + "> is invalid (must be a file and not a directory).");
    }
    // File upload management
    String[] pathElements = operation.getPath().split("/");
    for (int i = 0; i < pathElements.length; i++) {
        String pathElement = pathElements[i];
        TreeNode child = target.getChild(pathElement);
        if (child == null) {
            if (target.isLeaf()) {
                throw new InvalidPathException("Path <" + operation.getPath() + "> is invalid (one of the folder of the path is actualy a file).");
            }
            // add an element
            child = new TreeNode();
            child.setName(pathElement);
            child.setFullPath(target.getFullPath() + "/" + pathElement);
            child.setParent(target);
            target.getChildren().add(child);
            if (i == pathElements.length - 1) {
                child.setLeaf(true);
            } else {
                child.setChildren(new TreeSet<>());
            }
        }
        target = child;
    }
    if (target.isLeaf()) {
        // store the file in the local temporary file repository
        // If already applied the input stream is closed and I should just get the artifact id
        String artifactFileId = operation.getTempFileId();
        if (artifactFileId == null) {
            artifactFileId = artifactRepository.storeFile(operation.getArtifactStream());
            operation.setTempFileId(artifactFileId);
            // Note the input stream should be closed by the caller (controller in our situation).
            operation.setArtifactStream(null);
        }
        try {
            if (csar.getYamlFilePath().equals(operation.getPath())) {
                // the operation updates the topology file, we have to parse it and override the topology data out of it.
                editorTopologyUploadService.processTopology(artifactRepository.resolveFile(artifactFileId), topology.getWorkspace());
            }
        } catch (RuntimeException e) {
            // remove the file from the temp repository if the topology cannot be parsed
            artifactRepository.deleteFile(artifactFileId);
            throw e;
        }
        // let's just impact the url to point to the temp file.
        target.setArtifactId(artifactFileId);
    } else {
        // Fail as we cannot override a directory
        throw new InvalidPathException("Path <" + operation.getPath() + "> is invalid (must be a file and not a directory).");
    }
}
Also used : TreeNode(alien4cloud.utils.TreeNode) InvalidPathException(org.alien4cloud.tosca.editor.exception.InvalidPathException)

Example 2 with InvalidPathException

use of org.alien4cloud.tosca.editor.exception.InvalidPathException in project alien4cloud by alien4cloud.

the class DeleteFileProcessor method process.

@Override
public void process(Csar csar, Topology topology, DeleteFileOperation operation) {
    if (csar.getYamlFilePath().equals(operation.getPath())) {
        throw new InvalidPathException("Topology yaml file cannot be removed.");
    }
    TreeNode target = FileProcessorHelper.getFileTreeNode(operation.getPath());
    target.getParent().getChildren().remove(target);
    for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) {
        for (DeploymentArtifact artifact : safe(nodeTemplate.getArtifacts()).values()) {
            resetRemovedArtifact(artifact, operation.getPath());
        }
        cleanupInterfaces(nodeTemplate.getInterfaces(), operation.getPath());
        for (RelationshipTemplate relationshipTemplate : safe(nodeTemplate.getRelationships()).values()) {
            cleanupInterfaces(relationshipTemplate.getInterfaces(), operation.getPath());
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) TreeNode(alien4cloud.utils.TreeNode) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) InvalidPathException(org.alien4cloud.tosca.editor.exception.InvalidPathException)

Aggregations

TreeNode (alien4cloud.utils.TreeNode)2 InvalidPathException (org.alien4cloud.tosca.editor.exception.InvalidPathException)2 DeploymentArtifact (org.alien4cloud.tosca.model.definitions.DeploymentArtifact)1 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)1 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)1