Skip to content

Commit

Permalink
Add support for the use of Additional Cacheable Ports in Cache Rules
Browse files Browse the repository at this point in the history
  • Loading branch information
iveelsm committed Sep 27, 2023
1 parent 2968c3b commit a0e96dc
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .changelog/2754.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/cloudflare_ruleset: Add support for the use of Additional Cacheable Ports option in the Rulesets API
```
14 changes: 14 additions & 0 deletions internal/framework/expanders/intset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package expanders

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/types"
)

// Int64Set accepts a `types.Set` and returns a slice of int64.
func Int64Set(ctx context.Context, in types.Set) []int {
results := []int{}
_ = in.ElementsAs(ctx, &results, false)
return results
}
19 changes: 19 additions & 0 deletions internal/framework/flatteners/int64set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package flatteners

import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

// Int64Set accepts a `[]attr.Value` and returns a `basetypes.SetValue`. The
// return type automatically handles `SetNull` for empty results and coercing
// all element values to a string if there are any elements.
//
// nolint: contextcheck
func Int64Set(in []attr.Value) basetypes.SetValue {
if len(in) == 0 {
return types.SetNull(types.Int64Type)
}
return types.SetValueMust(types.Int64Type, in)
}
101 changes: 51 additions & 50 deletions internal/framework/service/rulesets/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,56 +29,57 @@ type RulesModel struct {
}

type ActionParametersModel struct {
Version types.String `tfsdk:"version"`
AutomaticHTTPSRewrites types.Bool `tfsdk:"automatic_https_rewrites"`
AutoMinify []*ActionParameterAutoMinifyModel `tfsdk:"autominify"`
BIC types.Bool `tfsdk:"bic"`
BrowserTTL []*ActionParameterBrowserTTLModel `tfsdk:"browser_ttl"`
Cache types.Bool `tfsdk:"cache"`
CacheKey []*ActionParameterCacheKeyModel `tfsdk:"cache_key"`
Content types.String `tfsdk:"content"`
ContentType types.String `tfsdk:"content_type"`
CookieFields types.Set `tfsdk:"cookie_fields"`
DisableApps types.Bool `tfsdk:"disable_apps"`
DisableRailgun types.Bool `tfsdk:"disable_railgun"`
DisableZaraz types.Bool `tfsdk:"disable_zaraz"`
EdgeTTL []*ActionParameterEdgeTTLModel `tfsdk:"edge_ttl"`
EmailObfuscation types.Bool `tfsdk:"email_obfuscation"`
FromList []*ActionParameterFromListModel `tfsdk:"from_list"`
FromValue []*ActionParameterFromValueModel `tfsdk:"from_value"`
Headers []*ActionParametersHeadersModel `tfsdk:"headers"`
HostHeader types.String `tfsdk:"host_header"`
HotlinkProtection types.Bool `tfsdk:"hotlink_protection"`
ID types.String `tfsdk:"id"`
Increment types.Int64 `tfsdk:"increment"`
MatchedData []*ActionParametersMatchedDataModel `tfsdk:"matched_data"`
Mirage types.Bool `tfsdk:"mirage"`
OpportunisticEncryption types.Bool `tfsdk:"opportunistic_encryption"`
Origin []*ActionParameterOriginModel `tfsdk:"origin"`
OriginCacheControl types.Bool `tfsdk:"origin_cache_control"`
OriginErrorPagePassthru types.Bool `tfsdk:"origin_error_page_passthru"`
Overrides []*ActionParameterOverridesModel `tfsdk:"overrides"`
Phases types.Set `tfsdk:"phases"`
Polish types.String `tfsdk:"polish"`
Products types.Set `tfsdk:"products"`
ReadTimeout types.Int64 `tfsdk:"read_timeout"`
RequestFields types.Set `tfsdk:"request_fields"`
RespectStrongEtags types.Bool `tfsdk:"respect_strong_etags"`
Response []*ActionParameterResponseModel `tfsdk:"response"`
ResponseFields types.Set `tfsdk:"response_fields"`
RocketLoader types.Bool `tfsdk:"rocket_loader"`
Rules map[string]types.String `tfsdk:"rules"`
Ruleset types.String `tfsdk:"ruleset"`
Rulesets types.Set `tfsdk:"rulesets"`
SecurityLevel types.String `tfsdk:"security_level"`
ServerSideExcludes types.Bool `tfsdk:"server_side_excludes"`
ServeStale []*ActionParameterServeStaleModel `tfsdk:"serve_stale"`
SNI []*ActionParameterSNIModel `tfsdk:"sni"`
SSL types.String `tfsdk:"ssl"`
StatusCode types.Int64 `tfsdk:"status_code"`
SXG types.Bool `tfsdk:"sxg"`
URI []*ActionParametersURIModel `tfsdk:"uri"`
Algorithms []*ActionParametersCompressionAlgorithmModel `tfsdk:"algorithms"`
Version types.String `tfsdk:"version"`
AdditionalCacheablePorts types.Set `tfsdk:"additional_cacheable_ports"`
AutomaticHTTPSRewrites types.Bool `tfsdk:"automatic_https_rewrites"`
AutoMinify []*ActionParameterAutoMinifyModel `tfsdk:"autominify"`
BIC types.Bool `tfsdk:"bic"`
BrowserTTL []*ActionParameterBrowserTTLModel `tfsdk:"browser_ttl"`
Cache types.Bool `tfsdk:"cache"`
CacheKey []*ActionParameterCacheKeyModel `tfsdk:"cache_key"`
Content types.String `tfsdk:"content"`
ContentType types.String `tfsdk:"content_type"`
CookieFields types.Set `tfsdk:"cookie_fields"`
DisableApps types.Bool `tfsdk:"disable_apps"`
DisableRailgun types.Bool `tfsdk:"disable_railgun"`
DisableZaraz types.Bool `tfsdk:"disable_zaraz"`
EdgeTTL []*ActionParameterEdgeTTLModel `tfsdk:"edge_ttl"`
EmailObfuscation types.Bool `tfsdk:"email_obfuscation"`
FromList []*ActionParameterFromListModel `tfsdk:"from_list"`
FromValue []*ActionParameterFromValueModel `tfsdk:"from_value"`
Headers []*ActionParametersHeadersModel `tfsdk:"headers"`
HostHeader types.String `tfsdk:"host_header"`
HotlinkProtection types.Bool `tfsdk:"hotlink_protection"`
ID types.String `tfsdk:"id"`
Increment types.Int64 `tfsdk:"increment"`
MatchedData []*ActionParametersMatchedDataModel `tfsdk:"matched_data"`
Mirage types.Bool `tfsdk:"mirage"`
OpportunisticEncryption types.Bool `tfsdk:"opportunistic_encryption"`
Origin []*ActionParameterOriginModel `tfsdk:"origin"`
OriginCacheControl types.Bool `tfsdk:"origin_cache_control"`
OriginErrorPagePassthru types.Bool `tfsdk:"origin_error_page_passthru"`
Overrides []*ActionParameterOverridesModel `tfsdk:"overrides"`
Phases types.Set `tfsdk:"phases"`
Polish types.String `tfsdk:"polish"`
Products types.Set `tfsdk:"products"`
ReadTimeout types.Int64 `tfsdk:"read_timeout"`
RequestFields types.Set `tfsdk:"request_fields"`
RespectStrongEtags types.Bool `tfsdk:"respect_strong_etags"`
Response []*ActionParameterResponseModel `tfsdk:"response"`
ResponseFields types.Set `tfsdk:"response_fields"`
RocketLoader types.Bool `tfsdk:"rocket_loader"`
Rules map[string]types.String `tfsdk:"rules"`
Ruleset types.String `tfsdk:"ruleset"`
Rulesets types.Set `tfsdk:"rulesets"`
SecurityLevel types.String `tfsdk:"security_level"`
ServerSideExcludes types.Bool `tfsdk:"server_side_excludes"`
ServeStale []*ActionParameterServeStaleModel `tfsdk:"serve_stale"`
SNI []*ActionParameterSNIModel `tfsdk:"sni"`
SSL types.String `tfsdk:"ssl"`
StatusCode types.Int64 `tfsdk:"status_code"`
SXG types.Bool `tfsdk:"sxg"`
URI []*ActionParametersURIModel `tfsdk:"uri"`
Algorithms []*ActionParametersCompressionAlgorithmModel `tfsdk:"algorithms"`
}

type ActionParameterOverridesModel struct {
Expand Down
12 changes: 12 additions & 0 deletions internal/framework/service/rulesets/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ func toRulesetResourceModel(ctx context.Context, zoneID, accountID basetypes.Str
Version: flatteners.String(cloudflare.String(ruleResponse.ActionParameters.Version)),
})

if !reflect.ValueOf(ruleResponse.ActionParameters.AdditionalCacheablePorts).IsNil() {
var ports []attr.Value
for _, s := range ruleResponse.ActionParameters.AdditionalCacheablePorts {
ports = append(ports, types.Int64Value((int64(s))))
}
rule.ActionParameters[0].AdditionalCacheablePorts = flatteners.Int64Set(ports)
}

if !reflect.ValueOf(ruleResponse.ActionParameters.Polish).IsNil() {
rule.ActionParameters[0].Polish = flatteners.String(ruleResponse.ActionParameters.Polish.String())
}
Expand Down Expand Up @@ -850,6 +858,10 @@ func (r *RulesModel) toRulesetRule(ctx context.Context) cloudflare.RulesetRule {
rr.ActionParameters.StatusCode = uint16(ap.StatusCode.ValueInt64())
}

if !ap.AdditionalCacheablePorts.IsNull() {
rr.ActionParameters.AdditionalCacheablePorts = expanders.Int64Set(ctx, ap.AdditionalCacheablePorts)
}

if !ap.AutomaticHTTPSRewrites.IsNull() {
rr.ActionParameters.AutomaticHTTPSRewrites = cloudflare.BoolPtr(ap.AutomaticHTTPSRewrites.ValueBool())
}
Expand Down
2 changes: 2 additions & 0 deletions internal/framework/service/rulesets/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,7 @@ func TestAccCloudflareRuleset_CacheSettingsAllEnabled(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "rules.0.action", "set_cache_settings"),
resource.TestCheckResourceAttr(resourceName, "rules.0.description", rnd+" set cache settings rule"),

resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.additional_cacheable_ports.0", "8443"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.edge_ttl.0.mode", "override_origin"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.edge_ttl.0.default", "60"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.edge_ttl.0.status_code_ttl.#", "2"),
Expand Down Expand Up @@ -3917,6 +3918,7 @@ func testAccCloudflareRulesetCacheSettingsAllEnabled(rnd, accountID, zoneID stri
rules {
action = "set_cache_settings"
action_parameters {
additional_cacheable_ports = [8443]
edge_ttl {
mode = "override_origin"
default = 60
Expand Down
5 changes: 5 additions & 0 deletions internal/framework/service/rulesets/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ func (r *RulesetResource) Schema(ctx context.Context, req resource.SchemaRequest
MarkdownDescription: "List of parameters that configure the behavior of the ruleset rule action.",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"additional_cacheable_ports": schema.SetAttribute{
ElementType: types.Int64Type,
Optional: true,
MarkdownDescription: "Specifies uncommon ports to allow cacheable assets to be served from.",
},
"automatic_https_rewrites": schema.BoolAttribute{
Optional: true,
MarkdownDescription: "Turn on or off Cloudflare Automatic HTTPS rewrites.",
Expand Down
8 changes: 8 additions & 0 deletions internal/sdkv2provider/data_source_rulesets.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,14 @@ func resourceCloudflareRulesetSchema() map[string]*schema.Schema {
Optional: true,
Description: "Whether to cache if expression matches.",
},
"additional_cacheable_ports": {
Type: schema.TypeSet,
Optional: true,
Description: "Allows for the ability to support caching on non-standard ports.",
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"automatic_https_rewrites": {
Type: schema.TypeBool,
Optional: true,
Expand Down

0 comments on commit a0e96dc

Please sign in to comment.