From 065e714842307eca90edba599132fb1e0dab5809 Mon Sep 17 00:00:00 2001 From: clint shryock Date: Thu, 12 May 2016 15:05:32 -0500 Subject: [PATCH] re-go-fmt after rebase --- .../fastly/resource_fastly_service_v1.go | 304 +++++++++--------- 1 file changed, 152 insertions(+), 152 deletions(-) diff --git a/builtin/providers/fastly/resource_fastly_service_v1.go b/builtin/providers/fastly/resource_fastly_service_v1.go index 06166f744d2b..2d5452feba94 100644 --- a/builtin/providers/fastly/resource_fastly_service_v1.go +++ b/builtin/providers/fastly/resource_fastly_service_v1.go @@ -397,38 +397,38 @@ 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", - }, - }, - }, - }, + "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 - } + if err := validateVCLs(d); err != nil { + return err + } conn := meta.(*FastlyClient).conn service, err := conn.CreateService(&gofastly.CreateServiceInput{ @@ -445,9 +445,9 @@ func resourceServiceV1Create(d *schema.ResourceData, meta interface{}) error { } func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error { - if err := validateVCLs(d); err != nil { - return err - } + if err := validateVCLs(d); err != nil { + return err + } conn := meta.(*FastlyClient).conn @@ -476,7 +476,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error { "gzip", "s3logging", "condition", - "vcl", + "vcl", } { if d.HasChange(v) { needsChange = true @@ -888,71 +888,71 @@ 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 - } - - } - } - } + // 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) @@ -1134,21 +1134,21 @@ 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) - } + // 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) + vl := flattenVCLs(vclList) - if err := d.Set("vcl", vl); err != nil { - log.Printf("[WARN] Error setting VCLs for (%s): %s", d.Id(), err) - } + 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()) @@ -1444,50 +1444,50 @@ func flattenConditions(conditionList []*gofastly.Condition) []map[string]interfa } 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 + 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 + // 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 }