@@ -21,12 +21,12 @@ import (
21
21
"github.com/pkg/errors"
22
22
23
23
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
24
+ ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
24
25
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
25
26
svcapitypes "github.com/aws-controllers-k8s/s3-controller/apis/v1alpha1"
26
27
"github.com/aws/aws-sdk-go-v2/aws"
27
28
svcsdk "github.com/aws/aws-sdk-go-v2/service/s3"
28
29
svcsdktypes "github.com/aws/aws-sdk-go-v2/service/s3/types"
29
- "github.com/aws/smithy-go"
30
30
)
31
31
32
32
// bucketARN returns the ARN of the S3 bucket with the given name.
@@ -319,22 +319,27 @@ func (rm *resourceManager) addPutFieldsToSpec(
319
319
// This method is not supported in every region, ignore any errors if
320
320
// we attempt to describe this property in a region in which it's not
321
321
// supported.
322
- var awsErr smithy.APIError
323
- if errors .As (err , & awsErr ) && (awsErr .ErrorCode () == "MethodNotAllowed" || awsErr .ErrorCode () == "UnsupportedArgument" ) {
324
- getAccelerateResponse = & svcsdk.GetBucketAccelerateConfigurationOutput {}
325
- } else {
322
+ if awsErr , ok := ackerr .AWSError (err ); ! ok || (awsErr .ErrorCode () != "MethodNotAllowed" && awsErr .ErrorCode () != "UnsupportedArgument" ) {
326
323
return err
327
324
}
328
325
}
329
- ko .Spec .Accelerate = rm .setResourceAccelerate (r , getAccelerateResponse )
326
+ if getAccelerateResponse .Status != "" {
327
+ ko .Spec .Accelerate = rm .setResourceAccelerate (r , getAccelerateResponse )
328
+ } else {
329
+ ko .Spec .Accelerate = nil
330
+ }
330
331
331
332
listAnalyticsResponse , err := rm .sdkapi .ListBucketAnalyticsConfigurations (ctx , rm .newListBucketAnalyticsPayload (r ))
332
333
if err != nil {
333
334
return err
334
335
}
335
- ko .Spec .Analytics = make ([]* svcapitypes.AnalyticsConfiguration , len (listAnalyticsResponse .AnalyticsConfigurationList ))
336
- for i , analyticsConfiguration := range listAnalyticsResponse .AnalyticsConfigurationList {
337
- ko .Spec .Analytics [i ] = rm .setResourceAnalyticsConfiguration (r , analyticsConfiguration )
336
+ if listAnalyticsResponse != nil && len (listAnalyticsResponse .AnalyticsConfigurationList ) > 0 {
337
+ ko .Spec .Analytics = make ([]* svcapitypes.AnalyticsConfiguration , len (listAnalyticsResponse .AnalyticsConfigurationList ))
338
+ for i , analyticsConfiguration := range listAnalyticsResponse .AnalyticsConfigurationList {
339
+ ko .Spec .Analytics [i ] = rm .setResourceAnalyticsConfiguration (r , analyticsConfiguration )
340
+ }
341
+ } else {
342
+ ko .Spec .Analytics = nil
338
343
}
339
344
340
345
getACLResponse , err := rm .sdkapi .GetBucketAcl (ctx , rm .newGetBucketACLPayload (r ))
@@ -345,131 +350,142 @@ func (rm *resourceManager) addPutFieldsToSpec(
345
350
346
351
getCORSResponse , err := rm .sdkapi .GetBucketCors (ctx , rm .newGetBucketCORSPayload (r ))
347
352
if err != nil {
348
- var awsErr smithy.APIError
349
- if errors .As (err , & awsErr ) && awsErr .ErrorCode () == "NoSuchCORSConfiguration" {
350
- getCORSResponse = & svcsdk.GetBucketCorsOutput {}
351
- } else {
353
+ if awsErr , ok := ackerr .AWSError (err ); ! ok || awsErr .ErrorCode () != "NoSuchCORSConfiguration" {
352
354
return err
353
355
}
354
356
}
355
- ko .Spec .CORS = rm .setResourceCORS (r , getCORSResponse )
357
+ if getCORSResponse != nil {
358
+ ko .Spec .CORS = rm .setResourceCORS (r , getCORSResponse )
359
+ } else {
360
+ ko .Spec .CORS = nil
361
+ }
356
362
357
363
getEncryptionResponse , err := rm .sdkapi .GetBucketEncryption (ctx , rm .newGetBucketEncryptionPayload (r ))
358
364
if err != nil {
359
- var awsErr smithy.APIError
360
- if errors .As (err , & awsErr ) && awsErr .ErrorCode () == "ServerSideEncryptionConfigurationNotFoundError" {
361
- getEncryptionResponse = & svcsdk.GetBucketEncryptionOutput {
362
- ServerSideEncryptionConfiguration : & svcsdktypes.ServerSideEncryptionConfiguration {},
363
- }
364
- } else {
365
+ if awsErr , ok := ackerr .AWSError (err ); ! ok || awsErr .ErrorCode () != "ServerSideEncryptionConfigurationNotFoundError" {
365
366
return err
366
367
}
367
368
}
368
- ko .Spec .Encryption = rm .setResourceEncryption (r , getEncryptionResponse )
369
+ if getEncryptionResponse .ServerSideEncryptionConfiguration .Rules != nil {
370
+ ko .Spec .Encryption = rm .setResourceEncryption (r , getEncryptionResponse )
371
+ } else {
372
+ ko .Spec .Encryption = nil
373
+ }
369
374
370
375
listIntelligentTieringResponse , err := rm .sdkapi .ListBucketIntelligentTieringConfigurations (ctx , rm .newListBucketIntelligentTieringPayload (r ))
371
376
if err != nil {
372
377
return err
373
378
}
374
- ko .Spec .IntelligentTiering = make ([]* svcapitypes.IntelligentTieringConfiguration , len (listIntelligentTieringResponse .IntelligentTieringConfigurationList ))
375
- for i , intelligentTieringConfiguration := range listIntelligentTieringResponse .IntelligentTieringConfigurationList {
376
- ko .Spec .IntelligentTiering [i ] = rm .setResourceIntelligentTieringConfiguration (r , intelligentTieringConfiguration )
379
+ if len (listIntelligentTieringResponse .IntelligentTieringConfigurationList ) > 0 {
380
+ ko .Spec .IntelligentTiering = make ([]* svcapitypes.IntelligentTieringConfiguration , len (listIntelligentTieringResponse .IntelligentTieringConfigurationList ))
381
+ for i , intelligentTieringConfiguration := range listIntelligentTieringResponse .IntelligentTieringConfigurationList {
382
+ ko .Spec .IntelligentTiering [i ] = rm .setResourceIntelligentTieringConfiguration (r , intelligentTieringConfiguration )
383
+ }
384
+ } else {
385
+ ko .Spec .IntelligentTiering = nil
377
386
}
378
387
379
388
listInventoryResponse , err := rm .sdkapi .ListBucketInventoryConfigurations (ctx , rm .newListBucketInventoryPayload (r ))
380
389
if err != nil {
381
390
return err
382
391
}
392
+
383
393
ko .Spec .Inventory = make ([]* svcapitypes.InventoryConfiguration , len (listInventoryResponse .InventoryConfigurationList ))
384
394
for i , inventoryConfiguration := range listInventoryResponse .InventoryConfigurationList {
385
395
ko .Spec .Inventory [i ] = rm .setResourceInventoryConfiguration (r , inventoryConfiguration )
386
396
}
387
397
388
398
getLifecycleResponse , err := rm .sdkapi .GetBucketLifecycleConfiguration (ctx , rm .newGetBucketLifecyclePayload (r ))
389
399
if err != nil {
390
- var awsErr smithy.APIError
391
- if errors .As (err , & awsErr ) && awsErr .ErrorCode () == "NoSuchLifecycleConfiguration" {
392
- getLifecycleResponse = & svcsdk.GetBucketLifecycleConfigurationOutput {}
393
- } else {
400
+ if awsErr , ok := ackerr .AWSError (err ); ! ok || awsErr .ErrorCode () != "NoSuchLifecycleConfiguration" {
394
401
return err
395
402
}
396
403
}
397
- ko .Spec .Lifecycle = rm .setResourceLifecycle (r , getLifecycleResponse )
404
+ if getLifecycleResponse != nil {
405
+ ko .Spec .Lifecycle = rm .setResourceLifecycle (r , getLifecycleResponse )
406
+ } else {
407
+ ko .Spec .Lifecycle = nil
408
+ }
398
409
399
410
getLoggingResponse , err := rm .sdkapi .GetBucketLogging (ctx , rm .newGetBucketLoggingPayload (r ))
400
411
if err != nil {
401
412
return err
402
413
}
403
- ko .Spec .Logging = rm .setResourceLogging (r , getLoggingResponse )
414
+ if getLoggingResponse .LoggingEnabled != nil {
415
+ ko .Spec .Logging = rm .setResourceLogging (r , getLoggingResponse )
416
+ } else {
417
+ ko .Spec .Logging = nil
418
+ }
404
419
405
420
listMetricsResponse , err := rm .sdkapi .ListBucketMetricsConfigurations (ctx , rm .newListBucketMetricsPayload (r ))
406
421
if err != nil {
407
422
return err
408
423
}
409
- ko .Spec .Metrics = make ([]* svcapitypes.MetricsConfiguration , len (listMetricsResponse .MetricsConfigurationList ))
410
- for i , metricsConfiguration := range listMetricsResponse .MetricsConfigurationList {
411
- ko .Spec .Metrics [i ] = rm .setResourceMetricsConfiguration (r , & metricsConfiguration )
424
+ if len (listMetricsResponse .MetricsConfigurationList ) > 0 {
425
+ ko .Spec .Metrics = make ([]* svcapitypes.MetricsConfiguration , len (listMetricsResponse .MetricsConfigurationList ))
426
+ for i , metricsConfiguration := range listMetricsResponse .MetricsConfigurationList {
427
+ ko .Spec .Metrics [i ] = rm .setResourceMetricsConfiguration (r , & metricsConfiguration )
428
+ }
429
+ } else {
430
+ ko .Spec .Metrics = nil
412
431
}
413
432
414
433
getNotificationResponse , err := rm .sdkapi .GetBucketNotificationConfiguration (ctx , rm .newGetBucketNotificationPayload (r ))
415
434
if err != nil {
416
435
return err
417
436
}
418
- ko .Spec .Notification = rm .setResourceNotification (r , getNotificationResponse )
437
+ if getNotificationResponse .LambdaFunctionConfigurations != nil ||
438
+ getNotificationResponse .QueueConfigurations != nil ||
439
+ getNotificationResponse .TopicConfigurations != nil {
440
+
441
+ ko .Spec .Notification = rm .setResourceNotification (r , getNotificationResponse )
442
+ } else {
443
+ ko .Spec .Notification = nil
444
+ }
419
445
420
446
getOwnershipControlsResponse , err := rm .sdkapi .GetBucketOwnershipControls (ctx , rm .newGetBucketOwnershipControlsPayload (r ))
421
447
if err != nil {
422
- var awsErr smithy.APIError
423
- if errors .As (err , & awsErr ) && awsErr .ErrorCode () == "OwnershipControlsNotFoundError" {
424
- getOwnershipControlsResponse = & svcsdk.GetBucketOwnershipControlsOutput {
425
- OwnershipControls : & svcsdktypes.OwnershipControls {},
426
- }
427
- } else {
448
+ if awsErr , ok := ackerr .AWSError (err ); ! ok || awsErr .ErrorCode () != "OwnershipControlsNotFoundError" {
428
449
return err
429
450
}
430
451
}
431
- if getOwnershipControlsResponse . OwnershipControls != nil {
452
+ if getOwnershipControlsResponse != nil {
432
453
ko .Spec .OwnershipControls = rm .setResourceOwnershipControls (r , getOwnershipControlsResponse )
433
454
} else {
434
455
ko .Spec .OwnershipControls = nil
435
456
}
436
457
437
458
getPolicyResponse , err := rm .sdkapi .GetBucketPolicy (ctx , rm .newGetBucketPolicyPayload (r ))
438
459
if err != nil {
439
- var awsErr smithy.APIError
440
- if errors .As (err , & awsErr ) && awsErr .ErrorCode () == "NoSuchBucketPolicy" {
441
- getPolicyResponse = & svcsdk.GetBucketPolicyOutput {}
442
- } else {
460
+ if awsErr , ok := ackerr .AWSError (err ); ! ok || awsErr .ErrorCode () != "NoSuchBucketPolicy" {
443
461
return err
444
462
}
445
463
}
446
- ko .Spec .Policy = getPolicyResponse .Policy
464
+ if getPolicyResponse != nil {
465
+ ko .Spec .Policy = getPolicyResponse .Policy
466
+ } else {
467
+ ko .Spec .Policy = nil
468
+ }
447
469
448
470
getPublicAccessBlockResponse , err := rm .sdkapi .GetPublicAccessBlock (ctx , rm .newGetPublicAccessBlockPayload (r ))
449
471
if err != nil {
450
- var awsErr smithy.APIError
451
- if errors .As (err , & awsErr ) && awsErr .ErrorCode () == "NoSuchPublicAccessBlockConfiguration" {
452
- getPublicAccessBlockResponse = & svcsdk.GetPublicAccessBlockOutput {}
453
- } else {
472
+ if awsErr , ok := ackerr .AWSError (err ); ! ok || awsErr .ErrorCode () != "NoSuchPublicAccessBlockConfiguration" {
454
473
return err
455
474
}
456
475
}
457
- if getPublicAccessBlockResponse . PublicAccessBlockConfiguration != nil {
476
+ if getPublicAccessBlockResponse != nil {
458
477
ko .Spec .PublicAccessBlock = rm .setResourcePublicAccessBlock (r , getPublicAccessBlockResponse )
459
478
} else {
460
479
ko .Spec .PublicAccessBlock = nil
461
480
}
462
481
463
482
getReplicationResponse , err := rm .sdkapi .GetBucketReplication (ctx , rm .newGetBucketReplicationPayload (r ))
464
483
if err != nil {
465
- var awsErr smithy.APIError
466
- if errors .As (err , & awsErr ) && awsErr .ErrorCode () == "ReplicationConfigurationNotFoundError" {
467
- getReplicationResponse = & svcsdk.GetBucketReplicationOutput {}
468
- } else {
484
+ if awsErr , ok := ackerr .AWSError (err ); ! ok || awsErr .ErrorCode () != "ReplicationConfigurationNotFoundError" {
469
485
return err
470
486
}
471
487
}
472
- if getReplicationResponse . ReplicationConfiguration != nil {
488
+ if getReplicationResponse != nil {
473
489
ko .Spec .Replication = rm .setResourceReplication (r , getReplicationResponse )
474
490
} else {
475
491
ko .Spec .Replication = nil
@@ -479,35 +495,46 @@ func (rm *resourceManager) addPutFieldsToSpec(
479
495
if err != nil {
480
496
return nil
481
497
}
482
- ko .Spec .RequestPayment = rm .setResourceRequestPayment (r , getRequestPaymentResponse )
498
+ if getRequestPaymentResponse .Payer != "" {
499
+ ko .Spec .RequestPayment = rm .setResourceRequestPayment (r , getRequestPaymentResponse )
500
+ } else {
501
+ ko .Spec .RequestPayment = nil
502
+ }
483
503
484
504
getTaggingResponse , err := rm .sdkapi .GetBucketTagging (ctx , rm .newGetBucketTaggingPayload (r ))
485
505
if err != nil {
486
- var awsErr smithy.APIError
487
- if errors .As (err , & awsErr ) && awsErr .ErrorCode () == "NoSuchTagSet" {
488
- getTaggingResponse = & svcsdk.GetBucketTaggingOutput {}
489
- } else {
506
+ if awsErr , ok := ackerr .AWSError (err ); ! ok || awsErr .ErrorCode () != "NoSuchTagSet" {
490
507
return err
491
508
}
492
509
}
493
- ko .Spec .Tagging = rm .setResourceTagging (r , getTaggingResponse )
510
+ if getTaggingResponse != nil && getTaggingResponse .TagSet != nil {
511
+ ko .Spec .Tagging = rm .setResourceTagging (r , getTaggingResponse )
512
+ } else {
513
+ ko .Spec .Tagging = nil
514
+ }
494
515
495
516
getVersioningResponse , err := rm .sdkapi .GetBucketVersioning (ctx , rm .newGetBucketVersioningPayload (r ))
496
517
if err != nil {
497
518
return err
498
519
}
499
- ko .Spec .Versioning = rm .setResourceVersioning (r , getVersioningResponse )
520
+ if getVersioningResponse .Status != "" {
521
+ ko .Spec .Versioning = rm .setResourceVersioning (r , getVersioningResponse )
522
+ } else {
523
+ ko .Spec .Versioning = nil
524
+ }
500
525
501
526
getWebsiteResponse , err := rm .sdkapi .GetBucketWebsite (ctx , rm .newGetBucketWebsitePayload (r ))
502
527
if err != nil {
503
- var awsErr smithy.APIError
504
- if errors .As (err , & awsErr ) && awsErr .ErrorCode () == "NoSuchWebsiteConfiguration" {
505
- getWebsiteResponse = & svcsdk.GetBucketWebsiteOutput {}
506
- } else {
528
+ if awsErr , ok := ackerr .AWSError (err ); ! ok || awsErr .ErrorCode () != "NoSuchWebsiteConfiguration" {
507
529
return err
508
530
}
509
531
}
510
- ko .Spec .Website = rm .setResourceWebsite (r , getWebsiteResponse )
532
+ if getWebsiteResponse != nil {
533
+ ko .Spec .Website = rm .setResourceWebsite (r , getWebsiteResponse )
534
+ } else {
535
+ ko .Spec .Website = nil
536
+ }
537
+
511
538
return nil
512
539
}
513
540
@@ -700,16 +727,28 @@ func (rm *resourceManager) setResourceACL(
700
727
resp * svcsdk.GetBucketAclOutput ,
701
728
) {
702
729
grants := GetHeadersFromGrants (resp )
703
- ko .Spec .GrantFullControl = & grants .FullControl
704
- ko .Spec .GrantRead = & grants .Read
705
- ko .Spec .GrantReadACP = & grants .ReadACP
706
- ko .Spec .GrantWrite = & grants .Write
707
- ko .Spec .GrantWriteACP = & grants .WriteACP
730
+ if grants .FullControl != "" {
731
+ ko .Spec .GrantFullControl = & grants .FullControl
732
+ }
733
+ if grants .Read != "" {
734
+ ko .Spec .GrantRead = & grants .Read
735
+ }
736
+ if grants .ReadACP != "" {
737
+ ko .Spec .GrantReadACP = & grants .ReadACP
738
+ }
739
+ if grants .Write != "" {
740
+ ko .Spec .GrantWrite = & grants .Write
741
+ }
742
+ if grants .WriteACP != "" {
743
+ ko .Spec .GrantWriteACP = & grants .WriteACP
744
+ }
708
745
709
746
// Join possible ACLs into a single string, delimited by bar
710
747
cannedACLs := GetPossibleCannedACLsFromGrants (resp )
711
748
joinedACLs := strings .Join (cannedACLs , CannedACLJoinDelimiter )
712
- ko .Spec .ACL = & joinedACLs
749
+ if joinedACLs != "" {
750
+ ko .Spec .ACL = & joinedACLs
751
+ }
713
752
}
714
753
715
754
func (rm * resourceManager ) newGetBucketACLPayload (
0 commit comments