|
1 | 1 | package baremetal
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "fmt" |
4 | 6 | "github.com/fatih/color"
|
| 7 | + "github.com/scaleway/scaleway-cli/v2/core" |
5 | 8 | "github.com/scaleway/scaleway-cli/v2/core/human"
|
6 | 9 | "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
|
| 10 | + product_catalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1" |
| 11 | + "strings" |
7 | 12 | )
|
8 | 13 |
|
9 | 14 | var offerAvailabilityMarshalSpecs = human.EnumMarshalSpecs{
|
@@ -53,3 +58,75 @@ func listOfferMarshalerFunc(i any, opt *human.MarshalOpt) (string, error) {
|
53 | 58 |
|
54 | 59 | return str, nil
|
55 | 60 | }
|
| 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: "Disks", FieldName: "Disks"}, |
| 72 | + {Label: "CPUs", FieldName: "CPUs"}, |
| 73 | + {Label: "Memories", FieldName: "Memories"}, |
| 74 | + {Label: "Options", FieldName: "Options"}, |
| 75 | + {Label: "Bandwidth", FieldName: "Bandwidth"}, |
| 76 | + {Label: "PrivateBandwidth", FieldName: "PrivateBandwidth"}, |
| 77 | + {Label: "CO2 (kg)", FieldName: "KgCo2Equivalent"}, |
| 78 | + {Label: "Water (m³)", FieldName: "M3WaterUsage"}, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + c.Interceptor = func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) { |
| 83 | + rawResp, err := runner(ctx, argsI) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + offers, ok := rawResp.([]*baremetal.Offer) |
| 89 | + for _, offer := range offers { |
| 90 | + fmt.Printf("Print value of offer name: %s\n", offer.Name) |
| 91 | + } |
| 92 | + |
| 93 | + if !ok { |
| 94 | + return nil, fmt.Errorf("unexpected type for offer response") |
| 95 | + } |
| 96 | + |
| 97 | + client := core.ExtractClient(ctx) |
| 98 | + productAPI := product_catalog.NewPublicCatalogAPI(client) |
| 99 | + environmentalImpact, _ := productAPI.ListPublicCatalogProducts(&product_catalog.PublicCatalogAPIListPublicCatalogProductsRequest{ |
| 100 | + ProductTypes: []product_catalog.ListPublicCatalogProductsRequestProductType{ |
| 101 | + product_catalog.ListPublicCatalogProductsRequestProductTypeElasticMetal, |
| 102 | + }, |
| 103 | + }) |
| 104 | + |
| 105 | + impactMap := make(map[string]*product_catalog.PublicCatalogProduct) |
| 106 | + for _, impact := range environmentalImpact.Products { |
| 107 | + if impact != nil { |
| 108 | + key := strings.TrimSpace(strings.TrimPrefix(impact.Product, "Elastic Metal ")) |
| 109 | + fmt.Printf("Print value of key: %s region %s\n", key, impact.Locality) |
| 110 | + impactMap[key] = impact |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + var customOfferRes []customOffer |
| 115 | + for _, offer := range offers { |
| 116 | + fmt.Printf("Print value of offer: %s\n", offer.Name) |
| 117 | + impact, ok := impactMap[offer.Name] |
| 118 | + if !ok || impact == nil { |
| 119 | + fmt.Printf("No environmental impact data found for offer: %s\n", offer.Name) |
| 120 | + continue |
| 121 | + } |
| 122 | + customOfferRes = append(customOfferRes, customOffer{ |
| 123 | + Offer: *offer, |
| 124 | + KgCo2Equivalent: impact.EnvironmentalImpactEstimation.KgCo2Equivalent, |
| 125 | + M3WaterUsage: impact.EnvironmentalImpactEstimation.M3WaterUsage, |
| 126 | + }) |
| 127 | + } |
| 128 | + return customOfferRes, nil |
| 129 | + } |
| 130 | + |
| 131 | + return c |
| 132 | +} |
0 commit comments