Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,5 @@ jobs:
ELASTICSEARCH_USERNAME: "elastic"
ELASTICSEARCH_PASSWORD: ${{ env.ELASTIC_PASSWORD }}
KIBANA_ENDPOINT: "http://localhost:5601"
KIBANA_API_KEY: ${{ steps.get-api-key.outputs.apikey }}
KIBANA_USERNAME: "elastic"
KIBANA_PASSWORD: ${{ env.ELASTIC_PASSWORD }}
54 changes: 54 additions & 0 deletions docs/resources/kibana_synthetics_parameter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
subcategory: "Kibana"
layout: ""
page_title: "Elasticstack: elasticstack_kibana_synthetics_parameter Resource"
description: |-
Creates or updates a Kibana synthetics parameter.
---

# Resource: elasticstack_kibana_synthetics_parameter

Creates or updates a Kibana synthetics parameter.
See [Working with secrets and sensitive values](https://www.elastic.co/docs/solutions/observability/synthetics/work-with-params-secrets)
and [API docs](https://www.elastic.co/docs/api/doc/kibana/group/endpoint-synthetics)

## Example Usage

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

resource "elasticstack_kibana_synthetics_parameter" "example" {
key = "example_key"
value = "example_value"
description = "Example description"
tags = ["tag-a", "tag-b"]
}
```

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

### Required

- `key` (String) The key of the parameter.
- `value` (String, Sensitive) The value associated with the parameter.

### Optional

- `description` (String) A description of the parameter.
- `share_across_spaces` (Boolean) Whether the parameter should be shared across spaces.
- `tags` (List of String) An array of tags to categorize the parameter.

### Read-Only

- `id` (String) Generated id for the parameter.

## Import

Import is supported using the following syntax:

```shell
terraform import elasticstack_kibana_synthetics_parameter.my_param <space id>/<param_id>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import elasticstack_kibana_synthetics_parameter.my_param <space id>/<param_id>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
provider "elasticstack" {
kibana {}
}

resource "elasticstack_kibana_synthetics_parameter" "example" {
key = "example_key"
value = "example_value"
description = "Example description"
tags = ["tag-a", "tag-b"]
}
49 changes: 49 additions & 0 deletions internal/kibana/synthetics/parameter/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package parameter

import (
"context"
"fmt"

"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
kibanaClient := synthetics.GetKibanaClient(r, response.Diagnostics)
if kibanaClient == nil {
return
}

var plan tfModelV0
diags := request.Plan.Get(ctx, &plan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

input := plan.toParameterConfig(false)

result, err := kibanaClient.KibanaSynthetics.Parameter.Add(ctx, input)
if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("Failed to create parameter `%s`", input.Key), err.Error())
return
}

resourceId := result.Id

// We can't trust the response from the POST request. At least with Kibana
// 9.0.0, it responds without the `value` field set.
result, err = kibanaClient.KibanaSynthetics.Parameter.Get(ctx, resourceId)
if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("Failed to get parameter after creation `%s`", resourceId), err.Error())
return
}

plan = toModelV0(*result)

diags = response.State.Set(ctx, plan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
}
41 changes: 41 additions & 0 deletions internal/kibana/synthetics/parameter/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package parameter

import (
"context"
"fmt"

"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
kibanaClient := synthetics.GetKibanaClient(r, response.Diagnostics)
if kibanaClient == nil {
return
}

var plan tfModelV0
diags := request.State.Get(ctx, &plan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

resourceId := plan.ID.ValueString()

compositeId, dg := tryReadCompositeId(resourceId)
response.Diagnostics.Append(dg...)
if response.Diagnostics.HasError() {
return
}

if compositeId != nil {
resourceId = compositeId.ResourceId
}

_, err := kibanaClient.KibanaSynthetics.Parameter.Delete(ctx, resourceId)
if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("Failed to delete parameter `%s`", resourceId), err.Error())
return
}
}
58 changes: 58 additions & 0 deletions internal/kibana/synthetics/parameter/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package parameter

import (
"context"
"errors"
"fmt"

"github.com/disaster37/go-kibana-rest/v8/kbapi"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
kibanaClient := synthetics.GetKibanaClient(r, response.Diagnostics)
if kibanaClient == nil {
return
}

var state tfModelV0
diags := request.State.Get(ctx, &state)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

resourceId := state.ID.ValueString()

compositeId, dg := tryReadCompositeId(resourceId)
response.Diagnostics.Append(dg...)
if response.Diagnostics.HasError() {
return
}

if compositeId != nil {
resourceId = compositeId.ResourceId
}

result, err := kibanaClient.KibanaSynthetics.Parameter.Get(ctx, resourceId)
if err != nil {
var apiError *kbapi.APIError
if errors.As(err, &apiError) && apiError.Code == 404 {
response.State.RemoveResource(ctx)
return
}

response.Diagnostics.AddError(fmt.Sprintf("Failed to get parameter `%s`", resourceId), err.Error())
return
}

state = toModelV0(*result)

// Set refreshed state
diags = response.State.Set(ctx, &state)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
}
45 changes: 45 additions & 0 deletions internal/kibana/synthetics/parameter/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package parameter

import (
"context"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

const resourceName = synthetics.MetadataPrefix + "parameter"

// Ensure provider defined types fully satisfy framework interfaces
var _ resource.Resource = &Resource{}
var _ resource.ResourceWithConfigure = &Resource{}
var _ resource.ResourceWithImportState = &Resource{}
var _ synthetics.ESApiClient = &Resource{}

type Resource struct {
client *clients.ApiClient
synthetics.ESApiClient
}

func (r *Resource) GetClient() *clients.ApiClient {
return r.client
}

func (r *Resource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = parameterSchema()
}

func (r *Resource) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), request, response)
}

func (r *Resource) Configure(ctx context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) {
client, diags := clients.ConvertProviderData(request.ProviderData)
response.Diagnostics.Append(diags...)
r.client = client
}

func (r *Resource) Metadata(ctx context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
response.TypeName = request.ProviderTypeName + resourceName
}
89 changes: 89 additions & 0 deletions internal/kibana/synthetics/parameter/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package parameter_test

import (
"testing"

"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
"github.com/elastic/terraform-provider-elasticstack/internal/versionutils"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
providerConfig = `
provider "elasticstack" {
kibana {}
}
`
)

var (
minKibanaParameterAPIVersion = version.Must(version.NewVersion("8.12.0"))
)

func TestSyntheticParameterResource(t *testing.T) {
resourceId := "elasticstack_kibana_synthetics_parameter.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.Providers,
Steps: []resource.TestStep{
// Create and Read testing
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaParameterAPIVersion),
Config: providerConfig + `
resource "elasticstack_kibana_synthetics_parameter" "test" {
key = "test-key"
value = "test-value"
description = "Test description"
tags = ["a", "b"]
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceId, "key", "test-key"),
resource.TestCheckResourceAttr(resourceId, "value", "test-value"),
resource.TestCheckResourceAttr(resourceId, "description", "Test description"),
resource.TestCheckResourceAttr(resourceId, "tags.#", "2"),
resource.TestCheckResourceAttr(resourceId, "tags.0", "a"),
resource.TestCheckResourceAttr(resourceId, "tags.1", "b"),
),
},
// ImportState testing
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaParameterAPIVersion),
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
Config: providerConfig + `
resource "elasticstack_kibana_synthetics_parameter" "test" {
key = "test-key"
value = "test-value"
description = "Test description"
tags = ["a", "b"]
}
`,
},
// Update and Read testing
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaParameterAPIVersion),
Config: providerConfig + `
resource "elasticstack_kibana_synthetics_parameter" "test" {
key = "test-key-2"
value = "test-value-2"
description = "Test description 2"
tags = ["c", "d", "e"]
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceId, "key", "test-key-2"),
resource.TestCheckResourceAttr(resourceId, "value", "test-value-2"),
resource.TestCheckResourceAttr(resourceId, "description", "Test description 2"),
resource.TestCheckResourceAttr(resourceId, "tags.#", "3"),
resource.TestCheckResourceAttr(resourceId, "tags.0", "c"),
resource.TestCheckResourceAttr(resourceId, "tags.1", "d"),
resource.TestCheckResourceAttr(resourceId, "tags.2", "e"),
),
},
// Delete testing automatically occurs in TestCase
},
})
}
Loading
Loading