Document new API Invocable Scripts feature (was named-functions) (#3282)

* feat: add api-guides/guides and guides/api-invocable-scripts for the API-invocable scripts (managed functions) feature (##2547, #2233).

* feat: document API-invocable scripts. Add example for creating and invoking a script with POST. Unable to make GET work with query parameters.

* feat: moved API-invocable scripts under api-guide. Remove guides directory. Add docs for list and find. Add example for list. Add shortcuts and glossary links. Verified with team-compute that GET /invoke and python are not supported yet.

* feat: rename example function for simplicity since there's no GET for now.

* feat: add find_and_update example (#2547).

* feat: add update example. Clarify, expand invoke example. Add links. (#2547)

* Update content/influxdb/cloud/api-guide/api-invocable-scripts/_index.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* fix: remove accidental commit of generated redoc-static.html

* feat: manual update to cloud swagger. Merges meta, auth, tags, and x-tagGroups from src/cloud.yml to aggregate contracts/ref/cloud.yml.

* fix: replace /functions with /scripts

* feat: get swaggers from contracts/ref. Still requires copy-paste of info, tags, and auth from contracts/cloud.yml and contracts/oss.yml

* feat: invocable scripts (#2547)

* Update content/influxdb/cloud/api-guide/api-invocable-scripts/_index.md

Co-authored-by: pierwill <19642016+pierwill@users.noreply.github.com>

* feat: new section for params (#2547).

* fix: swagger server url (#2547)

* Update content/influxdb/cloud/api-guide/api-invocable-scripts/_index.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* feat: remove jq and function where there's no clear need.

Co-authored-by: pierwill <19642016+pierwill@users.noreply.github.com>
pull/3296/head
Jason Stirnaman 2021-10-25 08:30:31 -05:00 committed by GitHub
parent b79d5071ac
commit 6587ee1fdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 24034 additions and 25152 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ node_modules
*.log
/resources
/content/influxdb/*/api/*.html
/api-docs/redoc-static.html*

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
openapi: 3.0.0
# this is a manually maintained file for these old routes until oats#15 is resolved
openapi: "3.0.0"
info:
title: Influx API Service (V1 compatible endpoints)
version: 0.1.0
@ -7,7 +8,7 @@ servers:
description: V1 compatible api endpoints.
paths:
/write:
post:
post: # technically this functions with other methods as well
operationId: PostWriteV1
tags:
- Write
@ -20,17 +21,15 @@ paths:
schema:
type: string
parameters:
- $ref: '#/components/parameters/TraceSpan'
- $ref: '#/components/parameters/AuthUserV1'
- $ref: '#/components/parameters/AuthPassV1'
- $ref: "#/components/parameters/TraceSpan"
- $ref: "#/components/parameters/AuthUserV1"
- $ref: "#/components/parameters/AuthPassV1"
- in: query
name: db
schema:
type: string
required: true
description: >-
The bucket to write to. If none exist a bucket will be created with
a default 3 day retention policy.
description: The bucket to write to. If none exist a bucket will be created with a default 3 day retention policy.
- in: query
name: rp
schema:
@ -43,76 +42,54 @@ paths:
description: Write precision.
- in: header
name: Content-Encoding
description: >-
When present, its value indicates to the database that compression
is applied to the line-protocol body.
description: When present, its value indicates to the database that compression is applied to the line-protocol body.
schema:
type: string
description: >-
Specifies that the line protocol in the body is encoded with gzip
or not encoded with identity.
description: Specifies that the line protocol in the body is encoded with gzip or not encoded with identity.
default: identity
enum:
- gzip
- identity
responses:
'204':
description: >-
Write data is correctly formatted and accepted for writing to the
bucket.
'400':
description: >-
Line protocol poorly formed and no points were written. Response
can be used to determine the first malformed line in the body
line-protocol. All data in body was rejected and not written.
"204":
description: Write data is correctly formatted and accepted for writing to the bucket.
"400":
description: Line protocol poorly formed and no points were written. Response can be used to determine the first malformed line in the body line-protocol. All data in body was rejected and not written.
content:
application/json:
schema:
$ref: '#/components/schemas/LineProtocolError'
'401':
description: >-
Token does not have sufficient permissions to write to this
organization and bucket or the organization and bucket do not exist.
$ref: "#/components/schemas/LineProtocolError"
"401":
description: Token does not have sufficient permissions to write to this organization and bucket or the organization and bucket do not exist.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'403':
$ref: "#/components/schemas/Error"
"403":
description: No token was sent and they are required.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'413':
description: >-
Write has been rejected because the payload is too large. Error
message returns max size supported. All data in body was rejected
and not written.
$ref: "#/components/schemas/Error"
"413":
description: Write has been rejected because the payload is too large. Error message returns max size supported. All data in body was rejected and not written.
content:
application/json:
schema:
$ref: '#/components/schemas/LineProtocolLengthError'
'429':
description: >-
Token is temporarily over quota. The Retry-After header describes
when to try the write again.
$ref: "#/components/schemas/LineProtocolLengthError"
"429":
description: Token is temporarily over quota. The Retry-After header describes when to try the write again.
headers:
Retry-After:
description: >-
A non-negative decimal integer indicating the seconds to delay
after the response is received.
description: A non-negative decimal integer indicating the seconds to delay after the response is received.
schema:
type: integer
format: int32
'503':
description: >-
Server is temporarily unavailable to accept writes. The Retry-After
header describes when to try the write again.
"503":
description: Server is temporarily unavailable to accept writes. The Retry-After header describes when to try the write again.
headers:
Retry-After:
description: >-
A non-negative decimal integer indicating the seconds to delay
after the response is received.
description: A non-negative decimal integer indicating the seconds to delay after the response is received.
schema:
type: integer
format: int32
@ -121,9 +98,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
$ref: "#/components/schemas/Error"
/query:
post:
post: # technically this functions with other methods as well
operationId: PostQueryV1
tags:
- Query
@ -131,21 +108,18 @@ paths:
requestBody:
description: InfluxQL query to execute.
content:
text/plain:
text/plain: # although this should be `application/vnd.influxql`, oats breaks so we define the content-type header parameter
schema:
type: string
parameters:
- $ref: '#/components/parameters/TraceSpan'
- $ref: '#/components/parameters/AuthUserV1'
- $ref: '#/components/parameters/AuthPassV1'
- $ref: "#/components/parameters/TraceSpan"
- $ref: "#/components/parameters/AuthUserV1"
- $ref: "#/components/parameters/AuthPassV1"
- in: header
name: Accept
schema:
type: string
description: >-
Specifies how query results should be encoded in the response.
**Note:** With `application/csv`, query results include epoch
timestamps instead of RFC3339 timestamps.
description: Specifies how query results should be encoded in the response. **Note:** With `application/csv`, query results include epoch timestamps instead of RFC3339 timestamps.
default: application/json
enum:
- application/json
@ -154,15 +128,10 @@ paths:
- application/x-msgpack
- in: header
name: Accept-Encoding
description: >-
The Accept-Encoding request HTTP header advertises which content
encoding, usually a compression algorithm, the client is able to
understand.
description: The Accept-Encoding request HTTP header advertises which content encoding, usually a compression algorithm, the client is able to understand.
schema:
type: string
description: >-
Specifies that the query response in the body should be encoded
with gzip or not encoded with identity.
description: Specifies that the query response in the body should be encoded with gzip or not encoded with identity.
default: identity
enum:
- gzip
@ -179,53 +148,42 @@ paths:
schema:
type: string
responses:
'200':
"200":
description: Query results
headers:
Content-Encoding:
description: >-
The Content-Encoding entity header is used to compress the
media-type. When present, its value indicates which encodings
were applied to the entity-body
description: The Content-Encoding entity header is used to compress the media-type. When present, its value indicates which encodings were applied to the entity-body
schema:
type: string
description: >-
Specifies that the response in the body is encoded with gzip
or not encoded with identity.
description: Specifies that the response in the body is encoded with gzip or not encoded with identity.
default: identity
enum:
- gzip
- identity
Trace-Id:
description: >-
The Trace-Id header reports the request's trace ID, if one was
generated.
description: The Trace-Id header reports the request's trace ID, if one was generated.
schema:
type: string
description: Specifies the request's trace ID.
content:
application/csv:
schema:
$ref: '#/components/schemas/InfluxQLCSVResponse'
$ref: "#/components/schemas/InfluxQLCSVResponse"
text/csv:
schema:
$ref: '#/components/schemas/InfluxQLCSVResponse'
$ref: "#/components/schemas/InfluxQLCSVResponse"
application/json:
schema:
$ref: '#/components/schemas/InfluxQLResponse'
$ref: "#/components/schemas/InfluxQLResponse"
application/x-msgpack:
schema:
type: string
format: binary
'429':
description: >-
Token is temporarily over quota. The Retry-After header describes
when to try the read again.
"429":
description: Token is temporarily over quota. The Retry-After header describes when to try the read again.
headers:
Retry-After:
description: >-
A non-negative decimal integer indicating the seconds to delay
after the response is received.
description: A non-negative decimal integer indicating the seconds to delay after the response is received.
schema:
type: integer
format: int32
@ -234,7 +192,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
$ref: "#/components/schemas/Error"
components:
parameters:
TraceSpan:
@ -242,8 +200,8 @@ components:
name: Zap-Trace-Span
description: OpenTracing span context
example:
trace_id: '1'
span_id: '1'
trace_id: "1"
span_id: "1"
baggage:
key: value
required: false
@ -302,6 +260,7 @@ components:
description: Code is the machine-readable error code.
readOnly: true
type: string
# This set of enumerations must remain in sync with the constants defined in errors.go
enum:
- internal error
- not found
@ -318,9 +277,7 @@ components:
readOnly: true
description: Message is a human-readable message.
type: string
required:
- code
- message
required: [code, message]
LineProtocolError:
properties:
code:
@ -340,26 +297,18 @@ components:
type: string
op:
readOnly: true
description: >-
Op describes the logical code operation during error. Useful for
debugging.
description: Op describes the logical code operation during error. Useful for debugging.
type: string
err:
readOnly: true
description: >-
Err is a stack of errors that occurred during processing of the
request. Useful for debugging.
description: Err is a stack of errors that occurred during processing of the request. Useful for debugging.
type: string
line:
readOnly: true
description: First line within sent body containing malformed data
type: integer
format: int32
required:
- code
- message
- op
- err
required: [code, message, op, err]
LineProtocolLengthError:
properties:
code:
@ -377,7 +326,4 @@ components:
description: Max length in bytes for a body of line-protocol.
type: integer
format: int32
required:
- code
- message
- maxLength
required: [code, message, maxLength]

View File

@ -89,12 +89,12 @@ function showArgs {
function updateCloud {
echo "Updating Cloud swagger..."
curl ${verbose} ${baseUrl}/contracts/cloud.yml -s -o cloud/swagger.yml
curl ${verbose} ${baseUrl}/contracts/ref/cloud.yml -s -o cloud/swagger.yml
}
function updateOSS {
echo "Updating OSS ${ossVersion} swagger..."
mkdir -p ${ossVersion} && curl ${verbose} ${baseUrl}/contracts/oss.yml -s -o $_/swagger.yml
mkdir -p ${ossVersion} && curl ${verbose} ${baseUrl}/contracts/ref/oss.yml -s -o $_/swagger.yml
}
function updateV1Compat {

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
openapi: 3.0.0
# this is a manually maintained file for these old routes until oats#15 is resolved
openapi: "3.0.0"
info:
title: Influx API Service (V1 compatible endpoints)
version: 0.1.0
@ -7,7 +8,7 @@ servers:
description: V1 compatible api endpoints.
paths:
/write:
post:
post: # technically this functions with other methods as well
operationId: PostWriteV1
tags:
- Write
@ -20,17 +21,15 @@ paths:
schema:
type: string
parameters:
- $ref: '#/components/parameters/TraceSpan'
- $ref: '#/components/parameters/AuthUserV1'
- $ref: '#/components/parameters/AuthPassV1'
- $ref: "#/components/parameters/TraceSpan"
- $ref: "#/components/parameters/AuthUserV1"
- $ref: "#/components/parameters/AuthPassV1"
- in: query
name: db
schema:
type: string
required: true
description: >-
The bucket to write to. If none exist a bucket will be created with
a default 3 day retention policy.
description: The bucket to write to. If none exist a bucket will be created with a default 3 day retention policy.
- in: query
name: rp
schema:
@ -43,76 +42,54 @@ paths:
description: Write precision.
- in: header
name: Content-Encoding
description: >-
When present, its value indicates to the database that compression
is applied to the line-protocol body.
description: When present, its value indicates to the database that compression is applied to the line-protocol body.
schema:
type: string
description: >-
Specifies that the line protocol in the body is encoded with gzip
or not encoded with identity.
description: Specifies that the line protocol in the body is encoded with gzip or not encoded with identity.
default: identity
enum:
- gzip
- identity
responses:
'204':
description: >-
Write data is correctly formatted and accepted for writing to the
bucket.
'400':
description: >-
Line protocol poorly formed and no points were written. Response
can be used to determine the first malformed line in the body
line-protocol. All data in body was rejected and not written.
"204":
description: Write data is correctly formatted and accepted for writing to the bucket.
"400":
description: Line protocol poorly formed and no points were written. Response can be used to determine the first malformed line in the body line-protocol. All data in body was rejected and not written.
content:
application/json:
schema:
$ref: '#/components/schemas/LineProtocolError'
'401':
description: >-
Token does not have sufficient permissions to write to this
organization and bucket or the organization and bucket do not exist.
$ref: "#/components/schemas/LineProtocolError"
"401":
description: Token does not have sufficient permissions to write to this organization and bucket or the organization and bucket do not exist.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'403':
$ref: "#/components/schemas/Error"
"403":
description: No token was sent and they are required.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'413':
description: >-
Write has been rejected because the payload is too large. Error
message returns max size supported. All data in body was rejected
and not written.
$ref: "#/components/schemas/Error"
"413":
description: Write has been rejected because the payload is too large. Error message returns max size supported. All data in body was rejected and not written.
content:
application/json:
schema:
$ref: '#/components/schemas/LineProtocolLengthError'
'429':
description: >-
Token is temporarily over quota. The Retry-After header describes
when to try the write again.
$ref: "#/components/schemas/LineProtocolLengthError"
"429":
description: Token is temporarily over quota. The Retry-After header describes when to try the write again.
headers:
Retry-After:
description: >-
A non-negative decimal integer indicating the seconds to delay
after the response is received.
description: A non-negative decimal integer indicating the seconds to delay after the response is received.
schema:
type: integer
format: int32
'503':
description: >-
Server is temporarily unavailable to accept writes. The Retry-After
header describes when to try the write again.
"503":
description: Server is temporarily unavailable to accept writes. The Retry-After header describes when to try the write again.
headers:
Retry-After:
description: >-
A non-negative decimal integer indicating the seconds to delay
after the response is received.
description: A non-negative decimal integer indicating the seconds to delay after the response is received.
schema:
type: integer
format: int32
@ -121,9 +98,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
$ref: "#/components/schemas/Error"
/query:
post:
post: # technically this functions with other methods as well
operationId: PostQueryV1
tags:
- Query
@ -131,21 +108,18 @@ paths:
requestBody:
description: InfluxQL query to execute.
content:
text/plain:
text/plain: # although this should be `application/vnd.influxql`, oats breaks so we define the content-type header parameter
schema:
type: string
parameters:
- $ref: '#/components/parameters/TraceSpan'
- $ref: '#/components/parameters/AuthUserV1'
- $ref: '#/components/parameters/AuthPassV1'
- $ref: "#/components/parameters/TraceSpan"
- $ref: "#/components/parameters/AuthUserV1"
- $ref: "#/components/parameters/AuthPassV1"
- in: header
name: Accept
schema:
type: string
description: >-
Specifies how query results should be encoded in the response.
**Note:** With `application/csv`, query results include epoch
timestamps instead of RFC3339 timestamps.
description: Specifies how query results should be encoded in the response. **Note:** With `application/csv`, query results include epoch timestamps instead of RFC3339 timestamps.
default: application/json
enum:
- application/json
@ -154,15 +128,10 @@ paths:
- application/x-msgpack
- in: header
name: Accept-Encoding
description: >-
The Accept-Encoding request HTTP header advertises which content
encoding, usually a compression algorithm, the client is able to
understand.
description: The Accept-Encoding request HTTP header advertises which content encoding, usually a compression algorithm, the client is able to understand.
schema:
type: string
description: >-
Specifies that the query response in the body should be encoded
with gzip or not encoded with identity.
description: Specifies that the query response in the body should be encoded with gzip or not encoded with identity.
default: identity
enum:
- gzip
@ -179,53 +148,42 @@ paths:
schema:
type: string
responses:
'200':
"200":
description: Query results
headers:
Content-Encoding:
description: >-
The Content-Encoding entity header is used to compress the
media-type. When present, its value indicates which encodings
were applied to the entity-body
description: The Content-Encoding entity header is used to compress the media-type. When present, its value indicates which encodings were applied to the entity-body
schema:
type: string
description: >-
Specifies that the response in the body is encoded with gzip
or not encoded with identity.
description: Specifies that the response in the body is encoded with gzip or not encoded with identity.
default: identity
enum:
- gzip
- identity
Trace-Id:
description: >-
The Trace-Id header reports the request's trace ID, if one was
generated.
description: The Trace-Id header reports the request's trace ID, if one was generated.
schema:
type: string
description: Specifies the request's trace ID.
content:
application/csv:
schema:
$ref: '#/components/schemas/InfluxQLCSVResponse'
$ref: "#/components/schemas/InfluxQLCSVResponse"
text/csv:
schema:
$ref: '#/components/schemas/InfluxQLCSVResponse'
$ref: "#/components/schemas/InfluxQLCSVResponse"
application/json:
schema:
$ref: '#/components/schemas/InfluxQLResponse'
$ref: "#/components/schemas/InfluxQLResponse"
application/x-msgpack:
schema:
type: string
format: binary
'429':
description: >-
Token is temporarily over quota. The Retry-After header describes
when to try the read again.
"429":
description: Token is temporarily over quota. The Retry-After header describes when to try the read again.
headers:
Retry-After:
description: >-
A non-negative decimal integer indicating the seconds to delay
after the response is received.
description: A non-negative decimal integer indicating the seconds to delay after the response is received.
schema:
type: integer
format: int32
@ -234,7 +192,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
$ref: "#/components/schemas/Error"
components:
parameters:
TraceSpan:
@ -242,8 +200,8 @@ components:
name: Zap-Trace-Span
description: OpenTracing span context
example:
trace_id: '1'
span_id: '1'
trace_id: "1"
span_id: "1"
baggage:
key: value
required: false
@ -302,6 +260,7 @@ components:
description: Code is the machine-readable error code.
readOnly: true
type: string
# This set of enumerations must remain in sync with the constants defined in errors.go
enum:
- internal error
- not found
@ -318,9 +277,7 @@ components:
readOnly: true
description: Message is a human-readable message.
type: string
required:
- code
- message
required: [code, message]
LineProtocolError:
properties:
code:
@ -340,26 +297,18 @@ components:
type: string
op:
readOnly: true
description: >-
Op describes the logical code operation during error. Useful for
debugging.
description: Op describes the logical code operation during error. Useful for debugging.
type: string
err:
readOnly: true
description: >-
Err is a stack of errors that occurred during processing of the
request. Useful for debugging.
description: Err is a stack of errors that occurred during processing of the request. Useful for debugging.
type: string
line:
readOnly: true
description: First line within sent body containing malformed data
type: integer
format: int32
required:
- code
- message
- op
- err
required: [code, message, op, err]
LineProtocolLengthError:
properties:
code:
@ -377,7 +326,4 @@ components:
description: Max length in bytes for a body of line-protocol.
type: integer
format: int32
required:
- code
- message
- maxLength
required: [code, message, maxLength]

View File

@ -0,0 +1,189 @@
---
title: Invoke custom scripts as API endpoints
seotitle: Use InfluxDB API-invocable scripts
description: >
Use API-invocable scripts to create your own InfluxDB API endpoints that execute custom scripts.
weight: 101
menu:
influxdb_cloud:
name: Invoke custom scripts
parent: Develop with the API
influxdb/cloud/tags: [api guides]
---
Use API invocable scripts to create custom InfluxDB API endpoints that query, process, and shape data. API invocable scripts let you assign scripts to API endpoints and then execute them as standard REST operations in InfluxDB Cloud. Learn how to manage API invocable scripts and invoke them with runtime parameters.
Use the `/api/v2/functions` InfluxDB API endpoint to:
- [Create a script](#create-an-invocable-script)
- [Invoke a script](#invoke-a-script)
- [List scripts](#list-invocable-scripts)
- [Update a script](#update-an-invocable-script)
- [Delete a script](#delete-an-invocable-script)
## Create an invocable script
To create an API-invocable script
for your [organization](/influxdb/v2.0/reference/glossary/#organization),
send a request using the `POST` method to the `/api/v2/scripts` InfluxDB API endpoint.
[{{< api-endpoint method="post" endpoint="https://cloud2.influxdata.com/api/v2/scripts" >}}](/influxdb/cloud/api/#operation/PostScripts)
Provide the following in your API request:
#### Request headers
- **Content-Type**: application/json
- **Authorization**: Token *`INFLUX_API_TOKEN`*
#### Request body
JSON object with the following fields:
- **script** : [Flux](/{{% latest "flux" %}}) script as a string.
- **language** : language of your script ("flux")
- **name** : script name, unique within your organization
- **description** : script description
- **orgID**: your [InfluxDB organization ID](/influxdb/v2.0/organizations/view-orgs/#view-your-organization-id)
### Use parameters in a script
To create an invocable script that accepts parameters (variables),
reference the parameters as properties of the `params` object, e.g. `params.firstparam`.
`params` is an InfluxDB object that defines runtime variables.
You provide values for `params` when you [invoke a script](#invoke-a-script).
## Examples
### Create an invocable Flux script
The following example creates a new Flux script that references the `params.mybucket` parameter and returns the last point from the [bucket](/influxdb/v2.0/reference/glossary/#bucket).
```sh
{{% get-shared-text "api/v2.0/api-invocable-scripts/create-flux-script.sh" %}}
```
Replace the following:
- *`INFLUX_API_TOKEN`*: your [InfluxDB API token](/influxdb/cloud/reference/glossary/#token)
- *`INFLUX_ORG_ID`*: your [InfluxDB organization ID](/influxdb/v2.0/organizations/view-orgs/#view-your-organization-id)
InfluxDB returns the newly created script. Next, see how to [invoke a script](#invoke-a-script).
```json
{
"id": "084d693d93048000",
"orgID": "INFLUX_ORG_ID",
"name": "getLastPoint",
"script": "from(bucket: params.mybucket) |> range(start: -7d) |> limit(n:1)",
"description": "getLastPoint finds the last point in a bucket",
"language": "flux",
"createdAt": "2021-10-15T20:32:04.172938Z",
"updatedAt": "2021-10-15T20:32:04.172938Z"
}
```
## Invoke a script
To invoke a script, send a request using the `POST` method to the `/api/v2/scripts/SCRIPT_ID/invoke` InfluxDB API endpoint.
[{{< api-endpoint method="post" endpoint="https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID" >}}](/influxdb/cloud/api/#operation/PostScriptsIDInvoke)
Replace *`SCRIPT_ID`* with the ID of the script you want to execute. To find the script ID, see how to [list scripts](#list-scripts).
Provide the following in your request:
### Request headers
- **Content-Type**: application/json
- **Accept**: application/csv
- **Authorization**: Token *`INFLUX_API_TOKEN`*
### Request body
JSON object that contains a `params` object. In `params`, provide key-value pairs for parameters referenced in your script.
The [create](#create-an-invocable-script) example, references the parameter `params.mybucket`.
```json
"from(bucket: params.mybucket) \
|> range(start: -7d) \
|> limit(n:1)"
```
The following example invokes the created script and passes "air_sensor" as the value for `params.mybucket`.
```sh
{{% get-shared-text "api/v2.0/api-invocable-scripts/invoke-post.sh" %}}
```
InfluxDB returns query results in [annotated CSV](/influxdb/cloud/reference/syntax/annotated-csv/) from the `air_sensor` bucket.
## List invocable scripts
To list scripts for an organization, send a request using the `GET` method to the `/api/v2/scripts` InfluxDB API endpoint.
Provide the following in your request:
#### Request headers
- **Content-Type**: application/json
- **Authorization**: Token *`INFLUX_API_TOKEN`*
#### Request query parameters
- **org**: your organization name (mutually exclusive with **orgID**)
- **orgID**: your organization ID (mutually exclusive with **org**)
- **limit**: (Optional) number of scripts to return
- **offset**: (Optional) number to offset the pagination
```sh
{{% get-shared-text "api/v2.0/api-invocable-scripts/list.sh" %}}
```
To find a specific script for an organization, send a request using the `GET` method to the `/api/v2/scripts/SCRIPT_ID` InfluxDB API endpoint.
[{{< api-endpoint method="get" endpoint="https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID" >}}](/influxdb/cloud/api/#operation/GetScriptsID)
Replace *`SCRIPT_ID`* with the ID of the script you want to find.
Provide the following in your request:
#### Request headers
- **Authorization**: Token *`INFLUX_API_TOKEN`*
- **Accept**: application/json
```sh
{{% get-shared-text "api/v2.0/api-invocable-scripts/find.sh" %}}
```
## Update an invocable script
Use the API to replace the following properties of an invocable script:
- name
- description
- script
To update an existing script for an organization, send a request using the `PATCH` method to the `/api/v2/scripts/SCRIPT_ID` InfluxDB API endpoint.
Replace *`SCRIPT_ID`* with the ID of the script you want to update.
[{{< api-endpoint method="patch" endpoint="https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID" >}}](/influxdb/cloud/api/#operation/PatchScriptsID)
Provide the following in your request:
#### Request headers
- **Authorization**: Token *`INFLUX_API_TOKEN`*
- **Content-Type**: application/json
- **Accept**: application/json'
#### Request body
- A modified **invocable script** as a JSON object.
The following example finds an invocable script containing a numeric date range,
replaces the date with a new parameter, and updates the invocable script.
```sh
{{% get-shared-text "api/v2.0/api-invocable-scripts/update-flux-script.sh" %}}
```
1. Use `GET /api/v2/scripts` to retrieve an object that contains a list of scripts.
2. With the scripts array, use [`jq`](https://stedolan.github.io/jq/) to find the first **invocable script** object that has a `script` property that contains a hard-coded numeric date range.
3. Replace the hard-coded date range in the `script` property with a new `params.myrangestart` dynamic parameter and assign the object to a `new_script` variable.
4. Assign the script ID to a `script_id` variable.
5. Update the script by sending a request to `PATCH /api/v2/scripts/` with `$script_id` in the URL path and `$new_script` as data (in the request body).
InfluxDB returns the updated invocable script.
## Delete an invocable script
To delete a script, send a request using the `DELETE` method to the `/api/v2/scripts/SCRIPT_ID` InfluxDB API endpoint.
Replace *`SCRIPT_ID`* with the ID of the script you want to update.
[{{< api-endpoint method="delete" endpoint="https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID" >}}](/influxdb/cloud/api/#operation/DeleteScriptsID)
Provide the following in your request:
#### Request headers
- **Authorization**: Token *`INFLUX_API_TOKEN`*
- **Accept**: application/json'

View File

@ -129,7 +129,7 @@ Provide the following in your API request:
##### Request headers
- **Content-Type**: application/json
- **Authorization**: Token _YOURINFLUXDBTOKEN_**
- **Authorization**: Token *`YOUR_INFLUXDB_TOKEN`*
##### Request body
JSON object with the following fields:
@ -142,11 +142,16 @@ JSON object with the following fields:
```sh
curl --request POST 'https://us-west-2-1.aws.cloud2.influxdata.com/api/v2/tasks' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token <YOURTOKEN>' \
--header 'Authorization: Token INFLUX_TOKEN' \
--data-raw '{
"flux": "option task = {name: \"CPU Total 1 Hour New\", every: 1h}\n\nfrom(bucket: \"telegraf\")\n\t|> range(start: -1h)\n\t|> filter(fn: (r) =>\n\t\t(r._measurement == \"cpu\"))\n\t|> filter(fn: (r) =>\n\t\t(r._field == \"usage_system\"))\n\t|> filter(fn: (r) =>\n\t\t(r.cpu == \"cpu-total\"))\n\t|> aggregateWindow(every: 1h, fn: max)\n\t|> to(bucket: \"cpu_usage_user_total_1h\", org: \"<MYORG>\")",
"orgID": "<YOURORGID>",
"flux": "option task = {name: \"CPU Total 1 Hour New\", every: 1h}\n\nfrom(bucket: \"telegraf\")\n\t|> range(start: -1h)\n\t|> filter(fn: (r) =>\n\t\t(r._measurement == \"cpu\"))\n\t|> filter(fn: (r) =>\n\t\t(r._field == \"usage_system\"))\n\t|> filter(fn: (r) =>\n\t\t(r.cpu == \"cpu-total\"))\n\t|> aggregateWindow(every: 1h, fn: max)\n\t|> to(bucket: \"cpu_usage_user_total_1h\", org: \"INFLUX_ORG\")",
"orgID": "INFLUX_ORG_ID",
"status": "active",
"description": "This task downsamples CPU data every hour"
}'
```
Replace the following:
- *`INFLUX_API_TOKEN`*: your [InfluxDB API token](/influxdb/v2.0/security/tokens/)
- *`INFLUX_ORG`*: your [InfluxDB organization name](influxdb/2.0/organizations/view-orgs/)
- *`INFLUX_ORG_ID`*: your [InfluxDB organization ID](/influxdb/v2.0/organizations/view-orgs/#view-your-organization-id)

View File

@ -0,0 +1,16 @@
curl -X 'POST' \
"http://cloud2.influxdata.com/api/v2/scripts" \
--header "Authorization: Token ${INFLUX_API_TOKEN}" \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--data-binary @- << EOF | jq .
{
"name": "getLastPoint",
"description": "getLastPoint finds the last point in a bucket",
"orgID": "${INFLUX_ORG_ID}",
"script": "from(bucket: params.mybucket) \
|> range(start: -7d) \
|> limit(n:1)",
"language": "flux"
}
EOF

View File

@ -0,0 +1,5 @@
curl -X 'GET' \
"${INFLUX_URL}/api/v2/scripts/${SCRIPT_ID}" \
--header "Authorization: Token ${INFLUX_API_TOKEN}" \
--header 'accept: application/json' \
--header 'Content-Type: application/json'

View File

@ -0,0 +1,8 @@
SCRIPT_ID=085138a111448000
curl -X 'POST' \
"${INFLUX_URL}/api/v2/scripts/${SCRIPT_ID}/invoke" \
--header "Authorization: Token ${INFLUX_TOKEN}" \
--header 'Accept: application/csv' \
--header 'Content-Type: application/json' \
--data-binary '{ "params": { "mybucket": "air_sensor" } }'

View File

@ -0,0 +1,6 @@
curl -X 'GET' \
"${INFLUX_URL}/api/v2/scripts" \
--header "Authorization: Token ${INFLUX_API_TOKEN}" \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--data-urlencode 'org=jstirnamaninflux&limit=10'

View File

@ -0,0 +1,21 @@
find_and_update() {
script=$(curl -X 'GET' \
"http://cloud2.influxdata.com/api/v2/scripts" \
--header "Authorization: Token ${INFLUX_API_TOKEN}" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data-urlencode 'org=jstirnamaninflux&limit=10' \
| jq '[.scripts[] | select(.script | test("start: -?\\d\\w"))]' \
| jq '.[0]')
new_script=$(jq '.script |= sub("start: .*d"; "start: params.myrangestart")' <<< "${script}")
script_id=$(jq -r '.id' <<< "${new_script}")
curl -X 'PATCH' \
"${INFLUX_URL}/api/v2/scripts/${script_id}" \
--header "Authorization: Token ${INFLUX_API_TOKEN}" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data "${new_script}" | jq .
}
find_and_update