Skip to content

Commit 148a613

Browse files
authored
feat(instance): fetch EoS date from PCU API and add it to warning (#3166)
1 parent 579647c commit 148a613

File tree

3 files changed

+295
-1
lines changed

3 files changed

+295
-1
lines changed

internal/services/instance/server.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010
"strconv"
1111
"strings"
12+
"time"
1213

1314
"github.com/google/go-cmp/cmp"
1415
"github.com/hashicorp/go-cty/cty"
@@ -21,6 +22,7 @@ import (
2122
instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
2223
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
2324
"github.com/scaleway/scaleway-sdk-go/api/marketplace/v2"
25+
product_catalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
2426
"github.com/scaleway/scaleway-sdk-go/scw"
2527
scwvalidation "github.com/scaleway/scaleway-sdk-go/validation"
2628
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
@@ -787,6 +789,11 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m a
787789
diags := diag.Diagnostics{}
788790

789791
if server.EndOfService {
792+
eosDate, err := GetEndOfServiceDate(ctx, meta.ExtractScwClient(m), server.Zone, server.CommercialType)
793+
if err != nil {
794+
return diag.FromErr(err)
795+
}
796+
790797
compatibleTypes, err := api.GetServerCompatibleTypes(&instanceSDK.GetServerCompatibleTypesRequest{
791798
Zone: zone,
792799
ServerID: id,
@@ -800,12 +807,13 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m a
800807
diags = append(diags, diag.Diagnostic{
801808
Severity: diag.Warning,
802809
Detail: fmt.Sprintf("Instance type %q will soon reach End of Service", server.CommercialType),
803-
Summary: fmt.Sprintf(`Your Instance will soon reach End of Service. You can check the exact date on the Scaleway console. We recommend that you migrate your Instance before that.
810+
Summary: fmt.Sprintf(`Your Instance will reach End of Service by %s. We recommend that you migrate your Instance before that.
804811
Here are the %d best options for %q, ordered by relevance: [%s]
805812
806813
You can check the full list of compatible server types:
807814
- on the Scaleway console
808815
- using the CLI command 'scw instance server get-compatible-types %s zone=%s'`,
816+
eosDate,
809817
len(mostRelevantTypes),
810818
server.CommercialType,
811819
strings.Join(mostRelevantTypes, ", "),
@@ -1572,3 +1580,26 @@ func instanceServerVolumesUpdate(ctx context.Context, d *schema.ResourceData, ap
15721580

15731581
return volumes, nil
15741582
}
1583+
1584+
func GetEndOfServiceDate(ctx context.Context, client *scw.Client, zone scw.Zone, commercialType string) (string, error) {
1585+
api := product_catalog.NewPublicCatalogAPI(client)
1586+
1587+
products, err := api.ListPublicCatalogProducts(&product_catalog.PublicCatalogAPIListPublicCatalogProductsRequest{
1588+
ProductTypes: []product_catalog.ListPublicCatalogProductsRequestProductType{
1589+
product_catalog.ListPublicCatalogProductsRequestProductTypeInstance,
1590+
},
1591+
}, scw.WithAllPages(), scw.WithContext(ctx))
1592+
if err != nil {
1593+
return "", fmt.Errorf("could not list product catalog entries: %w", err)
1594+
}
1595+
1596+
for _, product := range products.Products {
1597+
if strings.HasPrefix(product.Product, commercialType) {
1598+
if product.Locality.Zone != nil && *product.Locality.Zone == zone {
1599+
return product.EndOfLifeAt.Format(time.DateOnly), nil
1600+
}
1601+
}
1602+
}
1603+
1604+
return "", fmt.Errorf("could not find product catalog entry for %q in %s", commercialType, zone)
1605+
}

internal/services/instance/server_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
1414
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1515
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
16+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1617
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance"
1718
instancechecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance/testfuncs"
19+
"github.com/stretchr/testify/assert"
1820
)
1921

2022
func TestAccServer_Minimal1(t *testing.T) {
@@ -2094,3 +2096,16 @@ func TestAccServer_PrivateNetworkMissingPNIC(t *testing.T) {
20942096
},
20952097
})
20962098
}
2099+
2100+
func TestGetEndOfServiceDate(t *testing.T) {
2101+
tt := acctest.NewTestTools(t)
2102+
client := meta.ExtractScwClient(tt.Meta)
2103+
defer tt.Cleanup()
2104+
2105+
eosDate, err := instance.GetEndOfServiceDate(t.Context(), client, "fr-par-1", "ENT1-S")
2106+
if err != nil {
2107+
t.Fatal(err)
2108+
}
2109+
2110+
assert.Equal(t, "2025-09-01", eosDate)
2111+
}

internal/services/instance/testdata/test-get-end-of-service-date.cassette.yaml

Lines changed: 248 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)