Skip to content

Commit

Permalink
force creating new condition if type is changed (#518)
Browse files Browse the repository at this point in the history
* force creating new `condition` if `type` is changed

* Add test

* fix format
  • Loading branch information
smaeda-ks authored Dec 20, 2021
1 parent 38fdf4a commit 678097f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 15 deletions.
47 changes: 38 additions & 9 deletions fastly/block_fastly_service_v1_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,16 @@ func (h *ConditionServiceAttributeHandler) Read(_ context.Context, d *schema.Res

func (h *ConditionServiceAttributeHandler) Update(_ context.Context, d *schema.ResourceData, resource, modified map[string]interface {
}, serviceVersion int, conn *gofastly.Client) error {
opts := gofastly.UpdateConditionInput{
optsCreate := gofastly.CreateConditionInput{
ServiceID: d.Id(),
ServiceVersion: serviceVersion,
Name: resource["name"].(string),
Type: resource["type"].(string),
Statement: strings.TrimSpace(resource["statement"].(string)),
Priority: gofastly.Int(resource["priority"].(int)),
}

optsUpdate := gofastly.UpdateConditionInput{
ServiceID: d.Id(),
ServiceVersion: serviceVersion,
Name: resource["name"].(string),
Expand All @@ -112,20 +121,40 @@ func (h *ConditionServiceAttributeHandler) Update(_ context.Context, d *schema.R
// this and so we've updated the below code to convert the type asserted
// int into a uint before passing the value to gofastly.Uint().
if v, ok := modified["comment"]; ok {
opts.Comment = gofastly.String(v.(string))
optsUpdate.Comment = gofastly.String(v.(string))
}
if v, ok := modified["statement"]; ok {
opts.Statement = gofastly.String(v.(string))
}
if v, ok := modified["type"]; ok {
opts.Type = gofastly.String(v.(string))
optsCreate.Statement = v.(string)
optsUpdate.Statement = gofastly.String(v.(string))
}
if v, ok := modified["priority"]; ok {
opts.Priority = gofastly.Int(v.(int))
optsCreate.Priority = gofastly.Int(v.(int))
optsUpdate.Priority = gofastly.Int(v.(int))
}
// NOTE: Fastly API doesn't support updating the condition "type".
// Therefore, we need to DELETE and CREATE if "type" attribute is changed.
if v, ok := modified["type"]; ok {
optsCreate.Type = v.(string)
log.Printf("[DEBUG] Delete Condition: %s (type changed)", resource["name"].(string))
err := conn.DeleteCondition(&gofastly.DeleteConditionInput{
ServiceID: d.Id(),
ServiceVersion: serviceVersion,
Name: resource["name"].(string),
})
if err != nil {
return err
}

log.Printf("[DEBUG] Create Condition Opts: %#v", optsCreate)
_, err = conn.CreateCondition(&optsCreate)
if err != nil {
return err
}
return nil
}

log.Printf("[DEBUG] Update Condition Opts: %#v", opts)
_, err := conn.UpdateCondition(&opts)
log.Printf("[DEBUG] Update Condition Opts: %#v", optsUpdate)
_, err := conn.UpdateCondition(&optsUpdate)
if err != nil {
return err
}
Expand Down
65 changes: 59 additions & 6 deletions fastly/block_fastly_service_v1_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ func TestAccFastlyServiceV1_conditional_basic(t *testing.T) {
domainName1 := fmt.Sprintf("fastly-test.tf-%s.com", acctest.RandString(10))

con1 := gofastly.Condition{
Name: "some amz condition",
Name: "some test condition",
Priority: 10,
Type: "REQUEST",
Statement: `req.url ~ "^/yolo/"`,
}

con2 := gofastly.Condition{
Name: "some test condition",
Priority: 10,
Type: "CACHE",
Statement: `req.url ~ "^/yolo/"`,
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
Expand All @@ -74,6 +81,13 @@ func TestAccFastlyServiceV1_conditional_basic(t *testing.T) {
"fastly_service_v1.foo", "condition.#", "1"),
),
},
{
Config: testAccServiceV1ConditionConfig_update(name, domainName1, "CACHE"),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
testAccCheckFastlyServiceV1ConditionalAttributes(&service, name, []*gofastly.Condition{&con2}),
),
},
},
})
}
Expand Down Expand Up @@ -141,14 +155,16 @@ resource "fastly_service_v1" "foo" {
}
header {
destination = "http.x-amz-request-id"
type = "cache"
action = "delete"
name = "remove x-amz-request-id"
destination = "http.x-foo"
source = "\"bar\""
type = "request"
action = "set"
name = "set x-foo"
request_condition = "some test condition"
}
condition {
name = "some amz condition"
name = "some test condition"
type = "REQUEST"
statement = "req.url ~ \"^/yolo/\""
Expand All @@ -159,3 +175,40 @@ resource "fastly_service_v1" "foo" {
force_destroy = true
}`, name, domain)
}

func testAccServiceV1ConditionConfig_update(name, domain, condType string) string {
return fmt.Sprintf(`
resource "fastly_service_v1" "foo" {
name = "%s"
domain {
name = "%s"
comment = "tf-testing-domain"
}
backend {
address = "aws.amazon.com"
name = "amazon docs"
}
header {
destination = "http.x-foo"
source = "\"bar\""
type = "cache"
action = "set"
name = "set x-foo"
cache_condition = "some test condition"
}
condition {
name = "some test condition"
type = "%s"
statement = "req.url ~ \"^/yolo/\""
priority = 10
}
force_destroy = true
}`, name, domain, condType)
}

0 comments on commit 678097f

Please sign in to comment.