Skip to content

Commit

Permalink
refactor: rebase newrelic_alert_policy resource on newrelic-client-go (
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrombley authored Jan 22, 2020
1 parent 76db382 commit 9632f90
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 88 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/hashicorp/terraform-plugin-sdk v1.5.0
github.com/newrelic/go-agent/v3 v3.1.0
github.com/newrelic/go-insights v1.0.3
github.com/newrelic/newrelic-client-go v0.5.0
github.com/newrelic/newrelic-client-go v0.5.1
github.com/paultyng/go-newrelic/v4 v4.10.0
github.com/stretchr/testify v1.4.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ github.com/newrelic/go-agent/v3 v3.1.0 h1:fd3ILrFgoLOrVrCo9ZGrQ7JwGC/cw8eh7yEbOu
github.com/newrelic/go-agent/v3 v3.1.0/go.mod h1:H28zDNUC0U/b7kLoY4EFOhuth10Xu/9dchozUiOseQQ=
github.com/newrelic/go-insights v1.0.3 h1:zSNp1CEZnXktzSIEsbHJk8v6ZihdPFP2WsO/fzau3OQ=
github.com/newrelic/go-insights v1.0.3/go.mod h1:A20BoT8TNkqPGX2nS/Z2fYmKl3Cqa3iKZd4whzedCY4=
github.com/newrelic/newrelic-client-go v0.5.0 h1:wNPQex9deJAyqGOCGKl/XDJKybayqUbtpj4RtCw15Hc=
github.com/newrelic/newrelic-client-go v0.5.0/go.mod h1:cy6GDp11OfIj8Wfg0jfzDycso91NcojANZUqnjU9+QM=
github.com/newrelic/newrelic-client-go v0.5.1 h1:wWwJ4I9vCoqWIT//8v4IX6/UrAhbG6SO1h2dR5XTQdY=
github.com/newrelic/newrelic-client-go v0.5.1/go.mod h1:cy6GDp11OfIj8Wfg0jfzDycso91NcojANZUqnjU9+QM=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
Expand Down
29 changes: 11 additions & 18 deletions newrelic/data_source_newrelic_alert_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"log"
"strconv"
"strings"
"time"

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

func dataSourceNewRelicAlertPolicy() *schema.Resource {
Expand Down Expand Up @@ -36,18 +35,22 @@ func dataSourceNewRelicAlertPolicy() *schema.Resource {
}

func dataSourceNewRelicAlertPolicyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).Client
client := meta.(*ProviderConfig).NewClient

log.Printf("[INFO] Reading New Relic Alert Policies")

policies, err := client.ListAlertPolicies()
name := d.Get("name").(string)

params := alerts.ListPoliciesParams{
Name: name,
}

policies, err := client.Alerts.ListPolicies(&params)
if err != nil {
return err
}

var policy *newrelic.AlertPolicy

name := d.Get("name").(string)
var policy *alerts.Policy

for _, c := range policies {
if strings.EqualFold(c.Name, name) {
Expand All @@ -60,17 +63,7 @@ func dataSourceNewRelicAlertPolicyRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("the name '%s' does not match any New Relic alert policy", name)
}

// New Relic provides created_at and updated_at as millisecond unix timestamps
// https://www.terraform.io/docs/extend/schemas/schema-types.html#date-amp-time-data
// "TypeString is also used for date/time data, the preferred format is RFC 3339."
created := unixMillis(policy.CreatedAt).Format(time.RFC3339)
updated := unixMillis(policy.UpdatedAt).Format(time.RFC3339)

d.SetId(strconv.Itoa(policy.ID))
d.Set("name", policy.Name)
d.Set("incident_preference", policy.IncidentPreference)
d.Set("created_at", created)
d.Set("updated_at", updated)

return nil
return flattenAlertPolicyDataSource(policy, d)
}
58 changes: 13 additions & 45 deletions newrelic/resource_newrelic_alert_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package newrelic
import (
"log"
"strconv"
"time"

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

func resourceNewRelicAlertPolicy() *schema.Resource {
Expand Down Expand Up @@ -43,25 +42,13 @@ func resourceNewRelicAlertPolicy() *schema.Resource {
}
}

func buildAlertPolicyStruct(d *schema.ResourceData) *newrelic.AlertPolicy {
policy := newrelic.AlertPolicy{
Name: d.Get("name").(string),
}

if attr, ok := d.GetOk("incident_preference"); ok {
policy.IncidentPreference = attr.(string)
}

return &policy
}

func resourceNewRelicAlertPolicyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).Client
policy := buildAlertPolicyStruct(d)
client := meta.(*ProviderConfig).NewClient
policy := expandAlertPolicy(d)

log.Printf("[INFO] Creating New Relic alert policy %s", policy.Name)

policy, err := client.CreateAlertPolicy(*policy)
policy, err := client.Alerts.CreatePolicy(*policy)
if err != nil {
return err
}
Expand All @@ -71,16 +58,8 @@ func resourceNewRelicAlertPolicyCreate(d *schema.ResourceData, meta interface{})
return nil
}

func unixMillis(msec int64) time.Time {
sec := msec / 1000
nsec := (msec - (sec * 1000)) * 1000000
// Note: this will default to local time
created := time.Unix(sec, nsec)
return created
}

func resourceNewRelicAlertPolicyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).Client
client := meta.(*ProviderConfig).NewClient

id, err := strconv.ParseInt(d.Id(), 10, 32)
if err != nil {
Expand All @@ -89,33 +68,22 @@ func resourceNewRelicAlertPolicyRead(d *schema.ResourceData, meta interface{}) e

log.Printf("[INFO] Reading New Relic alert policy %v", id)

policy, err := client.GetAlertPolicy(int(id))
policy, err := client.Alerts.GetPolicy(int(id))
if err != nil {
if err == newrelic.ErrNotFound {
if _, ok := err.(*errors.NotFound); ok {
d.SetId("")
return nil
}

return err
}

// New Relic provides created_at and updated_at as millisecond unix timestamps
// https://www.terraform.io/docs/extend/schemas/schema-types.html#date-amp-time-data
// "TypeString is also used for date/time data, the preferred format is RFC 3339."
created := unixMillis(policy.CreatedAt).Format(time.RFC3339)
updated := unixMillis(policy.UpdatedAt).Format(time.RFC3339)

d.Set("name", policy.Name)
d.Set("incident_preference", policy.IncidentPreference)
d.Set("created_at", created)
d.Set("updated_at", updated)

return nil
return flattenAlertPolicy(policy, d)
}

func resourceNewRelicAlertPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).Client
policy := buildAlertPolicyStruct(d)
client := meta.(*ProviderConfig).NewClient
policy := expandAlertPolicy(d)

id, err := strconv.ParseInt(d.Id(), 10, 32)
if err != nil {
Expand All @@ -124,7 +92,7 @@ func resourceNewRelicAlertPolicyUpdate(d *schema.ResourceData, meta interface{})
policy.ID = int(id)

log.Printf("[INFO] Updating New Relic alert policy %d", id)
respPolicy, err := client.UpdateAlertPolicy(*policy)
respPolicy, err := client.Alerts.UpdatePolicy(*policy)
if err != nil {
return err
}
Expand All @@ -136,7 +104,7 @@ func resourceNewRelicAlertPolicyUpdate(d *schema.ResourceData, meta interface{})
}

func resourceNewRelicAlertPolicyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ProviderConfig).Client
client := meta.(*ProviderConfig).NewClient

id, err := strconv.ParseInt(d.Id(), 10, 32)
if err != nil {
Expand All @@ -145,7 +113,7 @@ func resourceNewRelicAlertPolicyDelete(d *schema.ResourceData, meta interface{})

log.Printf("[INFO] Deleting New Relic alert policy %v", id)

if err := client.DeleteAlertPolicy(int(id)); err != nil {
if _, err := client.Alerts.DeletePolicy(int(id)); err != nil {
return err
}

Expand Down
42 changes: 42 additions & 0 deletions newrelic/structures_newrelic_alert_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package newrelic

import (
"strconv"
"time"

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

func expandAlertPolicy(d *schema.ResourceData) *alerts.Policy {
policy := alerts.Policy{
Name: d.Get("name").(string),
}

if attr, ok := d.GetOk("incident_preference"); ok {
policy.IncidentPreference = attr.(string)
}

return &policy
}

func flattenAlertPolicyDataSource(policy *alerts.Policy, d *schema.ResourceData) error {
d.SetId(strconv.Itoa(policy.ID))

return flattenAlertPolicy(policy, d)
}

func flattenAlertPolicy(policy *alerts.Policy, d *schema.ResourceData) error {
// New Relic provides created_at and updated_at as millisecond unix timestamps
// https://www.terraform.io/docs/extend/schemas/schema-types.html#date-amp-time-data
// "TypeString is also used for date/time data, the preferred format is RFC 3339."
created := time.Time(*policy.CreatedAt).Format(time.RFC3339)
updated := time.Time(*policy.UpdatedAt).Format(time.RFC3339)

d.Set("name", policy.Name)
d.Set("incident_preference", policy.IncidentPreference)
d.Set("created_at", created)
d.Set("updated_at", updated)

return nil
}

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

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ github.com/newrelic/go-agent/v3/internal/utilization
github.com/newrelic/go-agent/v3/newrelic
# github.com/newrelic/go-insights v1.0.3
github.com/newrelic/go-insights/client
# github.com/newrelic/newrelic-client-go v0.5.0
# github.com/newrelic/newrelic-client-go v0.5.1
github.com/newrelic/newrelic-client-go/internal/http
github.com/newrelic/newrelic-client-go/internal/logging
github.com/newrelic/newrelic-client-go/internal/region
Expand Down

0 comments on commit 9632f90

Please sign in to comment.