Skip to content

Commit 7a229e6

Browse files
authored
feat(marketplace_image): add image_type filter (#2943)
* feat(marketplace_image): add image_type filter * add local image type validation
1 parent 108d512 commit 7a229e6

5 files changed

+381
-48
lines changed

docs/data-sources/marketplace_image.md

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ to find the right `label`.
2323
- `instance_type` - (Optional, default `DEV1-S`) The instance type the image is compatible with.
2424
You find all the available types on the [pricing page](https://www.scaleway.com/en/pricing/).
2525

26+
- `image_type` - (Optional, default `instance_local`) The local image type, `instance_local` or `instance_sbs`.
27+
2628
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the image exists.
2729

2830
## Attributes Reference

internal/services/marketplace/image_data_source.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/scaleway/scaleway-sdk-go/scw"
1010
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
1111
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1213
)
1314

1415
func DataSourceImage() *schema.Resource {
@@ -26,6 +27,13 @@ func DataSourceImage() *schema.Resource {
2627
Default: "DEV1-S",
2728
Description: "The instance commercial type of the desired image",
2829
},
30+
"image_type": {
31+
Type: schema.TypeString,
32+
Optional: true,
33+
Default: "instance_local", // Keep the old default as default to avoid a breaking change.
34+
Description: "The type of the desired image, instance_local or instance_sbs",
35+
ValidateDiagFunc: verify.ValidateEnum[marketplace.LocalImageType](),
36+
},
2937
"zone": zonal.Schema(),
3038
},
3139
}
@@ -41,7 +49,7 @@ func DataSourceMarketplaceImageRead(ctx context.Context, d *schema.ResourceData,
4149
ImageLabel: d.Get("label").(string),
4250
CommercialType: d.Get("instance_type").(string),
4351
Zone: zone,
44-
Type: marketplace.LocalImageTypeInstanceLocal,
52+
Type: marketplace.LocalImageType(d.Get("image_type").(string)),
4553
}, scw.WithContext(ctx))
4654
if err != nil {
4755
return diag.FromErr(err)
@@ -52,6 +60,7 @@ func DataSourceMarketplaceImageRead(ctx context.Context, d *schema.ResourceData,
5260
_ = d.Set("zone", zone)
5361
_ = d.Set("label", d.Get("label"))
5462
_ = d.Set("instance_type", d.Get("type"))
63+
_ = d.Set("image_type", image.Type)
5564

5665
return nil
5766
}

internal/services/marketplace/image_data_source_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ func TestAccDataSourceMarketplaceImage_Basic(t *testing.T) {
2424
Check: resource.ComposeTestCheckFunc(
2525
instancechecks.DoesImageExists(tt, "data.scaleway_marketplace_image.test1"),
2626
resource.TestCheckResourceAttr("data.scaleway_marketplace_image.test1", "label", "ubuntu_focal"),
27+
resource.TestCheckResourceAttr("data.scaleway_marketplace_image.test1", "image_type", "instance_local"),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
func TestAccDataSourceMarketplaceImage_SBS(t *testing.T) {
35+
tt := acctest.NewTestTools(t)
36+
defer tt.Cleanup()
37+
resource.ParallelTest(t, resource.TestCase{
38+
PreCheck: func() { acctest.PreCheck(t) },
39+
ProviderFactories: tt.ProviderFactories,
40+
Steps: []resource.TestStep{
41+
{
42+
Config: `
43+
data "scaleway_marketplace_image" "test1" {
44+
label = "ubuntu_focal"
45+
image_type = "instance_sbs"
46+
}
47+
`,
48+
Check: resource.ComposeTestCheckFunc(
49+
instancechecks.DoesImageExists(tt, "data.scaleway_marketplace_image.test1"),
50+
resource.TestCheckResourceAttr("data.scaleway_marketplace_image.test1", "label", "ubuntu_focal"),
51+
resource.TestCheckResourceAttr("data.scaleway_marketplace_image.test1", "image_type", "instance_sbs"),
2752
),
2853
},
2954
},

0 commit comments

Comments
 (0)