Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/newrelic: Add a New Relic provider #10317

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions builtin/providers/newrelic/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package newrelic

import (
"log"

"github.com/hashicorp/terraform/helper/logging"
newrelic "github.com/paultyng/go-newrelic/api"
)

// Config contains New Relic provider settings
type Config struct {
APIKey string
APIURL string
}

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

client := newrelic.New(nrConfig)

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

return &client, nil
}
65 changes: 65 additions & 0 deletions builtin/providers/newrelic/data_source_newrelic_application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package newrelic

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform/helper/schema"
newrelic "github.com/paultyng/go-newrelic/api"
)

func dataSourceNewRelicApplication() *schema.Resource {
return &schema.Resource{
Read: dataSourceNewRelicApplicationRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"instance_ids": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeInt},
Computed: true,
},
"host_ids": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeInt},
Computed: true,
},
},
}
}

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

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

applications, err := client.ListApplications()
if err != nil {
return err
}

var application *newrelic.Application
name := d.Get("name").(string)

for _, a := range applications {
if a.Name == name {
application = &a
break
}
}

if application == nil {
return fmt.Errorf("The name '%s' does not match any New Relic applications.", name)
}

d.SetId(strconv.Itoa(application.ID))
d.Set("name", application.Name)
d.Set("instance_ids", application.Links.InstanceIDs)
d.Set("host_ids", application.Links.HostIDs)

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package newrelic

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccNewRelicApplication_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccNewRelicApplicationConfig(),
Check: resource.ComposeTestCheckFunc(
testAccNewRelicApplication("data.newrelic_application.app"),
),
},
},
})
}

func testAccNewRelicApplication(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r := s.RootModule().Resources[n]
a := r.Primary.Attributes

if a["id"] == "" {
return fmt.Errorf("Expected to get an application from New Relic")
}

if a["name"] != testAccExpectedApplicationName {
return fmt.Errorf("Expected the application name to be: %s, but got: %s", testAccExpectedApplicationName, a["name"])
}

return nil
}
}

// The test application for this data source is created in provider_test.go
func testAccNewRelicApplicationConfig() string {
return fmt.Sprintf(`
data "newrelic_application" "app" {
name = "%s"
}
`, testAccExpectedApplicationName)
}
37 changes: 37 additions & 0 deletions builtin/providers/newrelic/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package newrelic

import (
"fmt"
"strconv"
"strings"
)

func parseIDs(serializedID string, count int) ([]int, error) {
rawIDs := strings.SplitN(serializedID, ":", count)
if len(rawIDs) != count {
return []int{}, fmt.Errorf("Unable to parse ID %v", serializedID)
}

ids := make([]int, count)

for i, rawID := range rawIDs {
id, err := strconv.ParseInt(rawID, 10, 32)
if err != nil {
return ids, err
}

ids[i] = int(id)
}

return ids, nil
}

func serializeIDs(ids []int) string {
idStrings := make([]string, len(ids))

for i, id := range ids {
idStrings[i] = strconv.Itoa(id)
}

return strings.Join(idStrings, ":")
}
26 changes: 26 additions & 0 deletions builtin/providers/newrelic/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package newrelic

import "testing"

func TestParseIDs_Basic(t *testing.T) {
ids, err := parseIDs("1:2", 2)
if err != nil {
t.Fatal(err)
}

if len(ids) != 2 {
t.Fatal(len(ids))
}

if ids[0] != 1 || ids[1] != 2 {
t.Fatal(ids)
}
}

func TestSerializeIDs_Basic(t *testing.T) {
id := serializeIDs([]int{1, 2})

if id != "1:2" {
t.Fatal(id)
}
}
29 changes: 29 additions & 0 deletions builtin/providers/newrelic/import_newrelic_alert_channel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package newrelic

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccNewRelicAlertChannel_import(t *testing.T) {
resourceName := "newrelic_alert_channel.foo"
rName := acctest.RandString(5)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicAlertChannelDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckNewRelicAlertChannelConfig(rName),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
30 changes: 30 additions & 0 deletions builtin/providers/newrelic/import_newrelic_alert_condition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package newrelic

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccNewRelicAlertCondition_import(t *testing.T) {
resourceName := "newrelic_alert_condition.foo"
rName := acctest.RandString(5)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicAlertConditionDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckNewRelicAlertConditionConfig(rName),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
30 changes: 30 additions & 0 deletions builtin/providers/newrelic/import_newrelic_alert_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package newrelic

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccNewRelicAlertPolicy_import(t *testing.T) {
resourceName := "newrelic_alert_policy.foo"
rName := acctest.RandString(5)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicAlertPolicyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckNewRelicAlertPolicyConfig(rName),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
49 changes: 49 additions & 0 deletions builtin/providers/newrelic/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package newrelic

import (
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

// Provider represents a resource provider in Terraform
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"api_key": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("NEWRELIC_API_KEY", nil),
Sensitive: true,
},
"api_url": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NEWRELIC_API_URL", "https://api.newrelic.com/v2"),
},
},

DataSourcesMap: map[string]*schema.Resource{
"newrelic_application": dataSourceNewRelicApplication(),
},

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

ConfigureFunc: providerConfigure,
}
}

func providerConfigure(data *schema.ResourceData) (interface{}, error) {
config := Config{
APIKey: data.Get("api_key").(string),
APIURL: data.Get("api_url").(string),
}
log.Println("[INFO] Initializing New Relic client")
return config.Client()
}
Loading