diff --git a/.changelog/797.txt b/.changelog/797.txt new file mode 100644 index 000000000..39e1f9ee7 --- /dev/null +++ b/.changelog/797.txt @@ -0,0 +1,3 @@ +```release-note:feature +datasource/deployments: Adds additional parameter `name` to allow searching by exact deployment name. +``` diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a02ab143f..1ee21cff1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -78,8 +78,8 @@ Closes #1234 ### Environment prerequisites -- [Terraform](https://www.terraform.io/downloads.html) 0.13+ -- [Go](https://golang.org/doc/install) 1.13 +- [Terraform](https://www.terraform.io/downloads.html) At least 1.2.7 +- [Go](https://golang.org/doc/install) 1.21 This project uses [Go Modules](https://blog.golang.org/using-go-modules) making it safe to work with it outside of your existing [GOPATH](http://golang.org/doc/code.html#GOPATH). Running `make vendor` will download all the required dependencies. diff --git a/docs/data-sources/deployments.md b/docs/data-sources/deployments.md index de213cb7b..32520fd51 100644 --- a/docs/data-sources/deployments.md +++ b/docs/data-sources/deployments.md @@ -51,6 +51,7 @@ data "ec_deployments" "example" { - `healthy` (String) Filter the result set by their health status. - `integrations_server` (Block List) Filter by Integrations Server resource kind status or configuration. (see [below for nested schema](#nestedblock--integrations_server)) - `kibana` (Block List) Filter by Kibana resource kind status or configuration. (see [below for nested schema](#nestedblock--kibana)) +- `name` (String) Filter the result by the full deployment name. - `name_prefix` (String) Prefix to filter the returned deployment list by. - `size` (Number) The maximum number of deployments to return. Defaults to `100`. - `tags` (Map of String) Filter the result set by their assigned tags. diff --git a/docs/resources/deployment.md b/docs/resources/deployment.md index a6ded1e0b..efd6fb9b0 100644 --- a/docs/resources/deployment.md +++ b/docs/resources/deployment.md @@ -919,6 +919,8 @@ Optional: - `apm` (String) - `fleet` (String) +- `profiling` (String) +- `symbols` (String) diff --git a/ec/acc/datasource_deployment_basic_test.go b/ec/acc/datasource_deployment_basic_test.go index 0d6682f4a..959c07866 100644 --- a/ec/acc/datasource_deployment_basic_test.go +++ b/ec/acc/datasource_deployment_basic_test.go @@ -186,6 +186,9 @@ func TestAccDatasourceDeployment_basic(t *testing.T) { resource.TestCheckResourceAttrPair(depsDatasourceName, "deployments.0.kibana_ref_id", resourceName, "kibana.ref_id"), resource.TestCheckResourceAttrPair(depsDatasourceName, "deployments.0.apm_ref_id", resourceName, "apm.ref_id"), resource.TestCheckResourceAttrPair(depsDatasourceName, "deployments.0.enterprise_search_ref_id", resourceName, "enterprise_search.ref_id"), + + // Query by name + resource.TestCheckResourceAttrPair("data.ec_deployments.name_query", "deployments.0.name", resourceName, "name"), ), }, }, diff --git a/ec/acc/testdata/datasource_deployment_basic.tf b/ec/acc/testdata/datasource_deployment_basic.tf index e80fa8cd6..e4078485f 100644 --- a/ec/acc/testdata/datasource_deployment_basic.tf +++ b/ec/acc/testdata/datasource_deployment_basic.tf @@ -87,3 +87,11 @@ data "ec_deployments" "query" { ec_deployment.basic_datasource, ] } + +data "ec_deployments" "name_query" { + name = ec_deployment.basic_datasource.name + + depends_on = [ + ec_deployment.basic_datasource, + ] +} diff --git a/ec/ecdatasource/deploymentsdatasource/datasource.go b/ec/ecdatasource/deploymentsdatasource/datasource.go index d75669d74..8f01abf54 100644 --- a/ec/ecdatasource/deploymentsdatasource/datasource.go +++ b/ec/ecdatasource/deploymentsdatasource/datasource.go @@ -21,8 +21,10 @@ import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" @@ -36,6 +38,7 @@ import ( var _ datasource.DataSource = &DataSource{} var _ datasource.DataSourceWithConfigure = &DataSource{} +var _ datasource.DataSourceWithConfigValidators = &DataSource{} type DataSource struct { client *api.API @@ -47,6 +50,16 @@ func (d *DataSource) Configure(ctx context.Context, request datasource.Configure d.client = client } +func (d *DataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator { + return []datasource.ConfigValidator{ + // Only one of name_prefix and name should be configured + datasourcevalidator.Conflicting( + path.MatchRoot("name_prefix"), + path.MatchRoot("name"), + ), + } +} + func (d *DataSource) Metadata(ctx context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { response.TypeName = request.ProviderTypeName + "_deployments" } diff --git a/ec/ecdatasource/deploymentsdatasource/expanders.go b/ec/ecdatasource/deploymentsdatasource/expanders.go index 07a16c85c..0e5b65535 100644 --- a/ec/ecdatasource/deploymentsdatasource/expanders.go +++ b/ec/ecdatasource/deploymentsdatasource/expanders.go @@ -45,6 +45,15 @@ func expandFilters(ctx context.Context, state modelV0) (*models.SearchRequest, d }) } + name := state.Name.ValueString() + if name != "" { + queries = append(queries, &models.QueryContainer{ + Term: map[string]models.TermQuery{ + "name.keyword": {Value: &name}, + }, + }) + } + depTemplateID := state.DeploymentTemplateID.ValueString() if depTemplateID != "" { esPath := "resources.elasticsearch" diff --git a/ec/ecdatasource/deploymentsdatasource/expanders_test.go b/ec/ecdatasource/deploymentsdatasource/expanders_test.go index 00e08dbd0..4f5333859 100644 --- a/ec/ecdatasource/deploymentsdatasource/expanders_test.go +++ b/ec/ecdatasource/deploymentsdatasource/expanders_test.go @@ -158,6 +158,35 @@ func Test_expandFilters(t *testing.T) { args: args{state: newInvalidFilters(t)}, diags: diag.Diagnostics{diag.NewErrorDiagnostic("invalid value for healthy", "expected either [true] or [false] but got [invalid value]")}, }, + { + name: "parses name filter correctly", + args: args{ + state: modelV0{ + Name: types.StringValue("test"), + Tags: types.MapNull(types.StringType), + }, + }, + want: &models.SearchRequest{ + Sort: []interface{}{"id"}, + Query: &models.QueryContainer{ + Bool: &models.BoolQuery{ + Filter: []*models.QueryContainer{ + { + Bool: &models.BoolQuery{ + Must: []*models.QueryContainer{ + { + Term: map[string]models.TermQuery{ + "name.keyword": {Value: ec.String("test")}, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/ec/ecdatasource/deploymentsdatasource/schema.go b/ec/ecdatasource/deploymentsdatasource/schema.go index 505490dcb..ffd0f66e8 100644 --- a/ec/ecdatasource/deploymentsdatasource/schema.go +++ b/ec/ecdatasource/deploymentsdatasource/schema.go @@ -37,6 +37,10 @@ func (d *DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, r Description: "Prefix to filter the returned deployment list by.", Optional: true, }, + "name": schema.StringAttribute{ + Description: "Filter the result by the full deployment name.", + Optional: true, + }, "healthy": schema.StringAttribute{ Description: "Filter the result set by their health status.", Optional: true, @@ -179,6 +183,7 @@ func resourceFiltersAttrTypes(resourceKind util.ResourceKind) map[string]attr.Ty type modelV0 struct { ID types.String `tfsdk:"id"` NamePrefix types.String `tfsdk:"name_prefix"` + Name types.String `tfsdk:"name"` Healthy types.String `tfsdk:"healthy"` DeploymentTemplateID types.String `tfsdk:"deployment_template_id"` Tags types.Map `tfsdk:"tags"`