github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/vault/vault_provider.go (about) 1 package vault 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 8 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 9 "github.com/zclconf/go-cty/cty" 10 ) 11 12 type Provider struct { 13 terraformutils.Provider 14 token string 15 address string 16 } 17 18 func (p *Provider) Init(args []string) error { 19 20 if address := os.Getenv("VAULT_ADDR"); address != "" { 21 p.address = os.Getenv("VAULT_ADDR") 22 } 23 24 if token := os.Getenv("VAULT_TOKEN"); token != "" { 25 p.token = os.Getenv("VAULT_TOKEN") 26 } 27 28 if len(args) > 0 && args[0] != "" { 29 p.address = args[0] 30 } 31 32 if len(args) > 1 && args[1] != "" { 33 p.token = args[1] 34 } 35 36 return nil 37 } 38 39 func (p *Provider) GetConfig() cty.Value { 40 return cty.ObjectVal(map[string]cty.Value{ 41 "token": cty.StringVal(p.token), 42 "address": cty.StringVal(p.address), 43 }) 44 } 45 46 func (p *Provider) GetName() string { 47 return "vault" 48 } 49 50 func (p *Provider) InitService(serviceName string, verbose bool) error { 51 if service, ok := p.GetSupportedService()[serviceName]; ok { 52 p.Service = service 53 p.Service.SetName(serviceName) 54 p.Service.SetVerbose(verbose) 55 p.Service.SetProviderName(p.GetName()) 56 p.Service.SetArgs(map[string]interface{}{ 57 "token": p.token, 58 "address": p.address, 59 }) 60 if err := service.(*ServiceGenerator).setVaultClient(); err != nil { 61 return err 62 } 63 return nil 64 } 65 return errors.New(p.GetName() + ": " + serviceName + " not supported service") 66 } 67 68 func getSupportedMountServices() map[string]terraformutils.ServiceGenerator { 69 services := make(map[string]terraformutils.ServiceGenerator) 70 mapping := map[string][]string{ 71 "secret_backend": {"ad", "aws", "azure", "consul", "gcp", "nomad", "pki", "rabbitmq", "terraform_cloud"}, 72 "secret_backend_role": {"ad", "aws", "azure", "consul", "database", "pki", "rabbitmq", "ssh"}, 73 "auth_backend": {"gcp", "github", "jwt", "ldap", "okta"}, 74 "auth_backend_role": {"alicloud", "approle", "aws", "azure", "cert", "gcp", "jwt", "kubernetes", "token"}, 75 "auth_backend_user": {"ldap", "okta"}, 76 "auth_backend_group": {"ldap", "okta"}, 77 } 78 for resource, mountTypes := range mapping { 79 for _, mountType := range mountTypes { 80 services[fmt.Sprintf("%s_%s", mountType, resource)] = 81 &ServiceGenerator{mountType: mountType, resource: resource} 82 } 83 } 84 return services 85 } 86 87 func (p *Provider) GetSupportedService() map[string]terraformutils.ServiceGenerator { 88 generators := getSupportedMountServices() 89 generators["policy"] = &ServiceGenerator{resource: "policy"} 90 generators["mount"] = &ServiceGenerator{resource: "mount"} 91 generators["generic_secret"] = &ServiceGenerator{resource: "generic_secret", mountType: "kv"} 92 return generators 93 } 94 95 func (Provider) GetResourceConnections() map[string]map[string][]string { 96 return map[string]map[string][]string{} 97 } 98 99 func (Provider) GetProviderData(_ ...string) map[string]interface{} { 100 return map[string]interface{}{} 101 }