7
7
"context"
8
8
"fmt"
9
9
"log"
10
- "reflect"
11
10
"time"
12
11
13
12
"github.com/YakDriver/regexache"
@@ -16,14 +15,15 @@ import (
16
15
"github.com/aws/aws-sdk-go-v2/service/iam"
17
16
awstypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
18
17
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
19
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
20
18
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
21
19
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
22
20
"github.com/hashicorp/terraform-provider-aws/internal/conns"
23
21
"github.com/hashicorp/terraform-provider-aws/internal/errs"
24
22
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
23
+ tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
25
24
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
26
25
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
26
+ inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
27
27
"github.com/hashicorp/terraform-provider-aws/names"
28
28
)
29
29
@@ -89,20 +89,20 @@ func resourceVirtualMFADeviceCreate(ctx context.Context, d *schema.ResourceData,
89
89
conn := meta .(* conns.AWSClient ).IAMClient (ctx )
90
90
91
91
name := d .Get ("virtual_mfa_device_name" ).(string )
92
- input := & iam.CreateVirtualMFADeviceInput {
92
+ input := iam.CreateVirtualMFADeviceInput {
93
93
Path : aws .String (d .Get (names .AttrPath ).(string )),
94
94
Tags : getTagsIn (ctx ),
95
95
VirtualMFADeviceName : aws .String (name ),
96
96
}
97
97
98
- output , err := conn .CreateVirtualMFADevice (ctx , input )
98
+ output , err := conn .CreateVirtualMFADevice (ctx , & input )
99
99
100
100
// Some partitions (e.g. ISO) may not support tag-on-create.
101
101
partition := meta .(* conns.AWSClient ).Partition (ctx )
102
102
if input .Tags != nil && errs .IsUnsupportedOperationInPartitionError (partition , err ) {
103
103
input .Tags = nil
104
104
105
- output , err = conn .CreateVirtualMFADevice (ctx , input )
105
+ output , err = conn .CreateVirtualMFADevice (ctx , & input )
106
106
}
107
107
108
108
if err != nil {
@@ -191,22 +191,26 @@ func resourceVirtualMFADeviceDelete(ctx context.Context, d *schema.ResourceData,
191
191
conn := meta .(* conns.AWSClient ).IAMClient (ctx )
192
192
193
193
if v := d .Get (names .AttrUserName ); v != "" {
194
- _ , err := conn .DeactivateMFADevice (ctx , & iam.DeactivateMFADeviceInput {
195
- UserName : aws .String (v .(string )),
194
+ input := iam.DeactivateMFADeviceInput {
196
195
SerialNumber : aws .String (d .Id ()),
197
- })
196
+ UserName : aws .String (v .(string )),
197
+ }
198
+ _ , err := conn .DeactivateMFADevice (ctx , & input )
199
+
198
200
if errs.IsA [* awstypes.NoSuchEntityException ](err ) {
199
201
return diags
200
202
}
203
+
201
204
if err != nil {
202
205
return sdkdiag .AppendErrorf (diags , "deactivating IAM Virtual MFA Device (%s): %s" , d .Id (), err )
203
206
}
204
207
}
205
208
206
209
log .Printf ("[INFO] Deleting IAM Virtual MFA Device: %s" , d .Id ())
207
- _ , err := conn . DeleteVirtualMFADevice ( ctx , & iam.DeleteVirtualMFADeviceInput {
210
+ input := iam.DeleteVirtualMFADeviceInput {
208
211
SerialNumber : aws .String (d .Id ()),
209
- })
212
+ }
213
+ _ , err := conn .DeleteVirtualMFADevice (ctx , & input )
210
214
211
215
if errs.IsA [* awstypes.NoSuchEntityException ](err ) {
212
216
return diags
@@ -220,29 +224,42 @@ func resourceVirtualMFADeviceDelete(ctx context.Context, d *schema.ResourceData,
220
224
}
221
225
222
226
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
225
246
226
247
pages := iam .NewListVirtualMFADevicesPaginator (conn , input )
227
248
for pages .HasMorePages () {
228
249
page , err := pages .NextPage (ctx )
250
+
229
251
if err != nil {
230
252
return nil , err
231
253
}
232
254
233
255
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 )
237
258
}
238
259
}
239
260
}
240
261
241
- if reflect .ValueOf (output ).IsZero () {
242
- return nil , & retry.NotFoundError {}
243
- }
244
-
245
- return & output , nil
262
+ return output , nil
246
263
}
247
264
248
265
func parseVirtualMFADeviceARN (s string ) (path , name string , err error ) {
0 commit comments