From d89f5ebd465736e6971a21e47aa8f75658b2577d Mon Sep 17 00:00:00 2001 From: Breee Date: Wed, 13 Dec 2023 18:05:48 +0100 Subject: [PATCH 1/2] required go version dynamic Signed-off-by: Breee --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6b7bbbe..0a03dac 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ NPROCS ?= 1 # to half the number of CPU cores. GO_TEST_PARALLEL := $(shell echo $$(( $(NPROCS) / 2 ))) -GO_REQUIRED_VERSION ?= 1.19 +GO_REQUIRED_VERSION ?= $(shell $(GO) version | sed -ne 's/[^0-9]*\(\([0-9]\.\)\{0,4\}[0-9][^.]\).*/\1/p') GO_STATIC_PACKAGES = $(GO_PROJECT)/cmd/provider $(GO_PROJECT)/cmd/generator GO_LDFLAGS += -X $(GO_PROJECT)/internal/version.Version=$(VERSION) GO_SUBDIRS += cmd internal apis From fe25d0f30b6284f63ff877e1dfc7169b38163dd7 Mon Sep 17 00:00:00 2001 From: Breee Date: Wed, 13 Dec 2023 18:26:48 +0100 Subject: [PATCH 2/2] feat(config): completeness and refactoring Signed-off-by: Breee --- internal/clients/keycloak.go | 79 ++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 45 deletions(-) diff --git a/internal/clients/keycloak.go b/internal/clients/keycloak.go index 709a1c5..35b2027 100644 --- a/internal/clients/keycloak.go +++ b/internal/clients/keycloak.go @@ -31,17 +31,24 @@ const ( // https://registry.terraform.io/providers/mrparkers/keycloak/latest/docs#argument-reference // https://registry.terraform.io/providers/mrparkers/keycloak/latest/docs#example-usage-client-credentials-grant // https://registry.terraform.io/providers/mrparkers/keycloak/latest/docs#example-usage-password-grant -const ( - usernameKey = "username" - passwordKey = "password" - clientIDKey = "client_id" - clientSecretKey = "client_secret" - urlKey = "url" - realmKey = "realm" - basePathKey = "base_path" - additionalHeadersKey = "additional_headers" - rootCaCertificateKey = "root_ca_certificate" -) + +var requiredKeycloakConfigKeys = []string{ + "client_id", + "url", +} + +var optionalKeycloakConfigKeys = []string{ + "client_secret", + "username", + "password", + "realm", + "initial_login", + "client_timeout", + "tls_insecure_skip_verify", + "root_ca_certificate", + "base_path", + "additional_headers", +} // TerraformSetupBuilder builds Terraform a terraform.SetupFn function which // returns Terraform provider setup configuration @@ -80,42 +87,24 @@ func TerraformSetupBuilder(version, providerSource, providerVersion string) terr // set provider configuration ps.Configuration = map[string]any{} - // username - if v, ok := creds[usernameKey]; ok { - ps.Configuration[usernameKey] = v - } - // password - if v, ok := creds[passwordKey]; ok { - ps.Configuration[passwordKey] = v - } - // client - if v, ok := creds[clientIDKey]; ok { - ps.Configuration[clientIDKey] = v + // Iterate over the requiredKeycloakConfigKeys, they must be set + for _, key := range requiredKeycloakConfigKeys { + if value, ok := creds[key]; ok { + if !ok { + // Return an error if a required key is missing + return ps, errors.Errorf("required Keycloak configuration key '%s' is missing", key) + } + ps.Configuration[key] = value + } } - // secret - if v, ok := creds[clientSecretKey]; ok { - ps.Configuration[clientSecretKey] = v - } - // url - if v, ok := creds[urlKey]; ok { - ps.Configuration[urlKey] = v - } - // realm - if v, ok := creds[realmKey]; ok { - ps.Configuration[realmKey] = v - } - // basepath - if v, ok := creds[basePathKey]; ok { - ps.Configuration[basePathKey] = v - } - // additional headers - if v, ok := creds[additionalHeadersKey]; ok { - ps.Configuration[additionalHeadersKey] = v - } - // root ca certificate - if v, ok := creds[rootCaCertificateKey]; ok { - ps.Configuration[rootCaCertificateKey] = v + + // Iterate over the optionalKeycloakConfigKeys, they can be set and do not have to be in the creds map + for _, key := range optionalKeycloakConfigKeys { + if value, ok := creds[key]; ok { + ps.Configuration[key] = value + } } + return ps, nil } }