Skip to content

Commit

Permalink
d/aws_devopsguru_resource_collection: new data source
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
jar-b committed Mar 29, 2024
1 parent 591d177 commit c459427
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/36657.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_devopsguru_resource_collection
```
3 changes: 3 additions & 0 deletions internal/service/devopsguru/devopsguru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
119 changes: 119 additions & 0 deletions internal/service/devopsguru/resource_collection_data_source.go
Original file line number Diff line number Diff line change
@@ -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"`
}
Original file line number Diff line number Diff line change
@@ -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
}
`
}
4 changes: 4 additions & 0 deletions internal/service/devopsguru/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions website/docs/d/devopsguru_resource_collection.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit c459427

Please sign in to comment.