Skip to content

Commit

Permalink
Add New Relic Infrastructure alert condition support
Browse files Browse the repository at this point in the history
  • Loading branch information
paul91 committed Jun 1, 2018
1 parent 744ea7c commit a2e0202
Show file tree
Hide file tree
Showing 24 changed files with 933 additions and 52 deletions.
21 changes: 21 additions & 0 deletions newrelic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ func (c *Config) Client() (*newrelic.Client, error) {

return &client, nil
}

// ClientInfra returns a new client for accessing New Relic
func (c *Config) ClientInfra() (*newrelic.InfraClient, error) {
nrConfig := newrelic.Config{
APIKey: c.APIKey,
Debug: logging.IsDebugOrHigher(),
BaseURL: c.APIURL,
}

client := newrelic.NewInfraClient(nrConfig)

log.Printf("[INFO] New Relic Infra client configured")

return &client, nil
}

// ProviderConfig for the custom provider
type ProviderConfig struct {
Client *newrelic.Client
InfraClient *newrelic.InfraClient
}
2 changes: 1 addition & 1 deletion newrelic/data_source_newrelic_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func dataSourceNewRelicApplication() *schema.Resource {
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion newrelic/data_source_newrelic_key_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func dataSourceNewRelicKeyTransaction() *schema.Resource {
}

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

log.Printf("[INFO] Reading New Relic key transactions")

Expand Down
43 changes: 36 additions & 7 deletions newrelic/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package newrelic

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -22,6 +23,11 @@ func Provider() terraform.ResourceProvider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NEWRELIC_API_URL", "https://api.newrelic.com/v2"),
},
"infra_api_url": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NEWRELIC_INFRA_API_URL", "https://infra-api.newrelic.com/v2"),
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand All @@ -30,12 +36,13 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"newrelic_alert_channel": resourceNewRelicAlertChannel(),
"newrelic_alert_condition": resourceNewRelicAlertCondition(),
"newrelic_nrql_alert_condition": resourceNewRelicNrqlAlertCondition(),
"newrelic_alert_policy": resourceNewRelicAlertPolicy(),
"newrelic_alert_policy_channel": resourceNewRelicAlertPolicyChannel(),
"newrelic_dashboard": resourceNewRelicDashboard(),
"newrelic_alert_channel": resourceNewRelicAlertChannel(),
"newrelic_alert_condition": resourceNewRelicAlertCondition(),
"newrelic_nrql_alert_condition": resourceNewRelicNrqlAlertCondition(),
"newrelic_infra_alert_condition": resourceNewRelicInfraAlertCondition(),
"newrelic_alert_policy": resourceNewRelicAlertPolicy(),
"newrelic_alert_policy_channel": resourceNewRelicAlertPolicyChannel(),
"newrelic_dashboard": resourceNewRelicDashboard(),
},

ConfigureFunc: providerConfigure,
Expand All @@ -48,5 +55,27 @@ func providerConfigure(data *schema.ResourceData) (interface{}, error) {
APIURL: data.Get("api_url").(string),
}
log.Println("[INFO] Initializing New Relic client")
return config.Client()

client, err := config.Client()
if err != nil {
return nil, fmt.Errorf("Error initializing New Relic client: %s", err)
}

infraConfig := Config{
APIKey: data.Get("api_key").(string),
APIURL: data.Get("infra_api_url").(string),
}
log.Println("[INFO] Initializing New Relic Infra client")

clientInfra, err := infraConfig.ClientInfra()
if err != nil {
return nil, fmt.Errorf("Error initializing New Relic Infra client: %s", err)
}

providerConfig := ProviderConfig{
Client: client,
InfraClient: clientInfra,
}

return &providerConfig, nil
}
6 changes: 3 additions & 3 deletions newrelic/resource_newrelic_alert_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func buildAlertChannelStruct(d *schema.ResourceData) *newrelic.AlertChannel {
}

func resourceNewRelicAlertChannelCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*newrelic.Client)
client := meta.(*ProviderConfig).Client
channel := buildAlertChannelStruct(d)

log.Printf("[INFO] Creating New Relic alert channel %s", channel.Name)
Expand All @@ -120,7 +120,7 @@ func resourceNewRelicAlertChannelCreate(d *schema.ResourceData, meta interface{}
}

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

id, err := strconv.ParseInt(d.Id(), 10, 32)
if err != nil {
Expand Down Expand Up @@ -149,7 +149,7 @@ func resourceNewRelicAlertChannelRead(d *schema.ResourceData, meta interface{})
}

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

id, err := strconv.ParseInt(d.Id(), 10, 32)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions newrelic/resource_newrelic_alert_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
newrelic "github.com/paultyng/go-newrelic/api"
)

func TestAccNewRelicAlertChannel_Basic(t *testing.T) {
Expand Down Expand Up @@ -51,7 +50,7 @@ func TestAccNewRelicAlertChannel_Basic(t *testing.T) {
}

func testAccCheckNewRelicAlertChannelDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*newrelic.Client)
client := testAccProvider.Meta().(*ProviderConfig).Client
for _, r := range s.RootModule().Resources {
if r.Type != "newrelic_alert_channel" {
continue
Expand Down Expand Up @@ -82,7 +81,7 @@ func testAccCheckNewRelicAlertChannelExists(n string) resource.TestCheckFunc {
return fmt.Errorf("No channel ID is set")
}

client := testAccProvider.Meta().(*newrelic.Client)
client := testAccProvider.Meta().(*ProviderConfig).Client

id, err := strconv.ParseInt(rs.Primary.ID, 10, 32)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions newrelic/resource_newrelic_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func readAlertConditionStruct(condition *newrelic.AlertCondition, d *schema.Reso
}

func resourceNewRelicAlertConditionCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*newrelic.Client)
client := meta.(*ProviderConfig).Client
condition := buildAlertConditionStruct(d)

log.Printf("[INFO] Creating New Relic alert condition %s", condition.Name)
Expand All @@ -294,7 +294,7 @@ func resourceNewRelicAlertConditionCreate(d *schema.ResourceData, meta interface
}

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

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

Expand All @@ -320,7 +320,7 @@ func resourceNewRelicAlertConditionRead(d *schema.ResourceData, meta interface{}
}

func resourceNewRelicAlertConditionUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*newrelic.Client)
client := meta.(*ProviderConfig).Client
condition := buildAlertConditionStruct(d)

ids, err := parseIDs(d.Id(), 2)
Expand All @@ -345,7 +345,7 @@ func resourceNewRelicAlertConditionUpdate(d *schema.ResourceData, meta interface
}

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

ids, err := parseIDs(d.Id(), 2)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions newrelic/resource_newrelic_alert_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
newrelic "github.com/paultyng/go-newrelic/api"
)

func TestAccNewRelicAlertCondition_Basic(t *testing.T) {
Expand Down Expand Up @@ -111,7 +110,7 @@ func TestAccNewRelicAlertCondition_ZeroThreshold(t *testing.T) {
// TODO: func_ TestAccNewRelicAlertCondition_Multi(t *testing.T) {

func testAccCheckNewRelicAlertConditionDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*newrelic.Client)
client := testAccProvider.Meta().(*ProviderConfig).Client
for _, r := range s.RootModule().Resources {
if r.Type != "newrelic_alert_condition" {
continue
Expand Down Expand Up @@ -144,7 +143,7 @@ func testAccCheckNewRelicAlertConditionExists(n string) resource.TestCheckFunc {
return fmt.Errorf("No alert condition ID is set")
}

client := testAccProvider.Meta().(*newrelic.Client)
client := testAccProvider.Meta().(*ProviderConfig).Client

ids, err := parseIDs(rs.Primary.ID, 2)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions newrelic/resource_newrelic_alert_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func buildAlertPolicyStruct(d *schema.ResourceData) *newrelic.AlertPolicy {
}

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

log.Printf("[INFO] Creating New Relic alert policy %s", policy.Name)
Expand All @@ -72,7 +72,7 @@ func resourceNewRelicAlertPolicyCreate(d *schema.ResourceData, meta interface{})
}

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

id, err := strconv.ParseInt(d.Id(), 10, 32)
if err != nil {
Expand Down Expand Up @@ -100,7 +100,7 @@ func resourceNewRelicAlertPolicyRead(d *schema.ResourceData, meta interface{}) e
}

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

id, err := strconv.ParseInt(d.Id(), 10, 32)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions newrelic/resource_newrelic_alert_policy_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func resourceNewRelicAlertPolicyChannel() *schema.Resource {
}

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

policyID := d.Get("policy_id").(int)
channelID := d.Get("channel_id").(int)
Expand All @@ -75,7 +75,7 @@ func resourceNewRelicAlertPolicyChannelCreate(d *schema.ResourceData, meta inter
}

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

ids, err := parseIDs(d.Id(), 2)
if err != nil {
Expand Down Expand Up @@ -104,7 +104,7 @@ func resourceNewRelicAlertPolicyChannelRead(d *schema.ResourceData, meta interfa
}

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

ids, err := parseIDs(d.Id(), 2)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions newrelic/resource_newrelic_alert_policy_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
newrelic "github.com/paultyng/go-newrelic/api"
)

func TestAccNewRelicAlertPolicyChannel_Basic(t *testing.T) {
Expand All @@ -34,7 +33,7 @@ func TestAccNewRelicAlertPolicyChannel_Basic(t *testing.T) {
}

func testAccCheckNewRelicAlertPolicyChannelDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*newrelic.Client)
client := testAccProvider.Meta().(*ProviderConfig).Client
for _, r := range s.RootModule().Resources {
if r.Type != "newrelic_alert_policy_channel" {
continue
Expand Down Expand Up @@ -70,7 +69,7 @@ func testAccCheckNewRelicAlertPolicyChannelExists(n string) resource.TestCheckFu
return fmt.Errorf("No resource ID is set")
}

client := testAccProvider.Meta().(*newrelic.Client)
client := testAccProvider.Meta().(*ProviderConfig).Client

ids, err := parseIDs(rs.Primary.ID, 2)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions newrelic/resource_newrelic_alert_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
newrelic "github.com/paultyng/go-newrelic/api"
)

func TestAccNewRelicAlertPolicy_Basic(t *testing.T) {
Expand Down Expand Up @@ -43,7 +42,7 @@ func TestAccNewRelicAlertPolicy_Basic(t *testing.T) {
}

func testAccCheckNewRelicAlertPolicyDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*newrelic.Client)
client := testAccProvider.Meta().(*ProviderConfig).Client
for _, r := range s.RootModule().Resources {
if r.Type != "newrelic_alert_policy" {
continue
Expand Down Expand Up @@ -74,7 +73,7 @@ func testAccCheckNewRelicAlertPolicyExists(n string) resource.TestCheckFunc {
return fmt.Errorf("No policy ID is set")
}

client := testAccProvider.Meta().(*newrelic.Client)
client := testAccProvider.Meta().(*ProviderConfig).Client

id, err := strconv.ParseInt(rs.Primary.ID, 10, 32)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions newrelic/resource_newrelic_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func flattenDashboard(dashboard *newrelic.Dashboard, d *schema.ResourceData) err
}

func resourceNewRelicDashboardCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*newrelic.Client)
client := meta.(*ProviderConfig).Client
dashboard := expandDashboard(d)
log.Printf("[INFO] Creating New Relic dashboard: %s", dashboard.Title)

Expand All @@ -223,7 +223,7 @@ func resourceNewRelicDashboardCreate(d *schema.ResourceData, meta interface{}) e
}

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

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

Expand All @@ -246,7 +246,7 @@ func resourceNewRelicDashboardRead(d *schema.ResourceData, meta interface{}) err
}

func resourceNewRelicDashboardUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*newrelic.Client)
client := meta.(*ProviderConfig).Client
dashboard := expandDashboard(d)

id, err := strconv.Atoi(d.Id())
Expand All @@ -266,7 +266,7 @@ func resourceNewRelicDashboardUpdate(d *schema.ResourceData, meta interface{}) e
}

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

id, err := strconv.Atoi(d.Id())
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions newrelic/resource_newrelic_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
newrelic "github.com/paultyng/go-newrelic/api"
)

func TestAccNewRelicDashboard_Basic(t *testing.T) {
Expand Down Expand Up @@ -88,7 +87,7 @@ func TestAccNewRelicDashboard_Basic(t *testing.T) {
}

func testAccCheckNewRelicDashboardDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*newrelic.Client)
client := testAccProvider.Meta().(*ProviderConfig).Client
for _, r := range s.RootModule().Resources {
if r.Type != "newrelic_dashboard" {
continue
Expand Down Expand Up @@ -119,7 +118,7 @@ func testAccCheckNewRelicDashboardExists(n string) resource.TestCheckFunc {
return fmt.Errorf("No dashboard ID is set")
}

client := testAccProvider.Meta().(*newrelic.Client)
client := testAccProvider.Meta().(*ProviderConfig).Client

id, err := strconv.ParseInt(rs.Primary.ID, 10, 32)
if err != nil {
Expand Down
Loading

0 comments on commit a2e0202

Please sign in to comment.