Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated docs around new label(s) validation #24049

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/fleetctl/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func queryCommand() *cli.Command {
Name: "query",
Usage: "Run a live query",
UsageText: `fleetctl query [options]`,
Description: `Runs the specified query as a live query on the specified targets.

Using the --hosts flag individual hosts can be specified with the host's hostname. Groups of hosts can
specified by using labels. Note if both the --hosts and --labels flags are specified, the query will
be run on the union of the hosts and hosts with matching labels.
`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "hosts",
Expand All @@ -37,7 +43,7 @@ func queryCommand() *cli.Command {
EnvVars: []string{"LABELS"},
Value: "",
Destination: &flLabels,
Usage: "Comma-separated label names to target",
Usage: "Comma-separated label names to target. Hosts with any of the labels will be targeted.",
},
&cli.BoolFlag{
Name: "quiet",
Expand Down
37 changes: 35 additions & 2 deletions docs/Contributing/API-for-contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2371,15 +2371,15 @@ Runs the specified saved query as a live query on the specified targets. Returns

After the query has been initiated, [get results via WebSocket](#retrieve-live-query-results-standard-websocket-api).

`POST /api/v1/fleet/queries/run_by_names`
`POST /api/v1/fleet/queries/run_by_identifiers`

#### Parameters

| Name | Type | In | Description |
| -------- | ------- | ---- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| query | string | body | The SQL of the query. |
| query_id | integer | body | The saved query (if any) that will be run. The `observer_can_run` property on the query effects which targets are included. |
| selected | object | body | **Required.** The object includes lists of selected hostnames (`selected.hosts`), label names (`labels`). When provided, builtin label names and custom label names become `AND` filters. Within each selector, selecting two or more builtin labels, or two or more custom labels, behave as `OR` filters. There's one special case for the builtin label `"All hosts"`, if such label is selected, then all other label and team selectors are ignored (and all hosts will be selected). If a host's hostname is explicitly included in `selected.hosts`, then it is assured that the query will be selected to run on it (no matter the contents of `selected.labels`). See examples below. |
| selected | object | body | **Required.** The object includes lists of selected hostnames (`selected.hosts`), label names (`labels`). When provided, builtin label names and custom label names become `AND` filters. Within each selector, selecting two or more builtin labels, or two or more custom labels, behave as `OR` filters. If a label provided could not be found in the database, a 400 bad request will be returned specifying which label is invalid. There's one special case for the builtin label `"All hosts"`, if such label is selected, then all other label and team selectors are ignored (and all hosts will be selected). If a host's hostname is explicitly included in `selected.hosts`, then it is assured that the query will be selected to run on it (no matter the contents of `selected.labels`). See examples below. |

One of `query` and `query_id` must be specified.

Expand Down Expand Up @@ -2461,6 +2461,39 @@ One of `query` and `query_id` must be specified.
}
```

#### Example with invalid label

`POST /api/v1/fleet/queries/run_by_names`

##### Request body

```json
{
"query": "SELECT instance_id FROM system_info",
"selected": {
"labels": ["Windows", "Banana", "Apple"]
}
}
```

##### Default response

`Status: 400`

```json
{
"message": "Bad request",
"errors": [
{
"name": "base",
"reason": "Invalid label name(s): Banana, Apple."
}
],
"uuid": "303649f4-5e45-4379-bae9-64ec0ef56287"
}
```


### Retrieve live query results (standard WebSocket API)

You can retrieve the results of a live query using the [standard WebSocket API](#https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications).
Expand Down
Loading