Skip to content

Commit

Permalink
s/librato/appoptics/
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kahn committed Aug 1, 2018
1 parent a471ef9 commit a78351c
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 181 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package librato
package appoptics

import (
"log"
Expand Down
38 changes: 38 additions & 0 deletions appoptics/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package appoptics

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/henrikhodne/go-appoptics/librato"
"net/url"
)

// Provider returns a schema.Provider for Librato.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"token": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("APPOPTICS_TOKEN", nil),
Description: "The auth token for the AppOptics account.",
},
},

ResourcesMap: map[string]*schema.Resource{
"appoptics_space": resourceAppOpticsSpace(),
"appoptics_space_chart": resourceAppOpticsSpaceChart(),
"appoptics_metric": resourceAppOpticsMetric(),
"appoptics_alert": resourceAppOpticsAlert(),
"appoptics_service": resourceAppOpticsService(),
},

ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
url, _ := url.Parse(" https://api.appoptics.com/v1/measurements/v1")
client := appoptics.NewClientWithBaseURL(url, d.Get("email").(string), d.Get("token").(string))
return client, nil
}
4 changes: 2 additions & 2 deletions librato/provider_test.go → appoptics/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package librato
package appoptics

import (
"os"
Expand All @@ -14,7 +14,7 @@ var testAccProvider *schema.Provider
func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"librato": testAccProvider,
"appoptics": testAccProvider,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package librato
package appoptics

import (
"bytes"
Expand All @@ -14,12 +14,12 @@ import (
"github.com/henrikhodne/go-librato/librato"
)

func resourceLibratoAlert() *schema.Resource {
func resourceAppOpticsAlert() *schema.Resource {
return &schema.Resource{
Create: resourceLibratoAlertCreate,
Read: resourceLibratoAlertRead,
Update: resourceLibratoAlertUpdate,
Delete: resourceLibratoAlertDelete,
Create: resourceAppOpticsAlertCreate,
Read: resourceAppOpticsAlertRead,
Update: resourceAppOpticsAlertUpdate,
Delete: resourceAppOpticsAlertDelete,

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -82,7 +82,7 @@ func resourceLibratoAlert() *schema.Resource {
},
},
},
Set: resourceLibratoAlertConditionsHash,
Set: resourceAppOpticsAlertConditionsHash,
},
"attributes": {
Type: schema.TypeList,
Expand All @@ -101,7 +101,7 @@ func resourceLibratoAlert() *schema.Resource {
}
}

func resourceLibratoAlertConditionsHash(v interface{}) int {
func resourceAppOpticsAlertConditionsHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["type"].(string)))
Expand Down Expand Up @@ -135,7 +135,7 @@ func resourceLibratoAlertConditionsHash(v interface{}) int {
return hashcode.String(buf.String())
}

func resourceLibratoAlertCreate(d *schema.ResourceData, meta interface{}) error {
func resourceAppOpticsAlertCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*librato.Client)

alert := librato.Alert{
Expand Down Expand Up @@ -208,9 +208,9 @@ func resourceLibratoAlertCreate(d *schema.ResourceData, meta interface{}) error
alertResult, _, err := client.Alerts.Create(&alert)

if err != nil {
return fmt.Errorf("Error creating Librato alert %s: %s", *alert.Name, err)
return fmt.Errorf("Error creating AppOptics alert %s: %s", *alert.Name, err)
}
log.Printf("[INFO] Created Librato alert: %s", *alertResult)
log.Printf("[INFO] Created AppOptics alert: %s", *alertResult)

retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, _, err := client.Alerts.Get(*alertResult.ID)
Expand All @@ -228,26 +228,26 @@ func resourceLibratoAlertCreate(d *schema.ResourceData, meta interface{}) error

d.SetId(strconv.FormatUint(uint64(*alertResult.ID), 10))

return resourceLibratoAlertRead(d, meta)
return resourceAppOpticsAlertRead(d, meta)
}

func resourceLibratoAlertRead(d *schema.ResourceData, meta interface{}) error {
func resourceAppOpticsAlertRead(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] Reading Librato Alert: %d", id)
log.Printf("[INFO] Reading AppOptics Alert: %d", id)
alert, _, err := client.Alerts.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 Alert %s: %s", d.Id(), err)
return fmt.Errorf("Error reading AppOptics Alert %s: %s", d.Id(), err)
}
log.Printf("[INFO] Received Librato Alert: %s", *alert)
log.Printf("[INFO] Received AppOptics Alert: %s", *alert)

d.Set("name", alert.Name)

Expand All @@ -269,25 +269,25 @@ func resourceLibratoAlertRead(d *schema.ResourceData, meta interface{}) error {

// Since the following aren't simple terraform types (TypeList), it's best to
// catch the error returned from the d.Set() function, and handle accordingly.
services := resourceLibratoAlertServicesGather(d, alert.Services.([]interface{}))
services := resourceAppOpticsAlertServicesGather(d, alert.Services.([]interface{}))
if err := d.Set("services", schema.NewSet(schema.HashString, services)); err != nil {
return err
}

conditions := resourceLibratoAlertConditionsGather(d, alert.Conditions)
if err := d.Set("condition", schema.NewSet(resourceLibratoAlertConditionsHash, conditions)); err != nil {
conditions := resourceAppOpticsAlertConditionsGather(d, alert.Conditions)
if err := d.Set("condition", schema.NewSet(resourceAppOpticsAlertConditionsHash, conditions)); err != nil {
return err
}

attributes := resourceLibratoAlertAttributesGather(d, alert.Attributes)
attributes := resourceAppOpticsAlertAttributesGather(d, alert.Attributes)
if err := d.Set("attributes", attributes); err != nil {
return err
}

return nil
}

func resourceLibratoAlertServicesGather(d *schema.ResourceData, services []interface{}) []interface{} {
func resourceAppOpticsAlertServicesGather(d *schema.ResourceData, services []interface{}) []interface{} {
retServices := make([]interface{}, 0, len(services))

for _, s := range services {
Expand All @@ -299,7 +299,7 @@ func resourceLibratoAlertServicesGather(d *schema.ResourceData, services []inter
return retServices
}

func resourceLibratoAlertConditionsGather(d *schema.ResourceData, conditions []librato.AlertCondition) []interface{} {
func resourceAppOpticsAlertConditionsGather(d *schema.ResourceData, conditions []librato.AlertCondition) []interface{} {
retConditions := make([]interface{}, 0, len(conditions))
for _, c := range conditions {
condition := make(map[string]interface{})
Expand Down Expand Up @@ -331,7 +331,7 @@ func resourceLibratoAlertConditionsGather(d *schema.ResourceData, conditions []l
}

// Flattens an attributes hash into something that flatmap.Flatten() can handle
func resourceLibratoAlertAttributesGather(d *schema.ResourceData, attributes *librato.AlertAttributes) []map[string]interface{} {
func resourceAppOpticsAlertAttributesGather(d *schema.ResourceData, attributes *librato.AlertAttributes) []map[string]interface{} {
result := make([]map[string]interface{}, 0, 1)

if attributes != nil {
Expand All @@ -345,7 +345,7 @@ func resourceLibratoAlertAttributesGather(d *schema.ResourceData, attributes *li
return result
}

func resourceLibratoAlertUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceAppOpticsAlertUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*librato.Client)

id, err := strconv.ParseUint(d.Id(), 10, 0)
Expand Down Expand Up @@ -416,23 +416,23 @@ func resourceLibratoAlertUpdate(d *schema.ResourceData, meta interface{}) error
alert.Attributes = attributes
}

log.Printf("[INFO] Updating Librato alert: %s", alert)
log.Printf("[INFO] Updating AppOptics alert: %s", alert)
_, updErr := client.Alerts.Update(uint(id), alert)
if updErr != nil {
return fmt.Errorf("Error updating Librato alert: %s", updErr)
return fmt.Errorf("Error updating AppOptics alert: %s", updErr)
}

log.Printf("[INFO] Updated Librato alert %d", id)
log.Printf("[INFO] Updated AppOptics alert %d", id)

// Wait for propagation since Librato updates are eventually consistent
// Wait for propagation since AppOptics updates are eventually consistent
wait := resource.StateChangeConf{
Pending: []string{fmt.Sprintf("%t", false)},
Target: []string{fmt.Sprintf("%t", true)},
Timeout: 5 * time.Minute,
MinTimeout: 2 * time.Second,
ContinuousTargetOccurence: 5,
Refresh: func() (interface{}, string, error) {
log.Printf("[DEBUG] Checking if Librato Alert %d was updated yet", id)
log.Printf("[DEBUG] Checking if AppOptics Alert %d was updated yet", id)
changedAlert, _, getErr := client.Alerts.Get(uint(id))
if getErr != nil {
return changedAlert, "", getErr
Expand All @@ -443,13 +443,13 @@ func resourceLibratoAlertUpdate(d *schema.ResourceData, meta interface{}) error

_, err = wait.WaitForState()
if err != nil {
return fmt.Errorf("Failed updating Librato Alert %d: %s", id, err)
return fmt.Errorf("Failed updating AppOptics Alert %d: %s", id, err)
}

return resourceLibratoAlertRead(d, meta)
return resourceAppOpticsAlertRead(d, meta)
}

func resourceLibratoAlertDelete(d *schema.ResourceData, meta interface{}) error {
func resourceAppOpticsAlertDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*librato.Client)
id, err := strconv.ParseUint(d.Id(), 10, 0)
if err != nil {
Expand All @@ -473,7 +473,7 @@ func resourceLibratoAlertDelete(d *schema.ResourceData, meta interface{}) error
return resource.RetryableError(fmt.Errorf("alert still exists"))
})
if retryErr != nil {
return fmt.Errorf("Error deleting librato alert: %s", err)
return fmt.Errorf("Error deleting AppOptics alert: %s", err)
}

return nil
Expand Down
2 ch