Skip to content

Commit 2214003

Browse files
committed
feat(instance) fetch EoS date from PCU API and add it to warning
1 parent f2bae55 commit 2214003

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

internal/namespaces/instance/v1/helpers_types.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"time"
78

89
"github.com/fatih/color"
910
"github.com/scaleway/scaleway-cli/v2/core"
1011
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
1112
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
13+
product_catalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
1214
"github.com/scaleway/scaleway-sdk-go/scw"
1315
)
1416

@@ -65,6 +67,11 @@ func warningServerTypeDeprecated(
6567
),
6668
}
6769

70+
eosDate, err := getEndOfServiceDate(ctx, client, server.Zone, server.CommercialType)
71+
if err != nil {
72+
return warning
73+
}
74+
6875
compatibleTypes, err := instance.NewAPI(client).
6976
GetServerCompatibleTypes(&instance.GetServerCompatibleTypesRequest{
7077
Zone: server.Zone,
@@ -76,11 +83,12 @@ func warningServerTypeDeprecated(
7683

7784
mostRelevantTypes := compatibleTypes.CompatibleTypes[:5]
7885
details := fmt.Sprintf(`
79-
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.
86+
Your Instance will reach End of Service by %s. You can check the exact date on the Scaleway console. We recommend that you migrate your Instance before that.
8087
Here are the %d best options for %q, ordered by relevance: [%s]
8188
You can check the full list of compatible server types:
8289
- on the Scaleway console
8390
- using the CLI command 'scw instance server get-compatible-types %s zone=%s'`,
91+
eosDate,
8492
len(mostRelevantTypes),
8593
server.CommercialType,
8694
strings.Join(mostRelevantTypes, ", "),
@@ -90,3 +98,35 @@ func warningServerTypeDeprecated(
9098

9199
return append(warning, details)
92100
}
101+
102+
func getEndOfServiceDate(
103+
ctx context.Context,
104+
client *scw.Client,
105+
zone scw.Zone,
106+
commercialType string,
107+
) (string, error) {
108+
api := product_catalog.NewPublicCatalogAPI(client)
109+
110+
products, err := api.ListPublicCatalogProducts(
111+
&product_catalog.PublicCatalogAPIListPublicCatalogProductsRequest{
112+
ProductTypes: []product_catalog.ListPublicCatalogProductsRequestProductType{
113+
product_catalog.ListPublicCatalogProductsRequestProductTypeInstance,
114+
},
115+
},
116+
scw.WithAllPages(),
117+
scw.WithContext(ctx),
118+
)
119+
if err != nil {
120+
return "", fmt.Errorf("could not list product catalog entries: %w", err)
121+
}
122+
123+
for _, product := range products.Products {
124+
if strings.HasPrefix(product.Product, commercialType) {
125+
if product.Locality.Zone != nil && *product.Locality.Zone == zone {
126+
return product.EndOfLifeAt.Format(time.DateOnly), nil
127+
}
128+
}
129+
}
130+
131+
return "", fmt.Errorf("could not find product catalog entry for %q in %s", commercialType, zone)
132+
}

0 commit comments

Comments
 (0)