Skip to content

Commit 6089f36

Browse files
committed
r/aws_iam_virtual_mfa_device: Use 'inttypes.IsZero'.
1 parent f30805f commit 6089f36

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

internal/service/iam/virtual_mfa_device.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99
"log"
10-
"reflect"
1110
"time"
1211

1312
"github.com/YakDriver/regexache"
@@ -16,14 +15,15 @@ import (
1615
"github.com/aws/aws-sdk-go-v2/service/iam"
1716
awstypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
1817
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
19-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
2018
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2119
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2220
"github.com/hashicorp/terraform-provider-aws/internal/conns"
2321
"github.com/hashicorp/terraform-provider-aws/internal/errs"
2422
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
23+
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
2524
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
2625
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
26+
inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
2727
"github.com/hashicorp/terraform-provider-aws/names"
2828
)
2929

@@ -89,20 +89,20 @@ func resourceVirtualMFADeviceCreate(ctx context.Context, d *schema.ResourceData,
8989
conn := meta.(*conns.AWSClient).IAMClient(ctx)
9090

9191
name := d.Get("virtual_mfa_device_name").(string)
92-
input := &iam.CreateVirtualMFADeviceInput{
92+
input := iam.CreateVirtualMFADeviceInput{
9393
Path: aws.String(d.Get(names.AttrPath).(string)),
9494
Tags: getTagsIn(ctx),
9595
VirtualMFADeviceName: aws.String(name),
9696
}
9797

98-
output, err := conn.CreateVirtualMFADevice(ctx, input)
98+
output, err := conn.CreateVirtualMFADevice(ctx, &input)
9999

100100
// Some partitions (e.g. ISO) may not support tag-on-create.
101101
partition := meta.(*conns.AWSClient).Partition(ctx)
102102
if input.Tags != nil && errs.IsUnsupportedOperationInPartitionError(partition, err) {
103103
input.Tags = nil
104104

105-
output, err = conn.CreateVirtualMFADevice(ctx, input)
105+
output, err = conn.CreateVirtualMFADevice(ctx, &input)
106106
}
107107

108108
if err != nil {
@@ -191,22 +191,26 @@ func resourceVirtualMFADeviceDelete(ctx context.Context, d *schema.ResourceData,
191191
conn := meta.(*conns.AWSClient).IAMClient(ctx)
192192

193193
if v := d.Get(names.AttrUserName); v != "" {
194-
_, err := conn.DeactivateMFADevice(ctx, &iam.DeactivateMFADeviceInput{
195-
UserName: aws.String(v.(string)),
194+
input := iam.DeactivateMFADeviceInput{
196195
SerialNumber: aws.String(d.Id()),
197-
})
196+
UserName: aws.String(v.(string)),
197+
}
198+
_, err := conn.DeactivateMFADevice(ctx, &input)
199+
198200
if errs.IsA[*awstypes.NoSuchEntityException](err) {
199201
return diags
200202
}
203+
201204
if err != nil {
202205
return sdkdiag.AppendErrorf(diags, "deactivating IAM Virtual MFA Device (%s): %s", d.Id(), err)
203206
}
204207
}
205208

206209
log.Printf("[INFO] Deleting IAM Virtual MFA Device: %s", d.Id())
207-
_, err := conn.DeleteVirtualMFADevice(ctx, &iam.DeleteVirtualMFADeviceInput{
210+
input := iam.DeleteVirtualMFADeviceInput{
208211
SerialNumber: aws.String(d.Id()),
209-
})
212+
}
213+
_, err := conn.DeleteVirtualMFADevice(ctx, &input)
210214

211215
if errs.IsA[*awstypes.NoSuchEntityException](err) {
212216
return diags
@@ -220,29 +224,42 @@ func resourceVirtualMFADeviceDelete(ctx context.Context, d *schema.ResourceData,
220224
}
221225

222226
func findVirtualMFADeviceBySerialNumber(ctx context.Context, conn *iam.Client, serialNumber string) (*awstypes.VirtualMFADevice, error) {
223-
input := &iam.ListVirtualMFADevicesInput{}
224-
var output awstypes.VirtualMFADevice
227+
var input iam.ListVirtualMFADevicesInput
228+
229+
return findVirtualMFADevice(ctx, conn, &input, func(v *awstypes.VirtualMFADevice) bool {
230+
return aws.ToString(v.SerialNumber) == serialNumber
231+
})
232+
}
233+
234+
func findVirtualMFADevice(ctx context.Context, conn *iam.Client, input *iam.ListVirtualMFADevicesInput, filter tfslices.Predicate[*awstypes.VirtualMFADevice]) (*awstypes.VirtualMFADevice, error) {
235+
output, err := findVirtualMFADevices(ctx, conn, input, filter)
236+
237+
if err != nil {
238+
return nil, err
239+
}
240+
241+
return tfresource.AssertSingleValueResult(output)
242+
}
243+
244+
func findVirtualMFADevices(ctx context.Context, conn *iam.Client, input *iam.ListVirtualMFADevicesInput, filter tfslices.Predicate[*awstypes.VirtualMFADevice]) ([]awstypes.VirtualMFADevice, error) {
245+
var output []awstypes.VirtualMFADevice
225246

226247
pages := iam.NewListVirtualMFADevicesPaginator(conn, input)
227248
for pages.HasMorePages() {
228249
page, err := pages.NextPage(ctx)
250+
229251
if err != nil {
230252
return nil, err
231253
}
232254

233255
for _, v := range page.VirtualMFADevices {
234-
if !reflect.ValueOf(v).IsZero() && aws.ToString(v.SerialNumber) == serialNumber {
235-
output = v
236-
break
256+
if p := &v; !inttypes.IsZero(p) && filter(p) {
257+
output = append(output, v)
237258
}
238259
}
239260
}
240261

241-
if reflect.ValueOf(output).IsZero() {
242-
return nil, &retry.NotFoundError{}
243-
}
244-
245-
return &output, nil
262+
return output, nil
246263
}
247264

248265
func parseVirtualMFADeviceARN(s string) (path, name string, err error) {

0 commit comments

Comments
 (0)