@@ -31,14 +31,14 @@ import (
3131 kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types"
3232 "github.com/aws/aws-sdk-go-v2/service/sts"
3333 "github.com/aws/smithy-go"
34- "github.com/onsi/ginkgo/v2/dsl/core"
34+ . "github.com/onsi/ginkgo/v2"
35+ . "github.com/onsi/gomega"
3536
3637 "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/pointer"
3738 awshelper "github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/e2e/api/aws"
3839)
3940
4041type AwsAction struct {
41- t core.GinkgoTInterface
4242 network * awsNetwork
4343
4444 cfg aws.Config
@@ -67,8 +67,6 @@ type statement struct {
6767}
6868
6969func (a * AwsAction ) CreateKMS (ctx context.Context , alias , region , atlasAccountArn , assumedRoleArn string ) (string , error ) {
70- a .t .Helper ()
71-
7270 //kmsClient := kms.New(a.session, aws.NewConfig().WithRegion(region))
7371 kmsClient := kms .NewFromConfig (a .cfg , func (o * kms.Options ) {
7472 o .Region = region
@@ -109,18 +107,14 @@ func (a *AwsAction) CreateKMS(ctx context.Context, alias, region, atlasAccountAr
109107 TargetKeyId : key .KeyMetadata .KeyId ,
110108 })
111109
112- if err != nil {
113- a .t .Log (fmt .Sprintf ("failed to create alias to key %s(%s): %s" , alias , * key .KeyMetadata .KeyId , err ))
114- }
110+ Expect (err ).NotTo (HaveOccurred ())
115111
116- a . t . Cleanup (func () {
112+ DeferCleanup (func (ctx SpecContext ) error {
117113 _ , err = kmsClient .ScheduleKeyDeletion (ctx , & kms.ScheduleKeyDeletionInput {
118114 KeyId : key .KeyMetadata .KeyId ,
119115 PendingWindowInDays : aws .Int32 (7 ), // this is the minimum possible and can be up to 24h longer than value set
120116 })
121- if err != nil {
122- a .t .Error (err )
123- }
117+ return err
124118 })
125119
126120 return * key .KeyMetadata .KeyId , nil
@@ -206,8 +200,6 @@ func defaultKMSPolicy(atlasAccountArn, assumedRoleArn string, adminARNs []string
206200}
207201
208202func (a * AwsAction ) GetAccountID (ctx context.Context ) (string , error ) {
209- a .t .Helper ()
210-
211203 stsClient := sts .NewFromConfig (a .cfg )
212204 identity , err := stsClient .GetCallerIdentity (ctx , & sts.GetCallerIdentityInput {})
213205 if err != nil {
@@ -218,8 +210,6 @@ func (a *AwsAction) GetAccountID(ctx context.Context) (string, error) {
218210}
219211
220212func (a * AwsAction ) InitNetwork (ctx context.Context , vpcName , cidr , region string , subnets map [string ]string , cleanup bool ) (string , error ) {
221- a .t .Helper ()
222-
223213 vpc , err := a .findVPC (ctx , vpcName , region )
224214 if err != nil {
225215 return "" , err
@@ -233,11 +223,8 @@ func (a *AwsAction) InitNetwork(ctx context.Context, vpcName, cidr, region strin
233223 }
234224
235225 if cleanup {
236- a .t .Cleanup (func () {
237- err = a .deleteVPC (ctx , vpc , region )
238- if err != nil {
239- a .t .Error (err )
240- }
226+ DeferCleanup (func (ctx SpecContext ) error {
227+ return a .deleteVPC (ctx , vpc , region )
241228 })
242229 }
243230
@@ -259,11 +246,8 @@ func (a *AwsAction) InitNetwork(ctx context.Context, vpcName, cidr, region strin
259246 }
260247
261248 if cleanup {
262- a .t .Cleanup (func () {
263- err = a .deleteSubnet (ctx , subnetID , region )
264- if err != nil {
265- a .t .Error (err )
266- }
249+ DeferCleanup (func (ctx SpecContext ) error {
250+ return a .deleteSubnet (ctx , subnetID , region )
267251 })
268252 }
269253 }
@@ -281,8 +265,6 @@ func (a *AwsAction) InitNetwork(ctx context.Context, vpcName, cidr, region strin
281265}
282266
283267func (a * AwsAction ) CreatePrivateEndpoint (ctx context.Context , serviceName , privateEndpointName , region string ) (string , error ) {
284- a .t .Helper ()
285-
286268 ec2Client := ec2 .NewFromConfig (a .cfg , func (o * ec2.Options ) {
287269 o .Region = region
288270 })
@@ -305,19 +287,19 @@ func (a *AwsAction) CreatePrivateEndpoint(ctx context.Context, serviceName, priv
305287 }
306288 vpcEndpointId := aws .ToString (result .VpcEndpoint .VpcEndpointId )
307289
308- a . t . Cleanup (func () {
290+ DeferCleanup (func (ctx SpecContext ) error {
309291 deleteInput := & ec2.DeleteVpcEndpointsInput {
310292 VpcEndpointIds : []string {vpcEndpointId },
311293 }
312294 _ , err = ec2Client .DeleteVpcEndpoints (ctx , deleteInput )
313295 if err != nil {
314- a . t . Error ( err )
296+ return err
315297 }
316298
317299 timeout := 10 * time .Minute
318300 start := time .Now ()
319301 for {
320- a . t . Log ( fmt .Sprintf ( "deleting VPC ID %q since %v" , vpcEndpointId , time .Since (start ) ))
302+ fmt .Fprintf ( GinkgoWriter , "deleting VPC ID %q since %v\n " , vpcEndpointId , time .Since (start ))
321303
322304 output , err := ec2Client .DescribeVpcEndpoints (ctx , & ec2.DescribeVpcEndpointsInput {
323305 VpcEndpointIds : []string {vpcEndpointId },
@@ -326,20 +308,17 @@ func (a *AwsAction) CreatePrivateEndpoint(ctx context.Context, serviceName, priv
326308 if err != nil {
327309 var apiErr * smithy.GenericAPIError
328310 if errors .As (err , & apiErr ) && apiErr .Code == "InvalidVpcEndpointId.NotFound" {
329- return
311+ return nil
330312 }
331-
332- a .t .Error (err )
333- return
313+ return err
334314 }
335315
336316 if len (output .VpcEndpoints ) == 0 {
337- return
317+ return nil
338318 }
339319
340320 if time .Since (start ) > timeout {
341- a .t .Error (errors .New ("timeout waiting for deletion of vpc endpoints" ))
342- return
321+ return errors .New ("timeout waiting for deletion of vpc endpoints" )
343322 }
344323
345324 // we do know that deletion of VPC endpoints takes time
@@ -351,8 +330,6 @@ func (a *AwsAction) CreatePrivateEndpoint(ctx context.Context, serviceName, priv
351330}
352331
353332func (a * AwsAction ) GetPrivateEndpoint (ctx context.Context , endpointID , region string ) (ec2types.VpcEndpoint , error ) {
354- a .t .Helper ()
355-
356333 ec2Client := ec2 .NewFromConfig (a .cfg , func (o * ec2.Options ) {
357334 o .Region = region
358335 })
@@ -370,8 +347,6 @@ func (a *AwsAction) GetPrivateEndpoint(ctx context.Context, endpointID, region s
370347}
371348
372349func (a * AwsAction ) AcceptVpcPeeringConnection (ctx context.Context , connectionID , region string ) error {
373- a .t .Helper ()
374-
375350 ec2Client := ec2 .NewFromConfig (a .cfg , func (o * ec2.Options ) {
376351 o .Region = region
377352 })
@@ -383,24 +358,20 @@ func (a *AwsAction) AcceptVpcPeeringConnection(ctx context.Context, connectionID
383358 },
384359 )
385360
386- a . t . Cleanup (func () {
361+ DeferCleanup (func (ctx SpecContext ) error {
387362 _ , err = ec2Client .DeleteVpcPeeringConnection (
388363 ctx ,
389364 & ec2.DeleteVpcPeeringConnectionInput {
390365 VpcPeeringConnectionId : aws .String (connectionID ),
391366 },
392367 )
393- if err != nil {
394- a .t .Error (err )
395- }
368+ return err
396369 })
397370
398371 return err
399372}
400373
401374func (a * AwsAction ) findVPC (ctx context.Context , name , region string ) (string , error ) {
402- a .t .Helper ()
403-
404375 ec2Client := ec2 .NewFromConfig (a .cfg , func (o * ec2.Options ) {
405376 o .Region = region
406377 })
@@ -424,8 +395,6 @@ func (a *AwsAction) findVPC(ctx context.Context, name, region string) (string, e
424395}
425396
426397func (a * AwsAction ) createVPC (ctx context.Context , name , cidr , region string ) (string , error ) {
427- a .t .Helper ()
428-
429398 ec2Client := ec2 .NewFromConfig (a .cfg , func (o * ec2.Options ) {
430399 o .Region = region
431400 })
@@ -464,8 +433,6 @@ func (a *AwsAction) createVPC(ctx context.Context, name, cidr, region string) (s
464433}
465434
466435func (a * AwsAction ) deleteVPC (ctx context.Context , id , region string ) error {
467- a .t .Helper ()
468-
469436 ec2Client := ec2 .NewFromConfig (a .cfg , func (o * ec2.Options ) {
470437 o .Region = region
471438 })
@@ -481,8 +448,6 @@ func (a *AwsAction) deleteVPC(ctx context.Context, id, region string) error {
481448}
482449
483450func (a * AwsAction ) getSubnets (ctx context.Context , vpcID , region string ) (map [string ]string , error ) {
484- a .t .Helper ()
485-
486451 ec2Client := ec2 .NewFromConfig (a .cfg , func (o * ec2.Options ) {
487452 o .Region = region
488453 })
@@ -511,8 +476,6 @@ func (a *AwsAction) getSubnets(ctx context.Context, vpcID, region string) (map[s
511476}
512477
513478func (a * AwsAction ) createSubnet (ctx context.Context , vpcID , name , cidr , region , az string ) (string , error ) {
514- a .t .Helper ()
515-
516479 ec2Client := ec2 .NewFromConfig (a .cfg , func (o * ec2.Options ) {
517480 o .Region = region
518481 })
@@ -541,8 +504,6 @@ func (a *AwsAction) createSubnet(ctx context.Context, vpcID, name, cidr, region,
541504}
542505
543506func (a * AwsAction ) deleteSubnet (ctx context.Context , subnetID string , region string ) error {
544- a .t .Helper ()
545-
546507 ec2Client := ec2 .NewFromConfig (a .cfg , func (o * ec2.Options ) {
547508 o .Region = region
548509 })
@@ -558,16 +519,12 @@ func (a *AwsAction) deleteSubnet(ctx context.Context, subnetID string, region st
558519 return nil
559520}
560521
561- func NewAWSAction (ctx context.Context , t core.GinkgoTInterface ) (* AwsAction , error ) {
562- t .Helper ()
563-
522+ func NewAWSAction (ctx context.Context ) (* AwsAction , error ) {
564523 cfg , err := config .LoadDefaultConfig (ctx )
565524 if err != nil {
566525 return nil , err
567526 }
568527 return & AwsAction {
569- t : t ,
570-
571528 cfg : cfg ,
572529 }, nil
573530}
0 commit comments