Skip to content

Add Fleet output and server host #327

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

Merged
merged 1 commit into from
May 17, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- 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)])
- Add `elasticstack_fleet_output` and `elasticstack_fleet_server_host` for managing Fleet outputs and server hosts ([#327](https://github.com./elastic/terraform-provider-elasticstack/pull/327)])

### 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
61 changes: 61 additions & 0 deletions docs/resources/fleet_output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
subcategory: "Fleet"
layout: ""
page_title: "Elasticstack: elasticstack_fleet_output Resource"
description: |-
Creates or updates a Fleet Output.
---

# Resource: elasticstack_fleet_output

Creates or updates a Fleet Output.

## Example Usage

```terraform
provider "elasticstack" {
kibana {}
}

resource "elasticstack_fleet_output" "test_output" {
name = "Test Output"
type = "elasticsearch"
config_yaml = yamlencode({
"ssl.verification_mode" : "none"
})
default_integrations = false
default_monitoring = false
hosts = [
"https://elasticsearch:9200"
]
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The name of the output.
- `type` (String) The output type.

### Optional

- `ca_sha256` (String) Fingerprint of the Elasticsearch CA certificate.
- `config_yaml` (String, Sensitive) Advanced YAML configuration. YAML settings here will be added to the output section of each agent policy.
- `default_integrations` (Boolean) Make this output the default for agent integrations.
- `default_monitoring` (Boolean) Make this output the default for agent monitoring.
- `hosts` (List of String) A list of hosts.
- `output_id` (String) Unique identifier of the output.

### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
terraform import elasticstack_fleet_output.my_output <space id>/<output id>
```
52 changes: 52 additions & 0 deletions docs/resources/fleet_server_host.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
subcategory: "Fleet"
layout: ""
page_title: "Elasticstack: elasticstack_fleet_server_host Resource"
description: |-
Creates or updates a Fleet Server Host.
---

# Resource: elasticstack_fleet_server_host

Creates or updates a Fleet Server Host.

## Example Usage

```terraform
provider "elasticstack" {
kibana {}
}

resource "elasticstack_fleet_server_host" "test_host" {
name = "Test Host"
default = false
hosts = [
"https://fleet-server:8220"
]
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `hosts` (List of String) A list of hosts.
- `name` (String) The name of the Fleet server host.

### Optional

- `default` (Boolean) Set as default.
- `host_id` (String) Unique identifier of the Fleet server host.

### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
terraform import elasticstack_fleet_server_host.my_host <space id>/<host id>
```
1 change: 1 addition & 0 deletions examples/resources/elasticstack_fleet_output/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import elasticstack_fleet_output.my_output <space id>/<output id>
16 changes: 16 additions & 0 deletions examples/resources/elasticstack_fleet_output/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
provider "elasticstack" {
kibana {}
}

resource "elasticstack_fleet_output" "test_output" {
name = "Test Output"
type = "elasticsearch"
config_yaml = yamlencode({
"ssl.verification_mode" : "none"
})
default_integrations = false
default_monitoring = false
hosts = [
"https://elasticsearch:9200"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import elasticstack_fleet_server_host.my_host <space id>/<host id>
11 changes: 11 additions & 0 deletions examples/resources/elasticstack_fleet_server_host/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
provider "elasticstack" {
kibana {}
}

resource "elasticstack_fleet_server_host" "test_host" {
name = "Test Host"
default = false
hosts = [
"https://fleet-server:8220"
]
}
128 changes: 128 additions & 0 deletions internal/clients/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,134 @@ func DeleteAgentPolicy(ctx context.Context, client *Client, id string) diag.Diag
}
}

// ReadOutput reads a specific output from the API.
func ReadOutput(ctx context.Context, client *Client, id string) (*fleetapi.Output, diag.Diagnostics) {
resp, err := client.API.GetOutputWithResponse(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)
}
}

// CreateOutput creates a new output.
func CreateOutput(ctx context.Context, client *Client, req fleetapi.PostOutputsJSONRequestBody) (*fleetapi.Output, diag.Diagnostics) {
resp, err := client.API.PostOutputsWithResponse(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)
}
}

// UpdateOutput updates an existing output.
func UpdateOutput(ctx context.Context, client *Client, id string, req fleetapi.UpdateOutputJSONRequestBody) (*fleetapi.Output, diag.Diagnostics) {
resp, err := client.API.UpdateOutputWithResponse(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)
}
}

// DeleteOutput deletes an existing output
func DeleteOutput(ctx context.Context, client *Client, id string) diag.Diagnostics {
resp, err := client.API.DeleteOutputWithResponse(ctx, id)
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)
}
}

// ReadFleetServerHost reads a specific fleet server host from the API.
func ReadFleetServerHost(ctx context.Context, client *Client, id string) (*fleetapi.FleetServerHost, diag.Diagnostics) {
resp, err := client.API.GetOneFleetServerHostsWithResponse(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)
}
}

// CreateFleetServerHost creates a new fleet server host.
func CreateFleetServerHost(ctx context.Context, client *Client, req fleetapi.PostFleetServerHostsJSONRequestBody) (*fleetapi.FleetServerHost, diag.Diagnostics) {
resp, err := client.API.PostFleetServerHostsWithResponse(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)
}
}

// UpdateFleetServerHost updates an existing fleet server host.
func UpdateFleetServerHost(ctx context.Context, client *Client, id string, req fleetapi.UpdateFleetServerHostsJSONRequestBody) (*fleetapi.FleetServerHost, diag.Diagnostics) {
resp, err := client.API.UpdateFleetServerHostsWithResponse(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)
}
}

// DeleteFleetServerHost deletes an existing fleet server host.
func DeleteFleetServerHost(ctx context.Context, client *Client, id string) diag.Diagnostics {
resp, err := client.API.DeleteFleetServerHostsWithResponse(ctx, id)
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{
Expand Down
Loading