@@ -3,11 +3,13 @@ package applesilicon
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
6
7
7
8
"github.com/fatih/color"
8
9
"github.com/scaleway/scaleway-cli/v2/core"
9
10
"github.com/scaleway/scaleway-cli/v2/core/human"
10
11
applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1"
12
+ productcatalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
11
13
)
12
14
13
15
var serverTypeStockMarshalSpecs = human.EnumMarshalSpecs {
@@ -51,6 +53,12 @@ func memoryMarshalerFunc(i any, _ *human.MarshalOpt) (string, error) {
51
53
return capacityStr , nil
52
54
}
53
55
56
+ type customServerType struct {
57
+ * applesilicon.ServerType
58
+ KgCo2Equivalent * float32 `json:"kg_co2_equivalent"`
59
+ M3WaterUsage * float32 `json:"m3_water_usage"`
60
+ }
61
+
54
62
func serverTypeBuilder (c * core.Command ) * core.Command {
55
63
c .View = & core.View {
56
64
Fields : []* core.ViewField {
@@ -78,6 +86,14 @@ func serverTypeBuilder(c *core.Command) *core.Command {
78
86
Label : "Minimum Lease Duration" ,
79
87
FieldName : "MinimumLeaseDuration" ,
80
88
},
89
+ {
90
+ Label : "CO2 (kg/day)" ,
91
+ FieldName : "KgCo2Equivalent" ,
92
+ },
93
+ {
94
+ Label : "Water (m³/day)" ,
95
+ FieldName : "M3WaterUsage" ,
96
+ },
81
97
},
82
98
}
83
99
@@ -88,9 +104,47 @@ func serverTypeBuilder(c *core.Command) *core.Command {
88
104
return nil , err
89
105
}
90
106
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
+ }
92
146
93
- return versionsResponse . ServerTypes , nil
147
+ return customServerTypeList , nil
94
148
},
95
149
)
96
150
0 commit comments