github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/librato/resource_librato_service.go (about) 1 package librato 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "log" 7 "strconv" 8 "time" 9 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/helper/schema" 12 "github.com/henrikhodne/go-librato/librato" 13 ) 14 15 func resourceLibratoService() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceLibratoServiceCreate, 18 Read: resourceLibratoServiceRead, 19 Update: resourceLibratoServiceUpdate, 20 Delete: resourceLibratoServiceDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "id": &schema.Schema{ 24 Type: schema.TypeInt, 25 Computed: true, 26 }, 27 "type": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 "title": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 }, 36 "settings": &schema.Schema{ 37 Type: schema.TypeString, 38 Required: true, 39 StateFunc: normalizeJson, 40 }, 41 }, 42 } 43 } 44 45 // Takes JSON in a string. Decodes JSON into 46 // settings hash 47 func resourceLibratoServicesExpandSettings(rawSettings string) (map[string]string, error) { 48 var settings map[string]string 49 50 settings = make(map[string]string) 51 err := json.Unmarshal([]byte(rawSettings), &settings) 52 if err != nil { 53 return nil, fmt.Errorf("Error decoding JSON: %s", err) 54 } 55 56 return settings, err 57 } 58 59 // Encodes a settings hash into a JSON string 60 func resourceLibratoServicesFlatten(settings map[string]string) (string, error) { 61 byteArray, err := json.Marshal(settings) 62 if err != nil { 63 return "", fmt.Errorf("Error encoding to JSON: %s", err) 64 } 65 66 return string(byteArray), nil 67 } 68 69 func normalizeJson(jsonString interface{}) string { 70 if jsonString == nil || jsonString == "" { 71 return "" 72 } 73 var j interface{} 74 err := json.Unmarshal([]byte(jsonString.(string)), &j) 75 if err != nil { 76 return fmt.Sprintf("Error parsing JSON: %s", err) 77 } 78 b, _ := json.Marshal(j) 79 return string(b[:]) 80 } 81 82 func resourceLibratoServiceCreate(d *schema.ResourceData, meta interface{}) error { 83 client := meta.(*librato.Client) 84 85 service := new(librato.Service) 86 if v, ok := d.GetOk("type"); ok { 87 service.Type = librato.String(v.(string)) 88 } 89 if v, ok := d.GetOk("title"); ok { 90 service.Title = librato.String(v.(string)) 91 } 92 if v, ok := d.GetOk("settings"); ok { 93 res, err := resourceLibratoServicesExpandSettings(normalizeJson(v.(string))) 94 if err != nil { 95 return fmt.Errorf("Error expanding Librato service settings: %s", err) 96 } 97 service.Settings = res 98 } 99 100 serviceResult, _, err := client.Services.Create(service) 101 102 if err != nil { 103 return fmt.Errorf("Error creating Librato service: %s", err) 104 } 105 106 resource.Retry(1*time.Minute, func() *resource.RetryError { 107 _, _, err := client.Services.Get(*serviceResult.ID) 108 if err != nil { 109 if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 { 110 return resource.RetryableError(err) 111 } 112 return resource.NonRetryableError(err) 113 } 114 return nil 115 }) 116 117 return resourceLibratoServiceReadResult(d, serviceResult) 118 } 119 120 func resourceLibratoServiceRead(d *schema.ResourceData, meta interface{}) error { 121 client := meta.(*librato.Client) 122 id, err := strconv.ParseUint(d.Id(), 10, 0) 123 if err != nil { 124 return err 125 } 126 127 service, _, err := client.Services.Get(uint(id)) 128 if err != nil { 129 if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 { 130 d.SetId("") 131 return nil 132 } 133 return fmt.Errorf("Error reading Librato Service %s: %s", d.Id(), err) 134 } 135 136 return resourceLibratoServiceReadResult(d, service) 137 } 138 139 func resourceLibratoServiceReadResult(d *schema.ResourceData, service *librato.Service) error { 140 d.SetId(strconv.FormatUint(uint64(*service.ID), 10)) 141 d.Set("id", *service.ID) 142 d.Set("type", *service.Type) 143 d.Set("title", *service.Title) 144 settings, _ := resourceLibratoServicesFlatten(service.Settings) 145 d.Set("settings", settings) 146 147 return nil 148 } 149 150 func resourceLibratoServiceUpdate(d *schema.ResourceData, meta interface{}) error { 151 client := meta.(*librato.Client) 152 153 serviceID, err := strconv.ParseUint(d.Id(), 10, 0) 154 if err != nil { 155 return err 156 } 157 158 service := new(librato.Service) 159 if d.HasChange("type") { 160 service.Type = librato.String(d.Get("type").(string)) 161 } 162 if d.HasChange("title") { 163 service.Title = librato.String(d.Get("title").(string)) 164 } 165 if d.HasChange("settings") { 166 res, err := resourceLibratoServicesExpandSettings(normalizeJson(d.Get("settings").(string))) 167 if err != nil { 168 return fmt.Errorf("Error expanding Librato service settings: %s", err) 169 } 170 service.Settings = res 171 } 172 173 _, err = client.Services.Edit(uint(serviceID), service) 174 if err != nil { 175 return fmt.Errorf("Error updating Librato service: %s", err) 176 } 177 178 return resourceLibratoServiceRead(d, meta) 179 } 180 181 func resourceLibratoServiceDelete(d *schema.ResourceData, meta interface{}) error { 182 client := meta.(*librato.Client) 183 id, err := strconv.ParseUint(d.Id(), 10, 0) 184 if err != nil { 185 return err 186 } 187 188 log.Printf("[INFO] Deleting Service: %d", id) 189 _, err = client.Services.Delete(uint(id)) 190 if err != nil { 191 return fmt.Errorf("Error deleting Service: %s", err) 192 } 193 194 resource.Retry(1*time.Minute, func() *resource.RetryError { 195 _, _, err := client.Services.Get(uint(id)) 196 if err != nil { 197 if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 { 198 return nil 199 } 200 return resource.NonRetryableError(err) 201 } 202 return resource.RetryableError(fmt.Errorf("service still exists")) 203 }) 204 205 d.SetId("") 206 return nil 207 }