Skip to content

Commit

Permalink
ec_deployments: Filter by name. (#797)
Browse files Browse the repository at this point in the history
Allows searching for a deployment by the exact name.
  • Loading branch information
gigerdo authored Mar 18, 2024
1 parent 518aa6b commit 8348ffc
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/797.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
datasource/deployments: Adds additional parameter `name` to allow searching by exact deployment name.
```
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/deployments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,8 @@ Optional:

- `apm` (String)
- `fleet` (String)
- `profiling` (String)
- `symbols` (String)



Expand Down
3 changes: 3 additions & 0 deletions ec/acc/datasource_deployment_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
Expand Down
8 changes: 8 additions & 0 deletions ec/acc/testdata/datasource_deployment_basic.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
}
13 changes: 13 additions & 0 deletions ec/ecdatasource/deploymentsdatasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -36,6 +38,7 @@ import (

var _ datasource.DataSource = &DataSource{}
var _ datasource.DataSourceWithConfigure = &DataSource{}
var _ datasource.DataSourceWithConfigValidators = &DataSource{}

type DataSource struct {
client *api.API
Expand All @@ -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"
}
Expand Down
9 changes: 9 additions & 0 deletions ec/ecdatasource/deploymentsdatasource/expanders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 29 additions & 0 deletions ec/ecdatasource/deploymentsdatasource/expanders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions ec/ecdatasource/deploymentsdatasource/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"`
Expand Down

0 comments on commit 8348ffc

Please sign in to comment.