5
5
"strconv"
6
6
"time"
7
7
8
+ "github.com/hashicorp/terraform-plugin-framework/attr"
8
9
"github.com/hashicorp/terraform-plugin-framework/datasource"
9
10
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
10
11
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -39,6 +40,7 @@ type projectDataSourceModel struct {
39
40
GrafanaFolderUID types.String `tfsdk:"grafana_folder_uid"`
40
41
Created types.String `tfsdk:"created"`
41
42
Updated types.String `tfsdk:"updated"`
43
+ AllowedLoadZones types.List `tfsdk:"allowed_load_zones"`
42
44
}
43
45
44
46
// projectDataSource is the data source implementation.
@@ -80,6 +82,11 @@ func (d *projectDataSource) Schema(_ context.Context, _ datasource.SchemaRequest
80
82
Description : "The date when the project was last updated." ,
81
83
Computed : true ,
82
84
},
85
+ "allowed_load_zones" : schema.ListAttribute {
86
+ Description : "List of allowed k6 load zone IDs for this project." ,
87
+ Computed : true ,
88
+ ElementType : types .StringType ,
89
+ },
83
90
},
84
91
}
85
92
}
@@ -122,6 +129,27 @@ func (d *projectDataSource) Read(ctx context.Context, req datasource.ReadRequest
122
129
state .Created = types .StringValue (p .GetCreated ().Format (time .RFC3339Nano ))
123
130
state .Updated = types .StringValue (p .GetUpdated ().Format (time .RFC3339Nano ))
124
131
132
+ // Get allowed load zones
133
+ allowedZones , err := d .getAllowedLoadZones (ctx , p .GetId ())
134
+ if err != nil {
135
+ resp .Diagnostics .AddError (
136
+ "Error reading allowed load zones" ,
137
+ "Could not read allowed load zones for k6 project: " + err .Error (),
138
+ )
139
+ return
140
+ }
141
+
142
+ // Convert to types.List
143
+ var zoneValues []attr.Value
144
+ for _ , zone := range allowedZones {
145
+ zoneValues = append (zoneValues , types .StringValue (zone ))
146
+ }
147
+ state .AllowedLoadZones , diags = types .ListValue (types .StringType , zoneValues )
148
+ resp .Diagnostics .Append (diags ... )
149
+ if resp .Diagnostics .HasError () {
150
+ return
151
+ }
152
+
125
153
diags = resp .State .Set (ctx , & state )
126
154
resp .Diagnostics .Append (diags ... )
127
155
}
@@ -132,3 +160,23 @@ func handleGrafanaFolderUID(grafanaFolderUID k6.NullableString) types.String {
132
160
}
133
161
return types .StringValue (* grafanaFolderUID .Get ())
134
162
}
163
+
164
+ // getAllowedLoadZones retrieves the allowed load zones for a project
165
+ // Returns k6_load_zone_ids directly from the API response
166
+ func (d * projectDataSource ) getAllowedLoadZones (ctx context.Context , projectID int32 ) ([]string , error ) {
167
+ ctx = context .WithValue (ctx , k6 .ContextAccessToken , d .config .Token )
168
+
169
+ resp , _ , err := d .client .LoadZonesAPI .ProjectsAllowedLoadZonesRetrieve (ctx , projectID ).
170
+ XStackId (d .config .StackID ).
171
+ Execute ()
172
+ if err != nil {
173
+ return nil , err
174
+ }
175
+
176
+ var k6LoadZoneIds []string
177
+ for _ , zone := range resp .GetValue () {
178
+ k6LoadZoneIds = append (k6LoadZoneIds , zone .GetK6LoadZoneId ())
179
+ }
180
+
181
+ return k6LoadZoneIds , nil
182
+ }
0 commit comments