Problem using API to POST a task exported from InfluxDB GUI

InfluxDB v2.7.0 OSS
Server: 85f725f
Frontend: 0c844dc

What I’m trying to do:

I’m trying to write a task in the InfluxDB GUI, export it, and then use source control / bash scripts to deploy during instance startup.

Steps:

1.) Using the GUI, define a task and confirm it works as expected. (it does)

2.) Export the task definition, and the resulting JSON file looks like this:

[
    {
        "apiVersion": "influxdata.com/v2alpha1",
        "kind": "Task",
        "metadata": {
            "name": "noshing-lumiere-9f2001"
        },
        "spec": {
            "every": "10m",
            "name": "john1",
            "offset": "5m0s",
            "query": "from(bucket: \"metrics\")\n    |> range(start: -10m)\n    |> filter(fn: (r) => r[\"_measurement\"] == \"cpu\")\n    |> filter(fn: (r) => r[\"_field\"] == \"usage_user\")\n    |> filter(fn: (r) => r[\"cpu\"] == \"cpu-total\")\n    |> aggregateWindow(every: 1m, fn: mean, createEmpty: false)\n    |> to(bucket: \"test\")"
        }
    }
]

3.) Add the JSON definition as the payload of a POST method to the /api/v2/tasks/ API endpoint. The POST fails, complaining of a missing “flux” field.

4.) The InfluxDB Postman collection suggests this is the “shape” of the payload, which contains similar data:

{
    "orgID": "{{orgID}}",
    "status": "inactive",
    "flux": "option task = {name: \"mytask\", every: 10m, offset: 1m}\n from(bucket: \"system\")\n |> range(start: -task.every)\n |> filter(fn: (r) => r[\"_measurement\"] == \"cpu\")\n |> filter(fn: (r) => r[\"_field\"] == \"usage_user\")\n |> filter(fn: (r) => r[\"cpu\"] == \"cpu-total\")\n |> aggregateWindow(every: 5m, fn: mean, createEmpty: false)\n |> to(bucket: \"test\")\n",
    "description": "downsamples total cpu usage_user data into 5m averages every 10m"
}

5.) Observe that the shape of the exported JSON and the working POST payload are quite different. (the queries in my example are a bit different.)

QUESTIONS:

1.) Am I wrong to expect that a JSON payload exported from the GUI can be POSTed back to API?
2.) What endpoint should I use to POST a task exported from the InfluxDB UI?
3.) Does anyone have general advice regarding bulk formatting/managing many tasks deployed from source code?

For example, the flux command seems to need to be rewritten using “backslash-n” and “backslash-t” rather than allowing clear text formatting: the result is quite obscure.

Hello @john_v,
Here’s an example but with parameterized queries:

  1. You’re not wrong
  2. but I don’t know if you can invoke a task since that runs on a schedule with the query engine. I think that’s causing the conflict.
  3. I’d use parameterized queries as shown in the examples above. And invoke the script on a schedule.

Thanks @Anaisdg,

The GUI’s task import feature seems to read a file exported from the GUI. As I noticed, and you confirmed, that exported file’s format is incompatible with the task API. That is not a good fit for my use case.

I understand your suggestion for a custom endpoint (really cool feature, BTW) to accept the GUI exported file format as a POST payload and create the task. I don’t think I want to go in that direction due to various resource constraints.

On a positive note, I found that the CLI command:

influx task create --file $filename

…allows for multi-line JSON files to be passed into it.

This gives me the ability to
a.) deploy tasks with “code” as well as
b.) write and maintain the JSON files in a clean, multi-line format that I can write and check into source control.

So I’m comfortable with that solution. Thanks again for the feedback.