Skip to content

Commit

Permalink
Charts (#42)
Browse files Browse the repository at this point in the history
* Finally remove old Librato stuff

* File rename

* Add Chart; rename Space to Dashboard

 Fixes #3
  • Loading branch information
Trevor Rosen committed Jan 16, 2020
1 parent a066d20 commit c5142e6
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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


10 changes: 5 additions & 5 deletions appoptics/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 6 additions & 7 deletions appoptics/resource_appoptics_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
28 changes: 24 additions & 4 deletions example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}


Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit c5142e6

Please sign in to comment.