Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Get function and fixing alert update/read/create/delete #26

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions vendor/github.com/spaceapegames/go-wavefront/alert.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 42 additions & 69 deletions wavefront/resource_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
}
Expand Down