-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasource_app.go
More file actions
289 lines (252 loc) · 8.97 KB
/
datasource_app.go
File metadata and controls
289 lines (252 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) Spice AI, Inc. 2025, 2026
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"encoding/json"
"fmt"
"strconv"
"terraform-provider-spiceai/internal/client"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &AppDataSource{}
func NewAppDataSource() datasource.DataSource {
return &AppDataSource{}
}
// AppDataSource defines the data source implementation.
type AppDataSource struct {
client *client.SpiceAIClient
}
// AppDataSourceModel describes the data source data model.
type AppDataSourceModel struct {
// Identity
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
// Basic configuration
Description types.String `tfsdk:"description"`
Visibility types.String `tfsdk:"visibility"`
ProductionBranch types.String `tfsdk:"production_branch"`
Tags types.Map `tfsdk:"tags"`
Cname types.String `tfsdk:"cname"`
// Spicepod configuration
Spicepod types.String `tfsdk:"spicepod"`
// Runtime configuration
Registry types.String `tfsdk:"registry"`
Image types.String `tfsdk:"image"`
ImageTag types.String `tfsdk:"image_tag"`
UpdateChannel types.String `tfsdk:"update_channel"`
Replicas types.Int64 `tfsdk:"replicas"`
NodeGroup types.String `tfsdk:"node_group"`
Region types.String `tfsdk:"region"`
StorageClaimSizeGB types.Float64 `tfsdk:"storage_claim_size_gb"`
// Read-only attributes
CreatedAt types.String `tfsdk:"created_at"`
ClusterID types.String `tfsdk:"cluster_id"`
APIKey types.String `tfsdk:"api_key"`
}
func (d *AppDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_app"
}
func (d *AppDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Retrieves details about an existing Spice.ai app by its ID.",
Attributes: map[string]schema.Attribute{
// Identity attributes
"id": schema.StringAttribute{
MarkdownDescription: "The unique identifier of the app.",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the app.",
Computed: true,
},
// Basic configuration attributes
"description": schema.StringAttribute{
MarkdownDescription: "A description of the app.",
Computed: true,
},
"visibility": schema.StringAttribute{
MarkdownDescription: "The visibility of the app (`public` or `private`).",
Computed: true,
},
"production_branch": schema.StringAttribute{
MarkdownDescription: "The production branch for the app.",
Computed: true,
},
"tags": schema.MapAttribute{
MarkdownDescription: "Key-value tags for the app.",
Computed: true,
ElementType: types.StringType,
},
"cname": schema.StringAttribute{
MarkdownDescription: "The region identifier (cname) for the app.",
Computed: true,
},
// Spicepod configuration
"spicepod": schema.StringAttribute{
MarkdownDescription: "The spicepod configuration as a JSON string.",
Computed: true,
},
// Runtime configuration attributes
"registry": schema.StringAttribute{
MarkdownDescription: "Registry for the spiced image.",
Computed: true,
},
"image": schema.StringAttribute{
MarkdownDescription: "Image name for the spiced container.",
Computed: true,
},
"image_tag": schema.StringAttribute{
MarkdownDescription: "The Spice.ai runtime image tag.",
Computed: true,
},
"update_channel": schema.StringAttribute{
MarkdownDescription: "Update channel for the spicepod.",
Computed: true,
},
"replicas": schema.Int64Attribute{
MarkdownDescription: "The number of replicas.",
Computed: true,
},
"node_group": schema.StringAttribute{
MarkdownDescription: "The node group for the app.",
Computed: true,
},
"region": schema.StringAttribute{
MarkdownDescription: "The region where the app is deployed.",
Computed: true,
},
"storage_claim_size_gb": schema.Float64Attribute{
MarkdownDescription: "The storage claim size in GB.",
Computed: true,
},
// Read-only attributes
"created_at": schema.StringAttribute{
MarkdownDescription: "The timestamp when the app was created.",
Computed: true,
},
"cluster_id": schema.StringAttribute{
MarkdownDescription: "The Kubernetes cluster identifier where the app is deployed.",
Computed: true,
},
"api_key": schema.StringAttribute{
MarkdownDescription: "The API key for the app.",
Computed: true,
Sensitive: true,
},
},
}
}
func (d *AppDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*client.SpiceAIClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *client.SpiceAIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *AppDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data AppDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
appID, err := strconv.ParseInt(data.ID.ValueString(), 10, 64)
if err != nil {
resp.Diagnostics.AddError("Invalid App ID", fmt.Sprintf("Unable to parse app ID: %s", err))
return
}
app, err := d.client.GetApp(ctx, appID)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read app, got error: %s", err))
return
}
if app == nil {
resp.Diagnostics.AddError("Not Found", fmt.Sprintf("App with ID %d not found", appID))
return
}
// Map response to model
d.mapAppToModel(&data, app)
tflog.Trace(ctx, "read app data source", map[string]interface{}{
"id": app.ID,
"name": app.Name,
})
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
// mapAppToModel maps an API App response to the data source model.
func (d *AppDataSource) mapAppToModel(data *AppDataSourceModel, app *client.App) {
data.ID = types.StringValue(strconv.FormatInt(app.ID, 10))
data.Name = types.StringValue(app.Name)
data.Description = types.StringValue(app.Description)
data.Visibility = types.StringValue(app.Visibility)
data.ProductionBranch = types.StringValue(app.ProductionBranch)
// Map tags
if len(app.Tags) > 0 {
tagElements := make(map[string]attr.Value)
for k, v := range app.Tags {
tagElements[k] = types.StringValue(v)
}
data.Tags = types.MapValueMust(types.StringType, tagElements)
} else {
data.Tags = types.MapNull(types.StringType)
}
data.CreatedAt = types.StringValue(app.CreatedAt)
data.ClusterID = types.StringValue(app.ClusterID)
data.APIKey = types.StringValue(app.APIKey)
// Map cname
if app.Cname != "" {
data.Cname = types.StringValue(app.Cname)
} else {
data.Cname = types.StringNull()
}
// Region is inside config
if app.Config != nil && app.Config.Region != "" {
data.Region = types.StringValue(app.Config.Region)
} else {
data.Region = types.StringNull()
}
// Map config fields if available
if app.Config != nil {
data.Registry = types.StringValue(app.Config.Registry)
data.Image = types.StringValue(app.Config.Image)
data.ImageTag = types.StringValue(app.Config.ImageTag)
data.UpdateChannel = types.StringValue(app.Config.UpdateChannel)
data.Replicas = types.Int64Value(int64(app.Config.Replicas))
data.NodeGroup = types.StringValue(app.Config.NodeGroup)
data.StorageClaimSizeGB = types.Float64Value(app.Config.StorageClaimSizeGB)
// Handle spicepod - convert to JSON string if present
if app.Config.Spicepod != nil {
if spicepodStr, ok := app.Config.Spicepod.(string); ok {
data.Spicepod = types.StringValue(spicepodStr)
} else {
if spicepodBytes, err := json.Marshal(app.Config.Spicepod); err == nil {
data.Spicepod = types.StringValue(string(spicepodBytes))
} else {
data.Spicepod = types.StringNull()
}
}
} else {
data.Spicepod = types.StringNull()
}
} else {
data.Registry = types.StringNull()
data.Image = types.StringNull()
data.ImageTag = types.StringNull()
data.UpdateChannel = types.StringNull()
data.Replicas = types.Int64Null()
data.NodeGroup = types.StringNull()
data.StorageClaimSizeGB = types.Float64Null()
data.Spicepod = types.StringNull()
}
}