Skip to content

Commit

Permalink
Exclude default/empty values from spec during a read operation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhtm committed Dec 17, 2024
1 parent 64382cd commit 8481230
Showing 1 changed file with 71 additions and 49 deletions.
120 changes: 71 additions & 49 deletions pkg/resource/bucket/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,21 +317,25 @@ func (rm *resourceManager) addPutFieldsToSpec(
// This method is not supported in every region, ignore any errors if
// we attempt to describe this property in a region in which it's not
// supported.
if awsErr, ok := ackerr.AWSError(err); ok && (awsErr.Code() == "MethodNotAllowed" || awsErr.Code() == "UnsupportedArgument") {
getAccelerateResponse = &svcsdk.GetBucketAccelerateConfigurationOutput{}
} else {
if awsErr, ok := ackerr.AWSError(err); !ok || (awsErr.Code() != "MethodNotAllowed" && awsErr.Code() != "UnsupportedArgument") {
return err
}
}
ko.Spec.Accelerate = rm.setResourceAccelerate(r, getAccelerateResponse)
if getAccelerateResponse.Status != nil && *getAccelerateResponse.Status != "" {
ko.Spec.Accelerate = rm.setResourceAccelerate(r, getAccelerateResponse)
} else if (getAccelerateResponse.Status == nil || *getAccelerateResponse.Status == "") && ko.Spec.Accelerate != nil {
ko.Spec.Accelerate = &svcapitypes.AccelerateConfiguration{}
}

listAnalyticsResponse, err := rm.sdkapi.ListBucketAnalyticsConfigurationsWithContext(ctx, rm.newListBucketAnalyticsPayload(r))
if err != nil {
return err
}
ko.Spec.Analytics = make([]*svcapitypes.AnalyticsConfiguration, len(listAnalyticsResponse.AnalyticsConfigurationList))
for i, analyticsConfiguration := range listAnalyticsResponse.AnalyticsConfigurationList {
ko.Spec.Analytics[i] = rm.setResourceAnalyticsConfiguration(r, analyticsConfiguration)
if listAnalyticsResponse != nil && len(listAnalyticsResponse.AnalyticsConfigurationList) > 0 {
ko.Spec.Analytics = make([]*svcapitypes.AnalyticsConfiguration, len(listAnalyticsResponse.AnalyticsConfigurationList))
for i, analyticsConfiguration := range listAnalyticsResponse.AnalyticsConfigurationList {
ko.Spec.Analytics[i] = rm.setResourceAnalyticsConfiguration(r, analyticsConfiguration)
}
}

getACLResponse, err := rm.sdkapi.GetBucketAclWithContext(ctx, rm.newGetBucketACLPayload(r))
Expand All @@ -342,42 +346,40 @@ func (rm *resourceManager) addPutFieldsToSpec(

getCORSResponse, err := rm.sdkapi.GetBucketCorsWithContext(ctx, rm.newGetBucketCORSPayload(r))
if err != nil {
if awsErr, ok := ackerr.AWSError(err); ok && awsErr.Code() == "NoSuchCORSConfiguration" {
getCORSResponse = &svcsdk.GetBucketCorsOutput{}
} else {
if awsErr, ok := ackerr.AWSError(err); !ok || awsErr.Code() != "NoSuchCORSConfiguration" {
return err
}
} else {
ko.Spec.CORS = rm.setResourceCORS(r, getCORSResponse)
}
ko.Spec.CORS = rm.setResourceCORS(r, getCORSResponse)

getEncryptionResponse, err := rm.sdkapi.GetBucketEncryptionWithContext(ctx, rm.newGetBucketEncryptionPayload(r))
if err != nil {
if awsErr, ok := ackerr.AWSError(err); ok && awsErr.Code() == "ServerSideEncryptionConfigurationNotFoundError" {
getEncryptionResponse = &svcsdk.GetBucketEncryptionOutput{
ServerSideEncryptionConfiguration: &svcsdk.ServerSideEncryptionConfiguration{},
}
} else {
if awsErr, ok := ackerr.AWSError(err); !ok || awsErr.Code() != "ServerSideEncryptionConfigurationNotFoundError" {
return err
}
} else {
ko.Spec.Encryption = rm.setResourceEncryption(r, getEncryptionResponse)
}
ko.Spec.Encryption = rm.setResourceEncryption(r, getEncryptionResponse)

listIntelligentTieringResponse, err := rm.sdkapi.ListBucketIntelligentTieringConfigurationsWithContext(ctx, rm.newListBucketIntelligentTieringPayload(r))
if err != nil {
return err
}
ko.Spec.IntelligentTiering = make([]*svcapitypes.IntelligentTieringConfiguration, len(listIntelligentTieringResponse.IntelligentTieringConfigurationList))
for i, intelligentTieringConfiguration := range listIntelligentTieringResponse.IntelligentTieringConfigurationList {
ko.Spec.IntelligentTiering[i] = rm.setResourceIntelligentTieringConfiguration(r, intelligentTieringConfiguration)
} else {
ko.Spec.IntelligentTiering = make([]*svcapitypes.IntelligentTieringConfiguration, len(listIntelligentTieringResponse.IntelligentTieringConfigurationList))
for i, intelligentTieringConfiguration := range listIntelligentTieringResponse.IntelligentTieringConfigurationList {
ko.Spec.IntelligentTiering[i] = rm.setResourceIntelligentTieringConfiguration(r, intelligentTieringConfiguration)
}
}

listInventoryResponse, err := rm.sdkapi.ListBucketInventoryConfigurationsWithContext(ctx, rm.newListBucketInventoryPayload(r))
if err != nil {
return err
}
ko.Spec.Inventory = make([]*svcapitypes.InventoryConfiguration, len(listInventoryResponse.InventoryConfigurationList))
for i, inventoryConfiguration := range listInventoryResponse.InventoryConfigurationList {
ko.Spec.Inventory[i] = rm.setResourceInventoryConfiguration(r, inventoryConfiguration)
} else {
ko.Spec.Inventory = make([]*svcapitypes.InventoryConfiguration, len(listInventoryResponse.InventoryConfigurationList))
for i, inventoryConfiguration := range listInventoryResponse.InventoryConfigurationList {
ko.Spec.Inventory[i] = rm.setResourceInventoryConfiguration(r, inventoryConfiguration)
}
}

getLifecycleResponse, err := rm.sdkapi.GetBucketLifecycleConfigurationWithContext(ctx, rm.newGetBucketLifecyclePayload(r))
Expand All @@ -387,29 +389,38 @@ func (rm *resourceManager) addPutFieldsToSpec(
} else {
return err
}
} else {
ko.Spec.Lifecycle = rm.setResourceLifecycle(r, getLifecycleResponse)
}
ko.Spec.Lifecycle = rm.setResourceLifecycle(r, getLifecycleResponse)

getLoggingResponse, err := rm.sdkapi.GetBucketLoggingWithContext(ctx, rm.newGetBucketLoggingPayload(r))
if err != nil {
return err
}
ko.Spec.Logging = rm.setResourceLogging(r, getLoggingResponse)
if getLoggingResponse.LoggingEnabled != nil {
ko.Spec.Logging = rm.setResourceLogging(r, getLoggingResponse)
}

listMetricsResponse, err := rm.sdkapi.ListBucketMetricsConfigurationsWithContext(ctx, rm.newListBucketMetricsPayload(r))
if err != nil {
return err
}
ko.Spec.Metrics = make([]*svcapitypes.MetricsConfiguration, len(listMetricsResponse.MetricsConfigurationList))
for i, metricsConfiguration := range listMetricsResponse.MetricsConfigurationList {
ko.Spec.Metrics[i] = rm.setResourceMetricsConfiguration(r, metricsConfiguration)
} else {
ko.Spec.Metrics = make([]*svcapitypes.MetricsConfiguration, len(listMetricsResponse.MetricsConfigurationList))
for i, metricsConfiguration := range listMetricsResponse.MetricsConfigurationList {
ko.Spec.Metrics[i] = rm.setResourceMetricsConfiguration(r, metricsConfiguration)
}
}

getNotificationResponse, err := rm.sdkapi.GetBucketNotificationConfigurationWithContext(ctx, rm.newGetBucketNotificationPayload(r))
if err != nil {
return err
}
ko.Spec.Notification = rm.setResourceNotification(r, getNotificationResponse)
if getNotificationResponse.LambdaFunctionConfigurations != nil ||
getNotificationResponse.QueueConfigurations != nil ||
getNotificationResponse.TopicConfigurations != nil {

ko.Spec.Notification = rm.setResourceNotification(r, getNotificationResponse)
}

getOwnershipControlsResponse, err := rm.sdkapi.GetBucketOwnershipControlsWithContext(ctx, rm.newGetBucketOwnershipControlsPayload(r))
if err != nil {
Expand All @@ -423,8 +434,6 @@ func (rm *resourceManager) addPutFieldsToSpec(
}
if getOwnershipControlsResponse.OwnershipControls != nil {
ko.Spec.OwnershipControls = rm.setResourceOwnershipControls(r, getOwnershipControlsResponse)
} else {
ko.Spec.OwnershipControls = nil
}

getPolicyResponse, err := rm.sdkapi.GetBucketPolicyWithContext(ctx, rm.newGetBucketPolicyPayload(r))
Expand All @@ -434,8 +443,9 @@ func (rm *resourceManager) addPutFieldsToSpec(
} else {
return err
}
} else {
ko.Spec.Policy = getPolicyResponse.Policy
}
ko.Spec.Policy = getPolicyResponse.Policy

getPublicAccessBlockResponse, err := rm.sdkapi.GetPublicAccessBlockWithContext(ctx, rm.newGetPublicAccessBlockPayload(r))
if err != nil {
Expand All @@ -447,8 +457,6 @@ func (rm *resourceManager) addPutFieldsToSpec(
}
if getPublicAccessBlockResponse.PublicAccessBlockConfiguration != nil {
ko.Spec.PublicAccessBlock = rm.setResourcePublicAccessBlock(r, getPublicAccessBlockResponse)
} else {
ko.Spec.PublicAccessBlock = nil
}

getReplicationResponse, err := rm.sdkapi.GetBucketReplicationWithContext(ctx, rm.newGetBucketReplicationPayload(r))
Expand All @@ -461,15 +469,14 @@ func (rm *resourceManager) addPutFieldsToSpec(
}
if getReplicationResponse.ReplicationConfiguration != nil {
ko.Spec.Replication = rm.setResourceReplication(r, getReplicationResponse)
} else {
ko.Spec.Replication = nil
}

getRequestPaymentResponse, err := rm.sdkapi.GetBucketRequestPaymentWithContext(ctx, rm.newGetBucketRequestPaymentPayload(r))
if err != nil {
return nil
} else {
ko.Spec.RequestPayment = rm.setResourceRequestPayment(r, getRequestPaymentResponse)
}
ko.Spec.RequestPayment = rm.setResourceRequestPayment(r, getRequestPaymentResponse)

getTaggingResponse, err := rm.sdkapi.GetBucketTaggingWithContext(ctx, rm.newGetBucketTaggingPayload(r))
if err != nil {
Expand All @@ -478,14 +485,16 @@ func (rm *resourceManager) addPutFieldsToSpec(
} else {
return err
}
} else {
ko.Spec.Tagging = rm.setResourceTagging(r, getTaggingResponse)
}
ko.Spec.Tagging = rm.setResourceTagging(r, getTaggingResponse)

getVersioningResponse, err := rm.sdkapi.GetBucketVersioningWithContext(ctx, rm.newGetBucketVersioningPayload(r))
if err != nil {
return err
} else {
ko.Spec.Versioning = rm.setResourceVersioning(r, getVersioningResponse)
}
ko.Spec.Versioning = rm.setResourceVersioning(r, getVersioningResponse)

getWebsiteResponse, err := rm.sdkapi.GetBucketWebsiteWithContext(ctx, rm.newGetBucketWebsitePayload(r))
if err != nil {
Expand All @@ -494,8 +503,9 @@ func (rm *resourceManager) addPutFieldsToSpec(
} else {
return err
}
} else {
ko.Spec.Website = rm.setResourceWebsite(r, getWebsiteResponse)
}
ko.Spec.Website = rm.setResourceWebsite(r, getWebsiteResponse)
return nil
}

Expand Down Expand Up @@ -688,16 +698,28 @@ func (rm *resourceManager) setResourceACL(
resp *svcsdk.GetBucketAclOutput,
) {
grants := GetHeadersFromGrants(resp)
ko.Spec.GrantFullControl = &grants.FullControl
ko.Spec.GrantRead = &grants.Read
ko.Spec.GrantReadACP = &grants.ReadACP
ko.Spec.GrantWrite = &grants.Write
ko.Spec.GrantWriteACP = &grants.WriteACP
if grants.FullControl != "" {
ko.Spec.GrantFullControl = &grants.FullControl
}
if grants.Read != "" {
ko.Spec.GrantRead = &grants.Read
}
if grants.ReadACP != "" {
ko.Spec.GrantReadACP = &grants.ReadACP
}
if grants.Write != "" {
ko.Spec.GrantWrite = &grants.Write
}
if grants.WriteACP != "" {
ko.Spec.GrantWriteACP = &grants.WriteACP
}

// Join possible ACLs into a single string, delimited by bar
cannedACLs := GetPossibleCannedACLsFromGrants(resp)
joinedACLs := strings.Join(cannedACLs, CannedACLJoinDelimiter)
ko.Spec.ACL = &joinedACLs
if joinedACLs != "" {
ko.Spec.ACL = &joinedACLs
}
}

func (rm *resourceManager) newGetBucketACLPayload(
Expand Down

0 comments on commit 8481230

Please sign in to comment.