@@ -2,13 +2,14 @@ package provider
22
33import (
44 "fmt"
5+ "regexp"
6+ "strings"
7+ "testing"
8+
59 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
610 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
711 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
812 "github.com/keycloak/terraform-provider-keycloak/keycloak"
9- "regexp"
10- "strings"
11- "testing"
1213)
1314
1415func TestAccKeycloakRole_basicRealm (t * testing.T ) {
@@ -354,7 +355,7 @@ func TestAccKeycloakRole_import(t *testing.T) {
354355 },
355356 {
356357 Config : testKeycloakRole_importClientRole ("view-profile" , "non-existing-client" ),
357- ExpectError : regexp .MustCompile ("openid client with name non-existing-client does not exist" ),
358+ ExpectError : regexp .MustCompile ("openid clientId non-existing-client does not exist in realm " ),
358359 },
359360 {
360361 Config : testKeycloakRole_importClientRole ("non-existing-role" , "account" ),
@@ -843,6 +844,10 @@ func testKeycloakRole_importClientRole(name, clientId string) string {
843844data "keycloak_realm" "realm" {
844845 realm = "%s"
845846}
847+ import {
848+ id = "${data.keycloak_realm.realm.id}/%s"
849+ to = keycloak_openid_client.imported-client
850+ }
846851resource "keycloak_openid_client" "imported-client" {
847852 realm_id = data.keycloak_realm.realm.id
848853 client_id = "%s"
@@ -854,14 +859,18 @@ resource "keycloak_role" "imported-client-role" {
854859 name = "%s"
855860 import = true
856861}
857- ` , testAccRealm .Realm , clientId , name )
862+ ` , testAccRealm .Realm , clientId , clientId , name )
858863}
859864
860865func testKeycloakRole_importAndModifyClientRole (name , clientId , newDescription string ) string {
861866 return fmt .Sprintf (`
862867data "keycloak_realm" "realm" {
863868 realm = "%s"
864869}
870+ import {
871+ id = "${data.keycloak_realm.realm.id}/%s"
872+ to = keycloak_openid_client.imported-client
873+ }
865874resource "keycloak_openid_client" "imported-client" {
866875 realm_id = data.keycloak_realm.realm.id
867876 client_id = "%s"
@@ -874,7 +883,7 @@ resource "keycloak_role" "imported-client-role" {
874883 import = true
875884 description = "%s"
876885}
877- ` , testAccRealm .Realm , clientId , name , newDescription )
886+ ` , testAccRealm .Realm , clientId , clientId , name , newDescription )
878887}
879888
880889type NestedRole struct {
@@ -901,20 +910,23 @@ resource "keycloak_role" "%s" {
901910 importedClientIdRef := fmt .Sprintf ("imported-client-%d" , i )
902911
903912 importedRoles += fmt .Sprintf (`
904- resource "keycloak_openid_client" "%s" {
913+ import {
914+ id = "${data.keycloak_realm.realm.id}/%s" # 1 ClientId
915+ to = keycloak_openid_client.%s # 2 importedClientIdRef
916+ }
917+ resource "keycloak_openid_client" "%s" { # 3 importedClientIdRef
905918 realm_id = data.keycloak_realm.realm.id
906- client_id = "%s"
907- import = true
919+ client_id = "%s" # 4 ClientId
908920 access_type = "PUBLIC"
909921}
910922
911- resource "keycloak_role" "%s" {
923+ resource "keycloak_role" "%s" { # 5 importedRoleRef
912924 realm_id = data.keycloak_realm.realm.id
913- client_id = keycloak_openid_client.%s.id
914- name = "%s"
925+ client_id = keycloak_openid_client.%s.id # 6 importedClientIdRef
926+ name = "%s" # 7 nestedRole.Name
915927 import = true
916928}
917- ` , importedClientIdRef , * nestedRole .ClientId , importedRoleRef , importedClientIdRef , nestedRole .Name )
929+ ` , * nestedRole . ClientId , importedClientIdRef , importedClientIdRef , * nestedRole .ClientId , importedRoleRef , importedClientIdRef , nestedRole .Name )
918930 }
919931
920932 if i != 0 {
0 commit comments