Skip to content

Commit 12f7195

Browse files
Mikhail PutilovMikhail Putilov
authored andcommitted
Revert back "import" property and replace it with native import block. Fixes (#870) (#1267)
1 parent be3b093 commit 12f7195

8 files changed

+97
-88
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
volumes:
1111
- postgres:/var/lib/postgresql
1212
openldap:
13-
image: bitnami/openldap:2.6
13+
image: bitnamilegacy/openldap:2.6
1414
environment:
1515
LDAP_PORT_NUMBER: 389
1616
keycloak:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/keycloak/terraform-provider-keycloak
33
require (
44
dario.cat/mergo v1.0.2
55
github.com/golang-jwt/jwt/v5 v5.3.0
6+
github.com/google/uuid v1.6.0
67
github.com/hashicorp/errwrap v1.1.0
78
github.com/hashicorp/go-cty v1.5.0
89
github.com/hashicorp/go-retryablehttp v0.7.8

keycloak/openid_client.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,28 @@ func (keycloakClient *KeycloakClient) NewOpenidClient(ctx context.Context, clien
161161
return nil
162162
}
163163

164+
func (keycloakClient *KeycloakClient) SearchOpenidClientExact(ctx context.Context, realmId string, clientId string) (*OpenidClient, error) {
165+
var clients []*OpenidClient
166+
167+
err := keycloakClient.get(ctx, fmt.Sprintf("/realms/%s/clients", realmId), &clients, map[string]string{
168+
"first": "0",
169+
"max": "101",
170+
"clientId": clientId,
171+
"search": "true",
172+
})
173+
if err != nil {
174+
return nil, err
175+
}
176+
for _, client := range clients {
177+
client.RealmId = realmId
178+
if client.ClientId == clientId {
179+
return client, nil
180+
}
181+
}
182+
183+
return nil, fmt.Errorf("openid clientId %s does not exist in realm %s", clientId, realmId)
184+
}
185+
164186
func (keycloakClient *KeycloakClient) GetOpenidClients(ctx context.Context, realmId string, withSecrets bool) ([]*OpenidClient, error) {
165187
var clients []*OpenidClient
166188
var clientSecret OpenidClientSecret

provider/data_source_keycloak_openid_client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,19 @@ func dataSourceKeycloakOpenidClient() *schema.Resource {
110110
},
111111
"client_offline_session_idle_timeout": {
112112
Type: schema.TypeString,
113-
Computed: true,
113+
Optional: true,
114114
},
115115
"client_offline_session_max_lifespan": {
116116
Type: schema.TypeString,
117-
Computed: true,
117+
Optional: true,
118118
},
119119
"client_session_idle_timeout": {
120120
Type: schema.TypeString,
121-
Computed: true,
121+
Optional: true,
122122
},
123123
"client_session_max_lifespan": {
124124
Type: schema.TypeString,
125-
Computed: true,
125+
Optional: true,
126126
},
127127
"exclude_session_state_from_auth_response": {
128128
Type: schema.TypeBool,

provider/provider_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/meta"
@@ -41,6 +42,15 @@ func init() {
4142
panic(err)
4243
}
4344
testAccProvider = KeycloakProvider(keycloakClient)
45+
46+
testAccProvider.ResourcesMap["keycloak_openid_client"].DeleteContext = func(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics {
47+
if data.State().Attributes["client_id"] == "account" {
48+
return diag.Diagnostics{}
49+
} else {
50+
return resourceKeycloakOpenidClientDelete(ctx, data, i)
51+
}
52+
}
53+
4454
testAccProviderFactories = map[string]func() (*schema.Provider, error){
4555
"keycloak": func() (*schema.Provider, error) {
4656
return testAccProvider, nil

provider/resource_keycloak_openid_client.go

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/hashicorp/go-cty/cty"
1111

12-
"dario.cat/mergo"
12+
"github.com/google/uuid"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -48,7 +48,6 @@ func resourceKeycloakOpenidClient() *schema.Resource {
4848
"name": {
4949
Type: schema.TypeString,
5050
Optional: true,
51-
Computed: true,
5251
},
5352
"enabled": {
5453
Type: schema.TypeBool,
@@ -58,7 +57,6 @@ func resourceKeycloakOpenidClient() *schema.Resource {
5857
"description": {
5958
Type: schema.TypeString,
6059
Optional: true,
61-
Computed: true,
6260
},
6361
"access_type": {
6462
Type: schema.TypeString,
@@ -106,34 +104,33 @@ func resourceKeycloakOpenidClient() *schema.Resource {
106104
"standard_flow_enabled": {
107105
Type: schema.TypeBool,
108106
Optional: true,
109-
Computed: true,
107+
Default: false,
110108
},
111109
"implicit_flow_enabled": {
112110
Type: schema.TypeBool,
113111
Optional: true,
114-
Computed: true,
112+
Default: false,
115113
},
116114
"direct_access_grants_enabled": {
117115
Type: schema.TypeBool,
118116
Optional: true,
119-
Computed: true,
117+
Default: false,
120118
},
121119
"service_accounts_enabled": {
122120
Type: schema.TypeBool,
123121
Optional: true,
124-
Computed: true,
122+
Default: false,
125123
},
126124
"frontchannel_logout_enabled": {
127125
Type: schema.TypeBool,
128126
Optional: true,
129-
Computed: true,
127+
Default: false,
130128
},
131129
"valid_redirect_uris": {
132130
Type: schema.TypeSet,
133131
Elem: &schema.Schema{Type: schema.TypeString},
134132
Set: schema.HashString,
135133
Optional: true,
136-
Computed: true,
137134
},
138135
"valid_post_logout_redirect_uris": {
139136
Type: schema.TypeSet,
@@ -147,22 +144,18 @@ func resourceKeycloakOpenidClient() *schema.Resource {
147144
Elem: &schema.Schema{Type: schema.TypeString},
148145
Set: schema.HashString,
149146
Optional: true,
150-
Computed: true,
151147
},
152148
"root_url": {
153149
Type: schema.TypeString,
154150
Optional: true,
155-
Computed: true,
156151
},
157152
"admin_url": {
158153
Type: schema.TypeString,
159154
Optional: true,
160-
Computed: true,
161155
},
162156
"base_url": {
163157
Type: schema.TypeString,
164158
Optional: true,
165-
Computed: true,
166159
},
167160
"service_account_user_id": {
168161
Type: schema.TypeString,
@@ -176,27 +169,22 @@ func resourceKeycloakOpenidClient() *schema.Resource {
176169
"access_token_lifespan": {
177170
Type: schema.TypeString,
178171
Optional: true,
179-
Computed: true,
180172
},
181173
"client_offline_session_idle_timeout": {
182174
Type: schema.TypeString,
183175
Optional: true,
184-
Computed: true,
185176
},
186177
"client_offline_session_max_lifespan": {
187178
Type: schema.TypeString,
188179
Optional: true,
189-
Computed: true,
190180
},
191181
"client_session_idle_timeout": {
192182
Type: schema.TypeString,
193183
Optional: true,
194-
Computed: true,
195184
},
196185
"client_session_max_lifespan": {
197186
Type: schema.TypeString,
198187
Optional: true,
199-
Computed: true,
200188
},
201189
"exclude_session_state_from_auth_response": {
202190
Type: schema.TypeBool,
@@ -250,17 +238,16 @@ func resourceKeycloakOpenidClient() *schema.Resource {
250238
"consent_required": {
251239
Type: schema.TypeBool,
252240
Optional: true,
253-
Computed: true,
241+
Default: false,
254242
},
255243
"display_on_consent_screen": {
256244
Type: schema.TypeBool,
257245
Optional: true,
258-
Computed: true,
246+
Default: false,
259247
},
260248
"consent_screen_text": {
261249
Type: schema.TypeString,
262250
Optional: true,
263-
Computed: true,
264251
},
265252
"authentication_flow_binding_overrides": {
266253
Type: schema.TypeSet,
@@ -342,12 +329,6 @@ func resourceKeycloakOpenidClient() *schema.Resource {
342329
Optional: true,
343330
Default: false,
344331
},
345-
"import": {
346-
Type: schema.TypeBool,
347-
Optional: true,
348-
Default: false,
349-
ForceNew: true,
350-
},
351332
},
352333
CustomizeDiff: resourceKeycloakOpenidClientDiff(),
353334
}
@@ -603,25 +584,9 @@ func resourceKeycloakOpenidClientCreate(ctx context.Context, data *schema.Resour
603584
return diag.FromErr(err)
604585
}
605586

606-
if data.Get("import").(bool) {
607-
existingClient, err := keycloakClient.GetOpenidClientByClientId(ctx, client.RealmId, client.ClientId)
608-
if err != nil {
609-
return diag.FromErr(err)
610-
}
611-
612-
if err = mergo.Merge(client, existingClient); err != nil {
613-
return diag.FromErr(err)
614-
}
615-
616-
err = keycloakClient.UpdateOpenidClient(ctx, client)
617-
if err != nil {
618-
return diag.FromErr(err)
619-
}
620-
} else {
621-
err = keycloakClient.NewOpenidClient(ctx, client)
622-
if err != nil {
623-
return diag.FromErr(err)
624-
}
587+
err = keycloakClient.NewOpenidClient(ctx, client)
588+
if err != nil {
589+
return diag.FromErr(err)
625590
}
626591

627592
err = setOpenidClientData(ctx, keycloakClient, data, client)
@@ -648,10 +613,6 @@ func resourceKeycloakOpenidClientRead(ctx context.Context, data *schema.Resource
648613
return diag.FromErr(err)
649614
}
650615

651-
if _, ok := data.GetOk("import"); !ok {
652-
data.Set("import", false)
653-
}
654-
655616
return nil
656617
}
657618

@@ -687,9 +648,6 @@ func resourceKeycloakOpenidClientUpdate(ctx context.Context, data *schema.Resour
687648
}
688649

689650
func resourceKeycloakOpenidClientDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
690-
if data.Get("import").(bool) {
691-
return nil
692-
}
693651
keycloakClient := meta.(*keycloak.KeycloakClient)
694652

695653
realmId := data.Get("realm_id").(string)
@@ -703,18 +661,28 @@ func resourceKeycloakOpenidClientImport(ctx context.Context, d *schema.ResourceD
703661

704662
parts := strings.Split(d.Id(), "/")
705663
if len(parts) != 2 {
706-
return nil, fmt.Errorf("Invalid import. Supported import formats: {{realmId}}/{{openidClientId}}")
664+
return nil, fmt.Errorf("invalid import. Supported import formats: {{realmId}}/{{openidClientId}} or {{realmId}}/{{clientUuid}}")
707665
}
708-
709-
_, err := keycloakClient.GetOpenidClient(ctx, parts[0], parts[1])
666+
if _, err := uuid.Parse(parts[1]); err == nil {
667+
// {{realmId}}/{{clientUuid}}
668+
_, err := keycloakClient.GetOpenidClient(ctx, parts[0], parts[1])
669+
if err != nil {
670+
return nil, err
671+
}
672+
d.SetId(parts[1])
673+
} else {
674+
// {{realmId}}/{{openidClientId}}
675+
c, err := keycloakClient.SearchOpenidClientExact(ctx, parts[0], parts[1])
676+
if err != nil {
677+
return nil, err
678+
}
679+
d.SetId(c.Id)
680+
}
681+
err := d.Set("realm_id", parts[0])
710682
if err != nil {
711683
return nil, err
712684
}
713685

714-
d.Set("realm_id", parts[0])
715-
d.Set("import", false)
716-
d.SetId(parts[1])
717-
718686
diagnostics := resourceKeycloakOpenidClientRead(ctx, d, meta)
719687
if diagnostics.HasError() {
720688
return nil, errors.New(diagnostics[0].Summary)

provider/resource_keycloak_openid_client_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -761,12 +761,12 @@ func TestAccKeycloakOpenidClient_import(t *testing.T) {
761761
CheckDestroy: testAccCheckKeycloakOpenidClientNotDestroyed(),
762762
Steps: []resource.TestStep{
763763
{
764-
Config: testKeycloakOpenidClient_import("non-existing-client", true),
765-
ExpectError: regexp.MustCompile("Error: openid client with name non-existing-client does not exist"),
766-
},
767-
{
768-
Config: testKeycloakOpenidClient_import("account", true),
769-
Check: testAccCheckKeycloakOpenidClientExistsWithEnabledStatus("keycloak_openid_client.client", true),
764+
ResourceName: "keycloak_openid_client.client",
765+
ImportState: true,
766+
ImportStateId: testAccRealm.Realm + "/account",
767+
768+
Config: testKeycloakOpenidClient_import("account", false),
769+
Check: testAccCheckKeycloakOpenidClientExistsWithEnabledStatus("keycloak_openid_client.client", false),
770770
},
771771
},
772772
})
@@ -2095,14 +2095,12 @@ func testKeycloakOpenidClient_import(clientId string, enabled bool) string {
20952095
data "keycloak_realm" "realm" {
20962096
realm = "%s"
20972097
}
2098-
20992098
resource "keycloak_openid_client" "client" {
21002099
client_id = "%s"
21012100
realm_id = data.keycloak_realm.realm.id
21022101
access_type = "PUBLIC"
21032102
root_url = ""
21042103
enabled = %t
2105-
import = true
21062104
}
21072105
`, testAccRealm.Realm, clientId, enabled)
21082106
}

0 commit comments

Comments
 (0)