-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Fleet agent policy and enrollment tokens (#322)
* Add Fleet agent policy and enrollment tokens - Add Fleet agent policy resource type - Add Fleet enrollment token data source type - Add new types to Fleet generator - Add missing requestBody field in Fleet OpenAPI struct * Update changelog * Fix up acc tests * Raise acc test minimum versions to 8.6.0 - While the stack supports these endpoints at earlier versions, there are fields missing in these versions that cause 400 errors. * review comments * Improve enrollment tokens test, regen docs
- Loading branch information
1 parent
d4d6f6d
commit 60113b1
Showing
21 changed files
with
1,822 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
subcategory: "Fleet" | ||
layout: "" | ||
page_title: "Elasticstack: elasticstack_fleet_enrollment_tokens Data Source" | ||
description: |- | ||
Gets information about Fleet Enrollment Tokens. See https://www.elastic.co/guide/en/fleet/current/fleet-enrollment-tokens.html | ||
--- | ||
|
||
# Data Source: elasticstack_fleet_enrollment_tokens | ||
|
||
This data source provides information about Fleet Enrollment Tokens. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
provider "elasticstack" { | ||
kibana {} | ||
} | ||
data "elasticstack_fleet_enrollment_tokens" "test" { | ||
policy_id = "223b1bf8-240f-463f-8466-5062670d0754" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Optional | ||
|
||
- `policy_id` (String) The identifier of the target agent policy. When provided, only the enrollment tokens associated with this agent policy will be selected. Omit this value to select all enrollment tokens. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `tokens` (List of Object) A list of enrollment tokens. (see [below for nested schema](#nestedatt--tokens)) | ||
|
||
<a id="nestedatt--tokens"></a> | ||
### Nested Schema for `tokens` | ||
|
||
Read-Only: | ||
|
||
- `active` (Boolean) | ||
- `api_key` (String) | ||
- `api_key_id` (String) | ||
- `created_at` (String) | ||
- `key_id` (String) | ||
- `name` (String) | ||
- `policy_id` (String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
subcategory: "Fleet" | ||
layout: "" | ||
page_title: "Elasticstack: elasticstack_fleet_agent_policy Resource" | ||
description: |- | ||
Creates or updates a Fleet Agent Policy. | ||
--- | ||
|
||
# Resource: elasticstack_fleet_agent_policy | ||
|
||
Creates or updates a Fleet Agent Policy. See https://www.elastic.co/guide/en/fleet/current/fleet-api-docs.html#create-agent-policy-api | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
provider "elasticstack" { | ||
kibana {} | ||
} | ||
resource "elasticstack_fleet_agent_policy" "test_policy" { | ||
name = "Test Policy" | ||
namespace = "default" | ||
description = "Test Agent Policy" | ||
sys_monitoring = true | ||
monitor_logs = true | ||
monitor_metrics = true | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the agent policy. | ||
- `namespace` (String) The namespace of the agent policy. | ||
|
||
### Optional | ||
|
||
- `data_output_id` (String) The identifier for the data output. | ||
- `description` (String) The description of the agent policy. | ||
- `download_source_id` (String) The identifier for the Elastic Agent binary download server. | ||
- `fleet_server_host_id` (String) The identifier for the Fleet server host. | ||
- `monitor_logs` (Boolean) Enable collection of agent logs. | ||
- `monitor_metrics` (Boolean) Enable collection of agent metrics. | ||
- `monitoring_output_id` (String) The identifier for monitoring output. | ||
- `policy_id` (String) Unique identifier of the agent policy. | ||
- `sys_monitoring` (Boolean) Enable collection of system logs and metrics. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
terraform import elasticstack_kibana_fleet_agent_policy.my_policy <space id>/<policy id> | ||
``` |
7 changes: 7 additions & 0 deletions
7
examples/data-sources/elasticstack_fleet_enrollment_tokens/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
provider "elasticstack" { | ||
kibana {} | ||
} | ||
|
||
data "elasticstack_fleet_enrollment_tokens" "test" { | ||
policy_id = "223b1bf8-240f-463f-8466-5062670d0754" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
terraform import elasticstack_kibana_fleet_agent_policy.my_policy <space id>/<policy id> |
12 changes: 12 additions & 0 deletions
12
examples/resources/elasticstack_fleet_agent_policy/resource.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
provider "elasticstack" { | ||
kibana {} | ||
} | ||
|
||
resource "elasticstack_fleet_agent_policy" "test_policy" { | ||
name = "Test Policy" | ||
namespace = "default" | ||
description = "Test Agent Policy" | ||
sys_monitoring = true | ||
monitor_logs = true | ||
monitor_metrics = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package fleet | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet/fleetapi" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
) | ||
|
||
// AllEnrollmentTokens reads all enrollment tokens from the API. | ||
func AllEnrollmentTokens(ctx context.Context, client *Client) ([]fleetapi.EnrollmentApiKey, diag.Diagnostics) { | ||
resp, err := client.API.GetEnrollmentApiKeysWithResponse(ctx) | ||
if err != nil { | ||
return nil, diag.FromErr(err) | ||
} | ||
|
||
if resp.StatusCode() == http.StatusOK { | ||
return resp.JSON200.Items, nil | ||
} | ||
return nil, reportUnknownError(resp.StatusCode(), resp.Body) | ||
} | ||
|
||
// ReadAgentPolicy reads a specific agent policy from the API. | ||
func ReadAgentPolicy(ctx context.Context, client *Client, id string) (*fleetapi.AgentPolicy, diag.Diagnostics) { | ||
resp, err := client.API.AgentPolicyInfoWithResponse(ctx, id) | ||
if err != nil { | ||
return nil, diag.FromErr(err) | ||
} | ||
|
||
switch resp.StatusCode() { | ||
case http.StatusOK: | ||
return &resp.JSON200.Item, nil | ||
case http.StatusNotFound: | ||
return nil, nil | ||
default: | ||
return nil, reportUnknownError(resp.StatusCode(), resp.Body) | ||
} | ||
} | ||
|
||
// CreateAgentPolicy creates a new agent policy. | ||
func CreateAgentPolicy(ctx context.Context, client *Client, req fleetapi.AgentPolicyCreateRequest) (*fleetapi.AgentPolicy, diag.Diagnostics) { | ||
resp, err := client.API.CreateAgentPolicyWithResponse(ctx, req) | ||
if err != nil { | ||
return nil, diag.FromErr(err) | ||
} | ||
|
||
switch resp.StatusCode() { | ||
case http.StatusOK: | ||
return resp.JSON200.Item, nil | ||
default: | ||
return nil, reportUnknownError(resp.StatusCode(), resp.Body) | ||
} | ||
} | ||
|
||
// UpdateAgentPolicy updates an existing agent policy. | ||
func UpdateAgentPolicy(ctx context.Context, client *Client, id string, req fleetapi.AgentPolicyUpdateRequest) (*fleetapi.AgentPolicy, diag.Diagnostics) { | ||
resp, err := client.API.UpdateAgentPolicyWithResponse(ctx, id, req) | ||
if err != nil { | ||
return nil, diag.FromErr(err) | ||
} | ||
|
||
switch resp.StatusCode() { | ||
case http.StatusOK: | ||
return &resp.JSON200.Item, nil | ||
default: | ||
return nil, reportUnknownError(resp.StatusCode(), resp.Body) | ||
} | ||
} | ||
|
||
// DeleteAgentPolicy deletes an existing agent policy | ||
func DeleteAgentPolicy(ctx context.Context, client *Client, id string) diag.Diagnostics { | ||
body := fleetapi.DeleteAgentPolicyJSONRequestBody{ | ||
AgentPolicyId: id, | ||
} | ||
|
||
resp, err := client.API.DeleteAgentPolicyWithResponse(ctx, body) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
switch resp.StatusCode() { | ||
case http.StatusOK: | ||
return nil | ||
case http.StatusNotFound: | ||
return nil | ||
default: | ||
return reportUnknownError(resp.StatusCode(), resp.Body) | ||
} | ||
} | ||
|
||
func reportUnknownError(statusCode int, body []byte) diag.Diagnostics { | ||
return diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: fmt.Sprintf("Unexpected status code from server: got HTTP %d", statusCode), | ||
Detail: string(body), | ||
}, | ||
} | ||
} |
Oops, something went wrong.