Skip to content

Commit 3b0c862

Browse files
Laure-dijremy42remyleone
authored
feat(baremetal): display environmental impact (#4844)
Co-authored-by: Jonathan R. <[email protected]> Co-authored-by: Rémy Léone <[email protected]>
1 parent 2ecf62d commit 3b0c862

File tree

7 files changed

+3963
-3
lines changed

7 files changed

+3963
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/mattn/go-isatty v0.0.20
2424
github.com/moby/buildkit v0.13.2
2525
github.com/opencontainers/go-digest v1.0.0
26-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250618150302-dd86fd661356
26+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250630080509-bd2b86613e39
2727
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
2828
github.com/spf13/cobra v1.9.1
2929
github.com/spf13/pflag v1.0.6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUz
466466
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
467467
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
468468
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
469-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250618150302-dd86fd661356 h1:9cMV1lFohW5MTZJ5YRa3bjhOjErTdnAV27GkGJUlaRk=
470-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250618150302-dd86fd661356/go.mod h1:zFWiHphneiey3s8HOtAEnGrRlWivNaxW5T6d5Xfco7g=
469+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250630080509-bd2b86613e39 h1:Xean40tUO2sN0pODaLwh+wLz5NYI578PPVIFJgob7cM=
470+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250630080509-bd2b86613e39/go.mod h1:zFWiHphneiey3s8HOtAEnGrRlWivNaxW5T6d5Xfco7g=
471471
github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8=
472472
github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM=
473473
github.com/secure-systems-lab/go-securesystemslib v0.8.0 h1:mr5An6X45Kb2nddcFlbmfHkLguCE9laoZCUzEEpIZXA=

internal/namespaces/baremetal/v1/custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func GetCommands() *core.Commands {
3838
cmds.MustFind("baremetal", "server", "start").Override(serverStartBuilder)
3939
cmds.MustFind("baremetal", "server", "stop").Override(serverStopBuilder)
4040
cmds.MustFind("baremetal", "server", "reboot").Override(serverRebootBuilder)
41+
cmds.MustFind("baremetal", "offer", "list").Override(serverOfferListBuilder)
4142

4243
cmds.MergeAll(baremetalV3.GetCommands())
4344

internal/namespaces/baremetal/v1/custom_offer.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package baremetal
22

33
import (
4+
"context"
5+
"strings"
6+
47
"github.com/fatih/color"
8+
"github.com/scaleway/scaleway-cli/v2/core"
59
"github.com/scaleway/scaleway-cli/v2/core/human"
610
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
11+
product_catalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
712
)
813

914
var offerAvailabilityMarshalSpecs = human.EnumMarshalSpecs{
@@ -53,3 +58,78 @@ func listOfferMarshalerFunc(i any, opt *human.MarshalOpt) (string, error) {
5358

5459
return str, nil
5560
}
61+
62+
type customOffer struct {
63+
*baremetal.Offer
64+
KgCo2Equivalent *float32 `json:"kg_co2_equivalent"`
65+
M3WaterUsage *float32 `json:"m3_water_usage"`
66+
}
67+
68+
func serverOfferListBuilder(c *core.Command) *core.Command {
69+
c.View = &core.View{
70+
Fields: []*core.ViewField{
71+
{Label: "ID", FieldName: "ID"},
72+
{Label: "Name", FieldName: "Name"},
73+
{Label: "Stock", FieldName: "Stock"},
74+
{Label: "Disks", FieldName: "Disks"},
75+
{Label: "CPUs", FieldName: "CPUs"},
76+
{Label: "Memories", FieldName: "Memories"},
77+
{Label: "Options", FieldName: "Options"},
78+
{Label: "Bandwidth", FieldName: "Bandwidth"},
79+
{Label: "PrivateBandwidth", FieldName: "PrivateBandwidth"},
80+
{Label: "CO2 (kg)", FieldName: "KgCo2Equivalent"},
81+
{Label: "Water (m³)", FieldName: "M3WaterUsage"},
82+
},
83+
}
84+
85+
c.Interceptor = func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) {
86+
req := argsI.(*baremetal.ListOffersRequest)
87+
rawResp, err := runner(ctx, argsI)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
offers, _ := rawResp.([]*baremetal.Offer)
93+
client := core.ExtractClient(ctx)
94+
95+
productAPI := product_catalog.NewPublicCatalogAPI(client)
96+
environmentalImpact, _ := productAPI.ListPublicCatalogProducts(
97+
&product_catalog.PublicCatalogAPIListPublicCatalogProductsRequest{
98+
ProductTypes: []product_catalog.ListPublicCatalogProductsRequestProductType{
99+
product_catalog.ListPublicCatalogProductsRequestProductTypeElasticMetal,
100+
},
101+
Zone: &req.Zone,
102+
},
103+
)
104+
105+
unitOfMeasure := product_catalog.PublicCatalogProductUnitOfMeasureCountableUnitMonth
106+
if req.SubscriptionPeriod == "hour" {
107+
unitOfMeasure = product_catalog.PublicCatalogProductUnitOfMeasureCountableUnitHour
108+
}
109+
110+
impactMap := make(map[string]*product_catalog.PublicCatalogProduct)
111+
for _, impact := range environmentalImpact.Products {
112+
if impact != nil && impact.UnitOfMeasure.Unit == unitOfMeasure {
113+
key := strings.TrimSpace(strings.TrimPrefix(impact.Product, "Elastic Metal "))
114+
impactMap[key] = impact
115+
}
116+
}
117+
118+
var customOfferRes []customOffer
119+
for _, offer := range offers {
120+
impact, ok := impactMap[offer.Name]
121+
if !ok || impact == nil {
122+
continue
123+
}
124+
customOfferRes = append(customOfferRes, customOffer{
125+
Offer: offer,
126+
KgCo2Equivalent: impact.EnvironmentalImpactEstimation.KgCo2Equivalent,
127+
M3WaterUsage: impact.EnvironmentalImpactEstimation.M3WaterUsage,
128+
})
129+
}
130+
131+
return customOfferRes, nil
132+
}
133+
134+
return c
135+
}

internal/namespaces/baremetal/v1/custom_offer_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ func Test_baremetalGetOffer(t *testing.T) {
1515
Check: core.TestCheckGolden(),
1616
}))
1717
}
18+
19+
func Test_baremetalListOffer(t *testing.T) {
20+
t.Run("Monthly", core.Test(&core.TestConfig{
21+
Commands: baremetal.GetCommands(),
22+
Cmd: "scw baremetal offer list",
23+
Check: core.TestCheckGolden(),
24+
}))
25+
}

0 commit comments

Comments
 (0)