From a066d20489976b6688ceda325affc61cd875a2f8 Mon Sep 17 00:00:00 2001 From: Trevor Rosen Date: Wed, 15 Jan 2020 13:46:08 -0600 Subject: [PATCH] E2E validation (#41) * Add example.tf; update some types; extend Makefile #40 * Adds e2e tests; fixes to support same Fixes #40 --- .gitignore | 1 - Makefile | 6 ++ appoptics/resource_appoptics_alert.go | 28 +++++++- appoptics/resource_appoptics_metric.go | 39 +++++++---- example.tf | 90 ++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 +- 7 files changed, 151 insertions(+), 19 deletions(-) create mode 100644 example.tf diff --git a/.gitignore b/.gitignore index 44b56d5..d75ef8f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ *.dll *.exe .DS_Store -example.tf terraform.tfplan terraform.tfstate bin/ diff --git a/Makefile b/Makefile index 147accd..16e96db 100644 --- a/Makefile +++ b/Makefile @@ -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 + + diff --git a/appoptics/resource_appoptics_alert.go b/appoptics/resource_appoptics_alert.go index 9d4a914..f5dee6c 100644 --- a/appoptics/resource_appoptics_alert.go +++ b/appoptics/resource_appoptics_alert.go @@ -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) @@ -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 +} diff --git a/appoptics/resource_appoptics_metric.go b/appoptics/resource_appoptics_metric.go index abdc198..1a370d2 100644 --- a/appoptics/resource_appoptics_metric.go +++ b/appoptics/resource_appoptics_metric.go @@ -27,7 +27,7 @@ func resourceAppOpticsMetric() *schema.Resource { }, "type": { Type: schema.TypeString, - Computed: true, + Optional: true, }, "display_name": { Type: schema.TypeString, @@ -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": { @@ -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", @@ -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 != "" { @@ -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 } @@ -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) @@ -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) @@ -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 } @@ -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) } @@ -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 } diff --git a/example.tf b/example.tf new file mode 100644 index 0000000..671a265 --- /dev/null +++ b/example.tf @@ -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 = <