Skip to content

Commit

Permalink
Add Fleet agent policy and enrollment tokens (#322)
Browse files Browse the repository at this point in the history
* 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
taylor-swanson authored May 16, 2023
1 parent d4d6f6d commit 60113b1
Show file tree
Hide file tree
Showing 21 changed files with 1,822 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Add `elasticstack_elasticsearch_watch` for managing Elasticsearch Watches ([#155](https://github.com/elastic/terraform-provider-elasticstack/pull/155))
- Add `elasticstack_kibana_alerting_rule` for managing Kibana alerting rules ([#292](https://github.com/elastic/terraform-provider-elasticstack/pull/292))
- Add client for communicating with the Fleet APIs ([#311](https://github.com/elastic/terraform-provider-elasticstack/pull/311)])
- Add `elasticstack_fleet_enrollment_tokens` and `elasticstack_fleet_agent_policy` for managing Fleet enrollment tokens and agent policies ([#322](https://github.com/elastic/terraform-provider-elasticstack/pull/322)])

### Fixed
- Updated unsupported queue_max_bytes_number and queue_max_bytes_units with queue.max_bytes ([#266](https://github.com/elastic/terraform-provider-elasticstack/issues/266))
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SWAGGER_VERSION ?= 8.7

GOVERSION ?= 1.19

STACK_VERSION ?= 8.0.0
STACK_VERSION ?= 8.6.0

ELASTICSEARCH_NAME ?= terraform-elasticstack-es
ELASTICSEARCH_ENDPOINTS ?= http://$(ELASTICSEARCH_NAME):9200
Expand Down
48 changes: 48 additions & 0 deletions docs/data-sources/fleet_enrollment_tokens.md
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)
60 changes: 60 additions & 0 deletions docs/resources/fleet_agent_policy.md
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>
```
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"
}
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 examples/resources/elasticstack_fleet_agent_policy/resource.tf
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
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/elastic/terraform-provider-elasticstack
go 1.19

require (
github.com/deepmap/oapi-codegen v1.12.4
github.com/disaster37/go-kibana-rest/v8 v8.5.0
github.com/elastic/go-elasticsearch/v7 v7.17.7
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
Expand All @@ -16,12 +17,14 @@ require (

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-resty/resty/v2 v2.7.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@ github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo=
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deepmap/oapi-codegen v1.12.4 h1:pPmn6qI9MuOtCz82WY2Xaw46EQjgvxednXXrP7g5Q2s=
github.com/deepmap/oapi-codegen v1.12.4/go.mod h1:3lgHGMu6myQ2vqbbTXH2H1o4eXFTGnFiDaOaKKl5yas=
github.com/disaster37/go-kibana-rest/v8 v8.5.0 h1:Wr2CMopHj0wAh7kq3p355jXctISL1LdAZ9kYN/v6abw=
github.com/disaster37/go-kibana-rest/v8 v8.5.0/go.mod h1:wVGm1b93CSmsHYoxzqhDszv347xr+fELdigS8XLO+bg=
github.com/elastic/go-elasticsearch/v7 v7.17.7 h1:pcYNfITNPusl+cLwLN6OLmVT+F73Els0nbaWOmYachs=
Expand Down Expand Up @@ -55,6 +61,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -110,6 +118,7 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -163,11 +172,13 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
101 changes: 101 additions & 0 deletions internal/clients/fleet/fleet.go
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),
},
}
}
Loading

0 comments on commit 60113b1

Please sign in to comment.