Skip to content

Commit

Permalink
Update rules tf resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
steveworley committed Jan 19, 2024
1 parent 601a493 commit c03947c
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 85 deletions.
2 changes: 1 addition & 1 deletion examples/project/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ terraform {
}

resource "quant_project" "tf" {
name = "Terraform project"
name = "Terraform project 1"
allow_query_params = false
basic_auth_username = "quant"
basic_auth_password = "qcdn"
Expand Down
10 changes: 10 additions & 0 deletions examples/rules/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ output "header_rule_result" {
value = quant_rule_headers.header_rule.uuid
}

resource "quant_rule_custom_response" "custom_response" {
project = "api-test"
name = "Terraform custom response rule"
disabled = false
domain = "any"
url = "/test-response"

custom_response_status_code = 200
custom_response_body = "<h1>This is a custom response</h1>"
}

# resource "quant_rule_proxy" "proxy_rule" {
# project = "api-test"
Expand Down
65 changes: 60 additions & 5 deletions internal/provider/rule_auth_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
openapi "github.com/quantcdn/quant-admin-go"
)
Expand Down Expand Up @@ -80,6 +82,13 @@ func (r *ruleAuth) Metadata(_ context.Context, req resource.MetadataRequest, res
func (r *ruleAuth) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"uuid": schema.StringAttribute{
MarkdownDescription: "The rules UUID",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "A name for the rule",
Optional: true,
Expand All @@ -91,15 +100,21 @@ func (r *ruleAuth) Schema(_ context.Context, _ resource.SchemaRequest, resp *res
"disabled": schema.BoolAttribute{
MarkdownDescription: "If this rule is disabled",
Optional: true,
Default: booldefault.StaticBool(false),
Computed: true,
Default: booldefault.StaticBool(false),
},
"domain": schema.StringAttribute{
MarkdownDescription: "The domain the rule applies to",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("any"),
},
"url": schema.StringAttribute{
MarkdownDescription: "The URL to apply to",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("/*"),
},
"countries": schema.ListAttribute{
MarkdownDescription: "A list of countries",
Optional: true,
Expand Down Expand Up @@ -161,6 +176,27 @@ func (r *ruleAuth) Create(ctx context.Context, req resource.CreateRequest, resp

rule := openapi.NewRuleAuthRequest()

if plan.Url.IsNull() {
plan.Url = types.StringValue("*")
}

if plan.CountryInclude.IsNull() {
rule.SetCountry("any")
}

if plan.MethodInclude.IsNull() {
rule.SetMethod("any")
}

if plan.IpInclude.IsNull() {
rule.SetIp("any")
}

rule.SetName(plan.Name.ValueString())
rule.SetDisabled(plan.Disabled.ValueBool())
rule.SetDomain(plan.Domain.ValueString())
rule.SetUrl(plan.Url.ValueString())

if !plan.CountryInclude.IsNull() && !plan.CountryInclude.IsUnknown() {
if plan.CountryInclude.ValueBool() {
rule.SetCountryIs(helpers.FlattenToStrings(plan.Countries))
Expand All @@ -185,8 +221,7 @@ func (r *ruleAuth) Create(ctx context.Context, req resource.CreateRequest, resp
}
}

rule.SetName(plan.Name.ValueString())
rule.SetDomain(plan.Domain.ValueString())
// Rule behaviour.
rule.SetAuthUser(plan.AuthUser.ValueString())
rule.SetAuthPass(plan.AuthUser.ValueString())

Expand Down Expand Up @@ -257,6 +292,27 @@ func (r *ruleAuth) Update(ctx context.Context, req resource.UpdateRequest, resp

rule := openapi.NewRuleAuthRequest()

if plan.Url.IsNull() {
plan.Url = types.StringValue("*")
}

if plan.CountryInclude.IsNull() {
rule.SetCountry("any")
}

if plan.MethodInclude.IsNull() {
rule.SetMethod("any")
}

if plan.IpInclude.IsNull() {
rule.SetIp("any")
}

rule.SetName(plan.Name.ValueString())
rule.SetDisabled(plan.Disabled.ValueBool())
rule.SetDomain(plan.Domain.ValueString())
rule.SetUrl(plan.Url.ValueString())

if !plan.CountryInclude.IsNull() && !plan.CountryInclude.IsUnknown() {
if plan.CountryInclude.ValueBool() {
rule.SetCountryIs(helpers.FlattenToStrings(plan.Countries))
Expand All @@ -281,8 +337,7 @@ func (r *ruleAuth) Update(ctx context.Context, req resource.UpdateRequest, resp
}
}

rule.SetName(plan.Name.ValueString())
rule.SetDomain(plan.Domain.ValueString())
// Rule behaviour.
rule.SetAuthUser(plan.AuthUser.ValueString())
rule.SetAuthPass(plan.AuthUser.ValueString())

Expand Down
7 changes: 7 additions & 0 deletions internal/provider/rule_custom_response_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func (r *ruleCustomResponse) Metadata(_ context.Context, req resource.MetadataRe
func (r *ruleCustomResponse) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"uuid": schema.StringAttribute{
MarkdownDescription: "The rules UUID",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "A name for the rule",
Optional: true,
Expand Down
Loading

0 comments on commit c03947c

Please sign in to comment.