-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/librato: Add Librato provider
- Loading branch information
0 parents
commit 92c1ca0
Showing
6 changed files
with
997 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package librato | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/terraform" | ||
"github.com/henrikhodne/go-librato/librato" | ||
) | ||
|
||
// Provider returns a schema.Provider for Librato. | ||
func Provider() terraform.ResourceProvider { | ||
return &schema.Provider{ | ||
Schema: map[string]*schema.Schema{ | ||
"email": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
DefaultFunc: schema.EnvDefaultFunc("LIBRATO_EMAIL", nil), | ||
Description: "The email address for the Librato account.", | ||
}, | ||
|
||
"token": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
DefaultFunc: schema.EnvDefaultFunc("LIBRATO_TOKEN", nil), | ||
Description: "The auth token for the Librato account.", | ||
}, | ||
}, | ||
|
||
ResourcesMap: map[string]*schema.Resource{ | ||
"librato_space": resourceLibratoSpace(), | ||
"librato_space_chart": resourceLibratoSpaceChart(), | ||
}, | ||
|
||
ConfigureFunc: providerConfigure, | ||
} | ||
} | ||
|
||
func providerConfigure(d *schema.ResourceData) (interface{}, error) { | ||
client := librato.NewClient(d.Get("email").(string), d.Get("token").(string)) | ||
|
||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package librato | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
var testAccProviders map[string]terraform.ResourceProvider | ||
var testAccProvider *schema.Provider | ||
|
||
func init() { | ||
testAccProvider = Provider().(*schema.Provider) | ||
testAccProviders = map[string]terraform.ResourceProvider{ | ||
"librato": testAccProvider, | ||
} | ||
} | ||
|
||
func TestProvider(t *testing.T) { | ||
if err := Provider().(*schema.Provider).InternalValidate(); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
} | ||
|
||
func TestProvider_impl(t *testing.T) { | ||
var _ terraform.ResourceProvider = Provider() | ||
} | ||
|
||
func testAccPreCheck(t *testing.T) { | ||
if v := os.Getenv("LIBRATO_EMAIL"); v == "" { | ||
t.Fatal("LIBRATO_EMAIL must be set for acceptance tests") | ||
} | ||
|
||
if v := os.Getenv("LIBRATO_TOKEN"); v == "" { | ||
t.Fatal("LIBRATO_TOKEN must be set for acceptance tests") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
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 { | ||
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 | ||
} |
Oops, something went wrong.