github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/grafana/grafana_service.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 "crypto/tls" 19 "crypto/x509" 20 "io/ioutil" 21 "net/url" 22 "strings" 23 24 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 25 gapi "github.com/grafana/grafana-api-golang-client" 26 "github.com/hashicorp/go-cleanhttp" 27 "github.com/hashicorp/terraform/helper/logging" 28 ) 29 30 type GrafanaService struct { //nolint 31 terraformutils.Service 32 } 33 34 func (s *GrafanaService) buildClient() (*gapi.Client, error) { 35 auth := strings.SplitN(s.Args["auth"].(string), ":", 2) 36 cli := cleanhttp.DefaultClient() 37 transport := cleanhttp.DefaultTransport() 38 transport.TLSClientConfig = &tls.Config{} 39 40 // TLS Config 41 tlsKey := s.Args["tls_key"].(string) 42 tlsCert := s.Args["tls_cert"].(string) 43 caCert := s.Args["ca_cert"].(string) 44 insecure := s.Args["insecure_skip_verify"].(bool) 45 46 if caCert != "" { 47 ca, err := ioutil.ReadFile(caCert) 48 if err != nil { 49 return nil, err 50 } 51 pool := x509.NewCertPool() 52 pool.AppendCertsFromPEM(ca) 53 transport.TLSClientConfig.RootCAs = pool 54 } 55 56 if tlsKey != "" && tlsCert != "" { 57 cert, err := tls.LoadX509KeyPair(tlsCert, tlsKey) 58 if err != nil { 59 return nil, err 60 } 61 transport.TLSClientConfig.Certificates = []tls.Certificate{cert} 62 } 63 64 if insecure { 65 transport.TLSClientConfig.InsecureSkipVerify = true 66 } 67 68 cli.Transport = logging.NewTransport("Grafana", transport) 69 cfg := gapi.Config{ 70 Client: cli, 71 OrgID: int64(s.Args["org_id"].(int)), 72 } 73 74 if len(auth) == 2 { 75 cfg.BasicAuth = url.UserPassword(auth[0], auth[1]) 76 } else { 77 cfg.APIKey = auth[0] 78 } 79 80 client, err := gapi.New(s.Args["url"].(string), cfg) 81 if err != nil { 82 return nil, err 83 } 84 85 return client, nil 86 }