@@ -3,11 +3,13 @@ package applesilicon
33import (
44 "context"
55 "fmt"
6+ "strings"
67
78 "github.com/fatih/color"
89 "github.com/scaleway/scaleway-cli/v2/core"
910 "github.com/scaleway/scaleway-cli/v2/core/human"
1011 applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1"
12+ productcatalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
1113)
1214
1315var serverTypeStockMarshalSpecs = human.EnumMarshalSpecs {
@@ -51,6 +53,12 @@ func memoryMarshalerFunc(i any, _ *human.MarshalOpt) (string, error) {
5153 return capacityStr , nil
5254}
5355
56+ type customServerType struct {
57+ * applesilicon.ServerType
58+ KgCo2Equivalent * float32 `json:"kg_co2_equivalent"`
59+ M3WaterUsage * float32 `json:"m3_water_usage"`
60+ }
61+
5462func serverTypeBuilder (c * core.Command ) * core.Command {
5563 c .View = & core.View {
5664 Fields : []* core.ViewField {
@@ -78,6 +86,14 @@ func serverTypeBuilder(c *core.Command) *core.Command {
7886 Label : "Minimum Lease Duration" ,
7987 FieldName : "MinimumLeaseDuration" ,
8088 },
89+ {
90+ Label : "CO2 (kg/day)" ,
91+ FieldName : "KgCo2Equivalent" ,
92+ },
93+ {
94+ Label : "Water (m³/day)" ,
95+ FieldName : "M3WaterUsage" ,
96+ },
8197 },
8298 }
8399
@@ -88,9 +104,47 @@ func serverTypeBuilder(c *core.Command) *core.Command {
88104 return nil , err
89105 }
90106
91- versionsResponse := originalRes .(* applesilicon.ListServerTypesResponse )
107+ client := core .ExtractClient (ctx )
108+
109+ req := argsI .(* applesilicon.ListServerTypesRequest )
110+ serverTypes := originalRes .(* applesilicon.ListServerTypesResponse ).ServerTypes
111+
112+ productAPI := productcatalog .NewPublicCatalogAPI (client )
113+ environmentalImpact , err := productAPI .ListPublicCatalogProducts (
114+ & productcatalog.PublicCatalogAPIListPublicCatalogProductsRequest {
115+ ProductTypes : []productcatalog.ListPublicCatalogProductsRequestProductType {
116+ productcatalog .ListPublicCatalogProductsRequestProductTypeAppleSilicon ,
117+ },
118+ Zone : & req .Zone ,
119+ },
120+ )
121+ if err != nil {
122+ return nil , err
123+ }
124+
125+ impactMap := make (map [string ]* productcatalog.PublicCatalogProduct )
126+ for _ , impact := range environmentalImpact .Products {
127+ if impact != nil {
128+ key := strings .TrimSpace (strings .TrimPrefix (impact .Product , "Mac Mini " ))
129+ key = strings .ReplaceAll (key , " - " , "-" )
130+ impactMap [key ] = impact
131+ }
132+ }
133+
134+ var customServerTypeList []customServerType
135+ for _ , severType := range serverTypes {
136+ impact , ok := impactMap [severType .Name ]
137+ if ! ok {
138+ continue
139+ }
140+ customServerTypeList = append (customServerTypeList , customServerType {
141+ ServerType : severType ,
142+ KgCo2Equivalent : impact .EnvironmentalImpactEstimation .KgCo2Equivalent ,
143+ M3WaterUsage : impact .EnvironmentalImpactEstimation .M3WaterUsage ,
144+ })
145+ }
92146
93- return versionsResponse . ServerTypes , nil
147+ return customServerTypeList , nil
94148 },
95149 )
96150
0 commit comments