diff --git a/librato/common_helpers_test.go b/appoptics/common_helpers_test.go similarity index 92% rename from librato/common_helpers_test.go rename to appoptics/common_helpers_test.go index dde2797..75012b5 100644 --- a/librato/common_helpers_test.go +++ b/appoptics/common_helpers_test.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "log" diff --git a/appoptics/provider.go b/appoptics/provider.go new file mode 100644 index 0000000..206bbff --- /dev/null +++ b/appoptics/provider.go @@ -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 +} diff --git a/librato/provider_test.go b/appoptics/provider_test.go similarity index 94% rename from librato/provider_test.go rename to appoptics/provider_test.go index f25f17f..c0ff19a 100644 --- a/librato/provider_test.go +++ b/appoptics/provider_test.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "os" @@ -14,7 +14,7 @@ var testAccProvider *schema.Provider func init() { testAccProvider = Provider().(*schema.Provider) testAccProviders = map[string]terraform.ResourceProvider{ - "librato": testAccProvider, + "appoptics": testAccProvider, } } diff --git a/librato/resource_librato_alert.go b/appoptics/resource_librato_alert.go similarity index 83% rename from librato/resource_librato_alert.go rename to appoptics/resource_librato_alert.go index 26c1b0a..7a8aec7 100644 --- a/librato/resource_librato_alert.go +++ b/appoptics/resource_librato_alert.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "bytes" @@ -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": { @@ -82,7 +82,7 @@ func resourceLibratoAlert() *schema.Resource { }, }, }, - Set: resourceLibratoAlertConditionsHash, + Set: resourceAppOpticsAlertConditionsHash, }, "attributes": { Type: schema.TypeList, @@ -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))) @@ -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{ @@ -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) @@ -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) @@ -269,17 +269,17 @@ 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 } @@ -287,7 +287,7 @@ func resourceLibratoAlertRead(d *schema.ResourceData, meta interface{}) error { 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 { @@ -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{}) @@ -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 { @@ -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) @@ -416,15 +416,15 @@ 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)}, @@ -432,7 +432,7 @@ func resourceLibratoAlertUpdate(d *schema.ResourceData, meta interface{}) error 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 @@ -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 { @@ -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 diff --git a/librato/resource_librato_alert_test.go b/appoptics/resource_librato_alert_test.go similarity index 99% rename from librato/resource_librato_alert_test.go rename to appoptics/resource_librato_alert_test.go index 3d2a76c..c3419c7 100644 --- a/librato/resource_librato_alert_test.go +++ b/appoptics/resource_librato_alert_test.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "fmt" diff --git a/librato/resource_librato_metric.go b/appoptics/resource_librato_metric.go similarity index 87% rename from librato/resource_librato_metric.go rename to appoptics/resource_librato_metric.go index d5313f3..7f01ae3 100644 --- a/librato/resource_librato_metric.go +++ b/appoptics/resource_librato_metric.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "encoding/json" @@ -11,12 +11,12 @@ import ( "github.com/henrikhodne/go-librato/librato" ) -func resourceLibratoMetric() *schema.Resource { +func resourceAppOpticsMetric() *schema.Resource { return &schema.Resource{ - Create: resourceLibratoMetricCreate, - Read: resourceLibratoMetricRead, - Update: resourceLibratoMetricUpdate, - Delete: resourceLibratoMetricDelete, + Create: resourceAppOpticsMetricCreate, + Read: resourceAppOpticsMetricRead, + Update: resourceAppOpticsMetricUpdate, + Delete: resourceAppOpticsMetricDelete, Schema: map[string]*schema.Schema{ "name": { @@ -94,7 +94,7 @@ func resourceLibratoMetric() *schema.Resource { } } -func resourceLibratoMetricCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsMetricCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) metric := librato.Metric{ @@ -154,7 +154,7 @@ func resourceLibratoMetricCreate(d *schema.ResourceData, meta interface{}) error _, err := client.Metrics.Update(&metric) if err != nil { log.Printf("[INFO] ERROR creating Metric: %s", err) - return fmt.Errorf("Error creating Librato metric: %s", err) + return fmt.Errorf("Error creating AppOptics metric: %s", err) } retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError { @@ -168,26 +168,26 @@ func resourceLibratoMetricCreate(d *schema.ResourceData, meta interface{}) error return nil }) if retryErr != nil { - return fmt.Errorf("Error creating Librato metric: %s", retryErr) + return fmt.Errorf("Error creating AppOptics metric: %s", retryErr) } d.SetId(*metric.Name) - return resourceLibratoMetricRead(d, meta) + return resourceAppOpticsMetricRead(d, meta) } -func resourceLibratoMetricRead(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsMetricRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) id := d.Id() - log.Printf("[INFO] Reading Librato Metric: %s", id) + log.Printf("[INFO] Reading AppOptics Metric: %s", id) metric, _, err := client.Metrics.Get(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 Metric %s: %s", id, err) + return fmt.Errorf("Error reading AppOptics Metric %s: %s", id, err) } d.Set("name", metric.Name) @@ -220,7 +220,7 @@ func resourceLibratoMetricRead(d *schema.ResourceData, meta interface{}) error { return nil } -func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsMetricUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) id := d.Id() @@ -278,16 +278,16 @@ func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error metric.Attributes = attributes } - log.Printf("[INFO] Updating Librato metric: %v", structToString(metric)) + log.Printf("[INFO] Updating AppOptics metric: %v", structToString(metric)) _, err := client.Metrics.Update(metric) if err != nil { - return fmt.Errorf("Error updating Librato metric: %s", err) + return fmt.Errorf("Error updating AppOptics metric: %s", err) } - log.Printf("[INFO] Updated Librato metric %s", id) + log.Printf("[INFO] Updated AppOptics metric %s", 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)}, @@ -295,7 +295,7 @@ func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error MinTimeout: 2 * time.Second, ContinuousTargetOccurence: 5, Refresh: func() (interface{}, string, error) { - log.Printf("[INFO] Checking if Librato Metric %s was updated yet", id) + log.Printf("[INFO] Checking if AppOptics Metric %s was updated yet", id) changedMetric, _, getErr := client.Metrics.Get(id) if getErr != nil { return changedMetric, "", getErr @@ -306,14 +306,14 @@ func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error _, err = wait.WaitForState() if err != nil { - log.Printf("[INFO] ERROR - Failed updating Librato Metric %s: %s", id, err) - return fmt.Errorf("Failed updating Librato Metric %s: %s", id, err) + log.Printf("[INFO] ERROR - Failed updating AppOptics Metric %s: %s", id, err) + return fmt.Errorf("Failed updating AppOptics Metric %s: %s", id, err) } - return resourceLibratoMetricRead(d, meta) + return resourceAppOpticsMetricRead(d, meta) } -func resourceLibratoMetricDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsMetricDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) id := d.Id() diff --git a/librato/resource_librato_metric_test.go b/appoptics/resource_librato_metric_test.go similarity index 99% rename from librato/resource_librato_metric_test.go rename to appoptics/resource_librato_metric_test.go index 63ea414..91d0313 100644 --- a/librato/resource_librato_metric_test.go +++ b/appoptics/resource_librato_metric_test.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "fmt" diff --git a/librato/resource_librato_service.go b/appoptics/resource_librato_service.go similarity index 68% rename from librato/resource_librato_service.go rename to appoptics/resource_librato_service.go index 2e88a66..330e53e 100644 --- a/librato/resource_librato_service.go +++ b/appoptics/resource_librato_service.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "encoding/json" @@ -13,12 +13,12 @@ import ( "github.com/henrikhodne/go-librato/librato" ) -func resourceLibratoService() *schema.Resource { +func resourceAppOpticsService() *schema.Resource { return &schema.Resource{ - Create: resourceLibratoServiceCreate, - Read: resourceLibratoServiceRead, - Update: resourceLibratoServiceUpdate, - Delete: resourceLibratoServiceDelete, + Create: resourceAppOpticsServiceCreate, + Read: resourceAppOpticsServiceRead, + Update: resourceAppOpticsServiceUpdate, + Delete: resourceAppOpticsServiceDelete, Schema: map[string]*schema.Schema{ "id": { @@ -45,7 +45,7 @@ func resourceLibratoService() *schema.Resource { // Takes JSON in a string. Decodes JSON into // settings hash -func resourceLibratoServicesExpandSettings(rawSettings string) (map[string]string, error) { +func resourceAppOpticsServicesExpandSettings(rawSettings string) (map[string]string, error) { var settings map[string]string settings = make(map[string]string) @@ -58,7 +58,7 @@ func resourceLibratoServicesExpandSettings(rawSettings string) (map[string]strin } // Encodes a settings hash into a JSON string -func resourceLibratoServicesFlatten(settings map[string]string) (string, error) { +func resourceAppOpticsServicesFlatten(settings map[string]string) (string, error) { byteArray, err := json.Marshal(settings) if err != nil { return "", fmt.Errorf("Error encoding to JSON: %s", err) @@ -80,7 +80,7 @@ func normalizeJSON(jsonString interface{}) string { return string(b[:]) } -func resourceLibratoServiceCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsServiceCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) service := new(librato.Service) @@ -91,9 +91,9 @@ func resourceLibratoServiceCreate(d *schema.ResourceData, meta interface{}) erro service.Title = librato.String(v.(string)) } if v, ok := d.GetOk("settings"); ok { - res, expandErr := resourceLibratoServicesExpandSettings(normalizeJSON(v.(string))) + res, expandErr := resourceAppOpticsServicesExpandSettings(normalizeJSON(v.(string))) if expandErr != nil { - return fmt.Errorf("Error expanding Librato service settings: %s", expandErr) + return fmt.Errorf("Error expanding AppOptics service settings: %s", expandErr) } service.Settings = res } @@ -101,7 +101,7 @@ func resourceLibratoServiceCreate(d *schema.ResourceData, meta interface{}) erro serviceResult, _, err := client.Services.Create(service) if err != nil { - return fmt.Errorf("Error creating Librato service: %s", err) + return fmt.Errorf("Error creating AppOptics service: %s", err) } resource.Retry(1*time.Minute, func() *resource.RetryError { @@ -115,42 +115,42 @@ func resourceLibratoServiceCreate(d *schema.ResourceData, meta interface{}) erro return nil }) - return resourceLibratoServiceReadResult(d, serviceResult) + return resourceAppOpticsServiceReadResult(d, serviceResult) } -func resourceLibratoServiceRead(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsServiceRead(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 Service: %d", id) + log.Printf("[INFO] Reading AppOptics Service: %d", id) service, _, err := client.Services.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 Service %s: %s", d.Id(), err) + return fmt.Errorf("Error reading AppOptics Service %s: %s", d.Id(), err) } - log.Printf("[INFO] Received Librato Service: %s", service) + log.Printf("[INFO] Received AppOptics Service: %s", service) - return resourceLibratoServiceReadResult(d, service) + return resourceAppOpticsServiceReadResult(d, service) } -func resourceLibratoServiceReadResult(d *schema.ResourceData, service *librato.Service) error { +func resourceAppOpticsServiceReadResult(d *schema.ResourceData, service *librato.Service) error { d.SetId(strconv.FormatUint(uint64(*service.ID), 10)) d.Set("id", *service.ID) d.Set("type", *service.Type) d.Set("title", *service.Title) - settings, _ := resourceLibratoServicesFlatten(service.Settings) + settings, _ := resourceAppOpticsServicesFlatten(service.Settings) d.Set("settings", settings) return nil } -func resourceLibratoServiceUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsServiceUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) serviceID, err := strconv.ParseUint(d.Id(), 10, 0) @@ -174,22 +174,22 @@ func resourceLibratoServiceUpdate(d *schema.ResourceData, meta interface{}) erro fullService.Title = service.Title } if d.HasChange("settings") { - res, getErr := resourceLibratoServicesExpandSettings(normalizeJSON(d.Get("settings").(string))) + res, getErr := resourceAppOpticsServicesExpandSettings(normalizeJSON(d.Get("settings").(string))) if getErr != nil { - return fmt.Errorf("Error expanding Librato service settings: %s", getErr) + return fmt.Errorf("Error expanding AppOptics service settings: %s", getErr) } service.Settings = res fullService.Settings = res } - log.Printf("[INFO] Updating Librato Service %d: %s", serviceID, service) + log.Printf("[INFO] Updating AppOptics Service %d: %s", serviceID, service) _, err = client.Services.Update(uint(serviceID), service) if err != nil { - return fmt.Errorf("Error updating Librato service: %s", err) + return fmt.Errorf("Error updating AppOptics service: %s", err) } - log.Printf("[INFO] Updated Librato Service %d", serviceID) + log.Printf("[INFO] Updated AppOptics Service %d", serviceID) - // 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)}, @@ -197,26 +197,26 @@ func resourceLibratoServiceUpdate(d *schema.ResourceData, meta interface{}) erro MinTimeout: 2 * time.Second, ContinuousTargetOccurence: 5, Refresh: func() (interface{}, string, error) { - log.Printf("[DEBUG] Checking if Librato Service %d was updated yet", serviceID) + log.Printf("[DEBUG] Checking if AppOptics Service %d was updated yet", serviceID) changedService, _, getErr := client.Services.Get(uint(serviceID)) if getErr != nil { return changedService, "", getErr } isEqual := reflect.DeepEqual(*fullService, *changedService) - log.Printf("[DEBUG] Updated Librato Service %d match: %t", serviceID, isEqual) + log.Printf("[DEBUG] Updated AppOptics Service %d match: %t", serviceID, isEqual) return changedService, fmt.Sprintf("%t", isEqual), nil }, } _, err = wait.WaitForState() if err != nil { - return fmt.Errorf("Failed updating Librato Service %d: %s", serviceID, err) + return fmt.Errorf("Failed updating AppOptics Service %d: %s", serviceID, err) } - return resourceLibratoServiceRead(d, meta) + return resourceAppOpticsServiceRead(d, meta) } -func resourceLibratoServiceDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsServiceDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) id, err := strconv.ParseUint(d.Id(), 10, 0) if err != nil { diff --git a/librato/resource_librato_service_test.go b/appoptics/resource_librato_service_test.go similarity index 99% rename from librato/resource_librato_service_test.go rename to appoptics/resource_librato_service_test.go index fbdbd99..c511f44 100644 --- a/librato/resource_librato_service_test.go +++ b/appoptics/resource_librato_service_test.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "fmt" diff --git a/librato/resource_librato_space.go b/appoptics/resource_librato_space.go similarity index 74% rename from librato/resource_librato_space.go rename to appoptics/resource_librato_space.go index f8c68c0..20bebfb 100644 --- a/librato/resource_librato_space.go +++ b/appoptics/resource_librato_space.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "fmt" @@ -11,12 +11,12 @@ import ( "github.com/henrikhodne/go-librato/librato" ) -func resourceLibratoSpace() *schema.Resource { +func resourceAppOpticsSpace() *schema.Resource { return &schema.Resource{ - Create: resourceLibratoSpaceCreate, - Read: resourceLibratoSpaceRead, - Update: resourceLibratoSpaceUpdate, - Delete: resourceLibratoSpaceDelete, + Create: resourceAppOpticsSpaceCreate, + Read: resourceAppOpticsSpaceRead, + Update: resourceAppOpticsSpaceUpdate, + Delete: resourceAppOpticsSpaceDelete, Schema: map[string]*schema.Schema{ "name": { @@ -32,14 +32,14 @@ func resourceLibratoSpace() *schema.Resource { } } -func resourceLibratoSpaceCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsSpaceCreate(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) + return fmt.Errorf("Error creating AppOptics space %s: %s", name, err) } resource.Retry(1*time.Minute, func() *resource.RetryError { @@ -53,10 +53,10 @@ func resourceLibratoSpaceCreate(d *schema.ResourceData, meta interface{}) error return nil }) - return resourceLibratoSpaceReadResult(d, space) + return resourceAppOpticsSpaceReadResult(d, space) } -func resourceLibratoSpaceRead(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsSpaceRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) id, err := strconv.ParseUint(d.Id(), 10, 0) @@ -70,13 +70,13 @@ func resourceLibratoSpaceRead(d *schema.ResourceData, meta interface{}) error { d.SetId("") return nil } - return fmt.Errorf("Error reading Librato Space %s: %s", d.Id(), err) + return fmt.Errorf("Error reading AppOptics Space %s: %s", d.Id(), err) } - return resourceLibratoSpaceReadResult(d, space) + return resourceAppOpticsSpaceReadResult(d, space) } -func resourceLibratoSpaceReadResult(d *schema.ResourceData, space *librato.Space) error { +func resourceAppOpticsSpaceReadResult(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 @@ -87,7 +87,7 @@ func resourceLibratoSpaceReadResult(d *schema.ResourceData, space *librato.Space return nil } -func resourceLibratoSpaceUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsSpaceUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) id, err := strconv.ParseUint(d.Id(), 10, 0) if err != nil { @@ -102,10 +102,10 @@ func resourceLibratoSpaceUpdate(d *schema.ResourceData, meta interface{}) error } } - return resourceLibratoSpaceRead(d, meta) + return resourceAppOpticsSpaceRead(d, meta) } -func resourceLibratoSpaceDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsSpaceDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) id, err := strconv.ParseUint(d.Id(), 10, 0) if err != nil { diff --git a/librato/resource_librato_space_chart.go b/appoptics/resource_librato_space_chart.go similarity index 87% rename from librato/resource_librato_space_chart.go rename to appoptics/resource_librato_space_chart.go index 678b630..154c705 100644 --- a/librato/resource_librato_space_chart.go +++ b/appoptics/resource_librato_space_chart.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "bytes" @@ -15,12 +15,12 @@ import ( "github.com/henrikhodne/go-librato/librato" ) -func resourceLibratoSpaceChart() *schema.Resource { +func resourceAppOpticsSpaceChart() *schema.Resource { return &schema.Resource{ - Create: resourceLibratoSpaceChartCreate, - Read: resourceLibratoSpaceChartRead, - Update: resourceLibratoSpaceChartUpdate, - Delete: resourceLibratoSpaceChartDelete, + Create: resourceAppOpticsSpaceChartCreate, + Read: resourceAppOpticsSpaceChartRead, + Update: resourceAppOpticsSpaceChartUpdate, + Delete: resourceAppOpticsSpaceChartDelete, Schema: map[string]*schema.Schema{ "space_id": { @@ -120,13 +120,13 @@ func resourceLibratoSpaceChart() *schema.Resource { }, }, }, - Set: resourceLibratoSpaceChartHash, + Set: resourceAppOpticsSpaceChartHash, }, }, } } -func resourceLibratoSpaceChartHash(v interface{}) int { +func resourceAppOpticsSpaceChartHash(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) buf.WriteString(fmt.Sprintf("%s-", m["metric"].(string))) @@ -136,7 +136,7 @@ func resourceLibratoSpaceChartHash(v interface{}) int { return hashcode.String(buf.String()) } -func resourceLibratoSpaceChartCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsSpaceChartCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) spaceID := uint(d.Get("space_id").(int)) @@ -214,7 +214,7 @@ func resourceLibratoSpaceChartCreate(d *schema.ResourceData, meta interface{}) e spaceChartResult, _, err := client.Spaces.CreateChart(spaceID, spaceChart) if err != nil { - return fmt.Errorf("Error creating Librato space chart %s: %s", *spaceChart.Name, err) + return fmt.Errorf("Error creating AppOptics space chart %s: %s", *spaceChart.Name, err) } resource.Retry(1*time.Minute, func() *resource.RetryError { @@ -228,10 +228,10 @@ func resourceLibratoSpaceChartCreate(d *schema.ResourceData, meta interface{}) e return nil }) - return resourceLibratoSpaceChartReadResult(d, spaceChartResult) + return resourceAppOpticsSpaceChartReadResult(d, spaceChartResult) } -func resourceLibratoSpaceChartRead(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsSpaceChartRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) spaceID := uint(d.Get("space_id").(int)) @@ -247,13 +247,13 @@ func resourceLibratoSpaceChartRead(d *schema.ResourceData, meta interface{}) err d.SetId("") return nil } - return fmt.Errorf("Error reading Librato Space chart %s: %s", d.Id(), err) + return fmt.Errorf("Error reading AppOptics Space chart %s: %s", d.Id(), err) } - return resourceLibratoSpaceChartReadResult(d, chart) + return resourceAppOpticsSpaceChartReadResult(d, chart) } -func resourceLibratoSpaceChartReadResult(d *schema.ResourceData, chart *librato.SpaceChart) error { +func resourceAppOpticsSpaceChartReadResult(d *schema.ResourceData, chart *librato.SpaceChart) error { d.SetId(strconv.FormatUint(uint64(*chart.ID), 10)) if chart.Name != nil { if err := d.Set("name", *chart.Name); err != nil { @@ -286,7 +286,7 @@ func resourceLibratoSpaceChartReadResult(d *schema.ResourceData, chart *librato. } } - streams := resourceLibratoSpaceChartStreamsGather(d, chart.Streams) + streams := resourceAppOpticsSpaceChartStreamsGather(d, chart.Streams) if err := d.Set("stream", streams); err != nil { return err } @@ -294,7 +294,7 @@ func resourceLibratoSpaceChartReadResult(d *schema.ResourceData, chart *librato. return nil } -func resourceLibratoSpaceChartStreamsGather(d *schema.ResourceData, streams []librato.SpaceChartStream) []map[string]interface{} { +func resourceAppOpticsSpaceChartStreamsGather(d *schema.ResourceData, streams []librato.SpaceChartStream) []map[string]interface{} { retStreams := make([]map[string]interface{}, 0, len(streams)) for _, s := range streams { stream := make(map[string]interface{}) @@ -331,7 +331,7 @@ func resourceLibratoSpaceChartStreamsGather(d *schema.ResourceData, streams []li return retStreams } -func resourceLibratoSpaceChartUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsSpaceChartUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) spaceID := uint(d.Get("space_id").(int)) @@ -422,10 +422,10 @@ func resourceLibratoSpaceChartUpdate(d *schema.ResourceData, meta interface{}) e _, err = client.Spaces.UpdateChart(spaceID, uint(chartID), spaceChart) if err != nil { - return fmt.Errorf("Error updating Librato space chart %s: %s", *spaceChart.Name, err) + return fmt.Errorf("Error updating AppOptics space chart %s: %s", *spaceChart.Name, err) } - // 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)}, @@ -433,26 +433,26 @@ func resourceLibratoSpaceChartUpdate(d *schema.ResourceData, meta interface{}) e MinTimeout: 2 * time.Second, ContinuousTargetOccurence: 5, Refresh: func() (interface{}, string, error) { - log.Printf("[DEBUG] Checking if Librato Space Chart %d was updated yet", chartID) + log.Printf("[DEBUG] Checking if AppOptics Space Chart %d was updated yet", chartID) changedChart, _, getErr := client.Spaces.GetChart(spaceID, uint(chartID)) if getErr != nil { return changedChart, "", getErr } isEqual := reflect.DeepEqual(*fullChart, *changedChart) - log.Printf("[DEBUG] Updated Librato Space Chart %d match: %t", chartID, isEqual) + log.Printf("[DEBUG] Updated AppOptics Space Chart %d match: %t", chartID, isEqual) return changedChart, fmt.Sprintf("%t", isEqual), nil }, } _, err = wait.WaitForState() if err != nil { - return fmt.Errorf("Failed updating Librato Space Chart %d: %s", chartID, err) + return fmt.Errorf("Failed updating AppOptics Space Chart %d: %s", chartID, err) } - return resourceLibratoSpaceChartRead(d, meta) + return resourceAppOpticsSpaceChartRead(d, meta) } -func resourceLibratoSpaceChartDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAppOpticsSpaceChartDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*librato.Client) spaceID := uint(d.Get("space_id").(int)) diff --git a/librato/resource_librato_space_chart_test.go b/appoptics/resource_librato_space_chart_test.go similarity index 99% rename from librato/resource_librato_space_chart_test.go rename to appoptics/resource_librato_space_chart_test.go index e087b06..100fdfe 100644 --- a/librato/resource_librato_space_chart_test.go +++ b/appoptics/resource_librato_space_chart_test.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "fmt" diff --git a/librato/resource_librato_space_test.go b/appoptics/resource_librato_space_test.go similarity index 99% rename from librato/resource_librato_space_test.go rename to appoptics/resource_librato_space_test.go index 10d1b67..36e068f 100644 --- a/librato/resource_librato_space_test.go +++ b/appoptics/resource_librato_space_test.go @@ -1,4 +1,4 @@ -package librato +package appoptics import ( "fmt" diff --git a/librato/provider.go b/librato/provider.go deleted file mode 100644 index 203e06f..0000000 --- a/librato/provider.go +++ /dev/null @@ -1,44 +0,0 @@ -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(), - "librato_metric": resourceLibratoMetric(), - "librato_alert": resourceLibratoAlert(), - "librato_service": resourceLibratoService(), - }, - - ConfigureFunc: providerConfigure, - } -} - -func providerConfigure(d *schema.ResourceData) (interface{}, error) { - client := librato.NewClient(d.Get("email").(string), d.Get("token").(string)) - - return client, nil -}