Skip to content
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
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