github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/keycloak/keycloak_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 keycloak
    16  
    17  import (
    18  	"errors"
    19  	"strconv"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/zclconf/go-cty/cty"
    23  )
    24  
    25  type KeycloakProvider struct { //nolint
    26  	terraformutils.Provider
    27  	url                   string
    28  	clientID              string
    29  	clientSecret          string
    30  	realm                 string
    31  	clientTimeout         int
    32  	caCert                string
    33  	tlsInsecureSkipVerify bool
    34  	target                string
    35  }
    36  
    37  func getArg(arg string) string {
    38  	if arg == "-" {
    39  		return ""
    40  	}
    41  	return arg
    42  }
    43  
    44  func (p *KeycloakProvider) Init(args []string) error {
    45  	p.url = args[0]
    46  	p.clientID = args[1]
    47  	p.clientSecret = args[2]
    48  	p.realm = args[3]
    49  	p.clientTimeout, _ = strconv.Atoi(args[4])
    50  	p.caCert = getArg(args[5])
    51  	p.tlsInsecureSkipVerify, _ = strconv.ParseBool(args[6])
    52  	p.target = getArg(args[7])
    53  	return nil
    54  }
    55  
    56  func (p *KeycloakProvider) GetName() string {
    57  	return "keycloak"
    58  }
    59  
    60  func (p *KeycloakProvider) GetProviderData(arg ...string) map[string]interface{} {
    61  	return map[string]interface{}{}
    62  }
    63  
    64  func (p *KeycloakProvider) GetConfig() cty.Value {
    65  	return cty.ObjectVal(map[string]cty.Value{
    66  		"url":                      cty.StringVal(p.url),
    67  		"client_id":                cty.StringVal(p.clientID),
    68  		"client_secret":            cty.StringVal(p.clientSecret),
    69  		"realm":                    cty.StringVal(p.realm),
    70  		"client_timeout":           cty.NumberIntVal(int64(p.clientTimeout)),
    71  		"root_ca_certificate":      cty.StringVal(p.caCert),
    72  		"tls_insecure_skip_verify": cty.BoolVal(p.tlsInsecureSkipVerify),
    73  	})
    74  }
    75  
    76  func (p *KeycloakProvider) GetBasicConfig() cty.Value {
    77  	return p.GetConfig()
    78  }
    79  
    80  func (p *KeycloakProvider) InitService(serviceName string, verbose bool) error {
    81  	var isSupported bool
    82  	if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported {
    83  		return errors.New(p.GetName() + ": " + serviceName + " not supported service")
    84  	}
    85  	p.Service = p.GetSupportedService()[serviceName]
    86  	p.Service.SetName(serviceName)
    87  	p.Service.SetVerbose(verbose)
    88  	p.Service.SetProviderName(p.GetName())
    89  	p.Service.SetArgs(map[string]interface{}{
    90  		"url":                      p.url,
    91  		"client_id":                p.clientID,
    92  		"client_secret":            p.clientSecret,
    93  		"realm":                    p.realm,
    94  		"client_timeout":           p.clientTimeout,
    95  		"root_ca_certificate":      p.caCert,
    96  		"tls_insecure_skip_verify": p.tlsInsecureSkipVerify,
    97  		"target":                   p.target,
    98  	})
    99  	return nil
   100  }
   101  
   102  func (p *KeycloakProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator {
   103  	return map[string]terraformutils.ServiceGenerator{
   104  		"realms": &RealmGenerator{},
   105  	}
   106  }
   107  
   108  func (KeycloakProvider) GetResourceConnections() map[string]map[string][]string {
   109  	return map[string]map[string][]string{}
   110  }