@@ -3,10 +3,13 @@ package token
33import (
44 "context"
55 "fmt"
6+ "strings"
7+ "time"
68
79 "github.com/hashicorp/terraform-plugin-framework/datasource"
810 "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
911 "github.com/hashicorp/terraform-plugin-framework/schema/validator"
12+ "github.com/hashicorp/terraform-plugin-framework/types"
1013 "github.com/hashicorp/terraform-plugin-log/tflog"
1114 "github.com/stackitcloud/stackit-sdk-go/core/config"
1215 "github.com/stackitcloud/stackit-sdk-go/services/modelserving"
2023 _ datasource.DataSource = & tokenDataSource {}
2124)
2225
26+ type DatasourceModel struct {
27+ Id types.String `tfsdk:"id"` // needed by TF
28+ ProjectId types.String `tfsdk:"project_id"`
29+ Region types.String `tfsdk:"region"`
30+ TokenId types.String `tfsdk:"token_id"`
31+ Name types.String `tfsdk:"name"`
32+ Description types.String `tfsdk:"description"`
33+ State types.String `tfsdk:"state"`
34+ ValidUntil types.String `tfsdk:"valid_until"`
35+ }
36+
2337// NewTokenDataSource is a helper function to simplify the provider implementation.
2438func NewTokenDataSource () datasource.DataSource {
2539 return & tokenDataSource {}
@@ -71,10 +85,12 @@ func (d *tokenDataSource) Configure(
7185 apiClient , err = modelserving .NewAPIClient (
7286 config .WithCustomAuth (providerData .RoundTripper ),
7387 config .WithEndpoint (providerData .ModelServingCustomEndpoint ),
88+ config .WithRegion (providerData .GetRegion ()),
7489 )
7590 } else {
7691 apiClient , err = modelserving .NewAPIClient (
7792 config .WithCustomAuth (providerData .RoundTripper ),
93+ config .WithRegion (providerData .GetRegion ()),
7894 )
7995 }
8096
@@ -143,10 +159,6 @@ func (d *tokenDataSource) Schema(
143159 Description : "State of the model serving auth token." ,
144160 Computed : true ,
145161 },
146- "content" : schema.StringAttribute {
147- Description : "Content of the model serving auth token." ,
148- Computed : true ,
149- },
150162 "valid_until" : schema.StringAttribute {
151163 Description : "The time until the model serving auth token is valid." ,
152164 Computed : true ,
@@ -161,7 +173,7 @@ func (d *tokenDataSource) Read(
161173 req datasource.ReadRequest , //nolint:gocritic // function signature required by Terraform
162174 resp * datasource.ReadResponse ,
163175) { // nolint:gocritic // function signature required by Terraform
164- var model Model
176+ var model DatasourceModel
165177
166178 diags := req .Config .Get (ctx , & model )
167179 resp .Diagnostics .Append (diags ... )
@@ -195,7 +207,17 @@ func (d *tokenDataSource) Read(
195207 return
196208 }
197209
198- if getTokenResp != nil && getTokenResp .Token .State != nil &&
210+ if getTokenResp == nil {
211+ core .LogAndAddError (
212+ ctx ,
213+ & resp .Diagnostics ,
214+ "Error reading model serving auth token" ,
215+ "Model serving auth token no response" ,
216+ )
217+ return
218+ }
219+
220+ if getTokenResp .Token .State != nil &&
199221 * getTokenResp .Token .State == inactiveState {
200222 resp .State .RemoveResource (ctx )
201223 core .LogAndAddError (
@@ -207,17 +229,39 @@ func (d *tokenDataSource) Read(
207229 return
208230 }
209231
210- err = mapGetResponse (getTokenResp , & model , & model )
211- if err != nil {
232+ if getTokenResp .Token == nil {
212233 core .LogAndAddError (
213234 ctx ,
214235 & resp .Diagnostics ,
215236 "Error reading model serving auth token" ,
216- fmt . Sprintf ( "Processing API payload: %v" , err ) ,
237+ "Model serving auth token not found" ,
217238 )
218239 return
219240 }
220241
242+ // theoretically, should never happen, but still catch null pointers
243+ validUntil := types .StringNull ()
244+ if getTokenResp .Token .ValidUntil != nil {
245+ validUntil = types .StringValue (
246+ getTokenResp .Token .ValidUntil .Format (time .RFC3339 ),
247+ )
248+ }
249+
250+ idParts := []string {
251+ model .ProjectId .ValueString (),
252+ model .Region .ValueString (),
253+ model .TokenId .ValueString (),
254+ }
255+ model .Id = types .StringValue (
256+ strings .Join (idParts , core .Separator ),
257+ )
258+ model .TokenId = types .StringPointerValue (getTokenResp .Token .Id )
259+ model .Name = types .StringPointerValue (getTokenResp .Token .Name )
260+ model .State = types .StringPointerValue (getTokenResp .Token .State )
261+ model .ValidUntil = validUntil
262+ model .Description = types .StringPointerValue (getTokenResp .Token .Description )
263+ model .Region = types .StringValue (region )
264+
221265 diags = resp .State .Set (ctx , model )
222266 resp .Diagnostics .Append (diags ... )
223267 if resp .Diagnostics .HasError () {
0 commit comments