Skip to content

Commit

Permalink
Store CallerReference value for import scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: ezgidemirel <ezgidemirel91@gmail.com>
  • Loading branch information
ezgidemirel committed Apr 20, 2022
1 parent bb3583e commit c47d6e0
Show file tree
Hide file tree
Showing 8 changed files with 1,049 additions and 1,019 deletions.
1 change: 0 additions & 1 deletion apis/cloudfront/generator-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ ignore:
- FieldLevelEncryptionConfig
- Function
field_paths:
- DistributionConfig.CallerReference
- Origins.Quantity
- Aliases.Quantity
- CustomErrorResponses.Quantity
Expand Down
5 changes: 5 additions & 0 deletions apis/cloudfront/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions apis/cloudfront/v1alpha1/zz_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package/crds/cloudfront.aws.crossplane.io_distributions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ spec:
type: object
type: array
type: object
callerReference:
type: string
comment:
type: string
customErrorResponses:
Expand Down Expand Up @@ -1207,6 +1209,8 @@ spec:
type: object
type: array
type: object
callerReference:
type: string
comment:
type: string
customErrorResponses:
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/cloudfront/distribution/lateinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func lateInitialize(dp *svcapitypes.DistributionParameters, gdo *svcsdk.GetDistr
in := dp.DistributionConfig
from := gdo.Distribution.DistributionConfig

// "CallerReference" is not exposed to users from the console, therefore we need to derive it from the AWS output
// during import scenarios
in.CallerReference = awsclients.LateInitializeStringPtr(in.CallerReference, from.CallerReference)

if from.Aliases != nil {
if in.Aliases == nil {
in.Aliases = &svcapitypes.Aliases{}
Expand Down
64 changes: 34 additions & 30 deletions pkg/controller/cloudfront/distribution/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,151 +85,156 @@ func SetupDistribution(mgr ctrl.Manager, o controller.Options) error {
}

func preCreate(_ context.Context, cr *svcapitypes.Distribution, cdi *svcsdk.CreateDistributionInput) error {
cdi.DistributionConfig.CallerReference = awsclients.String(string(cr.UID))
if awsclients.StringValue(cr.Spec.ForProvider.DistributionConfig.CallerReference) != "" {
cdi.DistributionConfig.CallerReference = cr.Spec.ForProvider.DistributionConfig.CallerReference
} else {
cdi.DistributionConfig.CallerReference = awsclients.String(string(cr.UID))
}

// if cr.Spec.ForProvider.DistributionConfig.Origins is not nil then cdi.DistributionConfig.Origins is not nil
if cr.Spec.ForProvider.DistributionConfig.Origins != nil {
cdi.DistributionConfig.Origins.Quantity =
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.Origins.Items))
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.Origins.Items), 0)
}

if cr.Spec.ForProvider.DistributionConfig.Aliases != nil {
cdi.DistributionConfig.Aliases.Quantity =
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.Aliases.Items))
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.Aliases.Items), 0)
}

if cr.Spec.ForProvider.DistributionConfig.CustomErrorResponses != nil {
cdi.DistributionConfig.CustomErrorResponses.Quantity =
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.CustomErrorResponses.Items))
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.CustomErrorResponses.Items), 0)
}

if cr.Spec.ForProvider.DistributionConfig.Restrictions != nil && cr.Spec.ForProvider.DistributionConfig.Restrictions.GeoRestriction != nil {
cdi.DistributionConfig.Restrictions.GeoRestriction.Quantity =
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.Restrictions.GeoRestriction.Items))
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.Restrictions.GeoRestriction.Items), 0)
}

dcb := cr.Spec.ForProvider.DistributionConfig.DefaultCacheBehavior
if dcb != nil {
if dcb.AllowedMethods != nil {
cdi.DistributionConfig.DefaultCacheBehavior.AllowedMethods.Quantity =
awsclients.Int64(len(dcb.AllowedMethods.Items))
awsclients.Int64(len(dcb.AllowedMethods.Items), 0)

if dcb.AllowedMethods != nil && dcb.AllowedMethods.CachedMethods != nil {
cdi.DistributionConfig.DefaultCacheBehavior.AllowedMethods.CachedMethods.Quantity =
awsclients.Int64(len(dcb.AllowedMethods.CachedMethods.Items))
awsclients.Int64(len(dcb.AllowedMethods.CachedMethods.Items), 0)
}
}

if dcb.ForwardedValues != nil {
if dcb.ForwardedValues.Cookies != nil && dcb.ForwardedValues.Cookies.WhitelistedNames != nil {
cdi.DistributionConfig.DefaultCacheBehavior.ForwardedValues.Cookies.WhitelistedNames.Quantity =
awsclients.Int64(len(dcb.ForwardedValues.Cookies.WhitelistedNames.Items))
awsclients.Int64(len(dcb.ForwardedValues.Cookies.WhitelistedNames.Items), 0)
}

if dcb.ForwardedValues.Headers != nil {
cdi.DistributionConfig.DefaultCacheBehavior.ForwardedValues.Headers.Quantity =
awsclients.Int64(len(dcb.ForwardedValues.Headers.Items))
awsclients.Int64(len(dcb.ForwardedValues.Headers.Items), 0)
}

if dcb.ForwardedValues.QueryStringCacheKeys != nil {
cdi.DistributionConfig.DefaultCacheBehavior.ForwardedValues.QueryStringCacheKeys.Quantity =
awsclients.Int64(len(dcb.ForwardedValues.QueryStringCacheKeys.Items))
awsclients.Int64(len(dcb.ForwardedValues.QueryStringCacheKeys.Items), 0)
}
}

if dcb.LambdaFunctionAssociations != nil {
cdi.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Quantity =
awsclients.Int64(len(dcb.LambdaFunctionAssociations.Items))
awsclients.Int64(len(dcb.LambdaFunctionAssociations.Items), 0)
}

if dcb.TrustedKeyGroups != nil {
cdi.DistributionConfig.DefaultCacheBehavior.TrustedKeyGroups.Quantity =
awsclients.Int64(len(dcb.TrustedKeyGroups.Items))
awsclients.Int64(len(dcb.TrustedKeyGroups.Items), 0)
}

if dcb.TrustedSigners != nil {
cdi.DistributionConfig.DefaultCacheBehavior.TrustedSigners.Quantity =
awsclients.Int64(len(dcb.TrustedSigners.Items))
awsclients.Int64(len(dcb.TrustedSigners.Items), 0)
}
}

if cr.Spec.ForProvider.DistributionConfig.CacheBehaviors != nil {
cdi.DistributionConfig.CacheBehaviors.Quantity =
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.CacheBehaviors.Items))
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.CacheBehaviors.Items), 0)

for i, cbi := range cr.Spec.ForProvider.DistributionConfig.CacheBehaviors.Items {
if cbi.AllowedMethods != nil {
cdi.DistributionConfig.CacheBehaviors.Items[i].AllowedMethods.Quantity =
awsclients.Int64(len(cbi.AllowedMethods.Items))
awsclients.Int64(len(cbi.AllowedMethods.Items), 0)
}

if cbi.AllowedMethods != nil && cbi.AllowedMethods.CachedMethods != nil {
cdi.DistributionConfig.CacheBehaviors.Items[i].AllowedMethods.CachedMethods.Quantity =
awsclients.Int64(len(cbi.AllowedMethods.CachedMethods.Items))
awsclients.Int64(len(cbi.AllowedMethods.CachedMethods.Items), 0)
}

if cbi.ForwardedValues != nil {
if cbi.ForwardedValues.Cookies != nil && cbi.ForwardedValues.Cookies.WhitelistedNames != nil {
cdi.DistributionConfig.CacheBehaviors.Items[i].ForwardedValues.Cookies.WhitelistedNames.Quantity =
awsclients.Int64(len(cbi.ForwardedValues.Cookies.WhitelistedNames.Items))
awsclients.Int64(len(cbi.ForwardedValues.Cookies.WhitelistedNames.Items), 0)
}

if cbi.ForwardedValues.Headers != nil {
cdi.DistributionConfig.CacheBehaviors.Items[i].ForwardedValues.Headers.Quantity =
awsclients.Int64(len(cbi.ForwardedValues.Headers.Items))
awsclients.Int64(len(cbi.ForwardedValues.Headers.Items), 0)
}

if cbi.ForwardedValues.QueryStringCacheKeys != nil {
cdi.DistributionConfig.CacheBehaviors.Items[i].ForwardedValues.QueryStringCacheKeys.Quantity =
awsclients.Int64(len(cbi.ForwardedValues.QueryStringCacheKeys.Items))
awsclients.Int64(len(cbi.ForwardedValues.QueryStringCacheKeys.Items), 0)
}
}

if cbi.LambdaFunctionAssociations != nil {
cdi.DistributionConfig.CacheBehaviors.Items[i].LambdaFunctionAssociations.Quantity =
awsclients.Int64(len(cbi.LambdaFunctionAssociations.Items))
awsclients.Int64(len(cbi.LambdaFunctionAssociations.Items), 0)
}

if cbi.TrustedKeyGroups != nil {
cdi.DistributionConfig.CacheBehaviors.Items[i].TrustedKeyGroups.Quantity =
awsclients.Int64(len(cbi.TrustedKeyGroups.Items))
awsclients.Int64(len(cbi.TrustedKeyGroups.Items), 0)
}

if cbi.TrustedSigners != nil {
cdi.DistributionConfig.CacheBehaviors.Items[i].TrustedSigners.Quantity =
awsclients.Int64(len(cbi.TrustedSigners.Items))
awsclients.Int64(len(cbi.TrustedSigners.Items), 0)
}
}
}

if cr.Spec.ForProvider.DistributionConfig.OriginGroups != nil {
cdi.DistributionConfig.OriginGroups.Quantity =
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.OriginGroups.Items))
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.OriginGroups.Items), 0)

for i, ogi := range cr.Spec.ForProvider.DistributionConfig.OriginGroups.Items {
if ogi.FailoverCriteria != nil && ogi.FailoverCriteria.StatusCodes != nil {
cdi.DistributionConfig.OriginGroups.Items[i].FailoverCriteria.StatusCodes.Quantity =
awsclients.Int64(len(ogi.FailoverCriteria.StatusCodes.Items))
awsclients.Int64(len(ogi.FailoverCriteria.StatusCodes.Items), 0)
}

if ogi.Members != nil {
cdi.DistributionConfig.OriginGroups.Items[i].Members.Quantity = awsclients.Int64(len(ogi.Members.Items))
cdi.DistributionConfig.OriginGroups.Items[i].Members.Quantity = awsclients.Int64(len(ogi.Members.Items), 0)
}
}
}

if cr.Spec.ForProvider.DistributionConfig.Origins != nil {
cdi.DistributionConfig.Origins.Quantity =
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.Origins.Items))
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.Origins.Items), 0)

for i, io := range cr.Spec.ForProvider.DistributionConfig.Origins.Items {
if io.CustomHeaders != nil {
cdi.DistributionConfig.Origins.Items[i].CustomHeaders.Quantity =
awsclients.Int64(len(io.CustomHeaders.Items))
awsclients.Int64(len(io.CustomHeaders.Items), 0)
}

if io.CustomOriginConfig != nil && io.CustomOriginConfig.OriginSSLProtocols != nil {
cdi.DistributionConfig.Origins.Items[i].CustomOriginConfig.OriginSslProtocols.Quantity =
awsclients.Int64(len(io.CustomOriginConfig.OriginSSLProtocols.Items))
awsclients.Int64(len(io.CustomOriginConfig.OriginSSLProtocols.Items), 0)
}
}
}
Expand Down Expand Up @@ -323,9 +328,8 @@ func postUpdate(_ context.Context, cr *svcapitypes.Distribution, resp *svcsdk.Up
func preUpdate(_ context.Context, cr *svcapitypes.Distribution, udi *svcsdk.UpdateDistributionInput) error {
udi.Id = awsclients.String(meta.GetExternalName(cr))
udi.SetIfMatch(awsclients.StringValue(cr.Status.AtProvider.ETag))
udi.DistributionConfig.CallerReference = awsclients.String(string(cr.UID))
udi.DistributionConfig.Origins.Quantity =
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.Origins.Items))
awsclients.Int64(len(cr.Spec.ForProvider.DistributionConfig.Origins.Items), 0)

if cr.Spec.ForProvider.DistributionConfig.Aliases != nil {
udi.DistributionConfig.Aliases.Quantity =
Expand Down
Loading

0 comments on commit c47d6e0

Please sign in to comment.