Skip to content

Commit fedaf72

Browse files
authored
Feat/add datasource to query machine types (#968)
* feat(iaas): add datasource to query machine types Signed-off-by: Mauritz Uphoff <[email protected]> * review Signed-off-by: Mauritz Uphoff <[email protected]> * review Signed-off-by: Mauritz Uphoff <[email protected]> --------- Signed-off-by: Mauritz Uphoff <[email protected]>
1 parent 64787ff commit fedaf72

File tree

8 files changed

+662
-0
lines changed

8 files changed

+662
-0
lines changed

docs/data-sources/machine_type.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "stackit_machine_type Data Source - stackit"
4+
subcategory: ""
5+
description: |-
6+
Machine type data source.
7+
~> This datasource is in beta and may be subject to breaking changes in the future. Use with caution. See our guide https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources for how to opt-in to use beta resources.
8+
---
9+
10+
# stackit_machine_type (Data Source)
11+
12+
Machine type data source.
13+
14+
~> This datasource is in beta and may be subject to breaking changes in the future. Use with caution. See our [guide](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources) for how to opt-in to use beta resources.
15+
16+
## Example Usage
17+
18+
```terraform
19+
data "stackit_machine_type" "two_vcpus_filter" {
20+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
21+
filter = "vcpus==2"
22+
}
23+
24+
data "stackit_machine_type" "filter_sorted_ascending_false" {
25+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
26+
filter = "vcpus >= 2 && ram >= 2048"
27+
sort_ascending = false
28+
}
29+
30+
data "stackit_machine_type" "intel_icelake_generic_filter" {
31+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
32+
filter = "extraSpecs.cpu==\"intel-icelake-generic\" && vcpus == 2"
33+
}
34+
35+
# returns warning
36+
data "stackit_machine_type" "no_match" {
37+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
38+
filter = "vcpus == 99"
39+
}
40+
```
41+
42+
<!-- schema generated by tfplugindocs -->
43+
## Schema
44+
45+
### Required
46+
47+
- `filter` (String) Expr-lang filter for filtering machine types.
48+
49+
Examples:
50+
- vcpus == 2
51+
- ram >= 2048
52+
- extraSpecs.cpu == "intel-icelake-generic"
53+
- extraSpecs.cpu == "intel-icelake-generic" && vcpus == 2
54+
55+
See https://expr-lang.org/docs/language-definition for syntax.
56+
- `project_id` (String) STACKIT Project ID.
57+
58+
### Optional
59+
60+
- `sort_ascending` (Boolean) Sort machine types by name ascending (`true`) or descending (`false`). Defaults to `false`
61+
62+
### Read-Only
63+
64+
- `description` (String) Machine type description.
65+
- `disk` (Number) Disk size in GB.
66+
- `extra_specs` (Map of String) Extra specs (e.g., CPU type, overcommit ratio).
67+
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`image_id`".
68+
- `name` (String) Name of the machine type (e.g. 's1.2').
69+
- `ram` (Number) RAM size in MB.
70+
- `vcpus` (Number) Number of vCPUs.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
data "stackit_machine_type" "two_vcpus_filter" {
2+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
3+
filter = "vcpus==2"
4+
}
5+
6+
data "stackit_machine_type" "filter_sorted_ascending_false" {
7+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
8+
filter = "vcpus >= 2 && ram >= 2048"
9+
sort_ascending = false
10+
}
11+
12+
data "stackit_machine_type" "intel_icelake_generic_filter" {
13+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
14+
filter = "extraSpecs.cpu==\"intel-icelake-generic\" && vcpus == 2"
15+
}
16+
17+
# returns warning
18+
data "stackit_machine_type" "no_match" {
19+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
20+
filter = "vcpus == 99"
21+
}

stackit/internal/services/iaas/iaas_acc_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ var (
8888

8989
//go:embed testdata/resource-server-max-server-attachments.tf
9090
resourceServerMaxAttachmentConfig string
91+
92+
//go:embed testdata/datasource-machinetype.tf
93+
dataSourceMachineTypeConfig string
9194
)
9295

9396
const (
@@ -487,6 +490,10 @@ var testConfigKeyPairMaxUpdated = func() config.Variables {
487490
return updatedConfig
488491
}()
489492

493+
var testConfigMachineTypeVars = config.Variables{
494+
"project_id": config.StringVariable(testutil.ProjectId),
495+
}
496+
490497
// if no local file is provided the test should create a default file and work with this instead of failing
491498
var localFileForIaasImage os.File
492499

@@ -4054,6 +4061,47 @@ func TestAccProject(t *testing.T) {
40544061
})
40554062
}
40564063

4064+
func TestAccMachineType(t *testing.T) {
4065+
t.Logf("TestAccMachineType projectid: %s", testutil.ConvertConfigVariable(testConfigMachineTypeVars["project_id"]))
4066+
resource.ParallelTest(t, resource.TestCase{
4067+
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
4068+
Steps: []resource.TestStep{
4069+
{
4070+
ConfigVariables: testConfigMachineTypeVars,
4071+
Config: fmt.Sprintf("%s\n%s", dataSourceMachineTypeConfig, testutil.IaaSProviderConfigWithBetaResourcesEnabled()),
4072+
Check: resource.ComposeTestCheckFunc(
4073+
resource.TestCheckResourceAttr("data.stackit_machine_type.two_vcpus_filter", "project_id", testutil.ConvertConfigVariable(testConfigMachineTypeVars["project_id"])),
4074+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.two_vcpus_filter", "id"),
4075+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.two_vcpus_filter", "name"),
4076+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.two_vcpus_filter", "vcpus"),
4077+
resource.TestCheckResourceAttr("data.stackit_machine_type.two_vcpus_filter", "vcpus", "2"),
4078+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.two_vcpus_filter", "ram"),
4079+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.two_vcpus_filter", "disk"),
4080+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.two_vcpus_filter", "description"),
4081+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.two_vcpus_filter", "extra_specs.cpu"),
4082+
4083+
resource.TestCheckResourceAttr("data.stackit_machine_type.filter_sorted_ascending_false", "project_id", testutil.ConvertConfigVariable(testConfigMachineTypeVars["project_id"])),
4084+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.filter_sorted_ascending_false", "id"),
4085+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.filter_sorted_ascending_false", "name"),
4086+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.filter_sorted_ascending_false", "vcpus"),
4087+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.filter_sorted_ascending_false", "ram"),
4088+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.filter_sorted_ascending_false", "disk"),
4089+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.filter_sorted_ascending_false", "description"),
4090+
resource.TestCheckResourceAttrSet("data.stackit_machine_type.filter_sorted_ascending_false", "extra_specs.cpu"),
4091+
4092+
resource.TestCheckResourceAttr("data.stackit_machine_type.no_match", "project_id", testutil.ConvertConfigVariable(testConfigMachineTypeVars["project_id"])),
4093+
resource.TestCheckNoResourceAttr("data.stackit_machine_type.no_match", "description"),
4094+
resource.TestCheckNoResourceAttr("data.stackit_machine_type.no_match", "disk"),
4095+
resource.TestCheckNoResourceAttr("data.stackit_machine_type.no_match", "extra_specs"),
4096+
resource.TestCheckNoResourceAttr("data.stackit_machine_type.no_match", "id"),
4097+
resource.TestCheckNoResourceAttr("data.stackit_machine_type.no_match", "name"),
4098+
resource.TestCheckNoResourceAttr("data.stackit_machine_type.no_match", "ram"),
4099+
),
4100+
},
4101+
},
4102+
})
4103+
}
4104+
40574105
func testAccCheckDestroy(s *terraform.State) error {
40584106
checkFunctions := []func(s *terraform.State) error{
40594107
testAccCheckNetworkV1Destroy,

0 commit comments

Comments
 (0)