diff --git a/newrelic/resource_helpers.go b/newrelic/resource_helpers.go index 11ca628b2..492fbd442 100644 --- a/newrelic/resource_helpers.go +++ b/newrelic/resource_helpers.go @@ -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 diff --git a/newrelic/resource_newrelic_alert_condition.go b/newrelic/resource_newrelic_alert_condition.go index 4a49bc0cd..66111b656 100644 --- a/newrelic/resource_newrelic_alert_condition.go +++ b/newrelic/resource_newrelic_alert_condition.go @@ -3,6 +3,7 @@ package newrelic import ( "fmt" "log" + "strconv" "strings" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -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()) @@ -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 } diff --git a/newrelic/resource_newrelic_infra_alert_condition.go b/newrelic/resource_newrelic_infra_alert_condition.go index f2222e2b2..1fab63109 100644 --- a/newrelic/resource_newrelic_infra_alert_condition.go +++ b/newrelic/resource_newrelic_infra_alert_condition.go @@ -2,6 +2,7 @@ package newrelic import ( "log" + "strconv" "strings" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -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()) @@ -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("") diff --git a/newrelic/resource_newrelic_nrql_alert_condition.go b/newrelic/resource_newrelic_nrql_alert_condition.go index cb2766881..f258b90fd 100644 --- a/newrelic/resource_newrelic_nrql_alert_condition.go +++ b/newrelic/resource_newrelic_nrql_alert_condition.go @@ -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("") diff --git a/newrelic/resource_newrelic_plugins_alert_condition.go b/newrelic/resource_newrelic_plugins_alert_condition.go index bcbfe5ea4..2ecf56c52 100644 --- a/newrelic/resource_newrelic_plugins_alert_condition.go +++ b/newrelic/resource_newrelic_plugins_alert_condition.go @@ -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" @@ -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()) @@ -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("")