github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/newrelic/newrelic_provider.go (about)

     1  // Copyright 2019 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package newrelic
    16  
    17  import (
    18  	"errors"
    19  	"os"
    20  	"strconv"
    21  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  	"github.com/zclconf/go-cty/cty"
    24  )
    25  
    26  type NewRelicProvider struct { //nolint
    27  	terraformutils.Provider
    28  	accountID int
    29  	APIKey    string
    30  	Region    string
    31  }
    32  
    33  func (p *NewRelicProvider) Init(args []string) error {
    34  	if apiKey := os.Getenv("NEW_RELIC_API_KEY"); apiKey != "" {
    35  		p.APIKey = os.Getenv("NEW_RELIC_API_KEY")
    36  	}
    37  	if accountIDs := os.Getenv("NEW_RELIC_ACCOUNT_ID"); accountIDs != "" {
    38  		accountID, err := strconv.Atoi(accountIDs)
    39  		if err != nil {
    40  			return err
    41  
    42  		}
    43  		p.accountID = accountID
    44  	}
    45  	if len(args) > 0 {
    46  		p.APIKey = args[0]
    47  	}
    48  	if len(args) > 1 {
    49  		accountID, err := strconv.Atoi(args[1])
    50  		if err != nil {
    51  			return err
    52  		}
    53  		p.accountID = accountID
    54  	}
    55  	if len(args) > 1 {
    56  		p.Region = args[2]
    57  	}
    58  	if p.Region == "" {
    59  		p.Region = "US"
    60  	}
    61  	return nil
    62  }
    63  
    64  func (p *NewRelicProvider) GetName() string {
    65  	return "newrelic"
    66  }
    67  
    68  func (p *NewRelicProvider) GetConfig() cty.Value {
    69  	return cty.ObjectVal(map[string]cty.Value{
    70  		"account_id": cty.NumberIntVal(int64(p.accountID)),
    71  		"api_key":    cty.StringVal(p.APIKey),
    72  		"region":     cty.StringVal(p.Region),
    73  	})
    74  }
    75  
    76  func (p *NewRelicProvider) GetProviderData(arg ...string) map[string]interface{} {
    77  	return map[string]interface{}{}
    78  }
    79  
    80  func (NewRelicProvider) GetResourceConnections() map[string]map[string][]string {
    81  	return map[string]map[string][]string{}
    82  }
    83  
    84  func (p *NewRelicProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator {
    85  	return map[string]terraformutils.ServiceGenerator{
    86  		"alert":      &AlertGenerator{},
    87  		"dashboard":  &DashboardGenerator{},
    88  		"infra":      &InfraGenerator{},
    89  		"synthetics": &SyntheticsGenerator{},
    90  	}
    91  }
    92  
    93  func (p *NewRelicProvider) InitService(serviceName string, verbose bool) error {
    94  	var isSupported bool
    95  	if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported {
    96  		return errors.New("newrelic: " + serviceName + " not supported service")
    97  	}
    98  	p.Service = p.GetSupportedService()[serviceName]
    99  	p.Service.SetName(serviceName)
   100  	p.Service.SetVerbose(verbose)
   101  	p.Service.SetArgs(map[string]interface{}{"apiKey": p.APIKey})
   102  	p.Service.SetProviderName(p.GetName())
   103  
   104  	return nil
   105  }