Skip to content

Commit 620ca9e

Browse files
committed
feat(imagev2): add architecture attribute to filter
Signed-off-by: Mauritz Uphoff <mauritz.uphoff@stackit.cloud>
1 parent 706fc7b commit 620ca9e

6 files changed

Lines changed: 183 additions & 24 deletions

File tree

docs/data-sources/image_v2.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,50 @@ data "stackit_image_v2" "default" {
7070
data "stackit_image_v2" "name_match" {
7171
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
7272
name = "Ubuntu 22.04"
73+
filter = {
74+
architecture = "x86"
75+
}
7376
}
7477
7578
data "stackit_image_v2" "name_regex_latest" {
7679
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
7780
name_regex = "^Ubuntu .*"
81+
filter = {
82+
architecture = "x86"
83+
}
7884
}
7985
8086
data "stackit_image_v2" "name_regex_oldest" {
8187
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
8288
name_regex = "^Ubuntu .*"
8389
sort_ascending = true
90+
filter = {
91+
architecture = "x86"
92+
}
8493
}
8594
8695
data "stackit_image_v2" "filter_distro_version" {
8796
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
8897
filter = {
89-
distro = "debian"
90-
version = "11"
98+
distro = "debian"
99+
version = "11"
100+
architecture = "x86"
101+
}
102+
}
103+
104+
data "stackit_image_v2" "filter_architecture_x86" {
105+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
106+
filter = {
107+
distro = "ubuntu"
108+
architecture = "x86"
109+
}
110+
}
111+
112+
data "stackit_image_v2" "filter_architecture_arm64" {
113+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
114+
filter = {
115+
distro = "ubuntu"
116+
architecture = "arm64"
91117
}
92118
}
93119
```
@@ -125,6 +151,7 @@ data "stackit_image_v2" "filter_distro_version" {
125151

126152
Optional:
127153

154+
- `architecture` (String) Filter images by CPU architecture. Possible values: `arm64`, `x86`.
128155
- `distro` (String) Filter images by operating system distribution. For example: `ubuntu`, `debian`, `rhel`, etc.
129156
- `os` (String) Filter images by operating system type, such as `linux` or `windows`.
130157
- `secure_boot` (Boolean) Filter images with Secure Boot support. Set to `true` to match images that support Secure Boot.
@@ -146,6 +173,7 @@ Read-Only:
146173

147174
Read-Only:
148175

176+
- `architecture` (String) CPU architecture of the image. Possible values: `arm64`, `x86`.
149177
- `boot_menu` (Boolean) Enables the BIOS bootmenu.
150178
- `cdrom_bus` (String) Sets CDROM bus controller type.
151179
- `disk_bus` (String) Sets Disk bus controller type.

examples/data-sources/stackit_image_v2/data-source.tf

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,49 @@ data "stackit_image_v2" "default" {
66
data "stackit_image_v2" "name_match" {
77
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
88
name = "Ubuntu 22.04"
9+
filter = {
10+
architecture = "x86"
11+
}
912
}
1013

1114
data "stackit_image_v2" "name_regex_latest" {
1215
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1316
name_regex = "^Ubuntu .*"
17+
filter = {
18+
architecture = "x86"
19+
}
1420
}
1521

1622
data "stackit_image_v2" "name_regex_oldest" {
1723
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1824
name_regex = "^Ubuntu .*"
1925
sort_ascending = true
26+
filter = {
27+
architecture = "x86"
28+
}
2029
}
2130

2231
data "stackit_image_v2" "filter_distro_version" {
2332
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2433
filter = {
25-
distro = "debian"
26-
version = "11"
34+
distro = "debian"
35+
version = "11"
36+
architecture = "x86"
37+
}
38+
}
39+
40+
data "stackit_image_v2" "filter_architecture_x86" {
41+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
42+
filter = {
43+
distro = "ubuntu"
44+
architecture = "x86"
45+
}
46+
}
47+
48+
data "stackit_image_v2" "filter_architecture_arm64" {
49+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
50+
filter = {
51+
distro = "ubuntu"
52+
architecture = "arm64"
2753
}
2854
}

stackit/internal/services/iaas/iaas_acc_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5097,6 +5097,17 @@ func TestAccImageDatasourceSearchVariants(t *testing.T) {
50975097
resource.TestCheckResourceAttrSet("data.stackit_image_v2.ubuntu_oldest", "checksum.algorithm"),
50985098
resource.TestCheckResourceAttrSet("data.stackit_image_v2.ubuntu_oldest", "checksum.digest"),
50995099

5100+
resource.TestCheckResourceAttr("data.stackit_image_v2.filter_ubuntu_x86", "project_id", testutil.ConvertConfigVariable(testConfigImageVarsMax["project_id"])),
5101+
resource.TestCheckResourceAttrSet("data.stackit_image_v2.filter_ubuntu_x86", "image_id"),
5102+
resource.TestCheckResourceAttrSet("data.stackit_image_v2.filter_ubuntu_x86", "name"),
5103+
resource.TestCheckResourceAttrSet("data.stackit_image_v2.filter_ubuntu_x86", "min_disk_size"),
5104+
resource.TestCheckResourceAttrSet("data.stackit_image_v2.filter_ubuntu_x86", "min_ram"),
5105+
resource.TestCheckResourceAttrSet("data.stackit_image_v2.filter_ubuntu_x86", "protected"),
5106+
resource.TestCheckResourceAttrSet("data.stackit_image_v2.filter_ubuntu_x86", "scope"),
5107+
resource.TestCheckResourceAttrSet("data.stackit_image_v2.filter_ubuntu_x86", "checksum.algorithm"),
5108+
resource.TestCheckResourceAttrSet("data.stackit_image_v2.filter_ubuntu_x86", "checksum.digest"),
5109+
resource.TestCheckResourceAttr("data.stackit_image_v2.filter_ubuntu_x86", "config.architecture", "x86"),
5110+
51005111
// e2e test that ascending sort is working
51015112
func(s *terraform.State) error {
51025113
latest := s.RootModule().Resources["data.stackit_image_v2.ubuntu_latest"]

stackit/internal/services/iaas/imagev2/datasource.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,17 @@ type DataSourceModel struct {
5555
}
5656

5757
type Filter struct {
58-
OS types.String `tfsdk:"os"`
59-
Distro types.String `tfsdk:"distro"`
60-
Version types.String `tfsdk:"version"`
61-
UEFI types.Bool `tfsdk:"uefi"`
62-
SecureBoot types.Bool `tfsdk:"secure_boot"`
58+
OS types.String `tfsdk:"os"`
59+
Distro types.String `tfsdk:"distro"`
60+
Version types.String `tfsdk:"version"`
61+
UEFI types.Bool `tfsdk:"uefi"`
62+
SecureBoot types.Bool `tfsdk:"secure_boot"`
63+
Architecture types.String `tfsdk:"architecture"`
6364
}
6465

6566
// Struct corresponding to Model.Config
6667
type configModel struct {
68+
Architecture types.String `tfsdk:"architecture"`
6769
BootMenu types.Bool `tfsdk:"boot_menu"`
6870
CDROMBus types.String `tfsdk:"cdrom_bus"`
6971
DiskBus types.String `tfsdk:"disk_bus"`
@@ -81,6 +83,7 @@ type configModel struct {
8183

8284
// Types corresponding to configModel
8385
var configTypes = map[string]attr.Type{
86+
"architecture": basetypes.StringType{},
8487
"boot_menu": basetypes.BoolType{},
8588
"cdrom_bus": basetypes.StringType{},
8689
"disk_bus": basetypes.StringType{},
@@ -253,6 +256,10 @@ func (d *imageDataV2Source) Schema(_ context.Context, _ datasource.SchemaRequest
253256
Optional: true,
254257
Description: "Filter images with Secure Boot support. Set to `true` to match images that support Secure Boot.",
255258
},
259+
"architecture": schema.StringAttribute{
260+
Optional: true,
261+
Description: "Filter images by CPU architecture. Possible values: `arm64`, `x86`.",
262+
},
256263
},
257264
},
258265
"disk_format": schema.StringAttribute{
@@ -279,6 +286,10 @@ func (d *imageDataV2Source) Schema(_ context.Context, _ datasource.SchemaRequest
279286
Description: "Properties to set hardware and scheduling settings for an image.",
280287
Computed: true,
281288
Attributes: map[string]schema.Attribute{
289+
"architecture": schema.StringAttribute{
290+
Description: "CPU architecture of the image. Possible values: `arm64`, `x86`.",
291+
Computed: true,
292+
},
282293
"boot_menu": schema.BoolAttribute{
283294
Description: "Enables the BIOS bootmenu.",
284295
Computed: true,
@@ -509,6 +520,7 @@ func mapDataSourceFields(ctx context.Context, imageResp *iaas.Image, model *Data
509520
var configObject basetypes.ObjectValue
510521
diags := diag.Diagnostics{}
511522
if imageResp.Config != nil {
523+
configModel.Architecture = types.StringPointerValue(imageResp.Config.Architecture)
512524
configModel.BootMenu = types.BoolPointerValue(imageResp.Config.BootMenu)
513525
configModel.CDROMBus = types.StringPointerValue(imageResp.Config.CdromBus.Get())
514526
configModel.DiskBus = types.StringPointerValue(imageResp.Config.DiskBus.Get())
@@ -524,6 +536,7 @@ func mapDataSourceFields(ctx context.Context, imageResp *iaas.Image, model *Data
524536
configModel.VirtioScsi = types.BoolPointerValue(new(imageResp.Config.GetVirtioScsi()))
525537

526538
configObject, diags = types.ObjectValue(configTypes, map[string]attr.Value{
539+
"architecture": configModel.Architecture,
527540
"boot_menu": configModel.BootMenu,
528541
"cdrom_bus": configModel.CDROMBus,
529542
"disk_bus": configModel.DiskBus,
@@ -620,6 +633,11 @@ func imageMatchesFilter(img *iaas.Image, filter *Filter) bool {
620633
return false
621634
}
622635

636+
if !filter.Architecture.IsNull() &&
637+
(cfg.Architecture == nil || filter.Architecture.ValueString() != *cfg.Architecture) {
638+
return false
639+
}
640+
623641
return true
624642
}
625643

stackit/internal/services/iaas/imagev2/datasource_test.go

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func TestMapDataSourceFields(t *testing.T) {
6262
Protected: new(true),
6363
Scope: new("scope"),
6464
Config: &iaas.ImageConfig{
65+
Architecture: new("x86"),
6566
BootMenu: new(true),
6667
CdromBus: *iaas.NewNullableString(new("cdrom_bus")),
6768
DiskBus: *iaas.NewNullableString(new("disk_bus")),
@@ -97,6 +98,7 @@ func TestMapDataSourceFields(t *testing.T) {
9798
Protected: types.BoolValue(true),
9899
Scope: types.StringValue("scope"),
99100
Config: types.ObjectValueMust(configTypes, map[string]attr.Value{
101+
"architecture": types.StringValue("x86"),
100102
"boot_menu": types.BoolValue(true),
101103
"cdrom_bus": types.StringValue("cdrom_bus"),
102104
"disk_bus": types.StringValue("disk_bus"),
@@ -206,14 +208,16 @@ func TestImageMatchesFilter(t *testing.T) {
206208
OperatingSystemVersion: *iaas.NewNullableString(new("22.04")),
207209
Uefi: new(true),
208210
SecureBoot: new(true),
211+
Architecture: new("x86"),
209212
},
210213
},
211214
filter: &Filter{
212-
OS: types.StringValue("linux"),
213-
Distro: types.StringValue("ubuntu"),
214-
Version: types.StringValue("22.04"),
215-
UEFI: types.BoolValue(true),
216-
SecureBoot: types.BoolValue(true),
215+
OS: types.StringValue("linux"),
216+
Distro: types.StringValue("ubuntu"),
217+
Version: types.StringValue("22.04"),
218+
UEFI: types.BoolValue(true),
219+
SecureBoot: types.BoolValue(true),
220+
Architecture: types.StringValue("x86"),
217221
},
218222
expected: true,
219223
},
@@ -313,6 +317,66 @@ func TestImageMatchesFilter(t *testing.T) {
313317
},
314318
expected: false,
315319
},
320+
{
321+
name: "Architecture match x86",
322+
img: &iaas.Image{
323+
Config: &iaas.ImageConfig{
324+
Architecture: new("x86"),
325+
},
326+
},
327+
filter: &Filter{
328+
Architecture: types.StringValue("x86"),
329+
},
330+
expected: true,
331+
},
332+
{
333+
name: "Architecture match arm64",
334+
img: &iaas.Image{
335+
Config: &iaas.ImageConfig{
336+
Architecture: new("arm64"),
337+
},
338+
},
339+
filter: &Filter{
340+
Architecture: types.StringValue("arm64"),
341+
},
342+
expected: true,
343+
},
344+
{
345+
name: "Architecture mismatch",
346+
img: &iaas.Image{
347+
Config: &iaas.ImageConfig{
348+
Architecture: new("arm64"),
349+
},
350+
},
351+
filter: &Filter{
352+
Architecture: types.StringValue("x86"),
353+
},
354+
expected: false,
355+
},
356+
{
357+
name: "Architecture filter set but image has nil architecture",
358+
img: &iaas.Image{
359+
Config: &iaas.ImageConfig{
360+
Architecture: nil,
361+
},
362+
},
363+
filter: &Filter{
364+
Architecture: types.StringValue("x86"),
365+
},
366+
expected: false,
367+
},
368+
{
369+
name: "Architecture filter null skip check",
370+
img: &iaas.Image{
371+
Config: &iaas.ImageConfig{
372+
Architecture: new("arm64"),
373+
},
374+
},
375+
filter: &Filter{
376+
Architecture: types.StringNull(),
377+
},
378+
expected: true,
379+
},
316380
{
317381
name: "partial filter match - only distro set and match",
318382
img: &iaas.Image{
@@ -358,14 +422,16 @@ func TestImageMatchesFilter(t *testing.T) {
358422
OperatingSystemVersion: *iaas.NewNullableString(new("22.04")),
359423
Uefi: new(false),
360424
SecureBoot: new(false),
425+
Architecture: new("x86"),
361426
},
362427
},
363428
filter: &Filter{
364-
OS: types.StringNull(),
365-
Distro: types.StringNull(),
366-
Version: types.StringNull(),
367-
UEFI: types.BoolNull(),
368-
SecureBoot: types.BoolNull(),
429+
OS: types.StringNull(),
430+
Distro: types.StringNull(),
431+
Version: types.StringNull(),
432+
UEFI: types.BoolNull(),
433+
SecureBoot: types.BoolNull(),
434+
Architecture: types.StringNull(),
369435
},
370436
expected: true,
371437
},
@@ -378,14 +444,16 @@ func TestImageMatchesFilter(t *testing.T) {
378444
OperatingSystemVersion: *iaas.NewNullableString(nil),
379445
Uefi: nil,
380446
SecureBoot: nil,
447+
Architecture: nil,
381448
},
382449
},
383450
filter: &Filter{
384-
OS: types.StringValue("linux"),
385-
Distro: types.StringValue("ubuntu"),
386-
Version: types.StringValue("22.04"),
387-
UEFI: types.BoolValue(true),
388-
SecureBoot: types.BoolValue(true),
451+
OS: types.StringValue("linux"),
452+
Distro: types.StringValue("ubuntu"),
453+
Version: types.StringValue("22.04"),
454+
UEFI: types.BoolValue(true),
455+
SecureBoot: types.BoolValue(true),
456+
Architecture: types.StringValue("x86"),
389457
},
390458
expected: false,
391459
},

stackit/internal/services/iaas/testdata/datasource-image-v2-variants.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ data "stackit_image_v2" "ubuntu_oldest" {
5959
distro = "ubuntu"
6060
}
6161
sort_ascending = true
62+
}
63+
64+
data "stackit_image_v2" "filter_ubuntu_x86" {
65+
project_id = var.project_id
66+
filter = {
67+
distro = "ubuntu"
68+
architecture = "x86"
69+
}
6270
}

0 commit comments

Comments
 (0)