diff --git a/vendor/github.com/spaceapegames/go-wavefront/alert.go b/vendor/github.com/spaceapegames/go-wavefront/alert.go index 9489740..e0e8a58 100644 --- a/vendor/github.com/spaceapegames/go-wavefront/alert.go +++ b/vendor/github.com/spaceapegames/go-wavefront/alert.go @@ -12,7 +12,7 @@ type Alert struct { Name string `json:"name"` // ID is the Wavefront-assigned ID of an existing Alert - ID *string `json:"id,omitempty"` + ID string `json:"id,omitempty"` // AdditionalInfo is any extra information about the Alert AdditionalInfo string `json:"additionalInformation"` @@ -88,6 +88,16 @@ func (c *Client) Alerts() *Alerts { return &Alerts{client: c} } +// Get is used to retrieve an existing Alert by ID. +// The ID field must be provided +func (a Alerts) Get(alert *Alert) error { + if alert.ID == "" { + return fmt.Errorf("Alert id field is not set") + } + + return a.crudAlert("GET", fmt.Sprintf("%s/%s", baseAlertPath, alert.ID), alert) +} + // Find returns all alerts filtered by the given search conditions. // If filter is nil, all alerts are returned. func (a Alerts) Find(filter []*SearchCondition) ([]*Alert, error) { @@ -128,28 +138,28 @@ func (a Alerts) Create(alert *Alert) error { // Update is used to update an existing Alert. // The ID field of the alert must be populated func (a Alerts) Update(alert *Alert) error { - if alert.ID == nil { + if alert.ID == "" { return fmt.Errorf("alert id field not set") } - return a.crudAlert("PUT", fmt.Sprintf("%s/%s", baseAlertPath, *alert.ID), alert) + return a.crudAlert("PUT", fmt.Sprintf("%s/%s", baseAlertPath, alert.ID), alert) } // Delete is used to delete an existing Alert. // The ID field of the alert must be populated func (a Alerts) Delete(alert *Alert) error { - if alert.ID == nil { + if alert.ID == "" { return fmt.Errorf("alert id field not set") } - err := a.crudAlert("DELETE", fmt.Sprintf("%s/%s", baseAlertPath, *alert.ID), alert) + err := a.crudAlert("DELETE", fmt.Sprintf("%s/%s", baseAlertPath, alert.ID), alert) if err != nil { return err } //reset the ID field so deletion is not attempted again - alert.ID = nil + alert.ID = "" return nil } diff --git a/wavefront/resource_alert.go b/wavefront/resource_alert.go index ca7e45a..a81e49d 100644 --- a/wavefront/resource_alert.go +++ b/wavefront/resource_alert.go @@ -67,15 +67,15 @@ func trimSpaces(d interface{}) string { return strings.TrimSpace(d.(string)) } -func resourceAlertCreate(d *schema.ResourceData, m interface{}) error { - alerts := m.(*wavefrontClient).client.Alerts() +// Construct a Wavefront Alert +func buildAlert(d *schema.ResourceData) (*wavefront.Alert, error) { var tags []string for _, tag := range d.Get("tags").([]interface{}) { tags = append(tags, tag.(string)) } - a := &wavefront.Alert{ + return &wavefront.Alert{ Name: d.Get("name").(string), Target: d.Get("target").(string), Condition: d.Get("condition").(string), @@ -85,112 +85,85 @@ func resourceAlertCreate(d *schema.ResourceData, m interface{}) error { ResolveAfterMinutes: d.Get("resolve_after_minutes").(int), Severity: d.Get("severity").(string), Tags: tags, + }, nil +} + +// Create the alert on Wavefront +func resourceAlertCreate(d *schema.ResourceData, m interface{}) error { + alerts := m.(*wavefrontClient).client.Alerts() + alert, err := buildAlert(d) + if err != nil { + return fmt.Errorf("Failed to parse Alert, %s", err) } - // Create the alert on Wavefront - err := alerts.Create(a) + err = alerts.Create(alert) if err != nil { return fmt.Errorf("Error Creating Alert %s. %s", d.Get("name"), err) } - - d.SetId(*a.ID) + d.SetId(alert.ID) return nil } +// Read a Wavefront Alert func resourceAlertRead(d *schema.ResourceData, m interface{}) error { alerts := m.(*wavefrontClient).client.Alerts() - // search for an alert with our id. We should recieve 1 (Exact Match) or 0 (No Match) - results, err := alerts.Find( - []*wavefront.SearchCondition{ - { - Key: "id", - Value: d.Id(), - MatchingMethod: "EXACT", - }, - }) - if err != nil { - return fmt.Errorf("Error finding Wavefront Alert %s. %s", d.Id(), err) + alert := wavefront.Alert{ + ID: d.Id(), } - // resource has been deleted out of band. So unset ID - if len(results) != 1 { + + // search for an dashboard with our id. We should receive 1 (Exact Match) or 0 (No Match) + err := alerts.Get(&alert) + if err != nil { + // alert no longer exists d.SetId("") - return nil } // Use the Wavefront ID as the Terraform ID - d.SetId(*results[0].ID) - d.Set("name", results[0].Name) - d.Set("target", results[0].Target) - d.Set("condition", results[0].Condition) - d.Set("additional_information", results[0].AdditionalInfo) - d.Set("display_expression", results[0].DisplayExpression) - d.Set("minutes", results[0].Minutes) - d.Set("resolve_after_minutes", results[0].ResolveAfterMinutes) - d.Set("severity", results[0].Severity) - d.Set("tags", results[0].Tags) + d.SetId(alert.ID) + d.Set("name", alert.Name) + d.Set("target", alert.Target) + d.Set("condition", alert.Condition) + d.Set("additional_information", alert.AdditionalInfo) + d.Set("display_expression", alert.DisplayExpression) + d.Set("minutes", alert.Minutes) + d.Set("resolve_after_minutes", alert.ResolveAfterMinutes) + d.Set("severity", alert.Severity) + d.Set("tags", alert.Tags) return nil } +// Update the alert on Wavefront func resourceAlertUpdate(d *schema.ResourceData, m interface{}) error { alerts := m.(*wavefrontClient).client.Alerts() - - results, err := alerts.Find( - []*wavefront.SearchCondition{ - { - Key: "id", - Value: d.Id(), - MatchingMethod: "EXACT", - }, - }) + alert, err := buildAlert(d) if err != nil { - return fmt.Errorf("Error finding Wavefront Alert %s. %s", d.Id(), err) + return fmt.Errorf("Failed to parse Alert, %s", err) } - var tags []string - for _, tag := range d.Get("tags").([]interface{}) { - tags = append(tags, tag.(string)) - } - - a := results[0] - a.Name = d.Get("name").(string) - a.Target = d.Get("target").(string) - a.Condition = d.Get("condition").(string) - a.AdditionalInfo = d.Get("additional_information").(string) - a.DisplayExpression = d.Get("display_expression").(string) - a.Minutes = d.Get("minutes").(int) - a.ResolveAfterMinutes = d.Get("resolve_after_minutes").(int) - a.Severity = d.Get("severity").(string) - a.Tags = tags - - // Update the alert on Wavefront - err = alerts.Update(a) + err = alerts.Update(alert) if err != nil { return fmt.Errorf("Error Updating Alert %s. %s", d.Get("name"), err) } return nil } +// Delete the alert on Wavefront func resourceAlertDelete(d *schema.ResourceData, m interface{}) error { alerts := m.(*wavefrontClient).client.Alerts() + alert := wavefront.Alert{ + ID: d.Id(), + } - results, err := alerts.Find( - []*wavefront.SearchCondition{ - &wavefront.SearchCondition{ - Key: "id", - Value: d.Id(), - MatchingMethod: "EXACT", - }, - }) + err := alerts.Get(&alert) if err != nil { return fmt.Errorf("Error finding Wavefront Alert %s. %s", d.Id(), err) } - a := results[0] // Delete the Alert - err = alerts.Delete(a) + err = alerts.Delete(&alert) if err != nil { return fmt.Errorf("Failed to delete Alert %s. %s", d.Id(), err) }