Skip to content
Open
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/45185.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_notifications_managed_notification_account_contact_association
```
18 changes: 10 additions & 8 deletions internal/service/notifications/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package notifications

// Exports for use in tests only.
var (
ResourceChannelAssociation = newChannelAssociationResource
ResourceEventRule = newEventRuleResource
ResourceNotificationConfiguration = newNotificationConfigurationResource
ResourceNotificationHub = newNotificationHubResource
ResourceChannelAssociation = newChannelAssociationResource
ResourceEventRule = newEventRuleResource
ResourceManagedNotificationAccountContactAssociation = newManagedNotificationAccountContactAssociationResource
ResourceNotificationConfiguration = newNotificationConfigurationResource
ResourceNotificationHub = newNotificationHubResource

FindChannelAssociationByTwoPartKey = findChannelAssociationByTwoPartKey
FindEventRuleByARN = findEventRuleByARN
FindNotificationConfigurationByARN = findNotificationConfigurationByARN
FindNotificationHubByRegion = findNotificationHubByRegion
FindChannelAssociationByTwoPartKey = findChannelAssociationByTwoPartKey
FindEventRuleByARN = findEventRuleByARN
FindManagedNotificationAccountContactAssociationByTwoPartKey = findManagedNotificationAccountContactAssociationByTwoPartKey
FindNotificationConfigurationByARN = findNotificationConfigurationByARN
FindNotificationHubByRegion = findNotificationHubByRegion
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package notifications

import (
"context"
"fmt"
"slices"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/notifications"
awstypes "github.com/aws/aws-sdk-go-v2/service/notifications/types"
"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/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
intflex "github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
fwflex "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"
)

// @FrameworkResource("aws_notifications_managed_notification_account_contact_association", name="Managed Notification Account Contact Association")
func newManagedNotificationAccountContactAssociationResource(_ context.Context) (resource.ResourceWithConfigure, error) {
r := &managedNotificationAccountContactAssociationResource{}

return r, nil
}

type managedNotificationAccountContactAssociationResource struct {
framework.ResourceWithModel[managedNotificationAccountContactAssociationResourceModel]
framework.WithNoUpdate
}

func (r *managedNotificationAccountContactAssociationResource) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"contact_identifier": schema.StringAttribute{
CustomType: fwtypes.StringEnumType[awstypes.AccountContactType](),
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"managed_notification_configuration_arn": schema.StringAttribute{
CustomType: fwtypes.ARNType,
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}

func (r *managedNotificationAccountContactAssociationResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
var data managedNotificationAccountContactAssociationResourceModel
response.Diagnostics.Append(request.Plan.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := r.Meta().NotificationsClient(ctx)

var input notifications.AssociateManagedNotificationAccountContactInput
response.Diagnostics.Append(fwflex.Expand(ctx, data, &input)...)
if response.Diagnostics.HasError() {
return
}

_, err := conn.AssociateManagedNotificationAccountContact(ctx, &input)

if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("creating User Notifications Managed Notification Account Contact Association (%s,%s)", data.ManagedNotificationConfigurationARN.ValueString(), data.ContactIdentifier.ValueString()), err.Error())

return
}

response.Diagnostics.Append(response.State.Set(ctx, data)...)
}

func (r *managedNotificationAccountContactAssociationResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
var data managedNotificationAccountContactAssociationResourceModel
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := r.Meta().NotificationsClient(ctx)

managedNotificationConfigurationARN, contactIdentifier := fwflex.StringValueFromFramework(ctx, data.ManagedNotificationConfigurationARN), fwflex.StringValueFromFramework(ctx, data.ContactIdentifier)
err := findManagedNotificationAccountContactAssociationByTwoPartKey(ctx, conn, managedNotificationConfigurationARN, contactIdentifier)

if tfresource.NotFound(err) {
response.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err))
response.State.RemoveResource(ctx)

return
}

if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("reading User Notifications Managed Notification Account Contact Association (%s,%s)", managedNotificationConfigurationARN, contactIdentifier), err.Error())

return
}

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

func (r *managedNotificationAccountContactAssociationResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
var data managedNotificationAccountContactAssociationResourceModel
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := r.Meta().NotificationsClient(ctx)

var input notifications.DisassociateManagedNotificationAccountContactInput
response.Diagnostics.Append(fwflex.Expand(ctx, data, &input)...)
if response.Diagnostics.HasError() {
return
}

_, err := conn.DisassociateManagedNotificationAccountContact(ctx, &input)

if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return
}

if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("deleting User Notifications Managed Notification Account Contact Association (%s,%s)", data.ManagedNotificationConfigurationARN.ValueString(), data.ContactIdentifier.ValueString()), err.Error())

return
}
}

func (r *managedNotificationAccountContactAssociationResource) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
const (
managedNotificationAccountContactAssociationIDParts = 2
)
parts, err := intflex.ExpandResourceId(request.ID, managedNotificationAccountContactAssociationIDParts, false)

if err != nil {
response.Diagnostics.Append(fwdiag.NewParsingResourceIDErrorDiagnostic(err))

return
}

response.Diagnostics.Append(response.State.SetAttribute(ctx, path.Root("contact_identifier"), parts[1])...)
response.Diagnostics.Append(response.State.SetAttribute(ctx, path.Root("managed_notification_configuration_arn"), parts[0])...)
}

func findManagedNotificationAccountContactAssociationByTwoPartKey(ctx context.Context, conn *notifications.Client, managedNotificationConfigurationArn, contactIdentifier string) error {
input := notifications.ListManagedNotificationChannelAssociationsInput{
ManagedNotificationConfigurationArn: aws.String(managedNotificationConfigurationArn),
}

pages := notifications.NewListManagedNotificationChannelAssociationsPaginator(conn, &input)
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return &retry.NotFoundError{
LastError: err,
LastRequest: &input,
}
}

if err != nil {
return err
}

if slices.ContainsFunc(page.ChannelAssociations, func(ca awstypes.ManagedNotificationChannelAssociationSummary) bool {
return aws.ToString(ca.ChannelIdentifier) == contactIdentifier && string(ca.ChannelType) == "ACCOUNT_CONTACT"
}) {
return nil
}
}

return &retry.NotFoundError{
LastRequest: &input,
}
}

type managedNotificationAccountContactAssociationResourceModel struct {
ContactIdentifier fwtypes.StringEnum[awstypes.AccountContactType] `tfsdk:"contact_identifier"`
ManagedNotificationConfigurationARN fwtypes.ARN `tfsdk:"managed_notification_configuration_arn"`
}
Loading
Loading