diff --git a/src/main/java/gyro/google/compute/BackendBucketCdnBypassCacheOnRequestHeader.java b/src/main/java/gyro/google/compute/BackendBucketCdnBypassCacheOnRequestHeader.java new file mode 100644 index 00000000..c9f5440b --- /dev/null +++ b/src/main/java/gyro/google/compute/BackendBucketCdnBypassCacheOnRequestHeader.java @@ -0,0 +1,55 @@ +/* + * Copyright 2023, Brightspot. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package gyro.google.compute; + +import com.google.cloud.compute.v1.BackendBucketCdnPolicyBypassCacheOnRequestHeader; +import gyro.core.resource.Diffable; +import gyro.core.validation.Required; +import gyro.google.Copyable; + +public class BackendBucketCdnBypassCacheOnRequestHeader extends Diffable implements Copyable { + + private String headerName; + + /** + * The header name to match on when bypassing cache. + */ + @Required + public String getHeaderName() { + return headerName; + } + + public void setHeaderName(String headerName) { + this.headerName = headerName; + } + + @Override + public void copyFrom(BackendBucketCdnPolicyBypassCacheOnRequestHeader model) { + setHeaderName(model.getHeaderName()); + } + + @Override + public String primaryKey() { + return headerName != null ? getHeaderName() : ""; + } + + BackendBucketCdnPolicyBypassCacheOnRequestHeader toBackendBucketCdnPolicyBypassCacheOnRequestHeader() { + return BackendBucketCdnPolicyBypassCacheOnRequestHeader.newBuilder() + .setHeaderName(getHeaderName()) + .build(); + } +} diff --git a/src/main/java/gyro/google/compute/BackendBucketCdnCacheKeyPolicy.java b/src/main/java/gyro/google/compute/BackendBucketCdnCacheKeyPolicy.java new file mode 100644 index 00000000..9d49fd8b --- /dev/null +++ b/src/main/java/gyro/google/compute/BackendBucketCdnCacheKeyPolicy.java @@ -0,0 +1,81 @@ +/* + * Copyright 2023, Brightspot. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package gyro.google.compute; + +import java.util.ArrayList; +import java.util.List; + +import com.google.cloud.compute.v1.BackendBucketCdnPolicyCacheKeyPolicy; +import gyro.core.resource.Diffable; +import gyro.core.resource.Updatable; +import gyro.google.Copyable; + +public class BackendBucketCdnCacheKeyPolicy extends Diffable implements Copyable { + + private List includeHttpHeaders; + private List queryStringWhitelist; + + /** + * Headers to include in cache keys according to include_http_headers. + */ + @Updatable + public List getIncludeHttpHeaders() { + if (includeHttpHeaders == null) { + includeHttpHeaders = new ArrayList<>(); + } + + return includeHttpHeaders; + } + + public void setIncludeHttpHeaders(List includeHttpHeaders) { + this.includeHttpHeaders = includeHttpHeaders; + } + + /** + * Names of query string parameters to include in cache keys. + */ + @Updatable + public List getQueryStringWhitelist() { + if (queryStringWhitelist == null) { + queryStringWhitelist = new ArrayList<>(); + } + + return queryStringWhitelist; + } + + public void setQueryStringWhitelist(List queryStringWhitelist) { + this.queryStringWhitelist = queryStringWhitelist; + } + + @Override + public void copyFrom(BackendBucketCdnPolicyCacheKeyPolicy model) { + setIncludeHttpHeaders(model.getIncludeHttpHeadersList()); + setQueryStringWhitelist(model.getQueryStringWhitelistList()); + } + + BackendBucketCdnPolicyCacheKeyPolicy toBackendBucketCdnPolicyCacheKeyPolicy() { + BackendBucketCdnPolicyCacheKeyPolicy.Builder builder = BackendBucketCdnPolicyCacheKeyPolicy.newBuilder(); + builder.addAllIncludeHttpHeaders(getIncludeHttpHeaders()); + builder.addAllQueryStringWhitelist(getQueryStringWhitelist()); + return builder.build(); + } + + @Override + public String primaryKey() { + return ""; + } +} diff --git a/src/main/java/gyro/google/compute/BackendBucketCdnNegativeCachingPolicy.java b/src/main/java/gyro/google/compute/BackendBucketCdnNegativeCachingPolicy.java new file mode 100644 index 00000000..da2cda27 --- /dev/null +++ b/src/main/java/gyro/google/compute/BackendBucketCdnNegativeCachingPolicy.java @@ -0,0 +1,70 @@ +/* + * Copyright 2023, Brightspot. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package gyro.google.compute; + +import com.google.cloud.compute.v1.BackendBucketCdnPolicyNegativeCachingPolicy; +import gyro.core.resource.Diffable; +import gyro.core.validation.Required; +import gyro.google.Copyable; + +public class BackendBucketCdnNegativeCachingPolicy extends Diffable implements Copyable { + + private Integer code; + private Integer ttl; + + /** + * The HTTP status code to define custom set of rules. + */ + @Required + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + /** + * The TTL for the negative response. + */ + @Required + public Integer getTtl() { + return ttl; + } + + public void setTtl(Integer ttl) { + this.ttl = ttl; + } + + @Override + public void copyFrom(BackendBucketCdnPolicyNegativeCachingPolicy model) { + setCode(model.getCode()); + setTtl(model.getTtl()); + } + + @Override + public String primaryKey() { + return String.format("Code: %s, TTL: %s", getCode() != null ? getCode() : 0, getTtl() != null ? getTtl() : 0); + } + + BackendBucketCdnPolicyNegativeCachingPolicy toBackendBucketCdnPolicyNegativeCachingPolicy() { + return BackendBucketCdnPolicyNegativeCachingPolicy.newBuilder() + .setCode(getCode()) + .setTtl(getTtl()) + .build(); + } +} diff --git a/src/main/java/gyro/google/compute/BackendBucketCdnPolicy.java b/src/main/java/gyro/google/compute/BackendBucketCdnPolicy.java index 78b1f28d..5740782b 100644 --- a/src/main/java/gyro/google/compute/BackendBucketCdnPolicy.java +++ b/src/main/java/gyro/google/compute/BackendBucketCdnPolicy.java @@ -18,11 +18,14 @@ import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; +import com.google.cloud.compute.v1.BackendBucketCdnPolicyCacheKeyPolicy; import gyro.core.resource.Diffable; import gyro.core.resource.Output; import gyro.core.resource.Updatable; import gyro.core.validation.Required; +import gyro.core.validation.ValidStrings; import gyro.google.Copyable; public class BackendBucketCdnPolicy extends Diffable @@ -30,6 +33,16 @@ public class BackendBucketCdnPolicy extends Diffable private Long signedUrlMaxAge; private List signedUrlKeyNames; + private String cacheMode; + private BackendBucketCdnCacheKeyPolicy cacheKeyPolicy; + private List bypassCacheOnRequestHeader; + private Boolean negativeCaching; + private List negativeCachingPolicy; + private Integer clientTtl; + private Integer defaultTtl; + private Integer maxTtl; + private Boolean requestCoalescing; + private Integer serveWhileStale; /** * Maximum number of seconds the response to a signed URL request will be considered fresh. @@ -60,6 +73,154 @@ public void setSignedUrlKeyNames(List signedUrlKeyNames) { this.signedUrlKeyNames = signedUrlKeyNames; } + /** + * Specifies the cache mode for all responses from this backend bucket. + */ + @ValidStrings({"CACHE_ALL_STATIC", "FORCE_CACHE_ALL", "INVALID_CACHE_MODE", "USE_ORIGIN_HEADERS"}) + @Required + @Updatable + public String getCacheMode() { + if (cacheMode == null) { + cacheMode = "CACHE_ALL_STATIC"; + } + + return cacheMode; + } + + public void setCacheMode(String cacheMode) { + this.cacheMode = cacheMode; + } + + /** + * Specifies the cache key configuration. + * + * @subresource gyro.google.compute.BackendBucketCdnCacheKeyPolicy + */ + @Updatable + public BackendBucketCdnCacheKeyPolicy getCacheKeyPolicy() { + return cacheKeyPolicy; + } + + public void setCacheKeyPolicy(BackendBucketCdnCacheKeyPolicy cacheKeyPolicy) { + this.cacheKeyPolicy = cacheKeyPolicy; + } + + /** + * Specifies a list of request headers that will be used to bypass cache. + * + * @subresource gyro.google.compute.BackendBucketCdnBypassCacheOnRequestHeader + */ + @Updatable + public List getBypassCacheOnRequestHeader() { + if (bypassCacheOnRequestHeader == null) { + bypassCacheOnRequestHeader = new ArrayList<>(); + } + + return bypassCacheOnRequestHeader; + } + + public void setBypassCacheOnRequestHeader(List bypassCacheOnRequestHeader) { + this.bypassCacheOnRequestHeader = bypassCacheOnRequestHeader; + } + + /** + * Specifies whether to follow redirects from the origin. Defaults to ``false``. + */ + @Updatable + public Boolean getNegativeCaching() { + if (negativeCaching == null) { + negativeCaching = false; + } + + return negativeCaching; + } + + public void setNegativeCaching(Boolean negativeCaching) { + this.negativeCaching = negativeCaching; + } + + /** + * Specifies the negative caching configuration. + * + * @subresource gyro.google.compute.BackendBucketCdnNegativeCachingPolicy + */ + @Updatable + public List getNegativeCachingPolicy() { + if (negativeCachingPolicy == null) { + negativeCachingPolicy = new ArrayList<>(); + } + + return negativeCachingPolicy; + } + + public void setNegativeCachingPolicy(List negativeCachingPolicy) { + this.negativeCachingPolicy = negativeCachingPolicy; + } + + /** + * Specifies the TTL for cached content served to clients. + */ + @Updatable + public Integer getClientTtl() { + return clientTtl; + } + + public void setClientTtl(Integer clientTtl) { + this.clientTtl = clientTtl; + } + + /** + * Specifies the TTL for cached responses served to clients that do not send a max-age, min-fresh, or s-max-age directive. + */ + @Updatable + public Integer getDefaultTtl() { + return defaultTtl; + } + + public void setDefaultTtl(Integer defaultTtl) { + this.defaultTtl = defaultTtl; + } + + /** + * Specifies the maximum allowed TTL for cached content served to clients. + */ + @Updatable + public Integer getMaxTtl() { + return maxTtl; + } + + public void setMaxTtl(Integer maxTtl) { + this.maxTtl = maxTtl; + } + + /** + * Specifies whether to enable coalescing of multiple concurrent cache-fill requests into a single request. Defaults to ``false``. + */ + @Updatable + public Boolean getRequestCoalescing() { + if (requestCoalescing == null) { + requestCoalescing = false; + } + + return requestCoalescing; + } + + public void setRequestCoalescing(Boolean requestCoalescing) { + this.requestCoalescing = requestCoalescing; + } + + /** + * Specifies the number of seconds to serve the content without contacting the origin. + */ + @Updatable + public Integer getServeWhileStale() { + return serveWhileStale; + } + + public void setServeWhileStale(Integer serveWhileStale) { + this.serveWhileStale = serveWhileStale; + } + @Override public String primaryKey() { return ""; @@ -69,10 +230,63 @@ public String primaryKey() { public void copyFrom(com.google.cloud.compute.v1.BackendBucketCdnPolicy backendBucketCdnPolicy) { setSignedUrlKeyNames(backendBucketCdnPolicy.getSignedUrlKeyNamesList()); setSignedUrlMaxAge(backendBucketCdnPolicy.getSignedUrlCacheMaxAgeSec()); + setCacheMode(backendBucketCdnPolicy.getCacheMode()); + + setCacheKeyPolicy(null); + if (backendBucketCdnPolicy.getCacheKeyPolicy() != null) { + BackendBucketCdnCacheKeyPolicy cacheKeyPolicy = newSubresource(BackendBucketCdnCacheKeyPolicy.class); + cacheKeyPolicy.copyFrom(backendBucketCdnPolicy.getCacheKeyPolicy()); + setCacheKeyPolicy(cacheKeyPolicy); + } + + setBypassCacheOnRequestHeader(null); + if (!backendBucketCdnPolicy.getBypassCacheOnRequestHeadersList().isEmpty()) { + setBypassCacheOnRequestHeader(backendBucketCdnPolicy.getBypassCacheOnRequestHeadersList().stream() + .map(o -> { + BackendBucketCdnBypassCacheOnRequestHeader bypassCacheOnRequestHeader = newSubresource(BackendBucketCdnBypassCacheOnRequestHeader.class); + bypassCacheOnRequestHeader.copyFrom(o); + return bypassCacheOnRequestHeader; + }) + .collect(Collectors.toList())); + } + + setNegativeCaching(backendBucketCdnPolicy.getNegativeCaching()); + setNegativeCachingPolicy(null); + if (!backendBucketCdnPolicy.getNegativeCachingPolicyList().isEmpty()) { + setNegativeCachingPolicy(backendBucketCdnPolicy.getNegativeCachingPolicyList().stream() + .map(o -> { + BackendBucketCdnNegativeCachingPolicy negativeCachingPolicy = newSubresource(BackendBucketCdnNegativeCachingPolicy.class); + negativeCachingPolicy.copyFrom(o); + return negativeCachingPolicy; + }) + .collect(Collectors.toList())); + } + + setClientTtl(backendBucketCdnPolicy.getClientTtl()); + setDefaultTtl(backendBucketCdnPolicy.getDefaultTtl()); + setMaxTtl(backendBucketCdnPolicy.getMaxTtl()); + setRequestCoalescing(backendBucketCdnPolicy.getRequestCoalescing()); + setServeWhileStale(backendBucketCdnPolicy.getServeWhileStale()); } com.google.cloud.compute.v1.BackendBucketCdnPolicy toBackendBucketCdnPolicy() { return com.google.cloud.compute.v1.BackendBucketCdnPolicy.newBuilder() - .setSignedUrlCacheMaxAgeSec(getSignedUrlMaxAge()).build(); + .setSignedUrlCacheMaxAgeSec(getSignedUrlMaxAge()) + .setCacheMode(getCacheMode()) + .setCacheKeyPolicy(BackendBucketCdnPolicyCacheKeyPolicy.newBuilder().build()) + .addAllBypassCacheOnRequestHeaders(getBypassCacheOnRequestHeader().stream() + .map(BackendBucketCdnBypassCacheOnRequestHeader::toBackendBucketCdnPolicyBypassCacheOnRequestHeader) + .collect(Collectors.toList())) + .setClientTtl(getClientTtl() != null ? getClientTtl() : 0) + .setDefaultTtl(getDefaultTtl() != null ? getDefaultTtl() : 0) + .setMaxTtl(getMaxTtl() != null ? getMaxTtl() : 0) + .setNegativeCaching(getNegativeCaching()) + .addAllNegativeCachingPolicy(getNegativeCachingPolicy().stream() + .map(BackendBucketCdnNegativeCachingPolicy::toBackendBucketCdnPolicyNegativeCachingPolicy) + .collect(Collectors.toList())) + .setRequestCoalescing(getRequestCoalescing()) + .setServeWhileStale(getServeWhileStale() != null ? getServeWhileStale() : 0) + .addAllSignedUrlKeyNames(getSignedUrlKeyNames()) + .build(); } }