Skip to content

Commit

Permalink
d/aws_devopsguru_notification_channel: new data source (#36656)
Browse files Browse the repository at this point in the history
This data source will allow practitioners to read AWS DevOps Guru notification channel data with Terraform.

```console
% make testacc PKG=devopsguru TESTS=TestAccDevOpsGuru_serial/NotificationChannelDataSource/
==> 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/NotificationChannelDataSource/'  -timeout 360m
=== RUN   TestAccDevOpsGuru_serial
=== PAUSE TestAccDevOpsGuru_serial
=== CONT  TestAccDevOpsGuru_serial
=== RUN   TestAccDevOpsGuru_serial/NotificationChannelDataSource
=== RUN   TestAccDevOpsGuru_serial/NotificationChannelDataSource/basic
--- PASS: TestAccDevOpsGuru_serial (12.09s)
    --- PASS: TestAccDevOpsGuru_serial/NotificationChannelDataSource (12.09s)
        --- PASS: TestAccDevOpsGuru_serial/NotificationChannelDataSource/basic (12.09s)
PASS
ok      github.com/hashicorp/terraform-provider-aws/internal/service/devopsguru 17.651s
```
  • Loading branch information
jar-b authored Mar 29, 2024
1 parent 3424da0 commit 591d177
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/36656.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_devopsguru_notification_channel
```
3 changes: 3 additions & 0 deletions internal/service/devopsguru/devopsguru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func TestAccDevOpsGuru_serial(t *testing.T) {
"disappears": testAccNotificationChannel_disappears,
"filters": testAccNotificationChannel_filters,
},
"NotificationChannelDataSource": {
"basic": testAccNotificationChannelDataSource_basic,
},
"ResourceCollection": {
"basic": testAccResourceCollection_basic,
"cloudformation": testAccResourceCollection_cloudformation,
Expand Down
101 changes: 101 additions & 0 deletions internal/service/devopsguru/notification_channel_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package devopsguru

import (
"context"

"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="Notification Channel")
func newDataSourceNotificationChannel(context.Context) (datasource.DataSourceWithConfigure, error) {
return &dataSourceNotificationChannel{}, nil
}

const (
DSNameNotificationChannel = "Notification Channel Data Source"
)

type dataSourceNotificationChannel struct {
framework.DataSourceWithConfigure
}

func (d *dataSourceNotificationChannel) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name
resp.TypeName = "aws_devopsguru_notification_channel"
}

func (d *dataSourceNotificationChannel) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Required: true,
},
},
Blocks: map[string]schema.Block{
"filters": schema.ListNestedBlock{
CustomType: fwtypes.NewListNestedObjectTypeOf[filtersData](ctx),
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"message_types": schema.ListAttribute{
Computed: true,
CustomType: fwtypes.ListOfStringType,
ElementType: types.StringType,
},
"severities": schema.ListAttribute{
Computed: true,
CustomType: fwtypes.ListOfStringType,
ElementType: types.StringType,
},
},
},
},
"sns": schema.ListNestedBlock{
CustomType: fwtypes.NewListNestedObjectTypeOf[snsData](ctx),
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"topic_arn": schema.StringAttribute{
Computed: true,
CustomType: fwtypes.ARNType,
},
},
},
},
},
}
}
func (d *dataSourceNotificationChannel) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
conn := d.Meta().DevOpsGuruClient(ctx)

var data dataSourceNotificationChannelData
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

out, err := findNotificationChannelByID(ctx, conn, data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.DevOpsGuru, create.ErrActionReading, DSNameNotificationChannel, data.ID.String(), err),
err.Error(),
)
return
}

resp.Diagnostics.Append(flex.Flatten(ctx, out.Config, &data)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

type dataSourceNotificationChannelData struct {
Filters fwtypes.ListNestedObjectValueOf[filtersData] `tfsdk:"filters"`
ID types.String `tfsdk:"id"`
Sns fwtypes.ListNestedObjectValueOf[snsData] `tfsdk:"sns"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package devopsguru_test

import (
"fmt"
"testing"

sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func testAccNotificationChannelDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_devopsguru_notification_channel.test"
notificationChannelResourceName := "aws_devopsguru_notification_channel.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: testAccCheckNotificationChannelDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccNotificationChannelDataSourceConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "id", notificationChannelResourceName, "id"),
resource.TestCheckResourceAttr(dataSourceName, "sns.#", "1"),
resource.TestCheckResourceAttrPair(dataSourceName, "sns.0.topic_name", notificationChannelResourceName, "sns.0.topic_name"),
),
},
},
})
}

func testAccNotificationChannelDataSourceConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test" {
name = %[1]q
}
resource "aws_devopsguru_notification_channel" "test" {
sns {
topic_arn = aws_sns_topic.test.arn
}
}
data "aws_devopsguru_notification_channel" "test" {
id = aws_devopsguru_notification_channel.test.id
}
`, rName)
}
7 changes: 6 additions & 1 deletion internal/service/devopsguru/service_package_gen.go

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

43 changes: 43 additions & 0 deletions website/docs/d/devopsguru_notification_channel.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
subcategory: "DevOps Guru"
layout: "aws"
page_title: "AWS: aws_devopsguru_notification_channel"
description: |-
Terraform data source for managing an AWS DevOps Guru Notification Channel.
---

# Data Source: aws_devopsguru_notification_channel

Terraform data source for managing an AWS DevOps Guru Notification Channel.

## Example Usage

### Basic Usage

```terraform
data "aws_devopsguru_notification_channel" "example" {
id = "channel-1234"
}
```

## Argument Reference

The following arguments are required:

* `id` - (Required) Unique identifier for the notification channel.

## Attribute Reference

This data source exports the following attributes in addition to the arguments above:

* `filters` - Filter configurations for the Amazon SNS notification topic. See the [`filters` attribute reference](#filters-attribute-reference) below.
* `sns` - SNS noficiation channel configurations. See the [`sns` attribute reference](#sns-attribute-reference) below.

### `sns` Attribute Reference

* `topic_arn` - Amazon Resource Name (ARN) of an Amazon Simple Notification Service topic.

### `filters` Attribute Reference

* `message_types` - Events to receive notifications for.
* `severities` - Severity levels to receive notifications for.
4 changes: 2 additions & 2 deletions website/docs/r/devopsguru_notification_channel.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ The following arguments are required:

The following arguments are optional:

* `filters` - (Optional) Filter configurations for the Amazon SNS notification topic See the [`filters` argument reference](#filters-argument-reference) below.
* `filters` - (Optional) Filter configurations for the Amazon SNS notification topic. See the [`filters` argument reference](#filters-argument-reference) below.

### `sns` Argument Reference

* `topic_arn` - (Required)
* `topic_arn` - (Required) Amazon Resource Name (ARN) of an Amazon Simple Notification Service topic.

### `filters` Argument Reference

Expand Down

0 comments on commit 591d177

Please sign in to comment.