diff --git a/src/main/java/gyro/aws/cloudfront/CloudFrontCacheBehavior.java b/src/main/java/gyro/aws/cloudfront/CloudFrontCacheBehavior.java index 40ac3d778..75d33a149 100644 --- a/src/main/java/gyro/aws/cloudfront/CloudFrontCacheBehavior.java +++ b/src/main/java/gyro/aws/cloudfront/CloudFrontCacheBehavior.java @@ -23,6 +23,7 @@ import gyro.aws.Copyable; import gyro.core.resource.Diffable; import gyro.core.resource.Updatable; +import gyro.core.validation.ConflictsWith; import gyro.core.validation.ValidStrings; import software.amazon.awssdk.services.cloudfront.model.CacheBehavior; import software.amazon.awssdk.services.cloudfront.model.DefaultCacheBehavior; @@ -102,6 +103,8 @@ public void setViewerProtocolPolicy(String viewerProtocolPolicy) { * The minimum time objects will be cached in this distribution. */ @Updatable + @Deprecated + @ConflictsWith("cache-policy") public Long getMinTtl() { if (minTtl == null) { minTtl = 0L; @@ -150,6 +153,8 @@ public void setCachedMethods(Set cachedMethods) { * Headers to include the cache key for an object. */ @Updatable + @Deprecated + @ConflictsWith("cache-policy") public Set getHeaders() { if (headers == null) { headers = new HashSet<>(); @@ -166,6 +171,8 @@ public void setHeaders(Set headers) { * Whether to forward to cookies to the origin. */ @Updatable + @Deprecated + @ConflictsWith("cache-policy") public String getForwardCookies() { if (forwardCookies != null) { return forwardCookies.toLowerCase(); @@ -182,6 +189,8 @@ public void setForwardCookies(String forwardCookies) { * Whitelist of cookies to include the cache key for an object. */ @Updatable + @Deprecated + @ConflictsWith("cache-policy") public Set getCookies() { if (cookies == null) { cookies = new HashSet<>(); @@ -214,6 +223,8 @@ public void setSmoothStreaming(Boolean smoothStreaming) { * The time objects will be cached in this distribution. Only applies when one of ``Cache-Control: max-age``, ``Cache-Control: s-maxage``, or ``Expires`` are not returned by the origin. */ @Updatable + @Deprecated + @ConflictsWith("cache-policy") public Long getDefaultTtl() { if (defaultTtl == null) { defaultTtl = 86400L; @@ -230,6 +241,8 @@ public void setDefaultTtl(Long defaultTtl) { * The maximum time objects will be cached in this distribution. */ @Updatable + @Deprecated + @ConflictsWith("cache-policy") public Long getMaxTtl() { if (maxTtl == null) { maxTtl = 31536000L; @@ -262,6 +275,8 @@ public void setCompress(Boolean compress) { * Whether to forward query strings to origin. If true, query string parameters become part of the cache key. */ @Updatable + @Deprecated + @ConflictsWith("cache-policy") public Boolean getQueryString() { if (queryString == null) { queryString = false; @@ -278,6 +293,8 @@ public void setQueryString(Boolean queryString) { * Query string parameters that should be used in the cache key. */ @Updatable + @Deprecated + @ConflictsWith("cache-policy") public Set getQueryStringCacheKeys() { if (queryStringCacheKeys == null) { queryStringCacheKeys = new HashSet<>(); @@ -437,8 +454,8 @@ public void copyFrom(CacheBehavior cacheBehavior) { } } - findById(CachePolicyResource.class, cacheBehavior.cachePolicyId()); - findById(OriginRequestPolicyResource.class, cacheBehavior.originRequestPolicyId()); + setCachePolicy(findById(CachePolicyResource.class, cacheBehavior.cachePolicyId())); + setOriginRequestPolicy(findById(OriginRequestPolicyResource.class, cacheBehavior.originRequestPolicyId())); } @Override @@ -467,14 +484,6 @@ static CacheBehavior getCacheBehaviorFromDefault(DefaultCacheBehavior defaultCac } DefaultCacheBehavior toDefaultCacheBehavior() { - ForwardedValues forwardedValues = ForwardedValues.builder() - .headers(h -> h.items(getHeaders()).quantity(getHeaders().size())) - .cookies(c -> c.forward(getForwardCookies()) - .whitelistedNames(w -> w.items(getCookies()).quantity(getCookies().size()))) - .queryString(getQueryString()) - .queryStringCacheKeys(q -> q.items(getQueryStringCacheKeys()).quantity(getQueryStringCacheKeys().size())) - .build(); - TrustedSigners trustedSigners = TrustedSigners.builder() .items(getTrustedSigners()) .quantity(getTrustedSigners().size()) @@ -491,17 +500,13 @@ DefaultCacheBehavior toDefaultCacheBehavior() { .quantity(getFunctionAssociations().size()) .build(); - return DefaultCacheBehavior.builder() + DefaultCacheBehavior.Builder builder = DefaultCacheBehavior.builder() .allowedMethods(am -> am.itemsWithStrings(getAllowedMethods()) .quantity(getAllowedMethods().size()) .cachedMethods(cm -> cm.itemsWithStrings(getCachedMethods()).quantity(getCachedMethods().size())) ) - .defaultTTL(getDefaultTtl()) - .maxTTL(getMaxTtl()) - .minTTL(getMinTtl()) .smoothStreaming(getSmoothStreaming()) .targetOriginId(getTargetOriginId()) - .forwardedValues(forwardedValues) .trustedSigners(trustedSigners) .lambdaFunctionAssociations(lambdaFunctionAssociations) .functionAssociations(functionAssociations) @@ -509,18 +514,27 @@ DefaultCacheBehavior toDefaultCacheBehavior() { .fieldLevelEncryptionId(getFieldLevelEncryptionId()) .compress(getCompress()) .cachePolicyId(getCachePolicy() != null ? getCachePolicy().getId() : null) - .originRequestPolicyId(getOriginRequestPolicy() != null ? getOriginRequestPolicy().getId() : null) - .build(); + .originRequestPolicyId(getOriginRequestPolicy() != null ? getOriginRequestPolicy().getId() : null); + + if (getCachePolicy() == null) { + ForwardedValues forwardedValues = ForwardedValues.builder() + .headers(h -> h.items(getHeaders()).quantity(getHeaders().size())) + .cookies(c -> c.forward(getForwardCookies()) + .whitelistedNames(w -> w.items(getCookies()).quantity(getCookies().size()))) + .queryString(getQueryString()) + .queryStringCacheKeys( + q -> q.items(getQueryStringCacheKeys()).quantity(getQueryStringCacheKeys().size())) + .build(); + builder.forwardedValues(forwardedValues) + .defaultTTL(getDefaultTtl()) + .maxTTL(getMaxTtl()) + .minTTL(getMinTtl()); + } + + return builder.build(); } CacheBehavior toCachBehavior() { - ForwardedValues forwardedValues = ForwardedValues.builder() - .headers(h -> h.items(getHeaders()).quantity(getHeaders().size())) - .cookies(c -> c.forward(getForwardCookies()) - .whitelistedNames(w -> w.items(getCookies()).quantity(getCookies().size()))) - .queryString(getQueryString()) - .queryStringCacheKeys(q -> q.items(getQueryStringCacheKeys()).quantity(getQueryStringCacheKeys().size())) - .build(); TrustedSigners trustedSigners = TrustedSigners.builder() .items(getTrustedSigners()) @@ -538,18 +552,14 @@ CacheBehavior toCachBehavior() { .quantity(getFunctionAssociations().size()) .build(); - return CacheBehavior.builder() + CacheBehavior.Builder builder = CacheBehavior.builder() .allowedMethods(am -> am.itemsWithStrings(getAllowedMethods()) .quantity(getAllowedMethods().size()) .cachedMethods(cm -> cm.itemsWithStrings(getCachedMethods()).quantity(getCachedMethods().size())) ) - .defaultTTL(getDefaultTtl()) - .maxTTL(getMaxTtl()) - .minTTL(getMinTtl()) .smoothStreaming(getSmoothStreaming()) .targetOriginId(getTargetOriginId()) .pathPattern(getPathPattern()) - .forwardedValues(forwardedValues) .trustedSigners(trustedSigners) .lambdaFunctionAssociations(lambdaFunctionAssociations) .functionAssociations(functionAssociations) @@ -557,7 +567,23 @@ CacheBehavior toCachBehavior() { .fieldLevelEncryptionId(getFieldLevelEncryptionId()) .compress(getCompress()) .cachePolicyId(getCachePolicy() != null ? getCachePolicy().getId() : null) - .originRequestPolicyId(getOriginRequestPolicy() != null ? getOriginRequestPolicy().getId() : null) - .build(); + .originRequestPolicyId(getOriginRequestPolicy() != null ? getOriginRequestPolicy().getId() : null); + + if (getCachePolicy() == null) { + ForwardedValues forwardedValues = ForwardedValues.builder() + .headers(h -> h.items(getHeaders()).quantity(getHeaders().size())) + .cookies(c -> c.forward(getForwardCookies()) + .whitelistedNames(w -> w.items(getCookies()).quantity(getCookies().size()))) + .queryString(getQueryString()) + .queryStringCacheKeys( + q -> q.items(getQueryStringCacheKeys()).quantity(getQueryStringCacheKeys().size())) + .build(); + builder.forwardedValues(forwardedValues) + .defaultTTL(getDefaultTtl()) + .maxTTL(getMaxTtl()) + .minTTL(getMinTtl()); + } + + return builder.build(); } }