Skip to content

Commit

Permalink
Restore previous version for now
Browse files Browse the repository at this point in the history
  • Loading branch information
wchrisjohnson committed May 25, 2017
1 parent 445658a commit d8d0de8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
40 changes: 0 additions & 40 deletions common.go

This file was deleted.

56 changes: 47 additions & 9 deletions resource_librato_service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package librato

import (
"encoding/json"
"fmt"
"log"
"reflect"
Expand Down Expand Up @@ -36,12 +37,49 @@ func resourceLibratoService() *schema.Resource {
"settings": &schema.Schema{
Type: schema.TypeString,
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 {
client := meta.(*librato.Client)

Expand All @@ -53,7 +91,7 @@ func resourceLibratoServiceCreate(d *schema.ResourceData, meta interface{}) erro
service.Title = librato.String(v.(string))
}
if v, ok := d.GetOk("settings"); ok {
res, err := attributesExpand(normalizeJSON(v.(string)))
res, err := resourceLibratoServicesExpandSettings(normalizeJson(v.(string)))
if err != nil {
return fmt.Errorf("Error expanding Librato service settings: %s", err)
}
Expand Down Expand Up @@ -106,7 +144,7 @@ func resourceLibratoServiceReadResult(d *schema.ResourceData, service *librato.S
d.Set("id", *service.ID)
d.Set("type", *service.Type)
d.Set("title", *service.Title)
settings, _ := attributesFlatten(service.Settings)
settings, _ := resourceLibratoServicesFlatten(service.Settings)
d.Set("settings", settings)

return nil
Expand Down Expand Up @@ -136,9 +174,9 @@ func resourceLibratoServiceUpdate(d *schema.ResourceData, meta interface{}) erro
fullService.Title = service.Title
}
if d.HasChange("settings") {
res, getErr := attributesExpand(normalizeJSON(d.Get("settings").(string)))
if getErr != nil {
return fmt.Errorf("Error expanding Librato service settings: %s", getErr)
res, err := resourceLibratoServicesExpandSettings(normalizeJson(d.Get("settings").(string)))
if err != nil {
return fmt.Errorf("Error expanding Librato service settings: %s", err)
}
service.Settings = res
fullService.Settings = res
Expand All @@ -160,9 +198,9 @@ func resourceLibratoServiceUpdate(d *schema.ResourceData, meta interface{}) erro
ContinuousTargetOccurence: 5,
Refresh: func() (interface{}, string, error) {
log.Printf("[DEBUG] Checking if Librato Service %d was updated yet", serviceID)
changedService, _, scErr := client.Services.Get(uint(serviceID))
if scErr != nil {
return changedService, "", scErr
changedService, _, err := client.Services.Get(uint(serviceID))
if err != nil {
return changedService, "", err
}
isEqual := reflect.DeepEqual(*fullService, *changedService)
log.Printf("[DEBUG] Updated Librato Service %d match: %t", serviceID, isEqual)
Expand Down

0 comments on commit d8d0de8

Please sign in to comment.