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

r/aws_devopsguru_notification_channel: new resource #36557

Merged
merged 2 commits into from
Mar 25, 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/36557.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_devopsguru_notification_channel
```
8 changes: 7 additions & 1 deletion internal/framework/flex/auto_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,13 @@ func (expander autoExpander) listOfString(ctx context.Context, vFrom basetypes.L
return diags
}

vTo.Set(reflect.ValueOf(to))
// Copy elements individually to enable expansion of lists of
// custom string types (AWS enums)
vals := reflect.MakeSlice(vTo.Type(), len(to), len(to))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

for i := 0; i < len(to); i++ {
vals.Index(i).SetString(to[i])
}
vTo.Set(vals)
return diags

case reflect.Ptr:
Expand Down
7 changes: 7 additions & 0 deletions internal/service/devopsguru/devopsguru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ func TestAccDevOpsGuru_serial(t *testing.T) {
"basic": testAccEventSourcesConfig_basic,
"disappears": testAccEventSourcesConfig_disappears,
},
// A maxiumum of 2 notification channels can be configured at once, so
// serialize tests for safety.
"NotificationChannel": {
"basic": testAccNotificationChannel_basic,
"disappears": testAccNotificationChannel_disappears,
"filters": testAccNotificationChannel_filters,
},
"ResourceCollection": {
"basic": testAccResourceCollection_basic,
"cloudformation": testAccResourceCollection_cloudformation,
Expand Down
10 changes: 6 additions & 4 deletions internal/service/devopsguru/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ package devopsguru

// Exports for use in tests only.
var (
ResourceEventSourcesConfig = newResourceEventSourcesConfig
ResourceResourceCollection = newResourceResourceCollection
ResourceEventSourcesConfig = newResourceEventSourcesConfig
ResourceNotificationChannel = newResourceNotificationChannel
ResourceResourceCollection = newResourceResourceCollection

FindEventSourcesConfig = findEventSourcesConfig
FindResourceCollectionByID = findResourceCollectionByID
FindEventSourcesConfig = findEventSourcesConfig
FindNotificationChannelByID = findNotificationChannelByID
FindResourceCollectionByID = findResourceCollectionByID
)
250 changes: 250 additions & 0 deletions internal/service/devopsguru/notification_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package devopsguru

import (
"context"
"errors"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/devopsguru"
awstypes "github.com/aws/aws-sdk-go-v2/service/devopsguru/types"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"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/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkResource(name="Notification Channel")
func newResourceNotificationChannel(_ context.Context) (resource.ResourceWithConfigure, error) {
return &resourceNotificationChannel{}, nil
}

const (
ResNameNotificationChannel = "Notification Channel"
)

type resourceNotificationChannel struct {
framework.ResourceWithConfigure
}

func (r *resourceNotificationChannel) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = "aws_devopsguru_notification_channel"
}

func (r *resourceNotificationChannel) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": framework.IDAttribute(),
},
Blocks: map[string]schema.Block{
"filters": schema.ListNestedBlock{
CustomType: fwtypes.NewListNestedObjectTypeOf[filtersData](ctx),
Validators: []validator.List{
listvalidator.SizeAtMost(1),
},
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"message_types": schema.ListAttribute{
Optional: true,
CustomType: fwtypes.ListOfStringType,
ElementType: types.StringType,
PlanModifiers: []planmodifier.List{
listplanmodifier.RequiresReplace(),
},
Validators: []validator.List{
listvalidator.ValueStringsAre(
enum.FrameworkValidate[awstypes.NotificationMessageType](),
),
},
},
"severities": schema.ListAttribute{
Optional: true,
CustomType: fwtypes.ListOfStringType,
ElementType: types.StringType,
PlanModifiers: []planmodifier.List{
listplanmodifier.RequiresReplace(),
},
Validators: []validator.List{
listvalidator.ValueStringsAre(
enum.FrameworkValidate[awstypes.InsightSeverity](),
),
},
},
},
},
},
"sns": schema.ListNestedBlock{
CustomType: fwtypes.NewListNestedObjectTypeOf[snsData](ctx),
Validators: []validator.List{
listvalidator.IsRequired(),
listvalidator.SizeAtMost(1),
},
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"topic_arn": schema.StringAttribute{
Required: true,
CustomType: fwtypes.ARNType,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
},
},
},
}
}

func (r *resourceNotificationChannel) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
conn := r.Meta().DevOpsGuruClient(ctx)

var plan resourceNotificationChannelData
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}

cfg := &awstypes.NotificationChannelConfig{}
resp.Diagnostics.Append(flex.Expand(ctx, plan, cfg)...)
if resp.Diagnostics.HasError() {
return
}
in := &devopsguru.AddNotificationChannelInput{
Config: cfg,
}

out, err := conn.AddNotificationChannel(ctx, in)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.DevOpsGuru, create.ErrActionCreating, ResNameNotificationChannel, "", err),
err.Error(),
)
return
}
if out == nil || out.Id == nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.DevOpsGuru, create.ErrActionCreating, ResNameNotificationChannel, "", nil),
errors.New("empty output").Error(),
)
return
}

plan.ID = flex.StringToFramework(ctx, out.Id)
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
}

func (r *resourceNotificationChannel) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
conn := r.Meta().DevOpsGuruClient(ctx)

var state resourceNotificationChannelData
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

out, err := findNotificationChannelByID(ctx, conn, state.ID.ValueString())
if tfresource.NotFound(err) {
resp.State.RemoveResource(ctx)
return
}
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.DevOpsGuru, create.ErrActionSetting, ResNameNotificationChannel, state.ID.String(), err),
err.Error(),
)
return
}

state.ID = flex.StringToFramework(ctx, out.Id)

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

func (r *resourceNotificationChannel) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Update is a no-op
}

func (r *resourceNotificationChannel) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
conn := r.Meta().DevOpsGuruClient(ctx)

var state resourceNotificationChannelData
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

in := &devopsguru.RemoveNotificationChannelInput{
Id: aws.String(state.ID.ValueString()),
}

_, err := conn.RemoveNotificationChannel(ctx, in)
if err != nil {
if errs.IsA[*retry.NotFoundError](err) {
return
}
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.DevOpsGuru, create.ErrActionDeleting, ResNameNotificationChannel, state.ID.String(), err),
err.Error(),
)
return
}
}

func (r *resourceNotificationChannel) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func findNotificationChannelByID(ctx context.Context, conn *devopsguru.Client, id string) (*awstypes.NotificationChannel, error) {
in := &devopsguru.ListNotificationChannelsInput{}

paginator := devopsguru.NewListNotificationChannelsPaginator(conn, in)
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return nil, err
}

for _, channel := range page.Channels {
if aws.ToString(channel.Id) == id {
return &channel, nil
}
}
}

return nil, &retry.NotFoundError{
LastError: errors.New("not found"),
LastRequest: in,
}
}

type resourceNotificationChannelData struct {
Filters fwtypes.ListNestedObjectValueOf[filtersData] `tfsdk:"filters"`
ID types.String `tfsdk:"id"`
Sns fwtypes.ListNestedObjectValueOf[snsData] `tfsdk:"sns"`
}

type filtersData struct {
MessageTypes fwtypes.ListValueOf[types.String] `tfsdk:"message_types"`
Severities fwtypes.ListValueOf[types.String] `tfsdk:"severities"`
}

type snsData struct {
TopicARN fwtypes.ARN `tfsdk:"topic_arn"`
}
Loading
Loading