Skip to content

Commit 05bd25c

Browse files
committed
r/aws_notifications_notification_hub: 'region' -> 'notification_hub_region'.
1 parent 9491ad3 commit 05bd25c

File tree

3 files changed

+153
-160
lines changed

3 files changed

+153
-160
lines changed

internal/service/notifications/notification_hub.go

Lines changed: 135 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ import (
2020
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2121
"github.com/hashicorp/terraform-plugin-framework/types"
2222
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
23-
"github.com/hashicorp/terraform-provider-aws/internal/create"
2423
"github.com/hashicorp/terraform-provider-aws/internal/enum"
2524
"github.com/hashicorp/terraform-provider-aws/internal/errs"
2625
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
2726
"github.com/hashicorp/terraform-provider-aws/internal/framework"
28-
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
27+
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
28+
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
2929
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
3030
"github.com/hashicorp/terraform-provider-aws/names"
3131
)
3232

33-
// Function annotations are used for resource registration to the Provider. DO NOT EDIT.
3433
// @FrameworkResource("aws_notifications_notification_hub", name="Notification Hub")
3534
func newResourceNotificationHub(_ context.Context) (resource.ResourceWithConfigure, error) {
3635
r := &resourceNotificationHub{}
@@ -41,20 +40,16 @@ func newResourceNotificationHub(_ context.Context) (resource.ResourceWithConfigu
4140
return r, nil
4241
}
4342

44-
const (
45-
ResNameNotificationHub = "Notification Hub"
46-
)
47-
4843
type resourceNotificationHub struct {
4944
framework.ResourceWithConfigure
5045
framework.WithTimeouts
5146
framework.WithNoUpdate
5247
}
5348

54-
func (r *resourceNotificationHub) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
55-
resp.Schema = schema.Schema{
49+
func (r *resourceNotificationHub) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) {
50+
response.Schema = schema.Schema{
5651
Attributes: map[string]schema.Attribute{
57-
names.AttrRegion: schema.StringAttribute{
52+
"notification_hub_region": schema.StringAttribute{
5853
Required: true,
5954
PlanModifiers: []planmodifier.String{
6055
stringplanmodifier.RequiresReplace(),
@@ -70,164 +65,162 @@ func (r *resourceNotificationHub) Schema(ctx context.Context, req resource.Schem
7065
}
7166
}
7267

73-
func (r *resourceNotificationHub) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
74-
conn := r.Meta().NotificationsClient(ctx)
75-
76-
var plan resourceNotificationHubModel
77-
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
78-
if resp.Diagnostics.HasError() {
68+
func (r *resourceNotificationHub) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
69+
var data resourceNotificationHubModel
70+
response.Diagnostics.Append(request.Plan.Get(ctx, &data)...)
71+
if response.Diagnostics.HasError() {
7972
return
8073
}
8174

82-
var input notifications.RegisterNotificationHubInput
83-
resp.Diagnostics.Append(flex.Expand(ctx, plan, &input, flex.WithFieldNamePrefix("NotificationHub"))...)
84-
if resp.Diagnostics.HasError() {
85-
return
75+
conn := r.Meta().NotificationsClient(ctx)
76+
77+
region := fwflex.StringValueFromFramework(ctx, data.NotificationHubRegion)
78+
input := notifications.RegisterNotificationHubInput{
79+
NotificationHubRegion: aws.String(region),
8680
}
81+
_, err := conn.RegisterNotificationHub(ctx, &input)
8782

88-
out, err := conn.RegisterNotificationHub(ctx, &input)
8983
if err != nil {
90-
resp.Diagnostics.AddError(
91-
create.ProblemStandardMessage(names.Notifications, create.ErrActionCreating, ResNameNotificationHub, plan.Region.String(), err),
92-
err.Error(),
93-
)
94-
return
95-
}
96-
if out == nil {
97-
resp.Diagnostics.AddError(
98-
create.ProblemStandardMessage(names.Notifications, create.ErrActionCreating, ResNameNotificationHub, plan.Region.String(), nil),
99-
errors.New("empty output").Error(),
100-
)
101-
return
102-
}
84+
response.Diagnostics.AddError(fmt.Sprintf("registering User Notifications Notification Hub (%s)", region), err.Error())
10385

104-
resp.Diagnostics.Append(flex.Flatten(ctx, out, &plan)...)
105-
if resp.Diagnostics.HasError() {
10686
return
10787
}
10888

109-
createTimeout := r.CreateTimeout(ctx, plan.Timeouts)
110-
_, err = waitNotificationHubCreated(ctx, conn, plan.Region.ValueString(), createTimeout)
111-
if err != nil {
112-
resp.Diagnostics.AddError(
113-
create.ProblemStandardMessage(names.Notifications, create.ErrActionWaitingForCreation, ResNameNotificationHub, plan.Region.String(), err),
114-
err.Error(),
115-
)
89+
if _, err := waitNotificationHubCreated(ctx, conn, region, r.CreateTimeout(ctx, data.Timeouts)); err != nil {
90+
response.Diagnostics.AddError(fmt.Sprintf("waiting for User Notifications Notification Hub (%s) create", region), err.Error())
91+
11692
return
11793
}
11894

119-
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
95+
response.Diagnostics.Append(response.State.Set(ctx, data)...)
12096
}
12197

122-
func (r *resourceNotificationHub) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
123-
conn := r.Meta().NotificationsClient(ctx)
124-
125-
var state resourceNotificationHubModel
126-
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
127-
if resp.Diagnostics.HasError() {
98+
func (r *resourceNotificationHub) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
99+
var data resourceNotificationHubModel
100+
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
101+
if response.Diagnostics.HasError() {
128102
return
129103
}
130104

131-
out, err := findNotificationHubByRegion(ctx, conn, state.Region.ValueString())
105+
conn := r.Meta().NotificationsClient(ctx)
106+
107+
_, err := findNotificationHubByRegion(ctx, conn, data.NotificationHubRegion.ValueString())
108+
132109
if tfresource.NotFound(err) {
133-
resp.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err))
134-
resp.State.RemoveResource(ctx)
110+
response.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err))
111+
response.State.RemoveResource(ctx)
112+
135113
return
136114
}
115+
137116
if err != nil {
138-
resp.Diagnostics.AddError(
139-
create.ProblemStandardMessage(names.Notifications, create.ErrActionReading, ResNameNotificationHub, state.Region.String(), err),
140-
err.Error(),
141-
)
142-
return
143-
}
117+
response.Diagnostics.AddError(fmt.Sprintf("reading User Notifications Notification Hub (%s)", data.NotificationHubRegion.ValueString()), err.Error())
144118

145-
resp.Diagnostics.Append(flex.Flatten(ctx, out, &state)...)
146-
if resp.Diagnostics.HasError() {
147119
return
148120
}
149121

150-
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
122+
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
151123
}
152124

153-
func (r *resourceNotificationHub) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
125+
func (r *resourceNotificationHub) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
126+
var data resourceNotificationHubModel
127+
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
128+
if response.Diagnostics.HasError() {
129+
return
130+
}
131+
154132
conn := r.Meta().NotificationsClient(ctx)
155133

156-
var state resourceNotificationHubModel
157-
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
158-
if resp.Diagnostics.HasError() {
134+
region := fwflex.StringValueFromFramework(ctx, data.NotificationHubRegion)
135+
input := notifications.DeregisterNotificationHubInput{
136+
NotificationHubRegion: aws.String(region),
137+
}
138+
_, err := conn.DeregisterNotificationHub(ctx, &input)
139+
140+
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
159141
return
160142
}
161143

162-
input := notifications.DeregisterNotificationHubInput{
163-
NotificationHubRegion: state.Region.ValueStringPointer(),
144+
if errs.IsAErrorMessageContains[*awstypes.ConflictException](err, "Cannot deregister last ACTIVE notification hub") {
145+
return
164146
}
165147

166-
_, err := conn.DeregisterNotificationHub(ctx, &input)
167148
if err != nil {
168-
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
169-
return
170-
}
149+
response.Diagnostics.AddError(fmt.Sprintf("deregistering User Notifications Notification Hub (%s)", region), err.Error())
171150

172-
resp.Diagnostics.AddError(
173-
create.ProblemStandardMessage(names.Notifications, create.ErrActionDeleting, ResNameNotificationHub, state.Region.String(), err),
174-
err.Error(),
175-
)
176151
return
177152
}
178153

179-
deleteTimeout := r.DeleteTimeout(ctx, state.Timeouts)
180-
_, err = waitNotificationHubDeleted(ctx, conn, state.Region.ValueString(), deleteTimeout)
181-
if err != nil {
182-
resp.Diagnostics.AddError(
183-
create.ProblemStandardMessage(names.Notifications, create.ErrActionWaitingForDeletion, ResNameNotificationHub, state.Region.String(), err),
184-
err.Error(),
185-
)
154+
if _, err := waitNotificationHubDeleted(ctx, conn, region, r.DeleteTimeout(ctx, data.Timeouts)); err != nil {
155+
response.Diagnostics.AddError(fmt.Sprintf("waiting for User Notifications Notification Hub (%s) delete", region), err.Error())
156+
186157
return
187158
}
188159
}
189160

190-
func (r *resourceNotificationHub) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
191-
resource.ImportStatePassthroughID(ctx, path.Root(names.AttrRegion), req, resp)
161+
func (r *resourceNotificationHub) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
162+
resource.ImportStatePassthroughID(ctx, path.Root("notification_hub_region"), request, response)
192163
}
193164

194-
func waitNotificationHubCreated(ctx context.Context, conn *notifications.Client, id string, timeout time.Duration) (*awstypes.NotificationHubOverview, error) {
195-
stateConf := &retry.StateChangeConf{
196-
Pending: enum.Slice(awstypes.NotificationHubStatusRegistering),
197-
Target: enum.Slice(awstypes.NotificationHubStatusActive),
198-
Refresh: statusNotificationHub(ctx, conn, id),
199-
Timeout: timeout,
200-
NotFoundChecks: 20,
201-
ContinuousTargetOccurence: 2,
165+
func findNotificationHubByRegion(ctx context.Context, conn *notifications.Client, region string) (*awstypes.NotificationHubOverview, error) {
166+
var input notifications.ListNotificationHubsInput
167+
output, err := findNotificationHub(ctx, conn, &input, func(v *awstypes.NotificationHubOverview) bool {
168+
return aws.ToString(v.NotificationHubRegion) == region
169+
})
170+
171+
if err != nil {
172+
return nil, err
202173
}
203174

204-
outputRaw, err := stateConf.WaitForStateContext(ctx)
205-
if out, ok := outputRaw.(*awstypes.NotificationHubOverview); ok {
206-
return out, err
175+
if output.StatusSummary == nil {
176+
return nil, tfresource.NewEmptyResultError(input)
207177
}
208178

209-
return nil, err
179+
return output, nil
210180
}
211181

212-
func waitNotificationHubDeleted(ctx context.Context, conn *notifications.Client, id string, timeout time.Duration) (*awstypes.NotificationHubOverview, error) {
213-
stateConf := &retry.StateChangeConf{
214-
Pending: enum.Slice(awstypes.NotificationHubStatusDeregistering),
215-
Target: []string{},
216-
Refresh: statusNotificationHub(ctx, conn, id),
217-
Timeout: timeout,
182+
func findNotificationHub(ctx context.Context, conn *notifications.Client, input *notifications.ListNotificationHubsInput, filter tfslices.Predicate[*awstypes.NotificationHubOverview]) (*awstypes.NotificationHubOverview, error) {
183+
output, err := findNotificationHubs(ctx, conn, input, filter)
184+
185+
if err != nil {
186+
return nil, err
218187
}
219188

220-
outputRaw, err := stateConf.WaitForStateContext(ctx)
221-
if out, ok := outputRaw.(*awstypes.NotificationHubOverview); ok {
222-
return out, err
189+
return tfresource.AssertSingleValueResult(output)
190+
}
191+
192+
func findNotificationHubs(ctx context.Context, conn *notifications.Client, input *notifications.ListNotificationHubsInput, filter tfslices.Predicate[*awstypes.NotificationHubOverview]) ([]awstypes.NotificationHubOverview, error) {
193+
var output []awstypes.NotificationHubOverview
194+
195+
pages := notifications.NewListNotificationHubsPaginator(conn, input)
196+
for pages.HasMorePages() {
197+
page, err := pages.NextPage(ctx)
198+
199+
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
200+
return nil, &retry.NotFoundError{
201+
LastError: err,
202+
LastRequest: input,
203+
}
204+
}
205+
206+
if err != nil {
207+
return nil, err
208+
}
209+
210+
for _, v := range page.NotificationHubs {
211+
if filter(&v) {
212+
output = append(output, v)
213+
}
214+
}
223215
}
224216

225-
return nil, err
217+
return output, nil
226218
}
227219

228-
func statusNotificationHub(ctx context.Context, conn *notifications.Client, id string) retry.StateRefreshFunc {
220+
func statusNotificationHub(ctx context.Context, conn *notifications.Client, region string) retry.StateRefreshFunc {
229221
return func() (any, string, error) {
230-
out, err := findNotificationHubByRegion(ctx, conn, id)
222+
output, err := findNotificationHubByRegion(ctx, conn, region)
223+
231224
if tfresource.NotFound(err) {
232225
return nil, "", nil
233226
}
@@ -236,41 +229,50 @@ func statusNotificationHub(ctx context.Context, conn *notifications.Client, id s
236229
return nil, "", err
237230
}
238231

239-
return out, string(out.StatusSummary.Status), nil
232+
return output, string(output.StatusSummary.Status), nil
240233
}
241234
}
242235

243-
func findNotificationHubByRegion(ctx context.Context, conn *notifications.Client, region string) (*awstypes.NotificationHubOverview, error) {
244-
var input notifications.ListNotificationHubsInput
245-
246-
out, err := conn.ListNotificationHubs(ctx, &input)
247-
if err != nil {
248-
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
249-
return nil, &retry.NotFoundError{
250-
LastError: err,
251-
LastRequest: &input,
252-
}
253-
}
254-
return nil, err
236+
func waitNotificationHubCreated(ctx context.Context, conn *notifications.Client, region string, timeout time.Duration) (*awstypes.NotificationHubOverview, error) {
237+
stateConf := &retry.StateChangeConf{
238+
Pending: enum.Slice(awstypes.NotificationHubStatusRegistering),
239+
Target: enum.Slice(awstypes.NotificationHubStatusActive),
240+
Refresh: statusNotificationHub(ctx, conn, region),
241+
Timeout: timeout,
242+
ContinuousTargetOccurence: 2,
255243
}
256244

257-
if out == nil || out.NotificationHubs == nil {
258-
return nil, tfresource.NewEmptyResultError(&input)
245+
outputRaw, err := stateConf.WaitForStateContext(ctx)
246+
247+
if output, ok := outputRaw.(*awstypes.NotificationHubOverview); ok {
248+
tfresource.SetLastError(err, errors.New(aws.ToString(output.StatusSummary.Reason)))
249+
250+
return output, err
259251
}
260252

261-
for _, hub := range out.NotificationHubs {
262-
if aws.ToString(hub.NotificationHubRegion) == region {
263-
return &hub, nil
264-
}
253+
return nil, err
254+
}
255+
256+
func waitNotificationHubDeleted(ctx context.Context, conn *notifications.Client, region string, timeout time.Duration) (*awstypes.NotificationHubOverview, error) {
257+
stateConf := &retry.StateChangeConf{
258+
Pending: enum.Slice(awstypes.NotificationHubStatusDeregistering),
259+
Target: []string{},
260+
Refresh: statusNotificationHub(ctx, conn, region),
261+
Timeout: timeout,
265262
}
266263

267-
return nil, &retry.NotFoundError{
268-
LastError: fmt.Errorf("notification Hub for region %q not found", region),
269-
LastRequest: &input,
264+
outputRaw, err := stateConf.WaitForStateContext(ctx)
265+
266+
if output, ok := outputRaw.(*awstypes.NotificationHubOverview); ok {
267+
tfresource.SetLastError(err, errors.New(aws.ToString(output.StatusSummary.Reason)))
268+
269+
return output, err
270270
}
271+
272+
return nil, err
271273
}
272274

273275
type resourceNotificationHubModel struct {
274-
Region types.String `tfsdk:"region"`
275-
Timeouts timeouts.Value `tfsdk:"timeouts"`
276+
NotificationHubRegion types.String `tfsdk:"notification_hub_region"`
277+
Timeouts timeouts.Value `tfsdk:"timeouts"`
276278
}

0 commit comments

Comments
 (0)