Skip to content

Commit

Permalink
feat(infra_alert_condition): add violation_close_timer to newrelic_in…
Browse files Browse the repository at this point in the history
…fra_alert_condition resource
  • Loading branch information
sanderblue committed Jan 31, 2020
1 parent f519d86 commit d2d9442
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 161 deletions.
167 changes: 6 additions & 161 deletions newrelic/resource_newrelic_infra_alert_condition.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package newrelic

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/newrelic/newrelic-client-go/pkg/alerts"
"github.com/newrelic/newrelic-client-go/pkg/errors"
)

Expand Down Expand Up @@ -136,140 +134,15 @@ func resourceNewRelicInfraAlertCondition() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"violation_close_timer": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: intInSlice([]int{0, 1, 2, 4, 8, 12, 24, 48, 72}),
},
},
}
}

func expandInfraAlertCondition(d *schema.ResourceData) (*alerts.InfrastructureCondition, error) {
condition := alerts.InfrastructureCondition{
Name: d.Get("name").(string),
Enabled: d.Get("enabled").(bool),
PolicyID: d.Get("policy_id").(int),
Event: d.Get("event").(string),
Comparison: d.Get("comparison").(string),
Select: d.Get("select").(string),
Type: d.Get("type").(string),
Critical: expandAlertThreshold(d.Get("critical")),
}

if attr, ok := d.GetOk("runbook_url"); ok {
condition.RunbookURL = attr.(string)
}
if attr, ok := d.GetOk("warning"); ok {
condition.Warning = expandAlertThreshold(attr)
}

if attr, ok := d.GetOk("where"); ok {
condition.Where = attr.(string)
}

if attr, ok := d.GetOk("process_where"); ok {
condition.ProcessWhere = attr.(string)
}

if attr, ok := d.GetOk("integration_provider"); ok {
condition.IntegrationProvider = attr.(string)
}

err := validateAttributesForType(&condition)

if err != nil {
return nil, err
}

return &condition, nil
}

func validateAttributesForType(c *alerts.InfrastructureCondition) error {
switch c.Type {
case "infra_process_running":
if c.Event != "" {
return fmt.Errorf("event is not supported by condition type %s", c.Type)
}
if c.IntegrationProvider != "" {
return fmt.Errorf("integration_provider is not supported by condition type %s", c.Type)
}
if c.Select != "" {
return fmt.Errorf("select is not supported by condition type %s", c.Type)
}
if c.Critical.Function != "" {
return fmt.Errorf("time_function is not supported by condition type %s", c.Type)
}
case "infra_metric":
if c.ProcessWhere != "" {
return fmt.Errorf("process_where is not supported by condition type %s", c.Type)
}
case "infra_host_not_reporting":
if c.Event != "" {
return fmt.Errorf("event is not supported by condition type %s", c.Type)
}
if c.IntegrationProvider != "" {
return fmt.Errorf("integration_provider is not supported by condition type %s", c.Type)
}
if c.Select != "" {
return fmt.Errorf("select is not supported by condition type %s", c.Type)
}
if c.ProcessWhere != "" {
return fmt.Errorf("process_where is not supported by condition type %s", c.Type)
}
if c.Comparison != "" {
return fmt.Errorf("comparison is not supported by condition type %s", c.Type)
}
if c.Critical.Function != "" {
return fmt.Errorf("time_function is not supported by condition type %s", c.Type)
}
if c.Critical.Value != 0 {
return fmt.Errorf("value is not supported by condition type %s", c.Type)
}
}

return nil
}

func readInfraAlertConditionStruct(condition *alerts.InfrastructureCondition, d *schema.ResourceData) error {
ids, err := parseIDs(d.Id(), 2)
if err != nil {
return err
}

policyID := ids[0]

d.Set("policy_id", policyID)
d.Set("name", condition.Name)
d.Set("runbook_url", condition.RunbookURL)
d.Set("enabled", condition.Enabled)
d.Set("comparison", condition.Comparison)
d.Set("event", condition.Event)
d.Set("select", condition.Select)
d.Set("type", condition.Type)
d.Set("created_at", condition.CreatedAt)
d.Set("updated_at", condition.UpdatedAt)

if condition.Where != "" {
d.Set("where", condition.Where)
}

if condition.ProcessWhere != "" {
d.Set("process_where", condition.ProcessWhere)
}

if condition.IntegrationProvider != "" {
d.Set("integration_provider", condition.IntegrationProvider)
}

if err := d.Set("critical", flattenAlertThreshold(condition.Critical)); err != nil {
return err
}

if condition.Warning != nil {
if err := d.Set("warning", flattenAlertThreshold(condition.Warning)); err != nil {
return err
}
}

return nil
}

func resourceNewRelicInfraAlertConditionCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).NewClient
condition, err := expandInfraAlertCondition(d)
Expand Down Expand Up @@ -323,7 +196,7 @@ func resourceNewRelicInfraAlertConditionRead(d *schema.ResourceData, meta interf
return err
}

return readInfraAlertConditionStruct(condition, d)
return flattenInfraAlertCondition(condition, d)
}

func resourceNewRelicInfraAlertConditionUpdate(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -373,31 +246,3 @@ func resourceNewRelicInfraAlertConditionDelete(d *schema.ResourceData, meta inte

return nil
}

func expandAlertThreshold(v interface{}) *alerts.InfrastructureConditionThreshold {
rah := v.([]interface{})[0].(map[string]interface{})

alertInfraThreshold := &alerts.InfrastructureConditionThreshold{
Duration: rah["duration"].(int),
}

if val, ok := rah["value"]; ok {
alertInfraThreshold.Value = val.(float64)
}

if val, ok := rah["time_function"]; ok {
alertInfraThreshold.Function = val.(string)
}

return alertInfraThreshold
}

func flattenAlertThreshold(v *alerts.InfrastructureConditionThreshold) []interface{} {
alertInfraThreshold := map[string]interface{}{
"duration": v.Duration,
"value": v.Value,
"time_function": v.Function,
}

return []interface{}{alertInfraThreshold}
}
176 changes: 176 additions & 0 deletions newrelic/structures_newrelic_infra_alert_condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package newrelic

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/newrelic/newrelic-client-go/pkg/alerts"
)

func expandInfraAlertCondition(d *schema.ResourceData) (*alerts.InfrastructureCondition, error) {
condition := alerts.InfrastructureCondition{
Name: d.Get("name").(string),
Enabled: d.Get("enabled").(bool),
PolicyID: d.Get("policy_id").(int),
Event: d.Get("event").(string),
Comparison: d.Get("comparison").(string),
Select: d.Get("select").(string),
Type: d.Get("type").(string),
Critical: expandInfraAlertThreshold(d.Get("critical")),
}

if attr, ok := d.GetOk("runbook_url"); ok {
condition.RunbookURL = attr.(string)
}

if attr, ok := d.GetOk("warning"); ok {
condition.Warning = expandInfraAlertThreshold(attr)
}

if attr, ok := d.GetOk("where"); ok {
condition.Where = attr.(string)
}

if attr, ok := d.GetOk("process_where"); ok {
condition.ProcessWhere = attr.(string)
}

if attr, ok := d.GetOk("integration_provider"); ok {
condition.IntegrationProvider = attr.(string)
}

if attr, ok := d.GetOk("violation_close_timer"); ok {
t := attr.(interface{}).(int)
condition.ViolationCloseTimer = &t
}

err := validateAttributesForType(&condition)

if err != nil {
return nil, err
}

return &condition, nil
}

func expandInfraAlertThreshold(v interface{}) *alerts.InfrastructureConditionThreshold {
rah := v.([]interface{})[0].(map[string]interface{})

alertInfraThreshold := &alerts.InfrastructureConditionThreshold{
Duration: rah["duration"].(int),
}

if val, ok := rah["value"]; ok {
alertInfraThreshold.Value = val.(float64)
}

if val, ok := rah["time_function"]; ok {
alertInfraThreshold.Function = val.(string)
}

return alertInfraThreshold
}

func flattenInfraAlertCondition(condition *alerts.InfrastructureCondition, d *schema.ResourceData) error {
ids, err := parseIDs(d.Id(), 2)
if err != nil {
return err
}

policyID := ids[0]

d.Set("policy_id", policyID)
d.Set("name", condition.Name)
d.Set("runbook_url", condition.RunbookURL)
d.Set("enabled", condition.Enabled)
d.Set("comparison", condition.Comparison)
d.Set("event", condition.Event)
d.Set("select", condition.Select)
d.Set("type", condition.Type)
d.Set("created_at", condition.CreatedAt)
d.Set("updated_at", condition.UpdatedAt)

if condition.Where != "" {
d.Set("where", condition.Where)
}

if condition.ProcessWhere != "" {
d.Set("process_where", condition.ProcessWhere)
}

if condition.IntegrationProvider != "" {
d.Set("integration_provider", condition.IntegrationProvider)
}

if condition.ViolationCloseTimer != nil {
d.Set("violation_close_timer", condition.ViolationCloseTimer)
}

if err := d.Set("critical", flattenAlertThreshold(condition.Critical)); err != nil {
return err
}

if condition.Warning != nil {
if err := d.Set("warning", flattenAlertThreshold(condition.Warning)); err != nil {
return err
}
}

return nil
}

func flattenAlertThreshold(v *alerts.InfrastructureConditionThreshold) []interface{} {
alertInfraThreshold := map[string]interface{}{
"duration": v.Duration,
"value": v.Value,
"time_function": v.Function,
}

return []interface{}{alertInfraThreshold}
}

func validateAttributesForType(c *alerts.InfrastructureCondition) error {
switch c.Type {
case "infra_process_running":
if c.Event != "" {
return fmt.Errorf("event is not supported by condition type %s", c.Type)
}
if c.IntegrationProvider != "" {
return fmt.Errorf("integration_provider is not supported by condition type %s", c.Type)
}
if c.Select != "" {
return fmt.Errorf("select is not supported by condition type %s", c.Type)
}
if c.Critical.Function != "" {
return fmt.Errorf("time_function is not supported by condition type %s", c.Type)
}
case "infra_metric":
if c.ProcessWhere != "" {
return fmt.Errorf("process_where is not supported by condition type %s", c.Type)
}
case "infra_host_not_reporting":
if c.Event != "" {
return fmt.Errorf("event is not supported by condition type %s", c.Type)
}
if c.IntegrationProvider != "" {
return fmt.Errorf("integration_provider is not supported by condition type %s", c.Type)
}
if c.Select != "" {
return fmt.Errorf("select is not supported by condition type %s", c.Type)
}
if c.ProcessWhere != "" {
return fmt.Errorf("process_where is not supported by condition type %s", c.Type)
}
if c.Comparison != "" {
return fmt.Errorf("comparison is not supported by condition type %s", c.Type)
}
if c.Critical.Function != "" {
return fmt.Errorf("time_function is not supported by condition type %s", c.Type)
}
if c.Critical.Value != 0 {
return fmt.Errorf("value is not supported by condition type %s", c.Type)
}
}

return nil
}
Loading

0 comments on commit d2d9442

Please sign in to comment.