Skip to content
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

d/aws_devopsguru_resource_collection: new data source #36657

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading