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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
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
82 changes: 82 additions & 0 deletions internal/namespaces/baremetal/v1/custom_offer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package baremetal

import (
"context"
"errors"
"fmt"
"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 +60,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: "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) {
rawResp, err := runner(ctx, argsI)
if err != nil {
return nil, err
}

offers, ok := rawResp.([]*baremetal.Offer)
for _, offer := range offers {
fmt.Printf("Print value of offer name: %s\n", offer.Name)
}

if !ok {
return nil, errors.New("unexpected type for offer response")
}

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

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

var customOfferRes []customOffer
for _, offer := range offers {
fmt.Printf("Print value of offer: %s\n", offer.Name)
impact, ok := impactMap[offer.Name]
if !ok || impact == nil {
fmt.Printf("No environmental impact data found for offer: %s\n", offer.Name)

continue
}
customOfferRes = append(customOfferRes, customOffer{
Offer: *offer,
KgCo2Equivalent: impact.EnvironmentalImpactEstimation.KgCo2Equivalent,
M3WaterUsage: impact.EnvironmentalImpactEstimation.M3WaterUsage,
})
}

return customOfferRes, nil
}

return c
}
Loading