Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to add Cloudfront Cache Key Policy #649

Merged
merged 8 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/cloudfront/cache-policy.gyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
aws::cloudfront-cache-policy cache-policy-example
cache-policy-config
default-ttl: 3600
max-ttl: 86400
min-ttl: 0
name: "cache-policy-example"

key-param
accept-encoding-brotli: true
accept-encoding-gzip: true

headers-config
header-behavior: "whitelist"
headers: [
"example-header1",
"example-header2"
]
end

query-strings-config
query-string-behavior: "whitelist"
query-strings: [
"example-query1",
"example-query2"
]
end

cookies-config
cookie-behavior: "whitelist"
cookies: [
"example-cookie1",
"example-cookie2"
]
end
end
end
end
71 changes: 70 additions & 1 deletion examples/cloudfront/cloudfront.gyro
Original file line number Diff line number Diff line change
@@ -1,3 +1,71 @@
aws::cloudfront-cache-policy cache-policy-example
cache-policy-config
default-ttl: 3600
max-ttl: 86400
min-ttl: 0
name: "cache-policy-example"

key-param
accept-encoding-brotli: true
accept-encoding-gzip: true

headers-config
header-behavior: "whitelist"
headers: [
"example-header1",
"example-header2"
]
end

query-strings-config
query-string-behavior: "whitelist"
query-strings: [
"example-query1",
"example-query2"
]
end

cookies-config
cookie-behavior: "whitelist"
cookies: [
"example-cookie1",
"example-cookie2"
]
end
end
end
end

aws::cloudfront-origin-request-policy origin-request-policy-example
origin-request-policy-config
name: "origin-request-policy-example"

headers-config
header-behavior: "whitelist"
headers: [
"example-header1",
"example-header2"
]
end

query-strings-config
query-string-behavior: "whitelist"
query-strings: [
"example-query1",
"example-query2"
]
end

cookies-config
cookie-behavior: "whitelist"
cookies: [
"example-cookie1",
"example-cookie2"
]
end
end
end

aws::cloudfront cloudfront-example
enabled: true
ipv6-enabled: false
Expand All @@ -22,7 +90,8 @@ aws::cloudfront cloudfront-example
viewer-protocol-policy: "allow-all"
allowed-methods: ["GET", "HEAD"]
cached-methods: ["GET", "HEAD"]
headers: ["Origin"]
cache-policy: $(aws::cloudfront-cache-policy cache-policy-example)
origin-request-policy: $(aws::cloudfront-origin-request-policy origin-request-policy-example)
end

behavior
Expand Down
29 changes: 29 additions & 0 deletions examples/cloudfront/origin-request-policy.gyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
aws::cloudfront-origin-request-policy origin-request-policy-example
origin-request-policy-config
name: "origin-request-policy-example"

headers-config
header-behavior: "whitelist"
headers: [
"example-header1",
"example-header2"
]
end

query-strings-config
query-string-behavior: "whitelist"
query-strings: [
"example-query1",
"example-query2"
]
end

cookies-config
cookie-behavior: "whitelist"
cookies: [
"example-cookie1",
"example-cookie2"
]
end
end
end
138 changes: 138 additions & 0 deletions src/main/java/gyro/aws/cloudfront/CachePolicyConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2024, 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.aws.cloudfront;

import gyro.aws.Copyable;
import gyro.core.resource.Diffable;
import gyro.core.resource.Updatable;
import gyro.core.validation.Required;

public class CachePolicyConfig
extends Diffable implements Copyable<software.amazon.awssdk.services.cloudfront.model.CachePolicyConfig> {

private String comment;
private Long defaultTtl;
private String name;
private Long maxTtl;
private Long minTtl;
private CachePolicyKeyParam keyParam;

/**
* The comment for the cache policy.
*/
@Updatable
public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}

/**
* The default time to live for the cache policy.
*/
@Updatable
public Long getDefaultTtl() {
return defaultTtl;
}

public void setDefaultTtl(Long defaultTtl) {
this.defaultTtl = defaultTtl;
}

/**
* The name for the cache policy.
*/
@Required
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

/**
* The maximum time to live for the cache policy.
*/
@Updatable
public Long getMaxTtl() {
return maxTtl;
}

public void setMaxTtl(Long maxTtl) {
this.maxTtl = maxTtl;
}

/**
* The minimum time to live for the cache policy.
*/
@Updatable
public Long getMinTtl() {
return minTtl;
}

public void setMinTtl(Long minTtl) {
this.minTtl = minTtl;
}

/**
* The key param for the cache policy.
*
* @subresource gyro.aws.cloudfront.CachePolicyKeyParam
*/
public CachePolicyKeyParam getKeyParam() {
return keyParam;
}

public void setKeyParam(CachePolicyKeyParam keyParam) {
this.keyParam = keyParam;
}

@Override
public void copyFrom(software.amazon.awssdk.services.cloudfront.model.CachePolicyConfig model) {
setComment(model.comment());
setDefaultTtl(model.defaultTTL());
setName(model.name());
setMaxTtl(model.maxTTL());
setMinTtl(model.minTTL());

setKeyParam(null);
if (model.parametersInCacheKeyAndForwardedToOrigin() != null) {
CachePolicyKeyParam keyParam = newSubresource(CachePolicyKeyParam.class);
keyParam.copyFrom(model.parametersInCacheKeyAndForwardedToOrigin());
setKeyParam(keyParam);
}
}

@Override
public String primaryKey() {
return getName();
}

software.amazon.awssdk.services.cloudfront.model.CachePolicyConfig toCachePolicyConfig() {
return software.amazon.awssdk.services.cloudfront.model.CachePolicyConfig.builder()
.comment(getComment())
.defaultTTL(getDefaultTtl())
.name(getName())
.maxTTL(getMaxTtl())
.minTTL(getMinTtl())
.parametersInCacheKeyAndForwardedToOrigin(getKeyParam() != null ? getKeyParam().toParametersInCacheKeyAndForwardedToOrigin() : null)
.build();
}
}
Loading
Loading