-
Notifications
You must be signed in to change notification settings - Fork 122
Add support for synthetics global parameters #1155
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
+1,436
−3
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f147fb
Add support for synthetics global parameters
agunnerson-elastic 13df2f0
synthetics/parameter: Remove duplicate code when rereading in create/…
agunnerson-elastic c7d42a3
synthetics/parameter: Switch to using OAPI client
agunnerson-elastic 17bbace
Merge remote-tracking branch 'origin/main' into synthetics-params
tobio 0c81847
Merge remote-tracking branch 'origin/main' into synthetics-params
tobio 49c82dd
Changelog
tobio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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> | ||
``` |
1 change: 1 addition & 0 deletions
1
examples/resources/elasticstack_kibana_synthetics_parameter/import.sh
This file contains hidden or 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_synthetics_parameter.my_param <space id>/<param_id> |
10 changes: 10 additions & 0 deletions
10
examples/resources/elasticstack_kibana_synthetics_parameter/resource.tf
This file contains hidden or 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,10 @@ | ||
provider "elasticstack" { | ||
kibana {} | ||
} | ||
|
||
resource "elasticstack_kibana_synthetics_parameter" "example" { | ||
key = "example_key" | ||
value = "example_value" | ||
description = "Example description" | ||
tags = ["tag-a", "tag-b"] | ||
} |
This file contains hidden or 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,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 | ||
} | ||
} |
This file contains hidden or 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,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 | ||
} | ||
} |
This file contains hidden or 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,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 | ||
} | ||
agunnerson-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} |
This file contains hidden or 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,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 | ||
} |
This file contains hidden or 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,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 | ||
}, | ||
}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.