in internal/provider/apikey_resource.go [153:192]
func (r *apiKeyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var state apiKeyResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get refreshed apikey value from Devlake
apiKeys, err := r.client.GetApiKeys()
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Devlake ApiKeys",
err.Error(),
)
return
}
// Overwrite apikey with refreshed state
for _, apiKey := range apiKeys {
if types.StringValue(strconv.Itoa(apiKey.ID)) == state.ID {
state = apiKeyResourceModel{
ID: types.StringValue(strconv.Itoa(apiKey.ID)),
AllowedPath: types.StringValue(apiKey.AllowedPath),
ExpiredAt: types.StringValue(apiKey.ExpiredAt),
Name: types.StringValue(apiKey.Name),
Type: types.StringValue(apiKey.Type),
}
break
}
}
// Set refreshed state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}