From c5142e6bb6991be369b0ac4c9b5fef8f067fcd2b Mon Sep 17 00:00:00 2001 From: Trevor Rosen Date: Thu, 16 Jan 2020 13:36:58 -0600 Subject: [PATCH] Charts (#42) * Finally remove old Librato stuff * File rename * Add Chart; rename Space to Dashboard Fixes #3 --- Makefile | 2 +- appoptics/provider.go | 10 +++---- appoptics/resource_appoptics_metric.go | 13 ++++----- ...t.go => resource_appoptics_space_chart.go} | 27 ++++++++++-------- ...=> resource_appoptics_space_chart_test.go} | 0 example.tf | 28 ++++++++++++++++--- go.mod | 1 - go.sum | 2 -- 8 files changed, 51 insertions(+), 32 deletions(-) rename appoptics/{resource_librato_space_chart.go => resource_appoptics_space_chart.go} (93%) rename appoptics/{resource_librato_space_chart_test.go => resource_appoptics_space_chart_test.go} (100%) diff --git a/Makefile b/Makefile index 16e96db..4f2db8e 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,6 @@ release: # Convenient in dev to rebuild the plugin, re-init TF, and run a plan bounce: build - rm *tfstate* && terraform init && terraform plan + rm -f *.tfstate* && terraform init && terraform plan diff --git a/appoptics/provider.go b/appoptics/provider.go index 6832b73..3e8571e 100644 --- a/appoptics/provider.go +++ b/appoptics/provider.go @@ -21,11 +21,11 @@ func Provider() terraform.ResourceProvider { }, ResourcesMap: map[string]*schema.Resource{ - "appoptics_space": resourceAppOpticsSpace(), - "appoptics_space_chart": resourceAppOpticsSpaceChart(), - "appoptics_metric": resourceAppOpticsMetric(), - "appoptics_alert": resourceAppOpticsAlert(), - "appoptics_service": resourceAppOpticsService(), + "appoptics_dashboard": resourceAppOpticsSpace(), // name is legacy from Librato + "appoptics_dashboard_chart": resourceAppOpticsSpaceChart(), // name is legacy from Librato + "appoptics_metric": resourceAppOpticsMetric(), + "appoptics_alert": resourceAppOpticsAlert(), + "appoptics_service": resourceAppOpticsService(), }, ConfigureFunc: providerConfigure, diff --git a/appoptics/resource_appoptics_metric.go b/appoptics/resource_appoptics_metric.go index 1a370d2..a8d1c0a 100644 --- a/appoptics/resource_appoptics_metric.go +++ b/appoptics/resource_appoptics_metric.go @@ -6,7 +6,6 @@ import ( "log" "time" - "github.com/akahn/go-librato/librato" appoptics "github.com/appoptics/appoptics-api-go" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" @@ -277,16 +276,16 @@ func resourceAppOpticsMetricUpdate(d *schema.ResourceData, meta interface{}) err attributes.DisplayUnitsShort = v } if v, ok := attributeDataMap["created_by_ua"].(string); ok && v != "" { - attributes.CreatedByUA = *librato.String(v) + attributes.CreatedByUA = v } if v, ok := attributeDataMap["display_stacked"].(bool); ok { - attributes.DisplayStacked = *librato.Bool(v) + attributes.DisplayStacked = v } if v, ok := attributeDataMap["gap_detection"].(bool); ok { - attributes.GapDetection = *librato.Bool(v) + attributes.GapDetection = v } if v, ok := attributeDataMap["aggregate"].(bool); ok { - attributes.Aggregate = *librato.Bool(v) + attributes.Aggregate = v } metric.Attributes = attributes } @@ -343,7 +342,7 @@ func resourceAppOpticsMetricDelete(d *schema.ResourceData, meta interface{}) err log.Printf("[INFO] Getting Metric %s", id) _, err := client.MetricsService().Retrieve(id) if err != nil { - if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 { + if errResp, ok := err.(*appoptics.ErrorResponse); ok && errResp.Response.StatusCode == 404 { log.Printf("[INFO] Metric %s not found, removing from state", id) return nil } @@ -355,7 +354,7 @@ func resourceAppOpticsMetricDelete(d *schema.ResourceData, meta interface{}) err return resource.RetryableError(fmt.Errorf("metric still exists")) }) if retryErr != nil { - return fmt.Errorf("Error deleting librato metric: %s", retryErr) + return fmt.Errorf("Error deleting AppOptics metric: %s", retryErr) } return nil diff --git a/appoptics/resource_librato_space_chart.go b/appoptics/resource_appoptics_space_chart.go similarity index 93% rename from appoptics/resource_librato_space_chart.go rename to appoptics/resource_appoptics_space_chart.go index 0d0b06b..56ac219 100644 --- a/appoptics/resource_librato_space_chart.go +++ b/appoptics/resource_appoptics_space_chart.go @@ -63,7 +63,7 @@ func resourceAppOpticsSpaceChart() *schema.Resource { Optional: true, ConflictsWith: []string{"stream.composite"}, }, - "tag": { + "tags": { Type: schema.TypeList, Optional: true, ConflictsWith: []string{"stream.composite"}, @@ -184,7 +184,10 @@ func resourceAppOpticsSpaceChartCreate(d *schema.ResourceData, meta interface{}) spaceID := d.Get("space_id").(int) - spaceChart := new(appoptics.Chart) + spaceChart := &appoptics.Chart{ + Type: "line", // default per docs + } + if v, ok := d.GetOk("name"); ok { spaceChart.Name = v.(string) } @@ -255,7 +258,7 @@ func resourceAppOpticsSpaceChartCreate(d *schema.ResourceData, meta interface{}) spaceChartResult, err := client.ChartsService().Create(spaceChart, spaceID) if err != nil { - return fmt.Errorf("Error creating AppOptics space chart %s: %s", spaceChart.Name, err) + return fmt.Errorf("Error creating AppOptics chart %s: %s", spaceChart.Name, err) } retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError { @@ -292,7 +295,7 @@ func resourceAppOpticsSpaceChartRead(d *schema.ResourceData, meta interface{}) e d.SetId("") return nil } - return fmt.Errorf("Error reading AppOptics Space chart %s: %s", d.Id(), err) + return fmt.Errorf("Error reading AppOptics chart %s: %s", d.Id(), err) } return resourceAppOpticsSpaceChartReadResult(d, chart) @@ -387,21 +390,21 @@ func resourceAppOpticsSpaceChartUpdate(d *schema.ResourceData, meta interface{}) return err } - spaceChart := new(appoptics.Chart) + spaceChart := &appoptics.Chart{} if d.HasChange("name") { spaceChart.Name = d.Get("name").(string) fullChart.Name = spaceChart.Name } if d.HasChange("min") { if math.IsNaN(d.Get("min").(float64)) { - return fmt.Errorf("Error updating AppOptics space chart. 'min' cannot be converted to a float64. %s: %s", d.Get("min"), err) + return fmt.Errorf("Error updating AppOptics chart. 'min' cannot be converted to a float64. %s: %s", d.Get("min"), err) } spaceChart.Min = d.Get("min").(float64) fullChart.Min = spaceChart.Min } if d.HasChange("max") { if math.IsNaN(d.Get("max").(float64)) { - return fmt.Errorf("Error updating AppOptics space chart. 'max' cannot be converted to a float64. %s: %s", d.Get("max"), err) + return fmt.Errorf("Error updating AppOptics chart. 'max' cannot be converted to a float64. %s: %s", d.Get("max"), err) } spaceChart.Max = d.Get("max").(float64) fullChart.Max = spaceChart.Max @@ -461,7 +464,7 @@ func resourceAppOpticsSpaceChartUpdate(d *schema.ResourceData, meta interface{}) _, err = client.ChartsService().Update(spaceChart, spaceID) if err != nil { - return fmt.Errorf("Error updating AppOptics space chart %s: %s", spaceChart.Name, err) + return fmt.Errorf("Error updating AppOptics chart %s: %s", spaceChart.Name, err) } // Wait for propagation since AppOptics updates are eventually consistent @@ -472,7 +475,7 @@ func resourceAppOpticsSpaceChartUpdate(d *schema.ResourceData, meta interface{}) MinTimeout: 2 * time.Second, ContinuousTargetOccurence: 5, Refresh: func() (interface{}, string, error) { - log.Printf("[DEBUG] Checking if AppOptics Space Chart %d was updated yet", chartID) + log.Printf("[DEBUG] Checking if AppOptics chart %d was updated yet", chartID) changedChart, getErr := client.ChartsService().Retrieve(chartID, spaceID) if getErr != nil { return changedChart, "", getErr @@ -485,7 +488,7 @@ func resourceAppOpticsSpaceChartUpdate(d *schema.ResourceData, meta interface{}) _, err = wait.WaitForState() if err != nil { - return fmt.Errorf("Failed updating AppOptics Space Chart %d: %s", chartID, err) + return fmt.Errorf("Failed updating AppOptics chart %d: %s", chartID, err) } return resourceAppOpticsSpaceChartRead(d, meta) @@ -504,7 +507,7 @@ func resourceAppOpticsSpaceChartDelete(d *schema.ResourceData, meta interface{}) log.Printf("[INFO] Deleting Chart: %d/%d", spaceID, uint(id)) err = client.ChartsService().Delete(id, spaceID) if err != nil { - return fmt.Errorf("Error deleting space: %s", err) + return fmt.Errorf("Error deleting chart: %s", err) } retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError { @@ -515,7 +518,7 @@ func resourceAppOpticsSpaceChartDelete(d *schema.ResourceData, meta interface{}) } return resource.NonRetryableError(err) } - return resource.RetryableError(fmt.Errorf("space chart still exists")) + return resource.RetryableError(fmt.Errorf("chart still exists")) }) if retryErr != nil { diff --git a/appoptics/resource_librato_space_chart_test.go b/appoptics/resource_appoptics_space_chart_test.go similarity index 100% rename from appoptics/resource_librato_space_chart_test.go rename to appoptics/resource_appoptics_space_chart_test.go diff --git a/example.tf b/example.tf index 671a265..5ad9ebd 100644 --- a/example.tf +++ b/example.tf @@ -16,10 +16,10 @@ provider "appoptics" { } // -// Space +// Dashboard // -resource "appoptics_space" "test_space" { - name = "${var.tf-name-fragment} Space" +resource "appoptics_dashboard" "test_dashboard" { + name = "${var.tf-name-fragment} Dashboard" } @@ -54,8 +54,28 @@ resource "appoptics_metric" "test_metric"{ } // -// TODO: Chart +// Chart // +resource "appoptics_dashboard_chart" "test_chart"{ + space_id = "${appoptics_dashboard.test_dashboard.id}" + name = "Test Chart" + depends_on = ["appoptics_metric.test_metric"] + min = 0 + max = 100 + label = "Used" + + stream { + metric = "${appoptics_metric.test_metric.name}" + color = "#fa7268" + units_short = "%" + units_long = "Percentage used" + + tags = [{ + name = "environment" + values = ["staging"] + }] + } +} // // Alert diff --git a/go.mod b/go.mod index 9ad06be..d4a6d1f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.13 require ( cloud.google.com/go v0.37.2 // indirect github.com/agext/levenshtein v1.2.2 // indirect - github.com/akahn/go-librato v0.0.0-20180802162712-994aa1265ee6 github.com/apparentlymart/go-cidr v1.0.0 // indirect github.com/appoptics/appoptics-api-go v0.5.4 github.com/armon/go-radix v1.0.0 // indirect diff --git a/go.sum b/go.sum index dbda9e2..d5d1dd5 100644 --- a/go.sum +++ b/go.sum @@ -24,8 +24,6 @@ github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE= github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agl/ed25519 v0.0.0-20150830182803-278e1ec8e8a6/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0= -github.com/akahn/go-librato v0.0.0-20180802162712-994aa1265ee6 h1:EYsNMGTNpb6NzAU9WRrH3beiT9u2CIyimzIIVjvM61s= -github.com/akahn/go-librato v0.0.0-20180802162712-994aa1265ee6/go.mod h1:ezJdgQjH3LrqJNny4Wj8aLK24M/mHvCLrtM+gGT5eRI= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=