Search in sources :

Example 1 with TaskConfigurationModel

use of com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel in project intellij-tekton by redhat-developer.

the class YAMLBuilder method createRunInternal.

public static ObjectNode createRunInternal(String kind, ConfigurationModel model, boolean toDebug) {
    ObjectNode rootNode = YAML_MAPPER.createObjectNode();
    rootNode.put("apiVersion", "tekton.dev/v1beta1");
    rootNode.put("kind", kind);
    ObjectNode metadataNode = YAML_MAPPER.createObjectNode();
    metadataNode.put("generateName", model.getName() + "-");
    rootNode.set("metadata", metadataNode);
    ObjectNode spec = YAML_MAPPER.createObjectNode();
    if (model instanceof ActionToRunModel) {
        ActionToRunModel actionModel = (ActionToRunModel) model;
        if (kind.equalsIgnoreCase(KIND_PIPELINERUN)) {
            spec = createPipelineRunSpec(actionModel);
        } else {
            spec = createTaskRunSpec(actionModel.getResource().getName(), actionModel.getParams(), actionModel.getInputResources(), actionModel.getOutputResources(), actionModel.getWorkspaces(), actionModel.getServiceAccount().isEmpty() ? null : actionModel.getServiceAccount(), toDebug);
        }
    } else if (model instanceof TaskConfigurationModel) {
        Map<String, Workspace> workspaces = new HashMap<>();
        ((TaskConfigurationModel) model).getWorkspaces().stream().forEach(workspace -> {
            workspace.setKind(Workspace.Kind.EMPTYDIR);
            workspaces.put(workspace.getName(), workspace);
        });
        spec = createTaskRunSpec(model.getName(), ((TaskConfigurationModel) model).getParams(), ((TaskConfigurationModel) model).getInputResources(), ((TaskConfigurationModel) model).getOutputResources(), workspaces, "", toDebug);
    }
    rootNode.set("spec", spec);
    return rootNode;
}
Also used : WRITE_DOC_START_MARKER(com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature.WRITE_DOC_START_MARKER) ActionToRunModel(com.redhat.devtools.intellij.tektoncd.utils.model.actions.ActionToRunModel) YAMLHelper(com.redhat.devtools.intellij.common.utils.YAMLHelper) KIND_VCT(com.redhat.devtools.intellij.tektoncd.Constants.KIND_VCT) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Output(com.redhat.devtools.intellij.tektoncd.tkn.component.field.Output) Workspace(com.redhat.devtools.intellij.tektoncd.tkn.component.field.Workspace) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) HashMap(java.util.HashMap) KIND_PIPELINERUN(com.redhat.devtools.intellij.tektoncd.Constants.KIND_PIPELINERUN) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TaskConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.ConfigurationModel) List(java.util.List) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) KIND_PVC(com.redhat.devtools.intellij.tektoncd.Constants.KIND_PVC) Map(java.util.Map) Pair(com.intellij.openapi.util.Pair) Input(com.redhat.devtools.intellij.tektoncd.tkn.component.field.Input) JsonNode(com.fasterxml.jackson.databind.JsonNode) KIND_PIPELINE(com.redhat.devtools.intellij.tektoncd.Constants.KIND_PIPELINE) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TaskConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel) HashMap(java.util.HashMap) Map(java.util.Map) ActionToRunModel(com.redhat.devtools.intellij.tektoncd.utils.model.actions.ActionToRunModel)

Example 2 with TaskConfigurationModel

use of com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel in project intellij-tekton by redhat-developer.

the class VariableReferencesInspector method checkFile.

@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    ConfigurationModel model = getTektonModelFromFile(file);
    if (model == null) {
        return ProblemDescriptor.EMPTY_ARRAY;
    }
    List<PsiElement> unusedPsiElements = new ArrayList<>();
    if (model instanceof PipelineConfigurationModel) {
        unusedPsiElements = highlightInPipeline(file, (PipelineConfigurationModel) model);
    } else if (model instanceof TaskConfigurationModel) {
        unusedPsiElements = highlightInTask(file, (TaskConfigurationModel) model);
    }
    return unusedPsiElements.stream().map(item -> manager.createProblemDescriptor(item, item, "Variable " + StringHelper.getUnquotedValueFromPsi(item.getContext()) + " is never used", ProblemHighlightType.WEAK_WARNING, isOnTheFly, LocalQuickFix.EMPTY_ARRAY)).toArray(ProblemDescriptor[]::new);
}
Also used : PipelineConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.PipelineConfigurationModel) Output(com.redhat.devtools.intellij.tektoncd.tkn.component.field.Output) Workspace(com.redhat.devtools.intellij.tektoncd.tkn.component.field.Workspace) TaskConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel) PipelineConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.PipelineConfigurationModel) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) ConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.ConfigurationModel) List(java.util.List) InspectionManager(com.intellij.codeInspection.InspectionManager) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) FileASTNode(com.intellij.lang.FileASTNode) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) StringHelper(com.redhat.devtools.intellij.common.utils.StringHelper) Input(com.redhat.devtools.intellij.tektoncd.tkn.component.field.Input) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) Collections(java.util.Collections) TaskConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel) PipelineConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.PipelineConfigurationModel) ConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.ConfigurationModel) ArrayList(java.util.ArrayList) TaskConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with TaskConfigurationModel

use of com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel in project intellij-tekton by redhat-developer.

the class CreateTaskRunTemplateAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent anActionEvent, TreePath path, Object selected, Tkn tkncli) {
    ActionMessage telemetry = TelemetryService.instance().action(NAME_PREFIX_CRUD + "create task run");
    ParentableNode element = getElement(selected);
    String namespace = element.getNamespace();
    ExecHelper.submit(() -> {
        Notification notification;
        TaskConfigurationModel model;
        try {
            model = getModel(element, namespace, tkncli);
        } catch (IOException e) {
            String errorMessage = "Failed to create TaskRun templace from " + element.getName() + " in namespace " + namespace + "An error occurred while retrieving information.\n" + e.getLocalizedMessage();
            telemetry.error(anonymizeResource(element.getName(), namespace, errorMessage)).send();
            UIHelper.executeInUI(() -> {
                Messages.showErrorDialog(errorMessage, "Error");
            });
            logger.warn("Error: " + e.getLocalizedMessage(), e);
            return;
        }
        if (!model.isValid()) {
            String errorMessage = "Failed to create a TaskRun templace from " + element.getName() + " in namespace " + namespace + ". The task is not valid.";
            telemetry.error(anonymizeResource(element.getName(), namespace, errorMessage)).send();
            UIHelper.executeInUI(() -> Messages.showErrorDialog(errorMessage, "Error"));
            return;
        }
        try {
            String contentTask = new YAMLMapper().writeValueAsString(YAMLBuilder.createTaskRun(model));
            openEditor(anActionEvent.getProject(), namespace, telemetry, model, contentTask);
        } catch (IOException e) {
            String errorMessage = "Failed to create TaskRun templace from" + element.getName() + " in namespace " + namespace + " \n" + e.getLocalizedMessage();
            telemetry.error(anonymizeResource(element.getName(), namespace, errorMessage)).send();
            notification = new Notification(NOTIFICATION_ID, "Error", errorMessage, NotificationType.ERROR);
            Notifications.Bus.notify(notification);
            logger.warn(errorMessage, e);
        }
    });
}
Also used : YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) ParentableNode(com.redhat.devtools.intellij.tektoncd.tree.ParentableNode) ActionMessage(com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder.ActionMessage) TaskConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel) IOException(java.io.IOException) Notification(com.intellij.notification.Notification)

Example 4 with TaskConfigurationModel

use of com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel in project intellij-tekton by redhat-developer.

the class YAMLBuilderTest method checkTaskRunCreatedWithTaskConfigurationModelWithWorkspace.

@Test
public void checkTaskRunCreatedWithTaskConfigurationModelWithWorkspace() throws IOException {
    String content = load("task10.yaml");
    TaskConfigurationModel model = new TaskConfigurationModel(content);
    ObjectNode taskRunNode = YAMLBuilder.createTaskRun(model);
    assertEquals(taskRunNode.get("apiVersion").asText(), "tekton.dev/v1beta1");
    assertEquals(taskRunNode.get("kind").asText(), "TaskRun");
    assertEquals(taskRunNode.get("metadata").get("generateName").asText(), "foo-");
    assertEquals(taskRunNode.get("spec").get("taskRef").get("name").asText(), "foo");
    assertTrue(taskRunNode.get("spec").has("serviceAccountName"));
    assertEquals(taskRunNode.get("spec").get("serviceAccountName").asText(), "");
    assertFalse(taskRunNode.get("spec").has("serviceAccountNames"));
    assertFalse(taskRunNode.get("spec").has("params"));
    assertFalse(taskRunNode.get("spec").has("resources"));
    assertTrue(taskRunNode.get("spec").has("workspaces"));
    assertEquals(taskRunNode.get("spec").get("workspaces").get(0).get("name").asText(), "write-allowed");
    assertEquals(taskRunNode.get("spec").get("workspaces").get(1).get("name").asText(), "write-disallowed");
    assertTrue(taskRunNode.get("spec").get("workspaces").get(0).has("emptyDir"));
    assertTrue(taskRunNode.get("spec").get("workspaces").get(1).has("emptyDir"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TaskConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel) Test(org.junit.Test) BaseTest(com.redhat.devtools.intellij.tektoncd.BaseTest)

Example 5 with TaskConfigurationModel

use of com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel in project intellij-tekton by redhat-developer.

the class TaskConfigurationModelTest method checkTaskModelWithWorkspaces.

@Test
public void checkTaskModelWithWorkspaces() throws IOException {
    String configuration = load("task10.yaml");
    TaskConfigurationModel model = (TaskConfigurationModel) ConfigurationModelFactory.getModel(configuration);
    assertEquals(model.getName(), "foo");
    assertEquals(model.getNamespace(), "tekton");
    assertEquals(model.getKind(), "Task");
    assertTrue(model.getParams().isEmpty());
    assertTrue(model.getInputResources().isEmpty());
    assertTrue(model.getOutputResources().isEmpty());
    assertTrue(model.getWorkspaces().size() == 2);
    assertEquals(model.getWorkspaces().get(0).getName(), "write-allowed");
    assertEquals(model.getWorkspaces().get(1).getName(), "write-disallowed");
}
Also used : TaskConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel) Test(org.junit.Test) BaseTest(com.redhat.devtools.intellij.tektoncd.BaseTest)

Aggregations

TaskConfigurationModel (com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel)12 BaseTest (com.redhat.devtools.intellij.tektoncd.BaseTest)9 Test (org.junit.Test)9 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)2 Input (com.redhat.devtools.intellij.tektoncd.tkn.component.field.Input)2 Output (com.redhat.devtools.intellij.tektoncd.tkn.component.field.Output)2 Workspace (com.redhat.devtools.intellij.tektoncd.tkn.component.field.Workspace)2 ConfigurationModel (com.redhat.devtools.intellij.tektoncd.utils.model.ConfigurationModel)2 PipelineConfigurationModel (com.redhat.devtools.intellij.tektoncd.utils.model.resources.PipelineConfigurationModel)2 IOException (java.io.IOException)2 List (java.util.List)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 WRITE_DOC_START_MARKER (com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature.WRITE_DOC_START_MARKER)1 InspectionManager (com.intellij.codeInspection.InspectionManager)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1