Skip to content

Commit

Permalink
provider/fastly: add support for custom VCL configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Moyer authored and catsby committed May 12, 2016
1 parent f70f778 commit 6ba5bb3
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 0 deletions.
165 changes: 165 additions & 0 deletions builtin/providers/fastly/resource_fastly_service_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,40 @@ func resourceServiceV1() *schema.Resource {
},
},
},

"vcl": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "A name to refer to this VCL configuration",
},
"content": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The contents of this VCL configuration",
},
"main": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Should this VCL configuation be the main configuration",
},
},
},
},
},
}
}

func resourceServiceV1Create(d *schema.ResourceData, meta interface{}) error {
if err := validateVCLs(d); err != nil {
return err
}

conn := meta.(*FastlyClient).conn
service, err := conn.CreateService(&gofastly.CreateServiceInput{
Name: d.Get("name").(string),
Expand All @@ -416,6 +445,10 @@ func resourceServiceV1Create(d *schema.ResourceData, meta interface{}) error {
}

func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
if err := validateVCLs(d); err != nil {
return err
}

conn := meta.(*FastlyClient).conn

// Update Name. No new verions is required for this
Expand Down Expand Up @@ -443,6 +476,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
"gzip",
"s3logging",
"condition",
"vcl",
} {
if d.HasChange(v) {
needsChange = true
Expand Down Expand Up @@ -854,6 +888,72 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
}
}

// Find differences in VCLs
if d.HasChange("vcl") {
// Note: as above with Gzip and S3 logging, we don't utilize the PUT
// endpoint to update a VCL, we simply destroy it and create a new one.
oldVCLVal, newVCLVal := d.GetChange("vcl")
if oldVCLVal == nil {
oldVCLVal = new(schema.Set)
}
if newVCLVal == nil {
newVCLVal = new(schema.Set)
}

oldVCLSet := oldVCLVal.(*schema.Set)
newVCLSet := newVCLVal.(*schema.Set)

remove := oldVCLSet.Difference(newVCLSet).List()
add := newVCLSet.Difference(oldVCLSet).List()

// Delete removed VCL configurations
for _, dRaw := range remove {
df := dRaw.(map[string]interface{})
opts := gofastly.DeleteVCLInput{
Service: d.Id(),
Version: latestVersion,
Name: df["name"].(string),
}

log.Printf("[DEBUG] Fastly VCL Removal opts: %#v", opts)
err := conn.DeleteVCL(&opts)
if err != nil {
return err
}
}
// POST new VCL configurations
for _, dRaw := range add {
df := dRaw.(map[string]interface{})
opts := gofastly.CreateVCLInput{
Service: d.Id(),
Version: latestVersion,
Name: df["name"].(string),
Content: df["content"].(string),
}

log.Printf("[DEBUG] Fastly VCL Addition opts: %#v", opts)
_, err := conn.CreateVCL(&opts)
if err != nil {
return err
}

// if this new VCL is the main
if df["main"].(bool) {
opts := gofastly.ActivateVCLInput{
Service: d.Id(),
Version: latestVersion,
Name: df["name"].(string),
}
log.Printf("[DEBUG] Fastly VCL activation opts: %#v", opts)
_, err := conn.ActivateVCL(&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{
Expand Down Expand Up @@ -1034,6 +1134,22 @@ func resourceServiceV1Read(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARN] Error setting Conditions for (%s): %s", d.Id(), err)
}

// refresh VCLs
log.Printf("[DEBUG] Refreshing VCLs for (%s)", d.Id())
vclList, err := conn.ListVCLs(&gofastly.ListVCLsInput{
Service: d.Id(),
Version: s.ActiveVersion.Number,
})
if err != nil {
return fmt.Errorf("[ERR] Error looking up VCLs for (%s), version (%s): %s", d.Id(), s.ActiveVersion.Number, err)
}

vl := flattenVCLs(vclList)

if err := d.Set("vcl", vl); err != nil {
log.Printf("[WARN] Error setting VCLs for (%s): %s", d.Id(), err)
}

} else {
log.Printf("[DEBUG] Active Version for Service (%s) is empty, no state to refresh", d.Id())
}
Expand Down Expand Up @@ -1326,3 +1442,52 @@ func flattenConditions(conditionList []*gofastly.Condition) []map[string]interfa

return cl
}

func flattenVCLs(vclList []*gofastly.VCL) []map[string]interface{} {
var vl []map[string]interface{}
for _, vcl := range vclList {
// Convert VCLs to a map for saving to state.
vclMap := map[string]interface{}{
"name": vcl.Name,
"content": vcl.Content,
"main": vcl.Main,
}

// prune any empty values that come from the default string value in structs
for k, v := range vclMap {
if v == "" {
delete(vclMap, k)
}
}

vl = append(vl, vclMap)
}

return vl
}

func validateVCLs(d *schema.ResourceData) error {
// TODO: this would be nice to move into a resource/collection validation function, once that is available
// (see https://github.com/hashicorp/terraform/pull/4348 and https://github.com/hashicorp/terraform/pull/6508)
vcls, exists := d.GetOk("vcl")
if !exists {
return nil
}

numberOfMainVCLs, numberOfIncludeVCLs := 0, 0
for _, vclElem := range vcls.(*schema.Set).List() {
vcl := vclElem.(map[string]interface{})
if mainVal, hasMain := vcl["main"]; hasMain && mainVal.(bool) {
numberOfMainVCLs++
} else {
numberOfIncludeVCLs++
}
}
if numberOfMainVCLs == 0 && numberOfIncludeVCLs > 0 {
return fmt.Errorf("if you include VCL configurations, one of them should have main = true")
}
if numberOfMainVCLs > 1 {
return fmt.Errorf("you cannot have more than one VCL configuration with main = true")
}
return nil
}
43 changes: 43 additions & 0 deletions website/source/docs/providers/fastly/r/service_v1.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,38 @@ resource "aws_s3_bucket" "website" {
}
```

Basic usage with [custom VCL](https://docs.fastly.com/guides/vcl/uploading-custom-vcl) (must be enabled on your Fastly account):

```
resource "fastly_service_v1" "demo" {
name = "demofastly"
domain {
name = "demo.notexample.com"
comment = "demo"
}
backend {
address = "127.0.0.1"
name = "localhost"
port = 80
}
force_destroy = true
vcl {
name = "my_custom_main_vcl"
content = "${file("${path.module}/my_custom_main.vcl")}"
main = true
}
vcl {
name = "my_custom_library_vcl"
content = "${file("${path.module}/my_custom_library.vcl")}"
}
}
```

**Note:** For an AWS S3 Bucket, the Backend address is
`<domain>.s3-website-<region>.amazonaws.com`. The `default_host` attribute
should be set to `<bucket_name>.s3-website-<region>.amazonaws.com`. See the
Expand All @@ -112,6 +144,9 @@ below
order to destroy the Service, set `force_destroy` to `true`. Default `false`.
* `s3logging` - (Optional) A set of S3 Buckets to send streaming logs too.
Defined below
* `vcl` - (Optional) A set of custom VCL configuration blocks. Note that the
ability to upload custom VCL code is not enabled by default for new Fastly
accounts (see the [Fastly documentation](https://docs.fastly.com/guides/vcl/uploading-custom-vcl) for details).


The `domain` block supports:
Expand Down Expand Up @@ -204,6 +239,13 @@ compressed. Default `0`
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`).

The `vcl` block supports:

* `name` - (Required) A unique name for this configuration block
* `content` - (Required) The custom VCL code to upload.
* `main` - (Optional) If `true`, use this block as the main configuration. If
`false`, use this block as an includable library. Only a single VCL block can be
marked as the main block. Default is `false`.

## Attributes Reference

Expand All @@ -216,6 +258,7 @@ The following attributes are exported:
* `backend` – Set of Backends. See above for details
* `header` – Set of Headers. See above for details
* `s3logging` – Set of S3 Logging configurations. See above for details
* `vcl` – Set of custom VCL configurations. See above for details
* `default_host` – Default host specified
* `default_ttl` - Default TTL
* `force_destroy` - Force the destruction of the Service on delete
Expand Down

0 comments on commit 6ba5bb3

Please sign in to comment.