opentofu/builtin/providers/librato/resource_librato_space.go
stack72 4abfff21c0
provider/librato: Refresh space from state when not found
The librator provider is sometimes throwing errors when trying to delete
a space that is already deleted. The nightly tests shows this error:

```
Error: Error applying: 1 error(s) occurred:

                * librato_space.foobar: Error deleting space: DELETE
                * https://metrics-api.librato.com/v1/spaces/236303: 404
                * Request errors: Not Found,.
```

The Delete func should be aware if the space cannot be deleted as it is
already deleted and not error on this usecase

```
% make testacc TEST=./builtin/providers/librato TESTARGS='-run=Test'
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2016/09/01 09:24:21 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/librato -v -run=Test -timeout 120m
=== RUN   TestProvider
--- PASS: TestProvider (0.00s)
=== RUN   TestProvider_impl
--- PASS: TestProvider_impl (0.00s)
=== RUN   TestAccLibratoAlert_Basic
--- PASS: TestAccLibratoAlert_Basic (1.52s)
=== RUN   TestAccLibratoAlert_Full
--- PASS: TestAccLibratoAlert_Full (2.89s)
=== RUN   TestAccLibratoAlert_Updated
--- PASS: TestAccLibratoAlert_Updated (1.76s)
=== RUN   TestAccLibratoService_Basic
--- PASS: TestAccLibratoService_Basic (2.09s)
=== RUN   TestAccLibratoService_Updated
--- PASS: TestAccLibratoService_Updated (2.73s)
=== RUN   TestAccLibratoSpaceChart_Basic
--- PASS: TestAccLibratoSpaceChart_Basic (5.08s)
=== RUN   TestAccLibratoSpaceChart_Full
--- PASS: TestAccLibratoSpaceChart_Full (13.06s)
=== RUN   TestAccLibratoSpaceChart_Updated
--- PASS: TestAccLibratoSpaceChart_Updated (5.90s)
=== RUN   TestAccLibratoSpace_Basic
--- PASS: TestAccLibratoSpace_Basic (4.29s)
PASS
ok     	github.com/hashicorp/terraform/builtin/providers/librato       	39.321s
```
2016-09-01 09:25:53 +01:00

140 lines
3.5 KiB
Go

package librato
import (
"fmt"
"log"
"strconv"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/henrikhodne/go-librato/librato"
)
func resourceLibratoSpace() *schema.Resource {
return &schema.Resource{
Create: resourceLibratoSpaceCreate,
Read: resourceLibratoSpaceRead,
Update: resourceLibratoSpaceUpdate,
Delete: resourceLibratoSpaceDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"id": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func resourceLibratoSpaceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*librato.Client)
name := d.Get("name").(string)
space, _, err := client.Spaces.Create(&librato.Space{Name: librato.String(name)})
if err != nil {
return fmt.Errorf("Error creating Librato space %s: %s", name, err)
}
resource.Retry(1*time.Minute, func() *resource.RetryError {
_, _, err := client.Spaces.Get(*space.ID)
if err != nil {
if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
return resourceLibratoSpaceReadResult(d, space)
}
func resourceLibratoSpaceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*librato.Client)
id, err := strconv.ParseUint(d.Id(), 10, 0)
if err != nil {
return err
}
space, _, err := client.Spaces.Get(uint(id))
if err != nil {
if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Librato Space %s: %s", d.Id(), err)
}
return resourceLibratoSpaceReadResult(d, space)
}
func resourceLibratoSpaceReadResult(d *schema.ResourceData, space *librato.Space) error {
d.SetId(strconv.FormatUint(uint64(*space.ID), 10))
if err := d.Set("id", *space.ID); err != nil {
return err
}
if err := d.Set("name", *space.Name); err != nil {
return err
}
return nil
}
func resourceLibratoSpaceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*librato.Client)
id, err := strconv.ParseUint(d.Id(), 10, 0)
if err != nil {
return err
}
if d.HasChange("name") {
newName := d.Get("name").(string)
log.Printf("[INFO] Modifying name space attribute for %d: %#v", id, newName)
if _, err = client.Spaces.Edit(uint(id), &librato.Space{Name: &newName}); err != nil {
return err
}
}
return resourceLibratoSpaceRead(d, meta)
}
func resourceLibratoSpaceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*librato.Client)
id, err := strconv.ParseUint(d.Id(), 10, 0)
if err != nil {
return err
}
log.Printf("[INFO] Deleting Space: %d", id)
_, err = client.Spaces.Delete(uint(id))
if err != nil {
if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
log.Printf("Space %s not found", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error deleting space: %s", err)
}
resource.Retry(1*time.Minute, func() *resource.RetryError {
_, _, err := client.Spaces.Get(uint(id))
if err != nil {
if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
return nil
}
return resource.NonRetryableError(err)
}
return resource.RetryableError(fmt.Errorf("space still exists"))
})
d.SetId("")
return nil
}