Skip to content

Commit 1353e1b

Browse files
committed
fix datasource
1 parent 6c0eff3 commit 1353e1b

File tree

5 files changed

+128
-23
lines changed

5 files changed

+128
-23
lines changed

docs/data-sources/modelserving_token.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ data "stackit_modelserving_token" "example" {
3030
### Optional
3131

3232
- `description` (String) The description of the model serving auth token.
33-
- `region` (String) STACKIT region to which the model serving auth token is associated.
33+
- `region` (String) Region to which the model serving auth token is associated. If not defined, the provider region is used.
3434

3535
### Read-Only
3636

docs/resources/modelserving_token.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,48 @@ page_title: "stackit_modelserving_token Resource - stackit"
44
subcategory: ""
55
description: |-
66
Model Serving Auth Token Resource schema.
7+
Example Usage
8+
Automatically rotate model serving token
9+
10+
resource "time_rotating" "rotate" {
11+
rotation_days = 80
12+
}
13+
14+
resource "stackit_modelserving_token" "example" {
15+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
16+
name = "Example token"
17+
18+
rotate_when_changed = {
19+
rotation = time_rotating.rotate.id
20+
}
21+
}
722
---
823

924
# stackit_modelserving_token (Resource)
1025

1126
Model Serving Auth Token Resource schema.
1227

28+
29+
## Example Usage
30+
31+
32+
### Automatically rotate model serving token
33+
```terraform
34+
resource "time_rotating" "rotate" {
35+
rotation_days = 80
36+
}
37+
38+
resource "stackit_modelserving_token" "example" {
39+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
40+
name = "Example token"
41+
42+
rotate_when_changed = {
43+
rotation = time_rotating.rotate.id
44+
}
45+
}
46+
47+
```
48+
1349
## Example Usage
1450

1551
```terraform
@@ -31,7 +67,7 @@ resource "stackit_modelserving_token" "example" {
3167
### Optional
3268

3369
- `description` (String) The description of the model serving auth token.
34-
- `region` (String) STACKIT region to which the model serving auth token is associated.
70+
- `region` (String) Region to which the model serving auth token is associated. If not defined, the provider region is used.
3571
- `rotate_when_changed` (Map of String) A map of arbitrary key/value pairs that will force recreation of the token when they change, enabling token rotation based on external conditions such as a rotating timestamp. Changing this forces a new resource to be created.
3672
- `ttl_duration` (String) The TTL duration of the model serving auth token.
3773

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package token
2+
3+
const markdownDescription = `
4+
## Example Usage` + "\n" + `
5+
6+
### Automatically rotate model serving token` + "\n" +
7+
"```terraform" + `
8+
resource "time_rotating" "rotate" {
9+
rotation_days = 80
10+
}
11+
12+
resource "stackit_modelserving_token" "example" {
13+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
14+
name = "Example token"
15+
16+
rotate_when_changed = {
17+
rotation = time_rotating.rotate.id
18+
}
19+
}
20+
` + "\n```"

stackit/internal/services/modelserving/token/datasource.go

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package token
33
import (
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"
@@ -20,6 +23,17 @@ var (
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.
2438
func 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() {

stackit/internal/services/modelserving/token/resource.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,21 @@ const (
4242
)
4343

4444
type Model struct {
45-
Id types.String `tfsdk:"id"` // needed by TF
46-
ProjectId types.String `tfsdk:"project_id"`
47-
Region types.String `tfsdk:"region"`
48-
TokenId types.String `tfsdk:"token_id"`
49-
Name types.String `tfsdk:"name"`
50-
Description types.String `tfsdk:"description"`
51-
State types.String `tfsdk:"state"`
52-
ValidUntil types.String `tfsdk:"valid_until"`
53-
TTLDuration types.String `tfsdk:"ttl_duration"`
54-
Content types.String `tfsdk:"content"`
55-
RotateWhenChanged types.Map `tfsdk:"rotate_when_changed"`
45+
Id types.String `tfsdk:"id"` // needed by TF
46+
ProjectId types.String `tfsdk:"project_id"`
47+
Region types.String `tfsdk:"region"`
48+
TokenId types.String `tfsdk:"token_id"`
49+
Name types.String `tfsdk:"name"`
50+
Description types.String `tfsdk:"description"`
51+
State types.String `tfsdk:"state"`
52+
ValidUntil types.String `tfsdk:"valid_until"`
53+
TTLDuration types.String `tfsdk:"ttl_duration"`
54+
Content types.String `tfsdk:"content"`
55+
// RotateWhenChanged is a map of arbitrary key/value pairs that will force
56+
// recreation of the token when they change, enabling token rotation based on
57+
// external conditions such as a rotating timestamp. Changing this forces a new
58+
// resource to be created. This resource is not sent to the api.
59+
RotateWhenChanged types.Map `tfsdk:"rotate_when_changed"`
5660
}
5761

5862
// NewTokenResource is a helper function to simplify the provider implementation.
@@ -111,6 +115,7 @@ func (r *tokenResource) Configure(
111115
)
112116
apiClient, err = modelserving.NewAPIClient(
113117
config.WithCustomAuth(providerData.RoundTripper),
118+
config.WithEndpoint(providerData.ModelServingCustomEndpoint),
114119
)
115120
} else {
116121
apiClient, err = modelserving.NewAPIClient(
@@ -210,7 +215,7 @@ func (r *tokenResource) Schema(
210215
resp *resource.SchemaResponse,
211216
) {
212217
resp.Schema = schema.Schema{
213-
Description: "Model Serving Auth Token Resource schema.",
218+
Description: "Model Serving Auth Token Resource schema.\n\n" + markdownDescription,
214219
Attributes: map[string]schema.Attribute{
215220
"id": schema.StringAttribute{
216221
Description: "Terraform's internal data source. ID. It is structured as \"`project_id`,`token_id`\".",

0 commit comments

Comments
 (0)