@@ -163,7 +163,7 @@ func (r *AccessTokenResource) Create(ctx context.Context, req resource.CreateReq
163163 return
164164 }
165165
166- data = r .toModel (ctx , at )
166+ data = r .toModel (ctx , at , nil )
167167 if resp .Diagnostics .HasError () {
168168 return
169169 }
@@ -191,7 +191,7 @@ func (r *AccessTokenResource) Read(ctx context.Context, req resource.ReadRequest
191191 return
192192 }
193193
194- fromAPI := r .toModel (ctx , at )
194+ fromAPI := r .toModel (ctx , at , & fromState )
195195 if resp .Diagnostics .HasError () {
196196 return
197197 }
@@ -230,15 +230,11 @@ func (r *AccessTokenResource) Update(ctx context.Context, req resource.UpdateReq
230230 return
231231 }
232232
233- fromAPI := r .toModel (ctx , at )
233+ fromAPI := r .toModel (ctx , at , & fromState )
234234 if resp .Diagnostics .HasError () {
235235 return
236236 }
237237
238- // The token is not returned by the API after initial creation,
239- // so we need to copy it from the state
240- fromAPI .Token = fromState .Token
241-
242238 resp .Diagnostics .Append (resp .State .Set (ctx , & fromAPI )... )
243239}
244240
@@ -261,14 +257,27 @@ func (r *AccessTokenResource) ImportState(ctx context.Context, req resource.Impo
261257 resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("uuid" ), req .ID )... )
262258}
263259
264- func (r * AccessTokenResource ) toModel (ctx context.Context , at hubclient.AccessToken ) AccessTokenResourceModel {
260+ func (r * AccessTokenResource ) toModel (ctx context.Context , at hubclient.AccessToken , currentState * AccessTokenResourceModel ) AccessTokenResourceModel {
265261 scopes , _ := types .ListValueFrom (ctx , types .StringType , at .Scopes )
266- return AccessTokenResourceModel {
262+ result := AccessTokenResourceModel {
267263 UUID : types .StringValue (at .UUID ),
268264 IsActive : types .BoolValue (at .IsActive ),
269265 TokenLabel : types .StringValue (at .TokenLabel ),
270266 Scopes : scopes ,
271267 Token : types .StringValue (at .Token ),
272268 ExpiresAt : types .StringValue (at .ExpiresAt ),
273269 }
270+
271+ // If the current state is null, keep it as null instead of changing to empty string.
272+ if at .ExpiresAt == "" && currentState != nil && currentState .ExpiresAt .IsNull () {
273+ result .ExpiresAt = types .StringNull ()
274+ }
275+
276+ // The token is not returned by the API after initial creation,
277+ // so we need to copy it from the state
278+ if currentState != nil {
279+ result .Token = currentState .Token
280+ }
281+
282+ return result
274283}
0 commit comments