Skip to content

Commit ebf7deb

Browse files
committed
all set
1 parent ab4bcb1 commit ebf7deb

File tree

4 files changed

+506
-1245
lines changed

4 files changed

+506
-1245
lines changed

internal/services/instance/server_type_data_source.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
9+
product_catalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
910
"github.com/scaleway/scaleway-sdk-go/scw"
1011
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1113
)
1214

1315
func DataSourceServerType() *schema.Resource {
@@ -129,11 +131,6 @@ func DataSourceServerType() *schema.Resource {
129131
Computed: true,
130132
Description: "The hourly price of the server type in euro",
131133
},
132-
"monthly_price": {
133-
Type: schema.TypeFloat,
134-
Computed: true,
135-
Description: "The monthly price of the server type in euro",
136-
},
137134
"end_of_service": {
138135
Type: schema.TypeBool,
139136
Computed: true,
@@ -171,19 +168,40 @@ func DataSourceInstanceServerTypeRead(ctx context.Context, d *schema.ResourceDat
171168

172169
d.SetId(name)
173170
_ = d.Set("name", name)
171+
_ = d.Set("zone", zone)
174172
_ = d.Set("arch", serverType.Arch)
175173
_ = d.Set("cpu", int(serverType.Ncpus))
176174
_ = d.Set("ram", int(serverType.RAM))
177175
_ = d.Set("volumes", flattenVolumeConstraints(serverType))
178176
_ = d.Set("capabilities", flattenCapabilities(serverType.Capabilities))
179177
_ = d.Set("network", flattenNetwork(serverType))
180-
_ = d.Set("hourly_price", serverType.HourlyPrice)
181178
_ = d.Set("end_of_service", serverType.EndOfService)
182179

183180
if serverType.Gpu != nil {
184181
_ = d.Set("gpu", int(*serverType.Gpu))
185182
}
186183

184+
// Price (needs to be fetched from the Product Catalog)
185+
pcuAPI := product_catalog.NewPublicCatalogAPI(meta.ExtractScwClient(i))
186+
187+
pcuInstances, err := pcuAPI.ListPublicCatalogProducts(&product_catalog.PublicCatalogAPIListPublicCatalogProductsRequest{
188+
ProductTypes: []product_catalog.ListPublicCatalogProductsRequestProductType{
189+
product_catalog.ListPublicCatalogProductsRequestProductTypeInstance,
190+
},
191+
Zone: &zone,
192+
}, scw.WithAllPages(), scw.WithContext(ctx))
193+
if err != nil {
194+
return diag.FromErr(err)
195+
}
196+
197+
for _, pcuInstance := range pcuInstances.Products {
198+
if pcuInstance.Properties.Instance.OfferID != name {
199+
continue
200+
}
201+
202+
_ = d.Set("hourly_price", pcuInstance.Price.RetailPrice.ToFloat())
203+
}
204+
187205
// Availability
188206
availabilitiesResponse, err := instanceAPI.GetServerTypesAvailability(&instance.GetServerTypesAvailabilityRequest{
189207
Zone: zone,

internal/services/instance/server_type_data_source_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package instance_test
33
import (
44
"fmt"
55
"strconv"
6+
"strings"
67
"testing"
78

8-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
910
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
1011
product_catalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
1112
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -16,6 +17,7 @@ import (
1617
func TestAccDataSourceServerType_Basic(t *testing.T) {
1718
tt := acctest.NewTestTools(t)
1819
defer tt.Cleanup()
20+
1921
resource.ParallelTest(t, resource.TestCase{
2022
PreCheck: func() { acctest.PreCheck(t) },
2123
ProviderFactories: tt.ProviderFactories,
@@ -27,6 +29,7 @@ func TestAccDataSourceServerType_Basic(t *testing.T) {
2729
}`,
2830
Check: resource.ComposeTestCheckFunc(
2931
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "name", "DEV1-XL"),
32+
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "zone", "fr-par-1"),
3033
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "arch", "x86_64"),
3134
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "cpu", "4"),
3235
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "ram", "12884901888"),
@@ -37,13 +40,13 @@ func TestAccDataSourceServerType_Basic(t *testing.T) {
3740
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "volumes.0.max_size_per_local_volume", "800000000000"),
3841
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "volumes.0.scratch_storage_max_size", "0"),
3942
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "volumes.0.block_storage", "true"),
40-
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "hourly_price", "0.07308000326156616"),
4143
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "capabilities.0.boot_types.0", "local"),
4244
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "capabilities.0.boot_types.1", "rescue"),
4345
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "capabilities.0.max_file_systems", "0"),
4446
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "network.0.internal_bandwidth", "500000000"),
4547
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "network.0.public_bandwidth", "500000000"),
4648
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "network.0.block_bandwidth", "262144000"),
49+
resource.TestCheckResourceAttrSet("data.scaleway_instance_server_type.dev", "hourly_price"),
4750
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.dev", "end_of_service", "false"),
4851
resource.TestCheckResourceAttrSet("data.scaleway_instance_server_type.dev", "availability"),
4952
),
@@ -55,6 +58,7 @@ func TestAccDataSourceServerType_Basic(t *testing.T) {
5558
}`,
5659
Check: resource.ComposeTestCheckFunc(
5760
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "name", "RENDER-S"),
61+
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "zone", "fr-par-1"),
5862
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "arch", "x86_64"),
5963
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "cpu", "10"),
6064
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "ram", "45097156608"),
@@ -65,13 +69,13 @@ func TestAccDataSourceServerType_Basic(t *testing.T) {
6569
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "volumes.0.max_size_per_local_volume", "800000000000"),
6670
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "volumes.0.scratch_storage_max_size", "0"),
6771
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "volumes.0.block_storage", "true"),
68-
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "hourly_price", "1.2425999641418457"),
6972
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "capabilities.0.boot_types.0", "local"),
7073
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "capabilities.0.boot_types.1", "rescue"),
7174
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "capabilities.0.max_file_systems", "0"),
7275
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "network.0.internal_bandwidth", "2000000000"),
7376
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "network.0.public_bandwidth", "2000000000"),
7477
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "network.0.block_bandwidth", "2147483648"),
78+
resource.TestCheckResourceAttrSet("data.scaleway_instance_server_type.gpu", "hourly_price"),
7579
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.gpu", "end_of_service", "false"),
7680
resource.TestCheckResourceAttrSet("data.scaleway_instance_server_type.gpu", "availability"),
7781
),
@@ -83,6 +87,7 @@ func TestAccDataSourceServerType_Basic(t *testing.T) {
8387
}`,
8488
Check: resource.ComposeTestCheckFunc(
8589
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "name", "PRO2-XXS"),
90+
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "zone", "fr-par-1"),
8691
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "gpu", "0"),
8792
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "cpu", "2"),
8893
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "arch", "x86_64"),
@@ -93,13 +98,13 @@ func TestAccDataSourceServerType_Basic(t *testing.T) {
9398
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "volumes.0.max_size_per_local_volume", "0"),
9499
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "volumes.0.scratch_storage_max_size", "0"),
95100
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "volumes.0.block_storage", "true"),
96-
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "hourly_price", "0.054999999701976776"),
97101
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "capabilities.0.boot_types.0", "local"),
98102
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "capabilities.0.boot_types.1", "rescue"),
99103
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "capabilities.0.max_file_systems", "0"),
100104
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "network.0.internal_bandwidth", "350000000"),
101105
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "network.0.public_bandwidth", "350000000"),
102106
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "network.0.block_bandwidth", "131072000"),
107+
resource.TestCheckResourceAttrSet("data.scaleway_instance_server_type.pro", "hourly_price"),
103108
resource.TestCheckResourceAttr("data.scaleway_instance_server_type.pro", "end_of_service", "false"),
104109
resource.TestCheckResourceAttrSet("data.scaleway_instance_server_type.pro", "availability"),
105110
),
@@ -212,8 +217,8 @@ func TestAccDataSourceServerType_CompareWithPCU(t *testing.T) {
212217

213218
expectedInternalBandwidth := strconv.FormatUint(hardwareSpecs.Network.InternalBandwidth, 10)
214219
expectedPublicBandwidth := strconv.FormatUint(hardwareSpecs.Network.PublicBandwidth, 10)
215-
// TODO: prices differ between Instance and PCU
216-
// expectedHourlyPrice := strconv.FormatFloat(serverTypeToTest.Price.RetailPrice.ToFloat(), 'f', -1, 64)
220+
221+
expectedHourlyPrice := strings.TrimPrefix(pcuInstance.Price.RetailPrice.String(), "€ ")
217222

218223
// Create test step
219224
steps = append(steps, resource.TestStep{
@@ -231,7 +236,7 @@ func TestAccDataSourceServerType_CompareWithPCU(t *testing.T) {
231236
resource.TestCheckResourceAttr(datasourceTFName, "gpu", expectedGPU),
232237
resource.TestCheckResourceAttr(datasourceTFName, "network.0.internal_bandwidth", expectedInternalBandwidth),
233238
resource.TestCheckResourceAttr(datasourceTFName, "network.0.public_bandwidth", expectedPublicBandwidth),
234-
// resource.TestCheckResourceAttr(datasourceTFName, "hourly_price", expectedHourlyPrice),
239+
resource.TestCheckResourceAttr(datasourceTFName, "hourly_price", expectedHourlyPrice),
235240
),
236241
})
237242
}

internal/services/instance/testdata/data-source-server-type-basic.cassette.yaml

Lines changed: 401 additions & 1232 deletions
Large diffs are not rendered by default.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
subcategory: "Instances"
3+
page_title: "Scaleway: scaleway_instance_server_type"
4+
---
5+
6+
# scaleway_instance_server_type
7+
8+
Gets information about a server type.
9+
10+
## Example Usage
11+
12+
```hcl
13+
data "scaleway_instance_server_type" "pro2-s" {
14+
name = "PRO2-S"
15+
zone = "nl-ams-1"
16+
}
17+
```
18+
19+
## Argument Reference
20+
21+
To select the server type which information should be fetched, the following arguments can be used:
22+
23+
- `name` - (Required) The name of the server type.
24+
Only one of `name` and `snapshot_id` should be specified.
25+
26+
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) of the server type (to check the availability of the server type for example).
27+
28+
## Attributes Reference
29+
30+
The following attributes will be available:
31+
32+
- `arch` - The architecture of the server type.
33+
34+
- `cpu` - The number of CPU cores of the server type.
35+
36+
- `ram` - The amount of RAM of the server type (in bytes).
37+
38+
- `gpu` - The number of GPUs of the server type.
39+
40+
- `volumes` - The specifications of volumes allowed for the server type.
41+
42+
-> The `volumes` block contains:
43+
- `min_size_total` - The minimum total size in bytes of volumes allowed on the server type.
44+
- `max_size_total` - The maximum total size in bytes of volumes allowed on the server type.
45+
- `min_size_per_local_volume` - The minimum size in bytes per local volume allowed on the server type.
46+
- `max_size_per_local_volume` - The maximum size in bytes per local volume allowed on the server type.
47+
- `scratch_storage_max_size` - The maximum size in bytes of the scratch volume allowed on the server type.
48+
- `block_storage` - Whether block storage is allowed on the server type.
49+
50+
- `capabilities` - The specific capabilities of the server type.
51+
52+
-> The `capabilities` block contains:
53+
- `boot_types` - The boot types allowed for the server type.
54+
- `max_file_systems` - The maximum number of file systems that can be attached on the server type.
55+
56+
- `network` - The network specifications of the server type.
57+
58+
-> The `network` block contains:
59+
- `internal_bandwidth` - The internal bandwidth of the server type (in bytes/second).
60+
- `public_bandwidth` - The public bandwidth of the server type (in bytes/second).
61+
- `block_bandwidth` - The block bandwidth of the server type (in bytes/second).
62+
63+
- `hourly_price` - The hourly price of the server type (in euros).
64+
65+
- `monthly_price` - The monthly price of the server type (in euros).
66+
67+
- `end_of_service` - Whether the server type will soon reach End Of Service.
68+
69+
- `availabilty` - Whether the server type is available in the zone.

0 commit comments

Comments
 (0)