diff --git a/CHANGELOG.md b/CHANGELOG.md index cdfbcf33175..e302cd1006a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Bug Fixes 1. [#339](https://github.com/influxdata/influxdb-client-java/pull/339): Evaluation of connection string +1. [#352](https://github.com/influxdata/influxdb-client-java/pull/352): Creating `Tasks` with `import` statements ## 6.0.0 [2022-04-19] diff --git a/client/src/main/java/com/influxdb/client/internal/TasksApiImpl.java b/client/src/main/java/com/influxdb/client/internal/TasksApiImpl.java index 520cc6c3698..cbe8204e1e5 100644 --- a/client/src/main/java/com/influxdb/client/internal/TasksApiImpl.java +++ b/client/src/main/java/com/influxdb/client/internal/TasksApiImpl.java @@ -721,7 +721,11 @@ private Task createTask(@Nonnull final String name, repetition += "cron: "; repetition += "\"" + cron + "\""; } - String fluxWithOptions = String.format("option task = {name: \"%s\", %s} \n %s", name, repetition, flux); + + int fromBegin = flux.indexOf("from"); + String fluxWithOptions = flux.substring(0, fromBegin) + + String.format("option task = {name: \"%s\", %s}\n\n", name, repetition) + + flux.substring(fromBegin); task.setFlux(fluxWithOptions); diff --git a/client/src/test/java/com/influxdb/client/ITTasksApi.java b/client/src/test/java/com/influxdb/client/ITTasksApi.java index 1c930fd21ea..020df15086f 100644 --- a/client/src/test/java/com/influxdb/client/ITTasksApi.java +++ b/client/src/test/java/com/influxdb/client/ITTasksApi.java @@ -43,6 +43,7 @@ import com.influxdb.client.domain.Run; import com.influxdb.client.domain.RunManually; import com.influxdb.client.domain.Task; +import com.influxdb.client.domain.TaskCreateRequest; import com.influxdb.client.domain.TaskStatusType; import com.influxdb.client.domain.User; import com.influxdb.exceptions.NotFoundException; @@ -183,6 +184,53 @@ void createTaskCron() { Assertions.assertThat(task.getLinks().getLabels()).isEqualTo("/api/v2/tasks/" + task.getId() + "/labels"); } + @Test + void createTaskByTaskCreateRequest() { + String taskName = generateName("it task"); + String taskFlux = "import \"experimental\"\n" + + "\n" + + "option task = {name: \""+taskName+"\", cron: \"12 0 * * * *\"}" + + "\n" + + "from(bucket: \"my-bucket\")\n" + + " |> range(start: experimental.addDuration(d: -1d, to: today()), stop: today())\n" + + " |> max()"; + + TaskCreateRequest taskCreateRequest = new TaskCreateRequest() + .orgID(organization.getId()) + .flux(taskFlux) + .status(TaskStatusType.ACTIVE); + + Task task = tasksApi.createTask(taskCreateRequest); + + Assertions.assertThat(task).isNotNull(); + Assertions.assertThat(task.getId()).isNotBlank(); + Assertions.assertThat(task.getName()).isEqualTo(taskName); + Assertions.assertThat(task.getOrgID()).isEqualTo(organization.getId()); + Assertions.assertThat(task.getStatus()).isEqualTo(TaskStatusType.ACTIVE); + Assertions.assertThat(task.getCron()).isEqualTo("12 0 * * * *"); + Assertions.assertThat(task.getFlux()).isEqualToIgnoringWhitespace(taskFlux); + } + + @Test + void createTaskWithImport() { + String taskName = generateName("it task"); + String taskFlux = "import \"experimental\"\n" + + "\n" + + "from(bucket: \"my-bucket\")\n" + + " |> range(start: experimental.addDuration(d: -1d, to: today()), stop: today())\n" + + " |> max()"; + + Task task = tasksApi.createTaskCron(taskName, taskFlux, "10 0 * * * *", organization.getId()); + + Assertions.assertThat(task).isNotNull(); + Assertions.assertThat(task.getId()).isNotBlank(); + Assertions.assertThat(task.getName()).isEqualTo(taskName); + Assertions.assertThat(task.getOrgID()).isEqualTo(organization.getId()); + Assertions.assertThat(task.getStatus()).isEqualTo(TaskStatusType.ACTIVE); + Assertions.assertThat(task.getCron()).isEqualTo("10 0 * * * *"); + Assertions.assertThat(task.getFlux()).contains("import \"experimental\"\n"); + } + @Test void findTaskByID() {