From c4594273fd7c8aaa90c25eb449b388ae33714521 Mon Sep 17 00:00:00 2001 From: Jared Baker Date: Fri, 29 Mar 2024 15:08:11 -0400 Subject: [PATCH] d/aws_devopsguru_resource_collection: new data source This data source will allow practitioners to read AWS DevOps Guru resource collection data with Terraform. ```console % make testacc PKG=devopsguru TESTS=TestAccDevOpsGuru_serial/ResourceCollectionDataSource/ ==> Checking that code complies with gofmt requirements... TF_ACC=1 go1.21.8 test ./internal/service/devopsguru/... -v -count 1 -parallel 20 -run='TestAccDevOpsGuru_serial/ResourceCollectionDataSource/' -timeout 360m --- PASS: TestAccDevOpsGuru_serial (10.49s) --- PASS: TestAccDevOpsGuru_serial/ResourceCollectionDataSource (10.49s) --- PASS: TestAccDevOpsGuru_serial/ResourceCollectionDataSource/basic (10.49s) PASS ok github.com/hashicorp/terraform-provider-aws/internal/service/devopsguru 15.982s ``` --- .changelog/36657.txt | 3 + .../service/devopsguru/devopsguru_test.go | 3 + .../resource_collection_data_source.go | 119 ++++++++++++++++++ .../resource_collection_data_source_test.go | 51 ++++++++ .../service/devopsguru/service_package_gen.go | 4 + ...vopsguru_resource_collection.html.markdown | 44 +++++++ ...vopsguru_resource_collection.html.markdown | 2 +- 7 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 .changelog/36657.txt create mode 100644 internal/service/devopsguru/resource_collection_data_source.go create mode 100644 internal/service/devopsguru/resource_collection_data_source_test.go create mode 100644 website/docs/d/devopsguru_resource_collection.html.markdown diff --git a/.changelog/36657.txt b/.changelog/36657.txt new file mode 100644 index 00000000000..0f131c1fa6e --- /dev/null +++ b/.changelog/36657.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_devopsguru_resource_collection +``` diff --git a/internal/service/devopsguru/devopsguru_test.go b/internal/service/devopsguru/devopsguru_test.go index 127faa72120..849961dda59 100644 --- a/internal/service/devopsguru/devopsguru_test.go +++ b/internal/service/devopsguru/devopsguru_test.go @@ -34,6 +34,9 @@ func TestAccDevOpsGuru_serial(t *testing.T) { "tags": testAccResourceCollection_tags, "tagsAllResources": testAccResourceCollection_tagsAllResources, }, + "ResourceCollectionDataSource": { + "basic": testAccResourceCollectionDataSource_basic, + }, } acctest.RunSerialTests2Levels(t, testCases, 0) diff --git a/internal/service/devopsguru/resource_collection_data_source.go b/internal/service/devopsguru/resource_collection_data_source.go new file mode 100644 index 00000000000..06c4831d598 --- /dev/null +++ b/internal/service/devopsguru/resource_collection_data_source.go @@ -0,0 +1,119 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package devopsguru + +import ( + "context" + + awstypes "github.com/aws/aws-sdk-go-v2/service/devopsguru/types" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-provider-aws/internal/create" + "github.com/hashicorp/terraform-provider-aws/internal/framework" + "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" + fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" + "github.com/hashicorp/terraform-provider-aws/names" +) + +// @FrameworkDataSource(name="Resource Collection") +func newDataSourceResourceCollection(context.Context) (datasource.DataSourceWithConfigure, error) { + return &dataSourceResourceCollection{}, nil +} + +const ( + DSNameResourceCollection = "Resource Collection Data Source" +) + +type dataSourceResourceCollection struct { + framework.DataSourceWithConfigure +} + +func (d *dataSourceResourceCollection) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name + resp.TypeName = "aws_devopsguru_resource_collection" +} + +func (d *dataSourceResourceCollection) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "id": framework.IDAttribute(), + "type": schema.StringAttribute{ + Required: true, + CustomType: fwtypes.StringEnumType[awstypes.ResourceCollectionType](), + }, + }, + Blocks: map[string]schema.Block{ + "cloudformation": schema.ListNestedBlock{ + CustomType: fwtypes.NewListNestedObjectTypeOf[cloudformationData](ctx), + NestedObject: schema.NestedBlockObject{ + Attributes: map[string]schema.Attribute{ + "stack_names": schema.ListAttribute{ + Computed: true, + CustomType: fwtypes.ListOfStringType, + ElementType: types.StringType, + }, + }, + }, + }, + "tags": schema.ListNestedBlock{ + CustomType: fwtypes.NewListNestedObjectTypeOf[tagsData](ctx), + NestedObject: schema.NestedBlockObject{ + Attributes: map[string]schema.Attribute{ + "app_boundary_key": schema.StringAttribute{ + Computed: true, + }, + "tag_values": schema.ListAttribute{ + Computed: true, + CustomType: fwtypes.ListOfStringType, + ElementType: types.StringType, + }, + }, + }, + }, + }, + } +} +func (d *dataSourceResourceCollection) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + conn := d.Meta().DevOpsGuruClient(ctx) + + var data dataSourceResourceCollectionData + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + if resp.Diagnostics.HasError() { + return + } + data.ID = types.StringValue(data.Type.ValueString()) + + out, err := findResourceCollectionByID(ctx, conn, data.Type.ValueString()) + if err != nil { + resp.Diagnostics.AddError( + create.ProblemStandardMessage(names.DevOpsGuru, create.ErrActionReading, DSNameResourceCollection, data.Type.String(), err), + err.Error(), + ) + return + } + + resp.Diagnostics.Append(flex.Flatten(ctx, out, &data)...) + if resp.Diagnostics.HasError() { + return + } + + // Fields named "Tags" are currently hardcoded to be ignored by AutoFlex. Flattening the Tags + // struct from the response into state.Tags is a temporary workaround until the AutoFlex + // options implementation can be merged. + // + // Ref: https://github.com/hashicorp/terraform-provider-aws/pull/36437 + resp.Diagnostics.Append(flex.Flatten(ctx, out.Tags, &data.Tags)...) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} + +type dataSourceResourceCollectionData struct { + CloudFormation fwtypes.ListNestedObjectValueOf[cloudformationData] `tfsdk:"cloudformation"` + ID types.String `tfsdk:"id"` + Tags fwtypes.ListNestedObjectValueOf[tagsData] `tfsdk:"tags"` + Type fwtypes.StringEnum[awstypes.ResourceCollectionType] `tfsdk:"type"` +} diff --git a/internal/service/devopsguru/resource_collection_data_source_test.go b/internal/service/devopsguru/resource_collection_data_source_test.go new file mode 100644 index 00000000000..ace07ac60f7 --- /dev/null +++ b/internal/service/devopsguru/resource_collection_data_source_test.go @@ -0,0 +1,51 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package devopsguru_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/names" +) + +func testAccResourceCollectionDataSource_basic(t *testing.T) { + ctx := acctest.Context(t) + dataSourceName := "data.aws_devopsguru_resource_collection.test" + resourceName := "aws_devopsguru_resource_collection.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(ctx, t) + acctest.PreCheckPartitionHasService(t, names.DevOpsGuruEndpointID) + }, + ErrorCheck: acctest.ErrorCheck(t, names.DevOpsGuruServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckResourceCollectionDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccResourceCollectionDataSourceConfig_basic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(dataSourceName, "type", resourceName, "type"), + ), + }, + }, + }) +} + +func testAccResourceCollectionDataSourceConfig_basic() string { + return ` +resource "aws_devopsguru_resource_collection" "test" { + type = "AWS_SERVICE" + cloudformation { + stack_names = ["*"] + } +} + +data "aws_devopsguru_resource_collection" "test" { + type = aws_devopsguru_resource_collection.test.type +} +` +} diff --git a/internal/service/devopsguru/service_package_gen.go b/internal/service/devopsguru/service_package_gen.go index 88b58266929..42d8fc4ed07 100644 --- a/internal/service/devopsguru/service_package_gen.go +++ b/internal/service/devopsguru/service_package_gen.go @@ -20,6 +20,10 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newDataSourceNotificationChannel, Name: "Notification Channel", }, + { + Factory: newDataSourceResourceCollection, + Name: "Resource Collection", + }, } } diff --git a/website/docs/d/devopsguru_resource_collection.html.markdown b/website/docs/d/devopsguru_resource_collection.html.markdown new file mode 100644 index 00000000000..652f02a6508 --- /dev/null +++ b/website/docs/d/devopsguru_resource_collection.html.markdown @@ -0,0 +1,44 @@ +--- +subcategory: "DevOps Guru" +layout: "aws" +page_title: "AWS: aws_devopsguru_resource_collection" +description: |- + Terraform data source for managing an AWS DevOps Guru Resource Collection. +--- + +# Data Source: aws_devopsguru_resource_collection + +Terraform data source for managing an AWS DevOps Guru Resource Collection. + +## Example Usage + +### Basic Usage + +```terraform +data "aws_devopsguru_resource_collection" "example" { + type = "AWS_SERVICE" +} +``` + +## Argument Reference + +The following arguments are required: + +* `type` - (Required) Type of AWS resource collection to create. Valid values are `AWS_CLOUD_FORMATION`, `AWS_SERVICE`, and `AWS_TAGS`. + +## Attribute Reference + +This data source exports the following attributes in addition to the arguments above: + +* `cloudformation` - A collection of AWS CloudFormation stacks. See [`cloudformation`](#cloudformation-attribute-reference) below for additional details. +* `id` - Type of AWS resource collection to create (same value as `type`). +* `tags` - AWS tags used to filter the resources in the resource collection. See [`tags`](#tags-attribute-reference) below for additional details. + +### `cloudformation` Attribute Reference + +* `stack_names` - Array of the names of the AWS CloudFormation stacks. + +### `tags` Attribute Reference + +* `app_boundary_key` - An AWS tag key that is used to identify the AWS resources that DevOps Guru analyzes. +* `tag_values` - Array of tag values. diff --git a/website/docs/r/devopsguru_resource_collection.html.markdown b/website/docs/r/devopsguru_resource_collection.html.markdown index 717e77f6349..977f0129a92 100644 --- a/website/docs/r/devopsguru_resource_collection.html.markdown +++ b/website/docs/r/devopsguru_resource_collection.html.markdown @@ -70,7 +70,7 @@ The following arguments are required: The following arguments are optional: * `cloudformation` - (Optional) A collection of AWS CloudFormation stacks. See [`cloudformation`](#cloudformation-argument-reference) below for additional details. -* `tags` - (Optional) AWS tags used to filter the resources in the resource collection See [`tags`](#tags-argument-reference) below for additional details. +* `tags` - (Optional) AWS tags used to filter the resources in the resource collection. See [`tags`](#tags-argument-reference) below for additional details. ### `cloudformation` Argument Reference