provider/fastly: Add support for Service Request Settings
This commit is contained in:
parent
59a9bcf3dc
commit
5a06b603bd
|
@ -396,6 +396,79 @@ func resourceServiceV1() *schema.Resource {
|
|||
},
|
||||
},
|
||||
},
|
||||
|
||||
"request_setting": &schema.Schema{
|
||||
Type: schema.TypeSet,
|
||||
Optional: true,
|
||||
Elem: &schema.Resource{
|
||||
Schema: map[string]*schema.Schema{
|
||||
// Required fields
|
||||
"name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
Description: "Unique name to refer to this Request Setting",
|
||||
},
|
||||
"request_condition": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
Description: "Name of a RequestCondition to apply.",
|
||||
},
|
||||
// Optional fields
|
||||
"max_stale_age": &schema.Schema{
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
Default: 60,
|
||||
Description: "How old an object is allowed to be, in seconds. Default `60`",
|
||||
},
|
||||
"force_miss": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Description: "Force a cache miss for the request",
|
||||
},
|
||||
"force_ssl": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Description: "Forces the request use SSL",
|
||||
},
|
||||
"action": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: "Allows you to terminate request handling and immediately perform an action",
|
||||
},
|
||||
"bypass_busy_wait": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Description: "Disable collapsed forwarding",
|
||||
},
|
||||
"hash_keys": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: "Comma separated list of varnish request object fields that should be in the hash key",
|
||||
},
|
||||
"xff": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Default: "append",
|
||||
Description: "X-Forwarded-For options",
|
||||
},
|
||||
"timer_support": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Description: "Injects the X-Timer info into the request",
|
||||
},
|
||||
"geo_headers": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Description: "Inject Fastly-Geo-Country, Fastly-Geo-City, and Fastly-Geo-Region",
|
||||
},
|
||||
"default_host": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: "the host header",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -443,6 +516,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
|
|||
"gzip",
|
||||
"s3logging",
|
||||
"condition",
|
||||
"request_setting",
|
||||
} {
|
||||
if d.HasChange(v) {
|
||||
needsChange = true
|
||||
|
@ -584,7 +658,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
|
|||
Name: df["name"].(string),
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Fastly Domain Removal opts: %#v", opts)
|
||||
log.Printf("[DEBUG] Fastly Domain removal opts: %#v", opts)
|
||||
err := conn.DeleteDomain(&opts)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -636,7 +710,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
|
|||
Name: bf["name"].(string),
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Fastly Backend Removal opts: %#v", opts)
|
||||
log.Printf("[DEBUG] Fastly Backend removal opts: %#v", opts)
|
||||
err := conn.DeleteBackend(&opts)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -694,7 +768,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
|
|||
Name: df["name"].(string),
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Fastly Header Removal opts: %#v", opts)
|
||||
log.Printf("[DEBUG] Fastly Header removal opts: %#v", opts)
|
||||
err := conn.DeleteHeader(&opts)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -744,7 +818,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
|
|||
Name: df["name"].(string),
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Fastly Gzip Removal opts: %#v", opts)
|
||||
log.Printf("[DEBUG] Fastly Gzip removal opts: %#v", opts)
|
||||
err := conn.DeleteGzip(&opts)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -812,7 +886,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
|
|||
Name: sf["name"].(string),
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Fastly S3 Logging Removal opts: %#v", opts)
|
||||
log.Printf("[DEBUG] Fastly S3 Logging removal opts: %#v", opts)
|
||||
err := conn.DeleteS3(&opts)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -854,6 +928,55 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
|
|||
}
|
||||
}
|
||||
|
||||
// find difference in request settings
|
||||
if d.HasChange("request_setting") {
|
||||
os, ns := d.GetChange("request_setting")
|
||||
if os == nil {
|
||||
os = new(schema.Set)
|
||||
}
|
||||
if ns == nil {
|
||||
ns = new(schema.Set)
|
||||
}
|
||||
|
||||
ors := os.(*schema.Set)
|
||||
nrs := ns.(*schema.Set)
|
||||
removeRequestSettings := ors.Difference(nrs).List()
|
||||
addRequestSettings := nrs.Difference(ors).List()
|
||||
|
||||
// DELETE old Request Settings configurations
|
||||
for _, sRaw := range removeRequestSettings {
|
||||
sf := sRaw.(map[string]interface{})
|
||||
opts := gofastly.DeleteRequestSettingInput{
|
||||
Service: d.Id(),
|
||||
Version: latestVersion,
|
||||
Name: sf["name"].(string),
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Fastly Request Setting removal opts: %#v", opts)
|
||||
err := conn.DeleteRequestSetting(&opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// POST new/updated Request Setting
|
||||
for _, sRaw := range addRequestSettings {
|
||||
opts, err := buildRequestSetting(sRaw.(map[string]interface{}))
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG] Error building Requset Setting: %s", err)
|
||||
return err
|
||||
}
|
||||
opts.Service = d.Id()
|
||||
opts.Version = latestVersion
|
||||
|
||||
log.Printf("[DEBUG] Create Request Setting Opts: %#v", opts)
|
||||
_, err = conn.CreateRequestSetting(opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// validate version
|
||||
log.Printf("[DEBUG] Validating Fastly Service (%s), Version (%s)", d.Id(), latestVersion)
|
||||
valid, msg, err := conn.ValidateVersion(&gofastly.ValidateVersionInput{
|
||||
|
@ -1034,6 +1157,23 @@ func resourceServiceV1Read(d *schema.ResourceData, meta interface{}) error {
|
|||
log.Printf("[WARN] Error setting Conditions for (%s): %s", d.Id(), err)
|
||||
}
|
||||
|
||||
// refresh Request Settings
|
||||
log.Printf("[DEBUG] Refreshing Request Settings for (%s)", d.Id())
|
||||
rsList, err := conn.ListRequestSettings(&gofastly.ListRequestSettingsInput{
|
||||
Service: d.Id(),
|
||||
Version: s.ActiveVersion.Number,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("[ERR] Error looking up Request Settings for (%s), version (%s): %s", d.Id(), s.ActiveVersion.Number, err)
|
||||
}
|
||||
|
||||
rl := flattenRequestSettings(rsList)
|
||||
|
||||
if err := d.Set("request_setting", rl); err != nil {
|
||||
log.Printf("[WARN] Error setting Request Settings for (%s): %s", d.Id(), err)
|
||||
}
|
||||
|
||||
} else {
|
||||
log.Printf("[DEBUG] Active Version for Service (%s) is empty, no state to refresh", d.Id())
|
||||
}
|
||||
|
@ -1326,3 +1466,75 @@ func flattenConditions(conditionList []*gofastly.Condition) []map[string]interfa
|
|||
|
||||
return cl
|
||||
}
|
||||
|
||||
func flattenRequestSettings(rsList []*gofastly.RequestSetting) []map[string]interface{} {
|
||||
var rl []map[string]interface{}
|
||||
for _, r := range rsList {
|
||||
// Convert Request Settings to a map for saving to state.
|
||||
nrs := map[string]interface{}{
|
||||
"name": r.Name,
|
||||
"max_stale_age": r.MaxStaleAge,
|
||||
"force_miss": r.ForceMiss,
|
||||
"force_ssl": r.ForceSSL,
|
||||
"action": r.Action,
|
||||
"bypass_busy_wait": r.BypassBusyWait,
|
||||
"hash_keys": r.HashKeys,
|
||||
"xff": r.XForwardedFor,
|
||||
"timer_support": r.TimerSupport,
|
||||
"geo_headers": r.GeoHeaders,
|
||||
"default_host": r.DefaultHost,
|
||||
"request_condition": r.RequestCondition,
|
||||
}
|
||||
|
||||
// prune any empty values that come from the default string value in structs
|
||||
for k, v := range nrs {
|
||||
if v == "" {
|
||||
delete(nrs, k)
|
||||
}
|
||||
}
|
||||
|
||||
rl = append(rl, nrs)
|
||||
}
|
||||
|
||||
return rl
|
||||
}
|
||||
|
||||
func buildRequestSetting(requestSettingMap interface{}) (*gofastly.CreateRequestSettingInput, error) {
|
||||
df := requestSettingMap.(map[string]interface{})
|
||||
opts := gofastly.CreateRequestSettingInput{
|
||||
Name: df["name"].(string),
|
||||
MaxStaleAge: uint(df["max_stale_age"].(int)),
|
||||
ForceMiss: gofastly.Compatibool(df["force_miss"].(bool)),
|
||||
ForceSSL: gofastly.Compatibool(df["force_ssl"].(bool)),
|
||||
BypassBusyWait: gofastly.Compatibool(df["bypass_busy_wait"].(bool)),
|
||||
HashKeys: df["hash_keys"].(string),
|
||||
TimerSupport: gofastly.Compatibool(df["timer_support"].(bool)),
|
||||
GeoHeaders: gofastly.Compatibool(df["geo_headers"].(bool)),
|
||||
DefaultHost: df["default_host"].(string),
|
||||
RequestCondition: df["request_condition"].(string),
|
||||
}
|
||||
|
||||
act := strings.ToLower(df["action"].(string))
|
||||
switch act {
|
||||
case "lookup":
|
||||
opts.Action = gofastly.RequestSettingActionLookup
|
||||
case "pass":
|
||||
opts.Action = gofastly.RequestSettingActionPass
|
||||
}
|
||||
|
||||
xff := strings.ToLower(df["xff"].(string))
|
||||
switch xff {
|
||||
case "clear":
|
||||
opts.XForwardedFor = gofastly.RequestSettingXFFClear
|
||||
case "leave":
|
||||
opts.XForwardedFor = gofastly.RequestSettingXFFLeave
|
||||
case "append":
|
||||
opts.XForwardedFor = gofastly.RequestSettingXFFAppend
|
||||
case "append_all":
|
||||
opts.XForwardedFor = gofastly.RequestSettingXFFAppendAll
|
||||
case "overwrite":
|
||||
opts.XForwardedFor = gofastly.RequestSettingXFFOverwrite
|
||||
}
|
||||
|
||||
return &opts, nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
package fastly
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/acctest"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
gofastly "github.com/sethvargo/go-fastly"
|
||||
)
|
||||
|
||||
func TestAccFastlyServiceV1RequestSetting_basic(t *testing.T) {
|
||||
var service gofastly.ServiceDetail
|
||||
name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
||||
domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
|
||||
|
||||
rq1 := gofastly.RequestSetting{
|
||||
Name: "alt_backend",
|
||||
RequestCondition: "serve_alt_backend",
|
||||
DefaultHost: "tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com",
|
||||
XForwardedFor: "append",
|
||||
MaxStaleAge: uint(90),
|
||||
}
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckServiceV1Destroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccServiceV1RequestSetting(name, domainName1),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
|
||||
testAccCheckFastlyServiceV1RequestSettingsAttributes(&service, []*gofastly.RequestSetting{&rq1}),
|
||||
resource.TestCheckResourceAttr(
|
||||
"fastly_service_v1.foo", "name", name),
|
||||
resource.TestCheckResourceAttr(
|
||||
"fastly_service_v1.foo", "request_setting.#", "1"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"fastly_service_v1.foo", "condition.#", "1"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckFastlyServiceV1RequestSettingsAttributes(service *gofastly.ServiceDetail, rqs []*gofastly.RequestSetting) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
|
||||
conn := testAccProvider.Meta().(*FastlyClient).conn
|
||||
rqList, err := conn.ListRequestSettings(&gofastly.ListRequestSettingsInput{
|
||||
Service: service.ID,
|
||||
Version: service.ActiveVersion.Number,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("[ERR] Error looking up Request Setting for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
|
||||
}
|
||||
|
||||
if len(rqList) != len(rqs) {
|
||||
return fmt.Errorf("Request Setting List count mismatch, expected (%d), got (%d)", len(rqs), len(rqList))
|
||||
}
|
||||
|
||||
var found int
|
||||
for _, r := range rqs {
|
||||
for _, lr := range rqList {
|
||||
if r.Name == lr.Name {
|
||||
// we don't know these things ahead of time, so populate them now
|
||||
r.ServiceID = service.ID
|
||||
r.Version = service.ActiveVersion.Number
|
||||
if !reflect.DeepEqual(r, lr) {
|
||||
return fmt.Errorf("Bad match Request Setting match, expected (%#v), got (%#v)", r, lr)
|
||||
}
|
||||
found++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if found != len(rqs) {
|
||||
return fmt.Errorf("Error matching Request Setting rules (%d/%d)", found, len(rqs))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func testAccServiceV1RequestSetting(name, domain string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "fastly_service_v1" "foo" {
|
||||
name = "%s"
|
||||
|
||||
domain {
|
||||
name = "%s"
|
||||
comment = "demo"
|
||||
}
|
||||
|
||||
backend {
|
||||
address = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"
|
||||
name = "AWS S3 hosting"
|
||||
port = 80
|
||||
}
|
||||
|
||||
backend {
|
||||
address = "tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com"
|
||||
name = "OtherAWSS3hosting"
|
||||
port = 80
|
||||
}
|
||||
|
||||
condition {
|
||||
name = "serve_alt_backend"
|
||||
type = "REQUEST"
|
||||
priority = 10
|
||||
statement = "req.url ~ \"^/alt/\""
|
||||
}
|
||||
|
||||
request_setting {
|
||||
default_host = "tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com"
|
||||
name = "alt_backend"
|
||||
request_condition = "serve_alt_backend"
|
||||
max_stale_age = 90
|
||||
}
|
||||
|
||||
default_host = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"
|
||||
|
||||
force_destroy = true
|
||||
}`, name, domain)
|
||||
}
|
|
@ -110,6 +110,7 @@ below
|
|||
* `default_ttl` - (Optional) The default Time-to-live (TTL) for requests
|
||||
* `force_destroy` - (Optional) Services that are active cannot be destroyed. In
|
||||
order to destroy the Service, set `force_destroy` to `true`. Default `false`.
|
||||
* `request_setting` - (Optional) A set of Request modifiers. Defined below
|
||||
* `s3logging` - (Optional) A set of S3 Buckets to send streaming logs too.
|
||||
Defined below
|
||||
|
||||
|
@ -179,6 +180,32 @@ by the Action
|
|||
* `substitution` - (Optional) Value to substitute in place of regular expression. (Only applies to `regex` and `regex_repeat`.)
|
||||
* `priority` - (Optional) Lower priorities execute first. (Default: `100`.)
|
||||
|
||||
The `request_setting` block allow you to customize Fastly's request handling, by
|
||||
defining behavior that should change based on a predefined `condition`:
|
||||
|
||||
* `name` - (Required) The domain that this request setting
|
||||
* `request_condition` - (Required) The name of the corresponding `condition` to
|
||||
determin if this request setting should be applied. The `request_condition` must
|
||||
match the name of a defined `condition`
|
||||
* `max_stale_age` - (Optional) How old an object is allowed to be to serve
|
||||
stale-if-error or stale-while-revalidate, in seconds. Default `60`
|
||||
* `force_miss` - (Optional) Force a cache miss for the request. If specfified,
|
||||
can be `true` or `false`.
|
||||
* `force_ssl` - (Optional) Forces the request use SSL (redirects a non-SSL to SSL)
|
||||
* `action` - (Optional) Allows you to terminate request handling and immediately
|
||||
perform an action. When set it can be `lookup` or `pass` (ignore the cache completely)
|
||||
* `bypass_busy_wait` - (Optional) Disable collapsed forwarding, so you don't wait
|
||||
for other objects to origin
|
||||
* `hash_keys` - (Optional) Comma separated list of varnish request object fields
|
||||
that should be in the hash key
|
||||
* `xff` - (Optional) X-Forwarded-For -- should be `clear`, `leave`, `append`,
|
||||
`append_all`, or `overwrite`. Default `append`
|
||||
* `timer_support` - (Optional) Injects the X-Timer info into the request for
|
||||
viewing origin fetch durations
|
||||
* `geo_headers` - (Optional) Injects Fastly-Geo-Country, Fastly-Geo-City, and
|
||||
Fastly-Geo-Region into the request headers
|
||||
* `default_host` - (Optional) Sets the host header
|
||||
|
||||
The `s3logging` block supports:
|
||||
|
||||
* `name` - (Required) A unique name to identify this S3 Logging Bucket
|
||||
|
@ -203,6 +230,9 @@ compressed. Default `0`
|
|||
* `format` - (Optional) Apache-style string or VCL variables to use for log formatting. Default
|
||||
Apache Common Log format (`%h %l %u %t %r %>s`)
|
||||
* `timestamp_format` - (Optional) `strftime` specified timestamp formatting (default `%Y-%m-%dT%H:%M:%S.000`).
|
||||
* `request_condition` - (Optional) The VCL request condition to check if this
|
||||
Request Setting should be applied. For detailed information about Conditionals,
|
||||
see [Fastly's Documentation on Conditionals][fastly-conditionals]
|
||||
|
||||
|
||||
## Attributes Reference
|
||||
|
@ -223,3 +253,4 @@ The following attributes are exported:
|
|||
|
||||
[fastly-s3]: https://docs.fastly.com/guides/integrations/amazon-s3
|
||||
[fastly-cname]: https://docs.fastly.com/guides/basic-setup/adding-cname-records
|
||||
[fastly-conditionals]: https://docs.fastly.com/guides/conditions/using-conditions
|
||||
|
|
Loading…
Reference in New Issue