Skip to content

Commit

Permalink
fix newrelic v2
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeylanzman committed Apr 3, 2021
1 parent 7f0f4d8 commit 126de79
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 33 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Links to download Terraform Providers:
* GitHub provider >=2.2.1 - [here](https://releases.hashicorp.com/terraform-provider-github/)
* Monitoring & System Management
* Datadog provider >2.1.0 - [here](https://releases.hashicorp.com/terraform-provider-datadog/)
* New Relic provider >1.5.0 - [here](https://releases.hashicorp.com/terraform-provider-newrelic/)
* New Relic provider >2.0.0 - [here](https://releases.hashicorp.com/terraform-provider-newrelic/)
* Community
* Keycloak provider >=1.19.0 - [here](https://github.com/mrparkers/terraform-provider-keycloak/)
* Logz.io provider >=1.1.1 - [here](https://github.com/jonboydell/logzio_terraform_provider/)
Expand Down Expand Up @@ -1593,8 +1593,7 @@ List of supported Datadog services:
Example:
```
NEWRELIC_API_KEY=[API-KEY]
./terraformer import newrelic -r alert,dashboard,infra,synthetics
./terraformer import newrelic -r alert,dashboard,infra,synthetics --api-key=NRAK-XXXXXXXX --account-id=XXXXX
```
List of supported New Relic resources:
Expand Down
8 changes: 7 additions & 1 deletion cmd/provider_cmd_newrelic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ import (
)

func newCmdNewRelicImporter(options ImportOptions) *cobra.Command {
apiKey := ""
accountID := ""
region := ""
cmd := &cobra.Command{
Use: "newrelic",
Short: "Import current state to Terraform configuration from New Relic",
Long: "Import current state to Terraform configuration from New Relic",
RunE: func(cmd *cobra.Command, args []string) error {
provider := newNewRelicProvider()
err := Import(provider, options, []string{})
err := Import(provider, options, []string{apiKey, accountID, region})
if err != nil {
return err
}
Expand All @@ -36,6 +39,9 @@ func newCmdNewRelicImporter(options ImportOptions) *cobra.Command {
}

cmd.AddCommand(listCmd(newNewRelicProvider()))
cmd.PersistentFlags().StringVar(&apiKey, "api-key", "", "Your Personal API Key")
cmd.PersistentFlags().StringVar(&accountID, "account-id", "", "Your Account ID")
cmd.PersistentFlags().StringVar(&region, "region", "US", "")
baseProviderFlags(cmd.PersistentFlags(), &options, "alert", "dashboard=id1:id2:id4")
return cmd
}
Expand Down
42 changes: 42 additions & 0 deletions providers/newrelic/newrelic_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,63 @@ package newrelic

import (
"errors"
"os"
"strconv"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/zclconf/go-cty/cty"
)

type NewRelicProvider struct { //nolint
terraformutils.Provider
accountID int
APIKey string
Region string
}

func (p *NewRelicProvider) Init(args []string) error {
if apiKey := os.Getenv("NEW_RELIC_API_KEY"); apiKey != "" {
p.APIKey = os.Getenv("NEW_RELIC_API_KEY")
}
if accountIDs := os.Getenv("NEW_RELIC_ACCOUNT_ID"); accountIDs != "" {
accountID, err := strconv.Atoi(accountIDs)
if err != nil {
return err

}
p.accountID = accountID
}
if len(args) > 0 {
p.APIKey = args[0]
}
if len(args) > 1 {
accountID, err := strconv.Atoi(args[1])
if err != nil {
return err
}
p.accountID = accountID
}
if len(args) > 1 {
p.Region = args[2]
}
if p.Region == "" {
p.Region = "US"
}
return nil
}

func (p *NewRelicProvider) GetName() string {
return "newrelic"
}

func (p *NewRelicProvider) GetConfig() cty.Value {
return cty.ObjectVal(map[string]cty.Value{
"account_id": cty.NumberIntVal(int64(p.accountID)),
"api_key": cty.StringVal(p.APIKey),
"region": cty.StringVal(p.Region),
})
}

func (p *NewRelicProvider) GetProviderData(arg ...string) map[string]interface{} {
return map[string]interface{}{}
}
Expand All @@ -57,6 +98,7 @@ func (p *NewRelicProvider) InitService(serviceName string, verbose bool) error {
p.Service = p.GetSupportedService()[serviceName]
p.Service.SetName(serviceName)
p.Service.SetVerbose(verbose)
p.Service.SetArgs(map[string]interface{}{"apiKey": p.APIKey})
p.Service.SetProviderName(p.GetName())

return nil
Expand Down
32 changes: 3 additions & 29 deletions providers/newrelic/newrelic_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package newrelic

import (
"errors"
"os"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
synthetics "github.com/dollarshaveclub/new-relic-synthetics-go"
newrelic "github.com/paultyng/go-newrelic/v4/api"
Expand All @@ -28,41 +25,18 @@ type NewRelicService struct { //nolint
}

func (s *NewRelicService) Client() (*newrelic.Client, error) {
apiKey := os.Getenv("NEWRELIC_API_KEY")

if apiKey == "" {
err := errors.New("No NEWRELIC_API_KEY environment set")
return nil, err
}

client := newrelic.New(newrelic.Config{APIKey: apiKey})
client := newrelic.New(newrelic.Config{APIKey: s.GetArgs()["apiKey"].(string)})
return &client, nil
}

func (s *NewRelicService) InfraClient() (*newrelic.InfraClient, error) {
apiKey := os.Getenv("NEWRELIC_API_KEY")

if apiKey == "" {
err := errors.New("No NEWRELIC_API_KEY environment set")
return nil, err
}

client := newrelic.NewInfraClient(newrelic.Config{APIKey: apiKey, Debug: s.Verbose})

client := newrelic.NewInfraClient(newrelic.Config{APIKey: s.GetArgs()["apiKey"].(string), Debug: s.Verbose})
return &client, nil
}

func (s *NewRelicService) SyntheticsClient() (*synthetics.Client, error) {
apiKey := os.Getenv("NEWRELIC_API_KEY")

if apiKey == "" {
err := errors.New("No NEWRELIC_API_KEY environment set")
return nil, err
}

conf := func(c *synthetics.Client) {
c.APIKey = apiKey
c.APIKey = s.GetArgs()["apiKey"].(string)
}

return synthetics.NewClient(conf)
}

0 comments on commit 126de79

Please sign in to comment.