From 0fe0998a26ea7fd74bebf44589324b7c967035ed Mon Sep 17 00:00:00 2001 From: Breee Date: Mon, 8 Jul 2024 10:06:45 +0200 Subject: [PATCH] feat(resolvers): add transformer for non-cross-reference resolvers --- cmd/provider/main.go | 2 ++ config/authentication/config.go | 18 +++++++------- config/defaults/config.go | 6 ++--- config/group/config.go | 8 +++---- config/identityprovider/config.go | 2 +- config/ldap/config.go | 28 +++++++++++----------- config/mapper/config.go | 4 ++-- config/oidc/config.go | 2 +- config/openidclient/config.go | 26 +++++++++++++++++++-- config/openidgroup/config.go | 2 +- config/provider.go | 16 ++++++------- config/role/config.go | 2 +- config/saml/config.go | 2 +- config/user/config.go | 8 +++---- internal/apis/scheme.go | 39 +++++++++++++++++++++++++++++++ 15 files changed, 113 insertions(+), 52 deletions(-) create mode 100644 internal/apis/scheme.go diff --git a/cmd/provider/main.go b/cmd/provider/main.go index 7aa0ce9e..bae30639 100644 --- a/cmd/provider/main.go +++ b/cmd/provider/main.go @@ -32,6 +32,7 @@ import ( "github.com/crossplane-contrib/provider-keycloak/apis" "github.com/crossplane-contrib/provider-keycloak/apis/v1alpha1" "github.com/crossplane-contrib/provider-keycloak/config" + resolverapis "github.com/crossplane-contrib/provider-keycloak/internal/apis" "github.com/crossplane-contrib/provider-keycloak/internal/clients" "github.com/crossplane-contrib/provider-keycloak/internal/controller" "github.com/crossplane-contrib/provider-keycloak/internal/features" @@ -88,6 +89,7 @@ func main() { metrics.Registry.MustRegister(metricRecorder) metrics.Registry.MustRegister(stateMetrics) kingpin.FatalIfError(apis.AddToScheme(mgr.GetScheme()), "Cannot add keycloak APIs to scheme") + kingpin.FatalIfError(resolverapis.BuildScheme(apis.AddToSchemes), "Cannot register the keycloak APIs with the API resolver's runtime scheme") provider, err := config.GetProvider(false) kingpin.FatalIfError(err, "Cannot get provider configuration") diff --git a/config/authentication/config.go b/config/authentication/config.go index 0962b3e0..4218653f 100644 --- a/config/authentication/config.go +++ b/config/authentication/config.go @@ -18,7 +18,7 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_authentication_subflow", func(r *config.Resource) { r.ShortGroup = Group r.References["parent_flow_alias"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", + TerraformName: "keycloak_authentication_flow", Extractor: common.PathAuthenticationFlowAliasExtractor, RefFieldName: "ParentFlowAliasRef", SelectorFieldName: "ParentFlowAliasSelector", @@ -27,7 +27,7 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_authentication_execution", func(r *config.Resource) { r.ShortGroup = Group r.References["parent_flow_alias"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", + TerraformName: "keycloak_authentication_flow", Extractor: common.PathAuthenticationFlowAliasExtractor, RefFieldName: "ParentFlowAliasRef", SelectorFieldName: "ParentFlowAliasSelector", @@ -36,43 +36,43 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_authentication_execution_config", func(r *config.Resource) { r.ShortGroup = Group r.References["execution_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Execution", + TerraformName: "keycloak_authentication_execution", } }) p.AddResourceConfigurator("keycloak_authentication_bindings", func(r *config.Resource) { r.ShortGroup = Group r.References["browser_flow"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", + TerraformName: "keycloak_authentication_flow", Extractor: common.PathAuthenticationFlowAliasExtractor, RefFieldName: "BrowserFlowRef", SelectorFieldName: "BrowserFlowSelector", } r.References["registration_flow"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", + TerraformName: "keycloak_authentication_flow", Extractor: common.PathAuthenticationFlowAliasExtractor, RefFieldName: "RegistrationFlowRef", SelectorFieldName: "RegistrationFlowSelector", } r.References["direct_grant_flow"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", + TerraformName: "keycloak_authentication_flow", Extractor: common.PathAuthenticationFlowAliasExtractor, RefFieldName: "DirectGrantFlowRef", SelectorFieldName: "DirectGrantFlowSelector", } r.References["reset_credentials_flow"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", + TerraformName: "keycloak_authentication_flow", Extractor: common.PathAuthenticationFlowAliasExtractor, RefFieldName: "ResetCredentialsFlowRef", SelectorFieldName: "ResetCredentialsFlowSelector", } r.References["client_authentication_flow"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", + TerraformName: "keycloak_authentication_flow", Extractor: common.PathAuthenticationFlowAliasExtractor, RefFieldName: "ClientAuthenticationFlowRef", SelectorFieldName: "ClientAuthenticationFlowSelector", } r.References["docker_authentication_flow"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", + TerraformName: "keycloak_authentication_flow", Extractor: common.PathAuthenticationFlowAliasExtractor, RefFieldName: "DockerAuthenticationFlowRef", SelectorFieldName: "DockerAuthenticationFlowSelector", diff --git a/config/defaults/config.go b/config/defaults/config.go index eb1792b4..90921576 100644 --- a/config/defaults/config.go +++ b/config/defaults/config.go @@ -8,8 +8,8 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_default_roles", func(r *config.Resource) { r.ShortGroup = "defaults" r.References["default_roles"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/role/v1alpha1.Role", - Extractor: `github.com/crossplane/upjet/pkg/resource.ExtractParamPath("name", false)`, + TerraformName: "keycloak_role", + Extractor: `github.com/crossplane/upjet/pkg/resource.ExtractParamPath("name", false)`, } }) @@ -20,7 +20,7 @@ func Configure(p *config.Provider) { r.ShortGroup = "defaults" r.Kind = "DefaultGroups" r.References["group_ids"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/group/v1alpha1.Group", + TerraformName: "keycloak_group", } }) diff --git a/config/group/config.go b/config/group/config.go index 6420542a..32bc659b 100644 --- a/config/group/config.go +++ b/config/group/config.go @@ -9,14 +9,14 @@ func Configure(p *config.Provider) { r.ShortGroup = "group" r.References["parent_id"] = config.Reference{ - Type: "Group", + TerraformName: "keycloak_group", } }) p.AddResourceConfigurator("keycloak_group_memberships", func(r *config.Resource) { // We need to override the default group that upjet generated for r.ShortGroup = "group" r.References["group_id"] = config.Reference{ - Type: "Group", + TerraformName: "keycloak_group", } }) @@ -24,14 +24,14 @@ func Configure(p *config.Provider) { // We need to override the default group that upjet generated for r.ShortGroup = "group" r.References["group_id"] = config.Reference{ - Type: "Group", + TerraformName: "keycloak_group", } }) p.AddResourceConfigurator("keycloak_group_permissions", func(r *config.Resource) { // We need to override the default group that upjet generated for r.ShortGroup = "group" r.References["group_id"] = config.Reference{ - Type: "Group", + TerraformName: "keycloak_group", } }) } diff --git a/config/identityprovider/config.go b/config/identityprovider/config.go index 2ff82b1e..2d1ccac7 100644 --- a/config/identityprovider/config.go +++ b/config/identityprovider/config.go @@ -12,7 +12,7 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_custom_identity_provider_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["realm"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/realm/v1alpha1.Realm", + TerraformName: "keycloak_realm", } }) } diff --git a/config/ldap/config.go b/config/ldap/config.go index 2c3d38c3..22f983cf 100644 --- a/config/ldap/config.go +++ b/config/ldap/config.go @@ -16,21 +16,21 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_ldap_user_attribute_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["ldap_user_federation_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/ldap/v1alpha1.UserFederation", + TerraformName: "keycloak_ldap_user_federation", } }) p.AddResourceConfigurator("keycloak_ldap_role_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["ldap_user_federation_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/ldap/v1alpha1.UserFederation", + TerraformName: "keycloak_ldap_user_federation", } }) p.AddResourceConfigurator("keycloak_ldap_group_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["ldap_user_federation_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/ldap/v1alpha1.UserFederation", + TerraformName: "keycloak_ldap_user_federation", } }) @@ -38,11 +38,11 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_ldap_hardcoded_role_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["ldap_user_federation_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/ldap/v1alpha1.UserFederation", + TerraformName: "keycloak_ldap_user_federation", } r.References["role"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/role/v1alpha1.Role", - Extractor: `github.com/crossplane/upjet/pkg/resource.ExtractParamPath("name", false)`, + TerraformName: "keycloak_role", + Extractor: `github.com/crossplane/upjet/pkg/resource.ExtractParamPath("name", false)`, } }) @@ -50,18 +50,18 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_ldap_hardcoded_group_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["ldap_user_federation_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/ldap/v1alpha1.UserFederation", + TerraformName: "keycloak_ldap_user_federation", } r.References["group"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/group/v1alpha1.Group", - Extractor: `github.com/crossplane/upjet/pkg/resource.ExtractParamPath("name", false)`, + TerraformName: "keycloak_group", + Extractor: `github.com/crossplane/upjet/pkg/resource.ExtractParamPath("name", false)`, } }) p.AddResourceConfigurator("keycloak_ldap_msad_user_account_control_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["ldap_user_federation_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/ldap/v1alpha1.UserFederation", + TerraformName: "keycloak_ldap_user_federation", } }) @@ -69,7 +69,7 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_ldap_msad_lds_user_account_control_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["ldap_user_federation_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/ldap/v1alpha1.UserFederation", + TerraformName: "keycloak_ldap_user_federation", } }) @@ -77,7 +77,7 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_ldap_hardcoded_attribute_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["ldap_user_federation_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/ldap/v1alpha1.UserFederation", + TerraformName: "keycloak_ldap_user_federation", } }) @@ -85,14 +85,14 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_ldap_full_name_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["ldap_user_federation_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/ldap/v1alpha1.UserFederation", + TerraformName: "keycloak_ldap_user_federation", } }) p.AddResourceConfigurator("keycloak_ldap_custom_mapper", func(r *config.Resource) { r.ShortGroup = Group r.References["ldap_user_federation_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/ldap/v1alpha1.UserFederation", + TerraformName: "keycloak_ldap_user_federation", } }) } diff --git a/config/mapper/config.go b/config/mapper/config.go index acb08a42..be39b074 100644 --- a/config/mapper/config.go +++ b/config/mapper/config.go @@ -7,14 +7,14 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_generic_protocol_mapper", func(r *config.Resource) { r.ShortGroup = "client" r.References["client_scope_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/openidclient/v1alpha1.ClientScope", + TerraformName: "keycloak_openid_client_scope", } }) p.AddResourceConfigurator("keycloak_generic_role_mapper", func(r *config.Resource) { r.ShortGroup = "client" r.References["role_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/role/v1alpha1.Role", + TerraformName: "keycloak_role", } }) diff --git a/config/oidc/config.go b/config/oidc/config.go index 07cbe593..41118bcb 100644 --- a/config/oidc/config.go +++ b/config/oidc/config.go @@ -11,7 +11,7 @@ func Configure(p *config.Provider) { // We need to override the default group that upjet generated for r.ShortGroup = "oidc" r.References["realm"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/realm/v1alpha1.Realm", + TerraformName: "keycloak_realm", } r.References["first_broker_login_flow_alias"] = config.Reference{ Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", diff --git a/config/openidclient/config.go b/config/openidclient/config.go index 94278684..35362a1f 100644 --- a/config/openidclient/config.go +++ b/config/openidclient/config.go @@ -1,6 +1,7 @@ package openidclient import ( + "github.com/crossplane-contrib/provider-keycloak/config/common" "github.com/crossplane/upjet/pkg/config" ) @@ -16,10 +17,10 @@ func Configure(p *config.Provider) { r.ShortGroup = Group r.References["authentication_flow_binding_overrides.browser_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", + TerraformName: "keycloak_authentication_flow", } r.References["authentication_flow_binding_overrides.direct_grant_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/authenticationflow/v1alpha1.Flow", + TerraformName: "keycloak_authentication_flow", } }) @@ -36,6 +37,27 @@ func Configure(p *config.Provider) { p.AddResourceConfigurator("keycloak_openid_client_service_account_role", func(r *config.Resource) { r.ShortGroup = Group + // The id of the client that provides the role. + r.References["client_id"] = config.Reference{ + + TerraformName: "keycloak_openid_client", + Extractor: common.PathUUIDExtractor, + } + // The id of the service account that is assigned the role (the service account of the client that "consumes" the role). + r.References["service_account_user_id"] = config.Reference{ + TerraformName: "keycloak_openid_client", + Extractor: common.PathServiceAccountRoleIDExtractor, + RefFieldName: "ServiceAccountUserClientIDRef", + SelectorFieldName: "ServiceAccountUserClientIDSelector", + } + // The name of the role that is assigned. + r.References["role"] = config.Reference{ + TerraformName: "keycloak_role", + Extractor: `github.com/crossplane/upjet/pkg/resource.ExtractParamPath("name", false)`, + } + r.LateInitializer = config.LateInitializer{ + IgnoredFields: []string{"service_account_user_id"}, + } }) diff --git a/config/openidgroup/config.go b/config/openidgroup/config.go index 6f2e2747..24cd1156 100644 --- a/config/openidgroup/config.go +++ b/config/openidgroup/config.go @@ -9,7 +9,7 @@ func Configure(p *config.Provider) { r.ShortGroup = "openidgroup" r.References["client_scope_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/openidclient/v1alpha1.ClientScope", + TerraformName: "keycloak_openid_client_scope", } }) } diff --git a/config/provider.go b/config/provider.go index 13364018..c515ffc6 100644 --- a/config/provider.go +++ b/config/provider.go @@ -128,16 +128,16 @@ func KnownReferencers() config.ResourceOption { //nolint:gocyclo switch k { case "realm_id": r.References["realm_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/realm/v1alpha1.Realm", + TerraformName: "keycloak_realm", } case "client_id": r.References["client_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/openidclient/v1alpha1.Client", - Extractor: common.PathUUIDExtractor, + TerraformName: "keycloak_openid_client", + Extractor: common.PathUUIDExtractor, } case "service_account_user_id": r.References["service_account_user_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/openidclient/v1alpha1.Client", + TerraformName: "keycloak_openid_client", Extractor: common.PathServiceAccountRoleIDExtractor, RefFieldName: "ServiceAccountUserClientIDRef", SelectorFieldName: "ServiceAccountUserClientIDSelector", @@ -148,14 +148,14 @@ func KnownReferencers() config.ResourceOption { //nolint:gocyclo case "role_ids": r.References["role_ids"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/role/v1alpha1.Role", - Extractor: common.PathUUIDExtractor, + TerraformName: "keycloak_role", + Extractor: common.PathUUIDExtractor, } case "role_id": r.References["role_id"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/role/v1alpha1.Role", - Extractor: common.PathUUIDExtractor, + TerraformName: "keycloak_role", + Extractor: common.PathUUIDExtractor, } } diff --git a/config/role/config.go b/config/role/config.go index ad37b523..8966cac1 100644 --- a/config/role/config.go +++ b/config/role/config.go @@ -9,7 +9,7 @@ func Configure(p *config.Provider) { // this resource, which would be "github" r.ShortGroup = "role" r.References["composite_roles"] = config.Reference{ - Type: "Role", + TerraformName: "keycloak_role", } }) } diff --git a/config/saml/config.go b/config/saml/config.go index 71c855ec..cf09c35e 100644 --- a/config/saml/config.go +++ b/config/saml/config.go @@ -11,7 +11,7 @@ func Configure(p *config.Provider) { // We need to override the default group that upjet generated for r.ShortGroup = "saml" r.References["realm"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/realm/v1alpha1.Realm", + TerraformName: "keycloak_realm", } }) p.AddResourceConfigurator("keycloak_saml_client", func(r *config.Resource) { diff --git a/config/user/config.go b/config/user/config.go index 2eb7003b..13c83fac 100644 --- a/config/user/config.go +++ b/config/user/config.go @@ -12,19 +12,17 @@ func Configure(p *config.Provider) { r.ShortGroup = "user" r.References["user_id"] = config.Reference{ - Type: "User", + TerraformName: "keycloak_user", } - r.References["group_ids"] = config.Reference{ - Type: "github.com/crossplane-contrib/provider-keycloak/apis/group/v1alpha1.Group", - } + r.References["group_ids"] = config.Reference{} }) p.AddResourceConfigurator("keycloak_user_roles", func(r *config.Resource) { r.ShortGroup = "user" r.References["user_id"] = config.Reference{ - Type: "User", + TerraformName: "keycloak_user", } }) diff --git a/internal/apis/scheme.go b/internal/apis/scheme.go new file mode 100644 index 00000000..2c7dbe24 --- /dev/null +++ b/internal/apis/scheme.go @@ -0,0 +1,39 @@ +// SPDX-FileCopyrightText: 2024 The Crossplane Authors +// +// SPDX-License-Identifier: Apache-2.0 + +package apis + +import ( + xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" + "github.com/pkg/errors" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +var s = runtime.NewScheme() + +// GetManagedResource is Function to eliminate cross references using a transformer scheme +func GetManagedResource(group, version, kind, listKind string) (xpresource.Managed, xpresource.ManagedList, error) { + gv := schema.GroupVersion{ + Group: group, + Version: version, + } + kingGVK := gv.WithKind(kind) + m, err := s.New(kingGVK) + if err != nil { + return nil, nil, errors.Wrapf(err, "failed to get a new API object of GVK %q from the runtime scheme", kingGVK) + } + + listGVK := gv.WithKind(listKind) + l, err := s.New(listGVK) + if err != nil { + return nil, nil, errors.Wrapf(err, "failed to get a new API object list of GVK %q from the runtime scheme", listGVK) + } + return m.(xpresource.Managed), l.(xpresource.ManagedList), nil +} + +// BuildScheme builds the runtime scheme for the Crossplane resources +func BuildScheme(sb runtime.SchemeBuilder) error { + return errors.Wrap(sb.AddToScheme(s), "failed to register the GVKs with the runtime scheme") +}