github.com/GoogleCloudPlatform/terraformer@v0.8.18/cmd/provider_cmd_keycloak.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 cmd 16 17 import ( 18 "log" 19 "os" 20 "strconv" 21 "strings" 22 23 keycloak_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/keycloak" 24 25 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 26 "github.com/spf13/cobra" 27 ) 28 29 const ( 30 defaultKeycloakEndpoint = "https://localhost:8443" 31 defaultKeycloakRealm = "master" 32 defaultKeycloakClientTimeout = int64(30) 33 defaultKeycloakTLSInsecureSkipVerify = false 34 ) 35 36 func newCmdKeycloakImporter(options ImportOptions) *cobra.Command { 37 targets := []string{} 38 cmd := &cobra.Command{ 39 Use: "keycloak", 40 Short: "Import current state to Terraform configuration from Keycloak", 41 Long: "Import current state to Terraform configuration from Keycloak", 42 RunE: func(cmd *cobra.Command, args []string) error { 43 url := os.Getenv("KEYCLOAK_URL") 44 if len(url) == 0 { 45 url = defaultKeycloakEndpoint 46 } 47 clientID := os.Getenv("KEYCLOAK_CLIENT_ID") 48 clientSecret := os.Getenv("KEYCLOAK_CLIENT_SECRET") 49 realm := os.Getenv("KEYCLOAK_REALM") 50 if len(realm) == 0 { 51 realm = defaultKeycloakRealm 52 } 53 clientTimeout, err := strconv.ParseInt(os.Getenv("KEYCLOAK_CLIENT_TIMEOUT"), 10, 64) 54 if err != nil { 55 clientTimeout = defaultKeycloakClientTimeout 56 } 57 tlsInsecureSkipVerify, err := strconv.ParseBool(os.Getenv("KEYCLOAK_TLS_INSECURE_SKIP_VERIFY")) 58 if err != nil { 59 tlsInsecureSkipVerify = defaultKeycloakTLSInsecureSkipVerify 60 } 61 caCert := os.Getenv("KEYCLOAK_CACERT") 62 if len(caCert) == 0 { 63 caCert = "-" 64 } 65 if len(targets) > 0 { 66 originalPathPattern := options.PathPattern 67 for _, target := range targets { 68 provider := newKeycloakProvider() 69 log.Println(provider.GetName() + " importing realm " + target) 70 options.PathPattern = originalPathPattern 71 options.PathPattern = strings.ReplaceAll(options.PathPattern, "{provider}", "{provider}/"+target) 72 err := Import(provider, options, []string{url, clientID, clientSecret, realm, strconv.FormatInt(clientTimeout, 10), caCert, strconv.FormatBool(tlsInsecureSkipVerify), target}) 73 if err != nil { 74 return err 75 } 76 } 77 } else { 78 provider := newKeycloakProvider() 79 log.Println(provider.GetName() + " importing all realms") 80 err := Import(provider, options, []string{url, clientID, clientSecret, realm, strconv.FormatInt(clientTimeout, 10), caCert, strconv.FormatBool(tlsInsecureSkipVerify), "-"}) 81 if err != nil { 82 return err 83 } 84 } 85 return nil 86 }, 87 } 88 89 cmd.AddCommand(listCmd(newKeycloakProvider())) 90 baseProviderFlags(cmd.PersistentFlags(), &options, "realms", "type=id1:id2:id4") 91 cmd.PersistentFlags().StringSliceVarP(&targets, "targets", "", []string{}, "") 92 return cmd 93 } 94 95 func newKeycloakProvider() terraformutils.ProviderGenerator { 96 return &keycloak_terraforming.KeycloakProvider{} 97 }