Skip to content

feat(baremetal): display environmental impact #4844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.24.0

require (
github.com/aws/aws-sdk-go-v2 v1.36.5
github.com/aws/aws-sdk-go-v2/service/s3 v1.82.0
github.com/aws/aws-sdk-go-v2/service/s3 v1.80.3
github.com/buildpacks/pack v0.34.2
github.com/c-bata/go-prompt v0.2.6
github.com/charmbracelet/bubbletea v1.3.5
Expand All @@ -14,7 +14,7 @@ require (
github.com/docker/docker v27.5.1+incompatible
github.com/dustin/go-humanize v1.0.1
github.com/fatih/color v1.18.0
github.com/getsentry/sentry-go v0.34.0
github.com/getsentry/sentry-go v0.33.0
github.com/ghodss/yaml v1.0.0
github.com/gorilla/websocket v1.5.3
github.com/hashicorp/go-version v1.7.0
Expand All @@ -23,7 +23,7 @@ require (
github.com/mattn/go-isatty v0.0.20
github.com/moby/buildkit v0.13.2
github.com/opencontainers/go-digest v1.0.0
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250618150302-dd86fd661356
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250630080509-bd2b86613e39
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/cobra v1.9.1
github.com/spf13/pflag v1.0.6
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.17 h1:t0E6FzRE
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.17/go.mod h1:ygpklyoaypuyDvOM5ujWGrYWpAK3h7ugnmKCU/76Ys4=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.17 h1:qcLWgdhq45sDM9na4cvXax9dyLitn8EYBRl8Ak4XtG4=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.17/go.mod h1:M+jkjBFZ2J6DJrjMv2+vkBbuht6kxJYtJiwoVgX4p4U=
github.com/aws/aws-sdk-go-v2/service/s3 v1.82.0 h1:JubM8CGDDFaAOmBrd8CRYNr49ZNgEAiLwGwgNMdS0nw=
github.com/aws/aws-sdk-go-v2/service/s3 v1.82.0/go.mod h1:kUklwasNoCn5YpyAqC/97r6dzTA1SRKJfKq16SXeoDU=
github.com/aws/aws-sdk-go-v2/service/s3 v1.80.3 h1:jBOwbbIQlfZG079E0YEnfipULNr7wnXbG2gwJyG9hrc=
github.com/aws/aws-sdk-go-v2/service/s3 v1.80.3/go.mod h1:kUklwasNoCn5YpyAqC/97r6dzTA1SRKJfKq16SXeoDU=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.2 h1:XOPfar83RIRPEzfihnp+U6udOveKZJvPQ76SKWrLRHc=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.2/go.mod h1:Vv9Xyk1KMHXrR3vNQe8W5LMFdTjSeWk0gBZBzvf3Qa0=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.2 h1:pi0Skl6mNl2w8qWZXcdOyg197Zsf4G97U7Sso9JXGZE=
Expand Down Expand Up @@ -230,8 +230,8 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU=
github.com/gdamore/tcell/v2 v2.7.4/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg=
github.com/getsentry/sentry-go v0.34.0 h1:1FCHBVp8TfSc8L10zqSwXUZNiOSF+10qw4czjarTiY4=
github.com/getsentry/sentry-go v0.34.0/go.mod h1:C55omcY9ChRQIUcVcGcs+Zdy4ZpQGvNJ7JYHIoSWOtE=
github.com/getsentry/sentry-go v0.33.0 h1:YWyDii0KGVov3xOaamOnF0mjOrqSjBqwv48UEzn7QFg=
github.com/getsentry/sentry-go v0.33.0/go.mod h1:C55omcY9ChRQIUcVcGcs+Zdy4ZpQGvNJ7JYHIoSWOtE=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
Expand Down Expand Up @@ -466,8 +466,8 @@ github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUz
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250618150302-dd86fd661356 h1:9cMV1lFohW5MTZJ5YRa3bjhOjErTdnAV27GkGJUlaRk=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250618150302-dd86fd661356/go.mod h1:zFWiHphneiey3s8HOtAEnGrRlWivNaxW5T6d5Xfco7g=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250630080509-bd2b86613e39 h1:Xean40tUO2sN0pODaLwh+wLz5NYI578PPVIFJgob7cM=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33.0.20250630080509-bd2b86613e39/go.mod h1:zFWiHphneiey3s8HOtAEnGrRlWivNaxW5T6d5Xfco7g=
github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8=
github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM=
github.com/secure-systems-lab/go-securesystemslib v0.8.0 h1:mr5An6X45Kb2nddcFlbmfHkLguCE9laoZCUzEEpIZXA=
Expand Down
1 change: 1 addition & 0 deletions internal/namespaces/baremetal/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func GetCommands() *core.Commands {
cmds.MustFind("baremetal", "server", "start").Override(serverStartBuilder)
cmds.MustFind("baremetal", "server", "stop").Override(serverStopBuilder)
cmds.MustFind("baremetal", "server", "reboot").Override(serverRebootBuilder)
cmds.MustFind("baremetal", "offer", "list").Override(serverOfferListBuilder)

cmds.MergeAll(baremetalV3.GetCommands())

Expand Down
80 changes: 80 additions & 0 deletions internal/namespaces/baremetal/v1/custom_offer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package baremetal

import (
"context"
"strings"

"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/core"
"github.com/scaleway/scaleway-cli/v2/core/human"
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
product_catalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
)

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

return str, nil
}

type customOffer struct {
*baremetal.Offer
KgCo2Equivalent *float32 `json:"kg_co2_equivalent"`
M3WaterUsage *float32 `json:"m3_water_usage"`
}

func serverOfferListBuilder(c *core.Command) *core.Command {
c.View = &core.View{
Fields: []*core.ViewField{
{Label: "ID", FieldName: "ID"},
{Label: "Name", FieldName: "Name"},
{Label: "Stock", FieldName: "Stock"},
{Label: "Disks", FieldName: "Disks"},
{Label: "CPUs", FieldName: "CPUs"},
{Label: "Memories", FieldName: "Memories"},
{Label: "Options", FieldName: "Options"},
{Label: "Bandwidth", FieldName: "Bandwidth"},
{Label: "PrivateBandwidth", FieldName: "PrivateBandwidth"},
{Label: "CO2 (kg)", FieldName: "KgCo2Equivalent"},
{Label: "Water (m³)", FieldName: "M3WaterUsage"},
},
}

c.Interceptor = func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) {
req := argsI.(*baremetal.ListOffersRequest)
rawResp, err := runner(ctx, argsI)
if err != nil {
return nil, err
}

offers, _ := rawResp.([]*baremetal.Offer)
client := core.ExtractClient(ctx)

productAPI := product_catalog.NewPublicCatalogAPI(client)
environmentalImpact, _ := productAPI.ListPublicCatalogProducts(
&product_catalog.PublicCatalogAPIListPublicCatalogProductsRequest{
ProductTypes: []product_catalog.ListPublicCatalogProductsRequestProductType{
product_catalog.ListPublicCatalogProductsRequestProductTypeElasticMetal,
},
Zone: &req.Zone,
},
)

unitOfMeasure := product_catalog.PublicCatalogProductUnitOfMeasureCountableUnitMonth
if req.SubscriptionPeriod == "hour" {
unitOfMeasure = product_catalog.PublicCatalogProductUnitOfMeasureCountableUnitHour
}

impactMap := make(map[string]*product_catalog.PublicCatalogProduct)
for _, impact := range environmentalImpact.Products {
if impact != nil && impact.UnitOfMeasure.Unit == unitOfMeasure {
key := strings.TrimSpace(strings.TrimPrefix(impact.Product, "Elastic Metal "))
impactMap[key] = impact
}
}

var customOfferRes []customOffer
for _, offer := range offers {
impact, ok := impactMap[offer.Name]
if !ok || impact == nil {
continue
}
customOfferRes = append(customOfferRes, customOffer{
Offer: offer,
KgCo2Equivalent: impact.EnvironmentalImpactEstimation.KgCo2Equivalent,
M3WaterUsage: impact.EnvironmentalImpactEstimation.M3WaterUsage,
})
}

return customOfferRes, nil
}

return c
}
8 changes: 8 additions & 0 deletions internal/namespaces/baremetal/v1/custom_offer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ func Test_baremetalGetOffer(t *testing.T) {
Check: core.TestCheckGolden(),
}))
}

func Test_baremetalListOffer(t *testing.T) {
t.Run("Monthly", core.Test(&core.TestConfig{
Commands: baremetal.GetCommands(),
Cmd: "scw baremetal offer list",
Check: core.TestCheckGolden(),
}))
}
Loading
Loading