Skip to content

Commit

Permalink
provider/fastly Adds healthcheck service (#11709)
Browse files Browse the repository at this point in the history
* Adds schema for fastly healthcheck

* Handles changes to the fastly healthcheck

* Flattens and refreshed fastly healthchecks

* Adds testing for fastly healthcheck

* Adds website documentation for fastly healthcheck

* Fixes terraform syntax in test examples
  • Loading branch information
travertischio authored and stack72 committed Feb 7, 2017
1 parent e3e24f4 commit 441b1cc
Show file tree
Hide file tree
Showing 3 changed files with 395 additions and 0 deletions.
182 changes: 182 additions & 0 deletions builtin/providers/fastly/resource_fastly_service_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,80 @@ func resourceServiceV1() *schema.Resource {
},
},

"healthcheck": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// required fields
"name": {
Type: schema.TypeString,
Required: true,
Description: "A name to refer to this healthcheck",
},
"host": {
Type: schema.TypeString,
Required: true,
Description: "Which host to check",
},
"path": {
Type: schema.TypeString,
Required: true,
Description: "The path to check",
},
// optional fields
"check_interval": {
Type: schema.TypeInt,
Optional: true,
Default: 5000,
Description: "How often to run the healthcheck in milliseconds",
},
"expected_response": {
Type: schema.TypeInt,
Optional: true,
Default: 200,
Description: "The status code expected from the host",
},
"http_version": {
Type: schema.TypeString,
Optional: true,
Default: "1.1",
Description: "Whether to use version 1.0 or 1.1 HTTP",
},
"initial": {
Type: schema.TypeInt,
Optional: true,
Default: 2,
Description: "When loading a config, the initial number of probes to be seen as OK",
},
"method": {
Type: schema.TypeString,
Optional: true,
Default: "HEAD",
Description: "Which HTTP method to use",
},
"threshold": {
Type: schema.TypeInt,
Optional: true,
Default: 3,
Description: "How many healthchecks must succeed to be considered healthy",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 500,
Description: "Timeout in milliseconds",
},
"window": {
Type: schema.TypeInt,
Optional: true,
Default: 5,
Description: "The number of most recent healthcheck queries to keep for this healthcheck",
},
},
},
},

"s3logging": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -654,6 +728,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
"default_ttl",
"header",
"gzip",
"healthcheck",
"s3logging",
"papertrail",
"condition",
Expand Down Expand Up @@ -1008,6 +1083,65 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
}
}

// find difference in Healthcheck
if d.HasChange("healthcheck") {
oh, nh := d.GetChange("healthcheck")
if oh == nil {
oh = new(schema.Set)
}
if nh == nil {
nh = new(schema.Set)
}

ohs := oh.(*schema.Set)
nhs := nh.(*schema.Set)
removeHealthCheck := ohs.Difference(nhs).List()
addHealthCheck := nhs.Difference(ohs).List()

// DELETE old healthcheck configurations
for _, hRaw := range removeHealthCheck {
hf := hRaw.(map[string]interface{})
opts := gofastly.DeleteHealthCheckInput{
Service: d.Id(),
Version: latestVersion,
Name: hf["name"].(string),
}

log.Printf("[DEBUG] Fastly Healthcheck removal opts: %#v", opts)
err := conn.DeleteHealthCheck(&opts)
if err != nil {
return err
}
}

// POST new/updated Healthcheck
for _, hRaw := range addHealthCheck {
hf := hRaw.(map[string]interface{})

opts := gofastly.CreateHealthCheckInput{
Service: d.Id(),
Version: latestVersion,
Name: hf["name"].(string),
Host: hf["host"].(string),
Path: hf["path"].(string),
CheckInterval: uint(hf["check_interval"].(int)),
ExpectedResponse: uint(hf["expected_response"].(int)),
HTTPVersion: hf["http_version"].(string),
Initial: uint(hf["initial"].(int)),
Method: hf["method"].(string),
Threshold: uint(hf["threshold"].(int)),
Timeout: uint(hf["timeout"].(int)),
Window: uint(hf["window"].(int)),
}

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

// find difference in s3logging
if d.HasChange("s3logging") {
os, ns := d.GetChange("s3logging")
Expand Down Expand Up @@ -1438,6 +1572,23 @@ func resourceServiceV1Read(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARN] Error setting Gzips for (%s): %s", d.Id(), err)
}

// refresh Healthcheck
log.Printf("[DEBUG] Refreshing Healthcheck for (%s)", d.Id())
healthcheckList, err := conn.ListHealthChecks(&gofastly.ListHealthChecksInput{
Service: d.Id(),
Version: s.ActiveVersion.Number,
})

if err != nil {
return fmt.Errorf("[ERR] Error looking up Healthcheck for (%s), version (%s): %s", d.Id(), s.ActiveVersion.Number, err)
}

hcl := flattenHealthchecks(healthcheckList)

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

// refresh S3 Logging
log.Printf("[DEBUG] Refreshing S3 Logging for (%s)", d.Id())
s3List, err := conn.ListS3s(&gofastly.ListS3sInput{
Expand Down Expand Up @@ -1805,6 +1956,37 @@ func flattenGzips(gzipsList []*gofastly.Gzip) []map[string]interface{} {
return gl
}

func flattenHealthchecks(healthcheckList []*gofastly.HealthCheck) []map[string]interface{} {
var hl []map[string]interface{}
for _, h := range healthcheckList {
// Convert HealthChecks to a map for saving to state.
nh := map[string]interface{}{
"name": h.Name,
"host": h.Host,
"path": h.Path,
"check_interval": h.CheckInterval,
"expected_response": h.ExpectedResponse,
"http_version": h.HTTPVersion,
"initial": h.Initial,
"method": h.Method,
"threshold": h.Threshold,
"timeout": h.Timeout,
"window": h.Window,
}

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

hl = append(hl, nh)
}

return hl
}

func flattenS3s(s3List []*gofastly.S3) []map[string]interface{} {
var sl []map[string]interface{}
for _, s := range s3List {
Expand Down
Loading

0 comments on commit 441b1cc

Please sign in to comment.