Restore previous version for now

This commit is contained in:
Chris Johnson 2017-05-25 10:27:22 -04:00
parent 53a1a4b73b
commit 6a0dca93af
2 changed files with 47 additions and 49 deletions

View File

@ -1,40 +0,0 @@
package librato
import (
"encoding/json"
"fmt"
)
// Encodes a hash into a JSON string
func attributesFlatten(attrs map[string]string) (string, error) {
byteArray, err := json.Marshal(attrs)
if err != nil {
return "", fmt.Errorf("Error encoding to JSON: %s", err)
}
return string(byteArray), nil
}
// Takes JSON in a string & decodes into a hash
func attributesExpand(raw string) (map[string]string, error) {
attrs := make(map[string]string)
err := json.Unmarshal([]byte(raw), &attrs)
if err != nil {
return nil, fmt.Errorf("Error decoding JSON: %s", err)
}
return attrs, err
}
func normalizeJSON(jsonString interface{}) string {
if jsonString == nil || jsonString == "" {
return ""
}
var j interface{}
err := json.Unmarshal([]byte(jsonString.(string)), &j)
if err != nil {
return fmt.Sprintf("Error parsing JSON: %s", err)
}
b, _ := json.Marshal(j)
return string(b[:])
}

View File

@ -1,6 +1,7 @@
package librato package librato
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"reflect" "reflect"
@ -36,12 +37,49 @@ func resourceLibratoService() *schema.Resource {
"settings": &schema.Schema{ "settings": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
StateFunc: normalizeJSON, StateFunc: normalizeJson,
}, },
}, },
} }
} }
// Takes JSON in a string. Decodes JSON into
// settings hash
func resourceLibratoServicesExpandSettings(rawSettings string) (map[string]string, error) {
var settings map[string]string
settings = make(map[string]string)
err := json.Unmarshal([]byte(rawSettings), &settings)
if err != nil {
return nil, fmt.Errorf("Error decoding JSON: %s", err)
}
return settings, err
}
// Encodes a settings hash into a JSON string
func resourceLibratoServicesFlatten(settings map[string]string) (string, error) {
byteArray, err := json.Marshal(settings)
if err != nil {
return "", fmt.Errorf("Error encoding to JSON: %s", err)
}
return string(byteArray), nil
}
func normalizeJson(jsonString interface{}) string {
if jsonString == nil || jsonString == "" {
return ""
}
var j interface{}
err := json.Unmarshal([]byte(jsonString.(string)), &j)
if err != nil {
return fmt.Sprintf("Error parsing JSON: %s", err)
}
b, _ := json.Marshal(j)
return string(b[:])
}
func resourceLibratoServiceCreate(d *schema.ResourceData, meta interface{}) error { func resourceLibratoServiceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*librato.Client) client := meta.(*librato.Client)
@ -53,7 +91,7 @@ func resourceLibratoServiceCreate(d *schema.ResourceData, meta interface{}) erro
service.Title = librato.String(v.(string)) service.Title = librato.String(v.(string))
} }
if v, ok := d.GetOk("settings"); ok { if v, ok := d.GetOk("settings"); ok {
res, err := attributesExpand(normalizeJSON(v.(string))) res, err := resourceLibratoServicesExpandSettings(normalizeJson(v.(string)))
if err != nil { if err != nil {
return fmt.Errorf("Error expanding Librato service settings: %s", err) return fmt.Errorf("Error expanding Librato service settings: %s", err)
} }
@ -106,7 +144,7 @@ func resourceLibratoServiceReadResult(d *schema.ResourceData, service *librato.S
d.Set("id", *service.ID) d.Set("id", *service.ID)
d.Set("type", *service.Type) d.Set("type", *service.Type)
d.Set("title", *service.Title) d.Set("title", *service.Title)
settings, _ := attributesFlatten(service.Settings) settings, _ := resourceLibratoServicesFlatten(service.Settings)
d.Set("settings", settings) d.Set("settings", settings)
return nil return nil
@ -136,9 +174,9 @@ func resourceLibratoServiceUpdate(d *schema.ResourceData, meta interface{}) erro
fullService.Title = service.Title fullService.Title = service.Title
} }
if d.HasChange("settings") { if d.HasChange("settings") {
res, getErr := attributesExpand(normalizeJSON(d.Get("settings").(string))) res, err := resourceLibratoServicesExpandSettings(normalizeJson(d.Get("settings").(string)))
if getErr != nil { if err != nil {
return fmt.Errorf("Error expanding Librato service settings: %s", getErr) return fmt.Errorf("Error expanding Librato service settings: %s", err)
} }
service.Settings = res service.Settings = res
fullService.Settings = res fullService.Settings = res
@ -160,9 +198,9 @@ func resourceLibratoServiceUpdate(d *schema.ResourceData, meta interface{}) erro
ContinuousTargetOccurence: 5, ContinuousTargetOccurence: 5,
Refresh: func() (interface{}, string, error) { Refresh: func() (interface{}, string, error) {
log.Printf("[DEBUG] Checking if Librato Service %d was updated yet", serviceID) log.Printf("[DEBUG] Checking if Librato Service %d was updated yet", serviceID)
changedService, _, scErr := client.Services.Get(uint(serviceID)) changedService, _, err := client.Services.Get(uint(serviceID))
if scErr != nil { if err != nil {
return changedService, "", scErr return changedService, "", err
} }
isEqual := reflect.DeepEqual(*fullService, *changedService) isEqual := reflect.DeepEqual(*fullService, *changedService)
log.Printf("[DEBUG] Updated Librato Service %d match: %t", serviceID, isEqual) log.Printf("[DEBUG] Updated Librato Service %d match: %t", serviceID, isEqual)