Skip to content

Commit

Permalink
Merge pull request #919 from newrelic/conditionPolicyFetchNerdgraph
Browse files Browse the repository at this point in the history
chore: replace some v2 GetPolicy() with nerdgrpah QueryPolicy()
  • Loading branch information
zlesnr authored Sep 30, 2020
2 parents fcb7c6d + 00bd380 commit b9c25cd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
10 changes: 7 additions & 3 deletions newrelic/resource_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ func resourceImportStateWithMetadata(defaultIDCount int, attribute string) schem
// resources can be scoped to specific accounts. Bear in mind those accounts must be
// accessible with the provided Personal API Key (APIKS).
func selectAccountID(providerCondig *ProviderConfig, d *schema.ResourceData) int {
resourceAccountID := d.Get("account_id").(int)
resourceAccountIDAttr := d.Get("account_id")

if resourceAccountID != 0 {
return resourceAccountID
if resourceAccountIDAttr != nil {
resourceAccountID := resourceAccountIDAttr.(int)

if resourceAccountID != 0 {
return resourceAccountID
}
}

return providerCondig.AccountID
Expand Down
6 changes: 4 additions & 2 deletions newrelic/resource_newrelic_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package newrelic
import (
"fmt"
"log"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -221,7 +222,9 @@ func resourceNewRelicAlertConditionCreate(d *schema.ResourceData, meta interface
}

func resourceNewRelicAlertConditionRead(d *schema.ResourceData, meta interface{}) error {
providerConfig := meta.(*ProviderConfig)
client := meta.(*ProviderConfig).NewClient
accountID := selectAccountID(providerConfig, d)

log.Printf("[INFO] Reading New Relic alert condition %s", d.Id())

Expand All @@ -233,13 +236,12 @@ func resourceNewRelicAlertConditionRead(d *schema.ResourceData, meta interface{}
policyID := ids[0]
id := ids[1]

_, err = client.Alerts.GetPolicy(policyID)
_, err = client.Alerts.QueryPolicy(accountID, strconv.Itoa(policyID))
if err != nil {
if _, ok := err.(*errors.NotFound); ok {
d.SetId("")
return nil
}

return err
}

Expand Down
5 changes: 4 additions & 1 deletion newrelic/resource_newrelic_infra_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package newrelic

import (
"log"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -197,7 +198,9 @@ func resourceNewRelicInfraAlertConditionCreate(d *schema.ResourceData, meta inte
}

func resourceNewRelicInfraAlertConditionRead(d *schema.ResourceData, meta interface{}) error {
providerConfig := meta.(*ProviderConfig)
client := meta.(*ProviderConfig).NewClient
accountID := selectAccountID(providerConfig, d)

log.Printf("[INFO] Reading New Relic Infra alert condition %s", d.Id())

Expand All @@ -209,7 +212,7 @@ func resourceNewRelicInfraAlertConditionRead(d *schema.ResourceData, meta interf
policyID := ids[0]
id := ids[1]

_, err = client.Alerts.GetPolicy(policyID)
_, err = client.Alerts.QueryPolicy(accountID, strconv.Itoa(policyID))
if err != nil {
if _, ok := err.(*errors.NotFound); ok {
d.SetId("")
Expand Down
3 changes: 2 additions & 1 deletion newrelic/resource_newrelic_nrql_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,10 @@ func resourceNewRelicNrqlAlertConditionRead(d *schema.ResourceData, meta interfa
return err
}

policyID := ids[0]
conditionID := ids[1]

_, err = client.Alerts.QueryPolicy(accountID, strconv.Itoa(ids[0]))
_, err = client.Alerts.QueryPolicy(accountID, strconv.Itoa(policyID))
if err != nil {
if _, ok := err.(*errors.NotFound); ok {
d.SetId("")
Expand Down
5 changes: 4 additions & 1 deletion newrelic/resource_newrelic_plugins_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package newrelic

import (
"log"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -137,7 +138,9 @@ func resourceNewRelicPluginsAlertConditionCreate(d *schema.ResourceData, meta in
}

func resourceNewRelicPluginsAlertConditionRead(d *schema.ResourceData, meta interface{}) error {
providerConfig := meta.(*ProviderConfig)
client := meta.(*ProviderConfig).NewClient
accountID := selectAccountID(providerConfig, d)

log.Printf("[INFO] Reading New Relic alert condition %s", d.Id())

Expand All @@ -149,7 +152,7 @@ func resourceNewRelicPluginsAlertConditionRead(d *schema.ResourceData, meta inte
policyID := ids[0]
id := ids[1]

_, err = client.Alerts.GetPolicy(policyID)
_, err = client.Alerts.QueryPolicy(accountID, strconv.Itoa(policyID))
if err != nil {
if _, ok := err.(*errors.NotFound); ok {
d.SetId("")
Expand Down

0 comments on commit b9c25cd

Please sign in to comment.