github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/grafana/grafana_provider.go (about)

     1  // Copyright 2018 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 grafana
    16  
    17  import (
    18  	"os"
    19  	"strconv"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/pkg/errors"
    23  	"github.com/zclconf/go-cty/cty"
    24  )
    25  
    26  type GrafanaProvider struct { //nolint
    27  	terraformutils.Provider
    28  	auth               string
    29  	url                string
    30  	orgID              int
    31  	tlsKey             string
    32  	tlsCert            string
    33  	caCert             string
    34  	insecureSkipVerify bool
    35  }
    36  
    37  func (p GrafanaProvider) GetResourceConnections() map[string]map[string][]string {
    38  	return map[string]map[string][]string{
    39  		"grafana_dashboard": {
    40  			"grafana_folder": []string{"folder", "id"},
    41  		},
    42  	}
    43  }
    44  
    45  func (p GrafanaProvider) GetProviderData(arg ...string) map[string]interface{} {
    46  	return map[string]interface{}{
    47  		"provider": map[string]interface{}{
    48  			"grafana": map[string]interface{}{
    49  				"org_id":               p.orgID,
    50  				"url":                  p.url,
    51  				"auth":                 p.auth,
    52  				"tls_key":              p.tlsKey,
    53  				"tls_cert":             p.tlsCert,
    54  				"ca_cert":              p.caCert,
    55  				"insecure_skip_verify": p.insecureSkipVerify,
    56  			},
    57  		},
    58  	}
    59  }
    60  
    61  func (p *GrafanaProvider) GetConfig() cty.Value {
    62  	return cty.ObjectVal(map[string]cty.Value{
    63  		"org_id":               cty.NumberIntVal(int64(p.orgID)),
    64  		"url":                  cty.StringVal(p.url),
    65  		"auth":                 cty.StringVal(p.auth),
    66  		"tls_key":              cty.StringVal(p.tlsKey),
    67  		"tls_cert":             cty.StringVal(p.tlsCert),
    68  		"ca_cert":              cty.StringVal(p.caCert),
    69  		"insecure_skip_verify": cty.BoolVal(p.insecureSkipVerify),
    70  	})
    71  }
    72  
    73  func (p *GrafanaProvider) Init(args []string) error {
    74  	p.auth = os.Getenv("GRAFANA_AUTH")
    75  	if p.auth == "" {
    76  		return errors.New("Grafana API authentication must be set through `GRAFANA_AUTH` env var, either as an API token or as username:password for HTTP basic auth")
    77  	}
    78  
    79  	p.url = os.Getenv("GRAFANA_URL")
    80  	if p.url == "" {
    81  		return errors.New("Grafana API URL must be set through `GRAFANA_URL` env var")
    82  	}
    83  
    84  	orgID, err := strconv.Atoi(os.Getenv("GRAFANA_ORG_ID"))
    85  	if err != nil {
    86  		orgID = 1
    87  	}
    88  	p.orgID = orgID
    89  
    90  	p.tlsKey = os.Getenv("HTTPS_TLS_KEY")
    91  	p.tlsCert = os.Getenv("HTTPS_TLS_CERT")
    92  	p.caCert = os.Getenv("HTTPS_CA_CERT")
    93  
    94  	if os.Getenv("HTTPS_INSECURE_SKIP_VERIFY") == "1" {
    95  		p.insecureSkipVerify = true
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  func (p *GrafanaProvider) GetName() string {
   102  	return "grafana"
   103  }
   104  
   105  func (p *GrafanaProvider) InitService(serviceName string, verbose bool) error {
   106  	var isSupported bool
   107  	if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported {
   108  		return errors.New(p.GetName() + ": " + serviceName + " not supported service")
   109  	}
   110  
   111  	p.Service = p.GetSupportedService()[serviceName]
   112  	p.Service.SetName(serviceName)
   113  	p.Service.SetVerbose(verbose)
   114  	p.Service.SetProviderName(p.GetName())
   115  	p.Service.SetArgs(map[string]interface{}{
   116  		"org_id":               p.orgID,
   117  		"url":                  p.url,
   118  		"auth":                 p.auth,
   119  		"tls_key":              p.tlsKey,
   120  		"tls_cert":             p.tlsCert,
   121  		"ca_cert":              p.caCert,
   122  		"insecure_skip_verify": p.insecureSkipVerify,
   123  	})
   124  	return nil
   125  }
   126  
   127  func (p *GrafanaProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator {
   128  	return map[string]terraformutils.ServiceGenerator{
   129  		"grafana_dashboard": &DashboardGenerator{},
   130  		"grafana_folder":    &FolderGenerator{},
   131  	}
   132  }