Skip to content

Commit

Permalink
E2E validation (#41)
Browse files Browse the repository at this point in the history
* Add example.tf; update some types; extend Makefile

 #40

* Adds e2e tests; fixes to support same

 Fixes #40
  • Loading branch information
Trevor Rosen committed Jan 15, 2020
1 parent 789ec78 commit a066d20
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 19 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
*.dll
*.exe
.DS_Store
example.tf
terraform.tfplan
terraform.tfstate
bin/
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ test-release:
# Requires a GITHUB_TOKEN to be set in the environment
release:
goreleaser --rm-dist

# Convenient in dev to rebuild the plugin, re-init TF, and run a plan
bounce: build
rm *tfstate* && terraform init && terraform plan


28 changes: 26 additions & 2 deletions appoptics/resource_appoptics_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,12 @@ func resourceAppOpticsAlertUpdate(d *schema.ResourceData, meta interface{}) erro
return err
}

alert := new(appoptics.AlertRequest)
theAlert, err := client.AlertsService().Retrieve(int(id))
if err != nil {
return err
}
alert := alertToAlertRequest(theAlert)
alert.ID = int(id)
alert.Name = d.Get("name").(string)

if d.HasChange("description") {
alert.Description = d.Get("description").(string)
Expand Down Expand Up @@ -538,3 +541,24 @@ func resourceAppOpticsAlertDelete(d *schema.ResourceData, meta interface{}) erro

return nil
}

// used to deal w/ differing structures in API create/read
func alertToAlertRequest(a *appoptics.Alert) *appoptics.AlertRequest {
aReq := &appoptics.AlertRequest{}
aReq.ID = a.ID
aReq.Name = a.Name
aReq.Description = a.Description
aReq.Active = a.Active
aReq.RearmSeconds = a.RearmSeconds
aReq.Conditions = a.Conditions
aReq.Attributes = a.Attributes
aReq.CreatedAt = a.CreatedAt
aReq.UpdatedAt = a.UpdatedAt

serviceIDs := make([]int, len(a.Services))
for i, service := range a.Services {
serviceIDs[i] = service.ID
}
aReq.Services = serviceIDs
return aReq
}
39 changes: 26 additions & 13 deletions appoptics/resource_appoptics_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func resourceAppOpticsMetric() *schema.Resource {
},
"type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"display_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -55,12 +55,16 @@ func resourceAppOpticsMetric() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"display_max": {
"summarize_function": {
Type: schema.TypeString,
Optional: true,
},
"display_max": {
Type: schema.TypeFloat,
Optional: true,
},
"display_min": {
Type: schema.TypeString,
Type: schema.TypeFloat,
Optional: true,
},
"display_units_long": {
Expand Down Expand Up @@ -97,8 +101,6 @@ func resourceAppOpticsMetric() *schema.Resource {

func resourceAppOpticsMetricCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*appoptics.Client)
metricName := "some.metric"

metric := appoptics.Metric{
Name: d.Get("name").(string),
Type: "gauge",
Expand Down Expand Up @@ -126,10 +128,10 @@ func resourceAppOpticsMetricCreate(d *schema.ResourceData, meta interface{}) err
if v, ok := attributeDataMap["color"].(string); ok && v != "" {
attributes.Color = v
}
if v, ok := attributeDataMap["display_max"].(string); ok && v != "" {
if v, ok := attributeDataMap["display_max"].(float64); ok && v != 0.0 {
attributes.DisplayMax = v
}
if v, ok := attributeDataMap["display_min"].(string); ok && v != "" {
if v, ok := attributeDataMap["display_min"].(float64); ok && v != 0.0 {
attributes.DisplayMin = v
}
if v, ok := attributeDataMap["display_units_long"].(string); ok && v != "" {
Expand All @@ -141,6 +143,9 @@ func resourceAppOpticsMetricCreate(d *schema.ResourceData, meta interface{}) err
if v, ok := attributeDataMap["created_by_ua"].(string); ok && v != "" {
attributes.CreatedByUA = v
}
if v, ok := attributeDataMap["summarize_function"].(string); ok && v != "" {
attributes.SummarizeFunction = v
}
if v, ok := attributeDataMap["display_stacked"].(bool); ok {
attributes.DisplayStacked = v
}
Expand All @@ -154,7 +159,7 @@ func resourceAppOpticsMetricCreate(d *schema.ResourceData, meta interface{}) err
metric.Attributes = attributes
}

err := client.MetricsService().Update(metricName, &metric)
_, err := client.MetricsService().Create(&metric)
if err != nil {
log.Printf("[INFO] ERROR creating Metric: %s", err)
return fmt.Errorf("Error creating AppOptics metric: %s", err)
Expand Down Expand Up @@ -227,9 +232,11 @@ func resourceAppOpticsMetricUpdate(d *schema.ResourceData, meta interface{}) err
client := meta.(*appoptics.Client)

id := d.Id()
metric, err := client.MetricsService().Retrieve(id)

metric := &appoptics.Metric{}
metric.Name = id
if err != nil {
return err
}

if d.HasChange("type") {
metric.Type = d.Get("type").(string)
Expand All @@ -254,15 +261,18 @@ func resourceAppOpticsMetricUpdate(d *schema.ResourceData, meta interface{}) err
if v, ok := attributeDataMap["color"].(string); ok && v != "" {
attributes.Color = v
}
if v, ok := attributeDataMap["display_max"].(string); ok && v != "" {
if v, ok := attributeDataMap["display_max"].(float64); ok && v != 0.0 {
attributes.DisplayMax = v
}
if v, ok := attributeDataMap["display_min"].(string); ok && v != "" {
if v, ok := attributeDataMap["display_min"].(float64); ok && v != 0.0 {
attributes.DisplayMin = v
}
if v, ok := attributeDataMap["display_units_long"].(string); ok && v != "" {
attributes.DisplayUnitsLong = v
}
if v, ok := attributeDataMap["summarize_function"].(string); ok && v != "" {
attributes.SummarizeFunction = v
}
if v, ok := attributeDataMap["display_units_short"].(string); ok && v != "" {
attributes.DisplayUnitsShort = v
}
Expand All @@ -283,7 +293,7 @@ func resourceAppOpticsMetricUpdate(d *schema.ResourceData, meta interface{}) err

log.Printf("[INFO] Updating AppOptics metric: %v", structToString(metric))

err := client.MetricsService().Update(id, metric)
err = client.MetricsService().Update(id, metric)
if err != nil {
return fmt.Errorf("Error updating AppOptics metric: %s", err)
}
Expand Down Expand Up @@ -369,6 +379,9 @@ func metricAttributesGather(d *schema.ResourceData, attributes *appoptics.Metric
if attributes.DisplayUnitsLong != "" {
retAttributes["display_units_long"] = attributes.DisplayUnitsLong
}
if attributes.SummarizeFunction != "" {
retAttributes["summarize_function"] = attributes.SummarizeFunction
}
if attributes.DisplayUnitsShort != "" {
retAttributes["display_units_short"] = attributes.DisplayUnitsShort
}
Expand Down
90 changes: 90 additions & 0 deletions example.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Used to identify all things made in integration test runs
variable "tf-name-fragment" {
type = "string"
default = "tf_provider_test"
}

variable "test-metric-name" {
type = "string"
default = "tf_provider_test.cpu.percent.used" // can't interpolate into variables
}

provider "appoptics" {
//
// Implicitly consumes APPOPTICS_TOKEN from the environment
//
}

//
// Space
//
resource "appoptics_space" "test_space" {
name = "${var.tf-name-fragment} Space"
}


//
// Notification Service
//
resource "appoptics_service" "test_service"{
title = "${var.tf-name-fragment} Email Notification Service"
type = "mail"
settings = <<EOF
{
"addresses": "foobar@example.com"
}
EOF
}

//
// Metric
//
resource "appoptics_metric" "test_metric"{
name = "${var.test-metric-name}"
display_name = "Terraform Test CPU Utilization"
period = 60
type = "gauge"
attributes = {
color = "#A3BE8C"
summarize_function = "average"
display_max = 100.0
display_units_long = "CPU Utilization Percent"
display_units_short = "cpu %"
}
}

//
// TODO: Chart
//

//
// Alert
//
resource "appoptics_alert" "test_alert" {
name = "${var.tf-name-fragment}.Alert"
description = "Managed by Terraform"
rearm_seconds = 10800

depends_on = ["appoptics_metric.test_metric"]

condition {
type = "above"
threshold = 0
metric_name = "${var.test-metric-name}"
duration = 60
summary_function = "sum"

tag {
name = "logical_thing"
grouped = true
values = ["staging"]
}

tag {
name = "event_status"
grouped = true
values = ["count of scary things"]
}

}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
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.3
github.com/appoptics/appoptics-api-go v0.5.4
github.com/armon/go-radix v1.0.0 // indirect
github.com/aws/aws-sdk-go v1.19.11 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:o
github.com/apparentlymart/go-textseg v0.0.0-20170531203952-b836f5c4d331/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
github.com/appoptics/appoptics-api-go v0.5.3 h1:mYcKOvdPC4HS0uFcR5iwU5XCxJJtHAadjFyj/bE5wGA=
github.com/appoptics/appoptics-api-go v0.5.3/go.mod h1:wXPiQHhhV/ITjwshpxd3IACaFmEO9IsCCqnrlxEmmB0=
github.com/appoptics/appoptics-api-go v0.5.4 h1:hJRNRffot0W9p0jqSDXJAi9+aSUc8GDY4+vLdGutCDk=
github.com/appoptics/appoptics-api-go v0.5.4/go.mod h1:wXPiQHhhV/ITjwshpxd3IACaFmEO9IsCCqnrlxEmmB0=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20160115234725-4239b77079c7/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
Expand Down

0 comments on commit a066d20

Please sign in to comment.