Skip to content

Commit 652c24e

Browse files
GH-2482 | OKTA-1027777 | okta_realm Resource Created But Error Due To Null Realm Type (#2499)
1 parent df272d3 commit 652c24e

3 files changed

Lines changed: 261 additions & 81 deletions

File tree

okta/services/idaas/resource_okta_realm.go

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package idaas
22

33
import (
44
"context"
5-
"io"
65

76
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
87
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -53,7 +52,7 @@ func (r *realmResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
5352
Description: "The name of the Okta Realm.",
5453
},
5554
"realm_type": schema.StringAttribute{
56-
Required: true,
55+
Optional: true,
5756
Description: "The realm type. Valid values: `PARTNER` and `DEFAULT`",
5857
Validators: []validator.String{
5958
stringvalidator.OneOf("PARTNER", "DEFAULT"),
@@ -76,35 +75,30 @@ func (r *realmResource) Configure(_ context.Context, req resource.ConfigureReque
7675
}
7776

7877
func (r *realmResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
79-
var state realmModel
80-
resp.Diagnostics.Append(req.Plan.Get(ctx, &state)...)
78+
var data realmModel
79+
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
8180
if resp.Diagnostics.HasError() {
8281
return
8382
}
8483

8584
createRealmRequest := v5okta.NewCreateRealmRequest()
86-
profile := v5okta.NewRealmProfile(state.Name.ValueString())
87-
profile.RealmType = state.RealmType.ValueStringPointer()
88-
createRealmRequest.Profile = profile
85+
profile := v5okta.NewRealmProfile(data.Name.ValueString())
86+
profile.SetRealmType(data.RealmType.ValueString())
87+
createRealmRequest.SetProfile(*profile)
8988

90-
responseRealm, response, err := r.config.OktaIDaaSClient.OktaSDKClientV5().RealmAPI.CreateRealm(ctx).Body(*createRealmRequest).Execute()
89+
responseRealm, _, err := r.config.OktaIDaaSClient.OktaSDKClientV5().RealmAPI.CreateRealm(ctx).Body(*createRealmRequest).Execute()
9190
if err != nil {
92-
body, ioErr := io.ReadAll(response.Body)
93-
defer response.Body.Close()
94-
if ioErr != nil {
95-
resp.Diagnostics.AddError(err.Error(), "failed to read response body")
96-
return
97-
}
98-
resp.Diagnostics.AddError("failed to create realm:"+err.Error(), string(body))
91+
resp.Diagnostics.AddError("Error creating Okta realm ", err.Error())
9992
return
10093
}
94+
responseRealm.Profile.SetRealmType(data.RealmType.String()) // realm type isn't returned as part of the response, we need to set it manually.
10195

102-
resp.Diagnostics.Append(mapRealmResourceToState(responseRealm, &state)...)
96+
resp.Diagnostics.Append(mapRealmResourceToState(responseRealm, &data)...)
10397
if resp.Diagnostics.HasError() {
10498
return
10599
}
106100

107-
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
101+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
108102
if resp.Diagnostics.HasError() {
109103
return
110104
}
@@ -117,20 +111,9 @@ func (r *realmResource) Read(ctx context.Context, req resource.ReadRequest, resp
117111
return
118112
}
119113

120-
realm, response, err := r.config.OktaIDaaSClient.OktaSDKClientV5().RealmAPI.GetRealm(ctx, state.ID.ValueString()).Execute()
114+
realm, _, err := r.config.OktaIDaaSClient.OktaSDKClientV5().RealmAPI.GetRealm(ctx, state.ID.ValueString()).Execute()
121115
if err != nil {
122-
body, ioErr := io.ReadAll(response.Body)
123-
defer response.Body.Close()
124-
if ioErr != nil {
125-
resp.Diagnostics.AddError(err.Error(), "failed to read response body")
126-
return
127-
}
128-
resp.Diagnostics.AddError("failed to read realm:"+err.Error(), string(body))
129-
return
130-
}
131-
132-
if realm == nil {
133-
resp.State.RemoveResource(ctx)
116+
resp.Diagnostics.AddError("Error reading Okta realm ", err.Error())
134117
return
135118
}
136119

@@ -147,33 +130,33 @@ func (r *realmResource) Read(ctx context.Context, req resource.ReadRequest, resp
147130

148131
func (r *realmResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
149132
var state realmModel
150-
resp.Diagnostics.Append(req.Plan.Get(ctx, &state)...)
133+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
151134
if resp.Diagnostics.HasError() {
152135
return
153136
}
154137

138+
resp.Diagnostics.Append(req.Plan.Get(ctx, &state)...)
139+
if resp.Diagnostics.HasError() {
140+
return
141+
}
155142
updateRealmRequest := v5okta.NewUpdateRealmRequest()
156143
profile := v5okta.NewRealmProfile(state.Name.ValueString())
157-
profile.RealmType = state.RealmType.ValueStringPointer()
158-
updateRealmRequest.Profile = profile
144+
profile.SetRealmType(state.RealmType.ValueString())
145+
updateRealmRequest.SetProfile(*profile)
159146

160-
realm, response, err := r.config.OktaIDaaSClient.OktaSDKClientV5().RealmAPI.ReplaceRealm(ctx, state.ID.ValueString()).Body(*updateRealmRequest).Execute()
147+
realm, _, err := r.config.OktaIDaaSClient.OktaSDKClientV5().RealmAPI.ReplaceRealm(ctx, state.ID.ValueString()).Body(*updateRealmRequest).Execute()
161148
if err != nil {
162-
body, ioErr := io.ReadAll(response.Body)
163-
defer response.Body.Close()
164-
if ioErr != nil {
165-
resp.Diagnostics.AddError(err.Error(), "failed to read response body")
166-
return
167-
}
168-
resp.Diagnostics.AddError("failed to update realm:"+err.Error(), string(body))
149+
resp.Diagnostics.AddError("failed to update realm: ", err.Error())
169150
return
170151
}
152+
realm.Profile.SetRealmType(state.RealmType.String()) // realm type isn't returned as part of the response, we need to set it manually.
171153

172154
resp.Diagnostics.Append(mapRealmResourceToState(realm, &state)...)
173155
if resp.Diagnostics.HasError() {
174156
return
175157
}
176158

159+
// Save updated Data into Terraform state
177160
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
178161
if resp.Diagnostics.HasError() {
179162
return
@@ -187,15 +170,9 @@ func (r *realmResource) Delete(ctx context.Context, req resource.DeleteRequest,
187170
return
188171
}
189172

190-
response, err := r.config.OktaIDaaSClient.OktaSDKClientV5().RealmAPI.DeleteRealm(ctx, state.ID.ValueString()).Execute()
173+
_, err := r.config.OktaIDaaSClient.OktaSDKClientV5().RealmAPI.DeleteRealm(ctx, state.ID.ValueString()).Execute()
191174
if err != nil {
192-
body, ioErr := io.ReadAll(response.Body)
193-
defer response.Body.Close()
194-
if ioErr != nil {
195-
resp.Diagnostics.AddError(err.Error(), "failed to read response body")
196-
return
197-
}
198-
resp.Diagnostics.AddError("failed to delete realm:"+err.Error(), string(body))
175+
resp.Diagnostics.AddError("failed to delete realm: ", err.Error())
199176
return
200177
}
201178
}
@@ -206,11 +183,8 @@ func (r *realmResource) ImportState(ctx context.Context, req resource.ImportStat
206183

207184
func mapRealmResourceToState(realmResource *v5okta.Realm, state *realmModel) diag.Diagnostics {
208185
var diags diag.Diagnostics
209-
210186
state.ID = types.StringPointerValue(realmResource.Id)
211187
state.Name = types.StringValue(realmResource.Profile.Name)
212-
state.RealmType = types.StringPointerValue(realmResource.Profile.RealmType)
213188
state.IsDefault = types.BoolPointerValue(realmResource.IsDefault)
214-
215189
return diags
216190
}

test/fixtures/vcr/idaas/TestAccResourceOktaRealm_crud/realm-support.yaml renamed to test/fixtures/vcr/idaas/TestAccResourceOktaRealm_crud/classic-00.yaml

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interactions:
77
proto_major: 1
88
proto_minor: 1
99
content_length: 67
10-
host: realm-support.dne-okta.com
10+
host: classic-00.dne-okta.com
1111
body: |
1212
{"profile":{"name":"TestAcc Example Realm","realmType":"DEFAULT"}}
1313
headers:
@@ -17,100 +17,100 @@ interactions:
1717
- SSWS REDACTED
1818
Content-Type:
1919
- application/json
20-
url: https://realm-support.dne-okta.com/api/v1/realms
20+
url: https://classic-00.dne-okta.com/api/v1/realms
2121
method: POST
2222
response:
2323
proto: HTTP/2.0
2424
proto_major: 2
2525
proto_minor: 0
2626
content_length: -1
2727
uncompressed: true
28-
body: '{"id":"guotyyzbl1YWn63OB697","created":"2025-08-07T10:56:05.000Z","lastUpdated":"2025-08-07T10:56:05.000Z","profile":{"name":"TestAcc Example Realm","realmType":"DEFAULT","domains":[]},"isDefault":false,"_links":{"self":{"rel":"self","href":"https://realm-support.dne-okta.com/api/v1/realms/guotyyzbl1YWn63OB697","method":"GET"}}}'
28+
body: '{"id":"guoqfs91nlh03tfqh1d7","created":"2025-09-28T11:25:34.000Z","lastUpdated":"2025-09-28T11:25:34.000Z","profile":{"name":"TestAcc Example Realm","domains":[]},"isDefault":false,"_links":{"self":{"rel":"self","href":"https://classic-00.dne-okta.com/api/v1/realms/guoqfs91nlh03tfqh1d7","method":"GET"}}}'
2929
headers:
3030
Accept-Ch:
3131
- Sec-CH-UA-Platform-Version
3232
Content-Type:
3333
- application/json
3434
Date:
35-
- Thu, 07 Aug 2025 10:56:05 GMT
35+
- Sun, 28 Sep 2025 11:25:34 GMT
3636
Referrer-Policy:
3737
- strict-origin-when-cross-origin
3838
status: 200 OK
3939
code: 200
40-
duration: 1.061888292s
40+
duration: 1.316026667s
4141
- id: 1
4242
request:
4343
proto: HTTP/1.1
4444
proto_major: 1
4545
proto_minor: 1
4646
content_length: 0
47-
host: realm-support.dne-okta.com
47+
host: classic-00.dne-okta.com
4848
headers:
4949
Accept:
5050
- application/json
5151
Authorization:
5252
- SSWS REDACTED
53-
url: https://realm-support.dne-okta.com/api/v1/realms/guotyyzbl1YWn63OB697
53+
url: https://classic-00.dne-okta.com/api/v1/realms/guoqfs91nlh03tfqh1d7
5454
method: GET
5555
response:
5656
proto: HTTP/2.0
5757
proto_major: 2
5858
proto_minor: 0
5959
content_length: -1
6060
uncompressed: true
61-
body: '{"id":"guotyyzbl1YWn63OB697","created":"2025-08-07T10:56:05.000Z","lastUpdated":"2025-08-07T10:56:05.000Z","profile":{"name":"TestAcc Example Realm","realmType":"DEFAULT","domains":[]},"isDefault":false,"_links":{"self":{"rel":"self","href":"https://realm-support.dne-okta.com/api/v1/realms/guotyyzbl1YWn63OB697","method":"GET"}}}'
61+
body: '{"id":"guoqfs91nlh03tfqh1d7","created":"2025-09-28T11:25:34.000Z","lastUpdated":"2025-09-28T11:25:34.000Z","profile":{"name":"TestAcc Example Realm","domains":[]},"isDefault":false,"_links":{"self":{"rel":"self","href":"https://classic-00.dne-okta.com/api/v1/realms/guoqfs91nlh03tfqh1d7","method":"GET"}}}'
6262
headers:
6363
Accept-Ch:
6464
- Sec-CH-UA-Platform-Version
6565
Content-Type:
6666
- application/json
6767
Date:
68-
- Thu, 07 Aug 2025 10:56:06 GMT
68+
- Sun, 28 Sep 2025 11:25:35 GMT
6969
Referrer-Policy:
7070
- strict-origin-when-cross-origin
7171
status: 200 OK
7272
code: 200
73-
duration: 891.3495ms
73+
duration: 1.247994042s
7474
- id: 2
7575
request:
7676
proto: HTTP/1.1
7777
proto_major: 1
7878
proto_minor: 1
7979
content_length: 0
80-
host: realm-support.dne-okta.com
80+
host: classic-00.dne-okta.com
8181
headers:
8282
Accept:
8383
- application/json
8484
Authorization:
8585
- SSWS REDACTED
86-
url: https://realm-support.dne-okta.com/api/v1/realms/guotyyzbl1YWn63OB697
86+
url: https://classic-00.dne-okta.com/api/v1/realms/guoqfs91nlh03tfqh1d7
8787
method: GET
8888
response:
8989
proto: HTTP/2.0
9090
proto_major: 2
9191
proto_minor: 0
9292
content_length: -1
9393
uncompressed: true
94-
body: '{"id":"guotyyzbl1YWn63OB697","created":"2025-08-07T10:56:05.000Z","lastUpdated":"2025-08-07T10:56:05.000Z","profile":{"name":"TestAcc Example Realm","realmType":"DEFAULT","domains":[]},"isDefault":false,"_links":{"self":{"rel":"self","href":"https://realm-support.dne-okta.com/api/v1/realms/guotyyzbl1YWn63OB697","method":"GET"}}}'
94+
body: '{"id":"guoqfs91nlh03tfqh1d7","created":"2025-09-28T11:25:34.000Z","lastUpdated":"2025-09-28T11:25:34.000Z","profile":{"name":"TestAcc Example Realm","domains":[]},"isDefault":false,"_links":{"self":{"rel":"self","href":"https://classic-00.dne-okta.com/api/v1/realms/guoqfs91nlh03tfqh1d7","method":"GET"}}}'
9595
headers:
9696
Accept-Ch:
9797
- Sec-CH-UA-Platform-Version
9898
Content-Type:
9999
- application/json
100100
Date:
101-
- Thu, 07 Aug 2025 10:56:07 GMT
101+
- Sun, 28 Sep 2025 11:25:37 GMT
102102
Referrer-Policy:
103103
- strict-origin-when-cross-origin
104104
status: 200 OK
105105
code: 200
106-
duration: 876.239416ms
106+
duration: 1.214109084s
107107
- id: 3
108108
request:
109109
proto: HTTP/1.1
110110
proto_major: 1
111111
proto_minor: 1
112112
content_length: 75
113-
host: realm-support.dne-okta.com
113+
host: classic-00.dne-okta.com
114114
body: |
115115
{"profile":{"name":"TestAcc Example Realm Updated","realmType":"PARTNER"}}
116116
headers:
@@ -120,73 +120,73 @@ interactions:
120120
- SSWS REDACTED
121121
Content-Type:
122122
- application/json
123-
url: https://realm-support.dne-okta.com/api/v1/realms/guotyyzbl1YWn63OB697
123+
url: https://classic-00.dne-okta.com/api/v1/realms/guoqfs91nlh03tfqh1d7
124124
method: PUT
125125
response:
126126
proto: HTTP/2.0
127127
proto_major: 2
128128
proto_minor: 0
129129
content_length: -1
130130
uncompressed: true
131-
body: '{"id":"guotyyzbl1YWn63OB697","created":"2025-08-07T10:56:05.000Z","lastUpdated":"2025-08-07T10:56:08.000Z","profile":{"name":"TestAcc Example Realm Updated","realmType":"PARTNER","domains":[]},"isDefault":false,"_links":{"self":{"rel":"self","href":"https://realm-support.dne-okta.com/api/v1/realms/guotyyzbl1YWn63OB697","method":"GET"}}}'
131+
body: '{"id":"guoqfs91nlh03tfqh1d7","created":"2025-09-28T11:25:34.000Z","lastUpdated":"2025-09-28T11:25:38.000Z","profile":{"name":"TestAcc Example Realm Updated","domains":[]},"isDefault":false,"_links":{"self":{"rel":"self","href":"https://classic-00.dne-okta.com/api/v1/realms/guoqfs91nlh03tfqh1d7","method":"GET"}}}'
132132
headers:
133133
Accept-Ch:
134134
- Sec-CH-UA-Platform-Version
135135
Content-Type:
136136
- application/json
137137
Date:
138-
- Thu, 07 Aug 2025 10:56:08 GMT
138+
- Sun, 28 Sep 2025 11:25:38 GMT
139139
Referrer-Policy:
140140
- strict-origin-when-cross-origin
141141
status: 200 OK
142142
code: 200
143-
duration: 946.544667ms
143+
duration: 1.223590458s
144144
- id: 4
145145
request:
146146
proto: HTTP/1.1
147147
proto_major: 1
148148
proto_minor: 1
149149
content_length: 0
150-
host: realm-support.dne-okta.com
150+
host: classic-00.dne-okta.com
151151
headers:
152152
Accept:
153153
- application/json
154154
Authorization:
155155
- SSWS REDACTED
156-
url: https://realm-support.dne-okta.com/api/v1/realms/guotyyzbl1YWn63OB697
156+
url: https://classic-00.dne-okta.com/api/v1/realms/guoqfs91nlh03tfqh1d7
157157
method: GET
158158
response:
159159
proto: HTTP/2.0
160160
proto_major: 2
161161
proto_minor: 0
162162
content_length: -1
163163
uncompressed: true
164-
body: '{"id":"guotyyzbl1YWn63OB697","created":"2025-08-07T10:56:05.000Z","lastUpdated":"2025-08-07T10:56:08.000Z","profile":{"name":"TestAcc Example Realm Updated","realmType":"PARTNER","domains":[]},"isDefault":false,"_links":{"self":{"rel":"self","href":"https://realm-support.dne-okta.com/api/v1/realms/guotyyzbl1YWn63OB697","method":"GET"}}}'
164+
body: '{"id":"guoqfs91nlh03tfqh1d7","created":"2025-09-28T11:25:34.000Z","lastUpdated":"2025-09-28T11:25:38.000Z","profile":{"name":"TestAcc Example Realm Updated","domains":[]},"isDefault":false,"_links":{"self":{"rel":"self","href":"https://classic-00.dne-okta.com/api/v1/realms/guoqfs91nlh03tfqh1d7","method":"GET"}}}'
165165
headers:
166166
Accept-Ch:
167167
- Sec-CH-UA-Platform-Version
168168
Content-Type:
169169
- application/json
170170
Date:
171-
- Thu, 07 Aug 2025 10:56:09 GMT
171+
- Sun, 28 Sep 2025 11:25:39 GMT
172172
Referrer-Policy:
173173
- strict-origin-when-cross-origin
174174
status: 200 OK
175175
code: 200
176-
duration: 909.665958ms
176+
duration: 1.223881833s
177177
- id: 5
178178
request:
179179
proto: HTTP/1.1
180180
proto_major: 1
181181
proto_minor: 1
182182
content_length: 0
183-
host: realm-support.dne-okta.com
183+
host: classic-00.dne-okta.com
184184
headers:
185185
Accept:
186186
- application/json
187187
Authorization:
188188
- SSWS REDACTED
189-
url: https://realm-support.dne-okta.com/api/v1/realms/guotyyzbl1YWn63OB697
189+
url: https://classic-00.dne-okta.com/api/v1/realms/guoqfs91nlh03tfqh1d7
190190
method: DELETE
191191
response:
192192
proto: HTTP/2.0
@@ -198,9 +198,9 @@ interactions:
198198
Accept-Ch:
199199
- Sec-CH-UA-Platform-Version
200200
Date:
201-
- Thu, 07 Aug 2025 10:56:10 GMT
201+
- Sun, 28 Sep 2025 11:25:41 GMT
202202
Referrer-Policy:
203203
- strict-origin-when-cross-origin
204204
status: 204 No Content
205205
code: 204
206-
duration: 1.145766s
206+
duration: 1.054670125s

0 commit comments

Comments
 (0)