-
Notifications
You must be signed in to change notification settings - Fork 45
/
PriceInfoHandler.go
537 lines (460 loc) · 18.1 KB
/
PriceInfoHandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Proof of Concepts of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// This is a Cloud Driver Tester Example.
//
// by ETRI, 2023.12.
package resources
import (
"crypto"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/hmac" // Caution!! : Not "crypto/hmac"
// Ref) https://github.com/NaverCloudPlatform/ncloud-sdk-go-v2/blob/master/services/vserver/api_client.go
"io"
"net/http"
"net/url"
"strconv"
"time"
server "github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
cblog "github.com/cloud-barista/cb-log"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
type NcpPriceInfoHandler struct {
CredentialInfo idrv.CredentialInfo
RegionInfo idrv.RegionInfo
VMClient *server.APIClient
}
// Already declared in CommonNcpFunc.go
// var cblogger *logrus.Logger
func init() {
// cblog is a global variable.
cblogger = cblog.GetLogger("NCP Classic VMHandler")
}
// =========================== For ProductList ============================
type ProductItemKind struct {
Code string `json:"code"`
CodeName string `json:"code_name"`
}
type ProductItemKindDetail struct {
Code string `json:"code"`
CodeName string `json:"code_name"`
}
type Product struct {
ItemKind ProductItemKind `json:"productItemKind"`
ItemKindDetail ProductItemKindDetail `json:"productItemKindDetail"`
ProductCode string `json:"productCode"`
ProductName string `json:"productName"`
Description string `json:"productDescription"`
softwareType map[string]interface{} `json:"softwareType"`
}
type ProductListAPIResponse struct {
GetProductListResponse struct {
RequestID string `json:"request_id"`
ReturnCode int `json:"return_code"`
ReturnMessage string `returnMessage`
TotalRows int `json:"totalRows"`
ProductList []Product `json:"productList"`
} `json:"getProductListResponse"`
Error *ErrorResponse `json:"error,omitempty"`
}
// =========================== For ProductList ============================
// =========================== For PriceList ============================
type Code struct {
Code string `json:"code"`
CodeName string `json:"codeName"`
}
type Region struct {
RegionNo int `json:"regionNo"` // Not string
RegionCode string `json:"regionCode"`
RegionName string `json:"regionName"`
}
type Price struct {
PriceNo string `json:"priceNo"` // Not int
PriceType Code `json:"priceType"`
Region Region `json:"region"`
ChargingUnitType Code `json:"chargingUnitType"`
RatingUnitType Code `json:"ratingUnitType"`
ChargingUnitBasicValue string `json:"chargingUnitBasicValue"` // Not int
Unit Code `json:"unit"`
PriceValue float32 `json:"price"` // Not float64
ConditionType Code `json:"conditionType"`
ConditionPrice float32 `json:"conditionPrice"` // Not float64
PriceDescription string `json:"priceDescription"`
MeteringUnit Code `json:"meteringUnit"`
StartDate string `json:"startDate"`
PayCurrency Code `json:"payCurrency"`
}
type ProductPrice struct {
ProductItemKind Code `json:"productItemKind"`
ProductItemKindDetail Code `json:"productItemKindDetail"`
ProductCode string `json:"productCode"`
ProductName string `json:"productName"`
ProductDescription string `json:"productDescription"`
ProductType Code `json:"productType"`
GpuCount int `json:"gpuCount"`
CpuCount int `json:"cpuCount"`
MemorySize int64 `json:"memorySize"`
BaseBlockStorageSize int64 `json:"baseBlockStorageSize"`
DiskType Code `json:"diskType"`
DiskDetailType Code `json:"diskDetailType"`
GenerationCode string `json:"generationCode"`
PriceList []Price `json:"priceList"`
softwareType map[string]interface{} `json:"softwareType"`
}
type PriceListAPIResponse struct {
GetProductPriceListResponse struct {
RequestID string `json:"request_id"`
ReturnCode int `json:"return_code"`
ReturnMessage string `returnMessage`
TotalRows int `json:"totalRows"`
ProductPriceList []ProductPrice `json:"productPriceList"`
} `json:"getProductPriceListResponse"`
Error *ErrorResponse `json:"error,omitempty"`
}
// =========================== For PriceList ============================
// =========================== Common ============================
type ErrorResponse struct {
Code string `json:"errorCode,omitempty"`
Message string `json:"message,omitempty"`
Details string `json:"details,omitempty"`
}
// =========================== Common ============================
const (
BaseURL string = "https://billingapi.apigw.ntruss.com/billing/v1"
ProductListURL string = "/product/getProductList"
ProductPriceListURL string = "/product/getProductPriceList"
)
func (priceInfoHandler *NcpPriceInfoHandler) ListProductFamily(regionName string) ([]string, error) {
cblogger.Info("NCP Classic Cloud driver: called ListProductFamily()!!")
// API Guide : https://api.ncloud-docs.com/docs/platform-listprice-getproductlist
if strings.EqualFold(regionName, "") {
newErr := fmt.Errorf("Invalid regionName!!")
cblogger.Error(newErr.Error())
return nil, newErr
}
bodyInUint8, err := priceInfoHandler.GetRequestBody(regionName, ProductListURL)
if err != nil {
newErr := fmt.Errorf("Failed to Get the Body to Request : [%v]", err)
cblogger.Error(newErr.Error())
return nil, newErr
}
var productListResp ProductListAPIResponse
err = json.Unmarshal(bodyInUint8, &productListResp)
if err != nil {
newErr := fmt.Errorf("Failed to Unmarshal JSON : [%v]", err)
cblogger.Error(newErr.Error())
return nil, newErr
}
if productListResp.Error != nil {
newErr := fmt.Errorf("API Error Code: [%s], Message: [%s]", productListResp.Error.Code, productListResp.Error.Message)
cblogger.Error(newErr.Error())
return nil, nil
}
// log.Printf("### productListResp")
// spew.Dump(productListResp)
// # Remove Duplicated Product Code
uniqueCodes := make(map[string]bool)
uniqueProducts := []Product{}
if len(productListResp.GetProductListResponse.ProductList) > 0 {
for _, product := range productListResp.GetProductListResponse.ProductList {
if _, exists := uniqueCodes[product.ItemKind.Code]; !exists {
uniqueProducts = append(uniqueProducts, product)
uniqueCodes[product.ItemKind.Code] = true
}
}
}
var productCodeList []string
if len(uniqueProducts) > 0 {
for _, uniqueProduct := range uniqueProducts {
// fmt.Println("Code:", uniqueProduct.ItemKind.Code)
productCodeList = append(productCodeList, uniqueProduct.ItemKind.Code)
}
}
return productCodeList, nil
}
func (priceInfoHandler *NcpPriceInfoHandler) GetPriceInfo(productFamily string, regionName string, filterList []irs.KeyValue) (string, error) {
cblogger.Info("NCP Classic Cloud driver: called GetPriceInfo()!!")
// API Guide : https://api.ncloud-docs.com/docs/platform-listprice-getproductlist
if strings.EqualFold(regionName, "") {
newErr := fmt.Errorf("Invalid regionName!!")
cblogger.Error(newErr.Error())
return "", newErr
}
// Check whether the presented ProductFamily exists.
productList, err := priceInfoHandler.ListProductFamily(regionName)
if err != nil {
newErr := fmt.Errorf("Failed to Get ProductFamily : [%v]", err)
cblogger.Error(newErr.Error())
return "", newErr
}
found := false
for _, product := range productList {
if strings.EqualFold(product, productFamily) {
found = true
break
}
}
if found {
fmt.Printf("The ProductFamily '%s' is Included in the ProductFamily.\n", productFamily)
} else {
newErr := fmt.Errorf("The ProductFamily '%s' is Not Included in the ProductFamily.\n", productFamily)
cblogger.Error(newErr.Error())
return "", newErr
}
bodyInUint8, err := priceInfoHandler.GetRequestBodyWithProductCode(regionName, ProductPriceListURL, productFamily, filterList)
if err != nil {
newErr := fmt.Errorf("Failed to Get the Body to Request : [%v]", err)
cblogger.Error(newErr.Error())
return "", newErr
}
var priceListResp PriceListAPIResponse
err = json.Unmarshal(bodyInUint8, &priceListResp)
if err != nil {
newErr := fmt.Errorf("Failed to unmarshal JSON : [%v]", err)
cblogger.Error(newErr.Error())
return "", newErr
}
// log.Printf("### response")
// spew.Dump(response)
var priceList []irs.Price
switch productFamily {
case "SVR": // Server(VM)
for _, product := range priceListResp.GetProductPriceListResponse.ProductPriceList {
// fmt.Printf("ProductCode: %s\n", product.ProductCode)
// fmt.Printf("ProductItemKindDetail Code: %s\n", product.ProductItemKindDetail.Code)
// fmt.Printf("ProductItemKindDetail CodeName: %s\n", product.ProductItemKindDetail.CodeName)
// fmt.Printf("ProductType CodeName: %s\n", product.ProductType.CodeName)
// fmt.Printf("CpuCount: %d\n", product.CpuCount)
// fmt.Printf("MemorySize: %d\n", product.MemorySize)
// fmt.Printf("BaseBlockStorageSize: %d\n", product.BaseBlockStorageSize)
// fmt.Printf("GpuCount: %d\n", product.GpuCount)
// fmt.Printf("DiskType Code: %s\n", product.DiskType.Code)
// fmt.Printf("DiskType CodeName: %s\n", product.DiskType.CodeName)
// fmt.Println("------------------------------")
var regionCode string
for _, price := range product.PriceList {
if strings.EqualFold(price.Region.RegionCode, regionName) {
regionCode = price.Region.RegionCode
break
}
fmt.Printf("RegionCode: %s\n", price.Region.RegionCode)
}
var pricingPolicies []irs.PricingPolicies
for _, price := range product.PriceList {
priceString := fmt.Sprintf("%f", price.PriceValue)
pricingPolicies = append(pricingPolicies, irs.PricingPolicies{
PricingId: price.PriceNo,
PricingPolicy: price.PriceType.CodeName,
Unit: price.Unit.CodeName,
Currency: price.PayCurrency.Code,
Price: priceString,
Description: price.PriceDescription,
PricingPolicyInfo: nil,
})
}
vCPUs := strconv.Itoa(product.CpuCount)
vMemGb := strconv.FormatInt(product.MemorySize/(1024*1024*1024), 10)
storageGB := strconv.FormatInt(product.BaseBlockStorageSize/(1024*1024*1024), 10)
vGPUs := strconv.Itoa(product.GpuCount)
priceList = append(priceList, irs.Price{
ProductInfo: irs.ProductInfo{
ProductId: product.ProductCode,
RegionName: regionCode,
InstanceType: product.ProductType.CodeName,
Vcpu: vCPUs,
Memory: vMemGb,
Storage: storageGB,
Gpu: vGPUs,
GpuMemory: "N/A",
OperatingSystem: "N/A",
PreInstalledSw: "N/A",
VolumeType: product.DiskType.CodeName,
StorageMedia: product.DiskDetailType.CodeName,
Description: product.ProductDescription,
CSPProductInfo: product,
},
PriceInfo: irs.PriceInfo{
PricingPolicies: pricingPolicies,
CSPPriceInfo: product.PriceList,
},
})
}
case "BST": // Block Storage
for _, product := range priceListResp.GetProductPriceListResponse.ProductPriceList {
// fmt.Printf("ProductCode: %s\n", product.ProductCode)
// fmt.Printf("ProductType Code: %s\n", product.ProductType.Code)
// fmt.Printf("DiskDetailType CodeName: %s\n", product.DiskDetailType.CodeName)
// fmt.Println("------------------------------")
var regionCode string
for _, price := range product.PriceList {
if strings.EqualFold(price.Region.RegionCode, regionName) {
regionCode = price.Region.RegionCode
break
}
fmt.Printf("RegionCode: %s\n", price.Region.RegionCode)
}
var pricingPolicies []irs.PricingPolicies
for _, price := range product.PriceList {
priceString := fmt.Sprintf("%f", price.PriceValue)
pricingPolicies = append(pricingPolicies, irs.PricingPolicies{
PricingId: price.PriceNo,
PricingPolicy: price.PriceType.CodeName,
Unit: price.Unit.CodeName,
Currency: price.PayCurrency.Code,
Price: priceString,
Description: price.PriceDescription,
PricingPolicyInfo: nil,
})
}
priceList = append(priceList, irs.Price{
ProductInfo: irs.ProductInfo{
ProductId: product.ProductCode,
RegionName: regionCode,
VolumeType: product.ProductType.CodeName,
StorageMedia: product.DiskDetailType.CodeName,
MaxVolumeSize: "N/A",
MaxIOPSVolume: "N/A",
MaxThroughputVolume: "N/A",
Description: product.ProductDescription,
CSPProductInfo: product,
},
PriceInfo: irs.PriceInfo{
PricingPolicies: pricingPolicies,
CSPPriceInfo: product.PriceList,
},
})
}
default:
newErr := fmt.Errorf(productFamily + " is Not Supported Product Family on this driver yet!!")
cblogger.Error(newErr.Error())
return "", newErr
}
cloudPriceData := irs.CloudPriceData{
Meta: irs.Meta{
Version: "v0.1",
Description: "Multi-Cloud Price Info",
},
CloudPriceList: []irs.CloudPrice{
{
CloudName: "NCP Classic",
PriceList: priceList,
},
},
}
jsonData, err := json.MarshalIndent(cloudPriceData, "", " ")
// jsonData, err := json.Marshal(cloudPriceData)
if err != nil {
newErr := errors.New(fmt.Sprintf("Failed to Get PriceInfo Data : [%s]", err))
cblogger.Error(newErr.Error())
return "", newErr
}
return string(jsonData), nil
}
// This is necessary because NCP GoSDK does not support these PriceInfo APIs.
func (priceInfoHandler *NcpPriceInfoHandler) GetRequestBody(regionCode string, callURL string) ([]uint8, error) {
cblogger.Info("NCP Classic Cloud driver: called GetRequestBody()!!")
// ### Ref for Auth.) https://api.ncloud-docs.com/docs/common-ncpapi
// Set Query Parameters
params := url.Values{}
params.Add("responseFormatType", "json") // Note!! : 'json' or 'xml'
params.Add("regionCode", regionCode)
// Add Query Parameters to BaseURL
fullURL := BaseURL + callURL + "?" + params.Encode()
accessKey := priceInfoHandler.CredentialInfo.ClientId
secretKey := priceInfoHandler.CredentialInfo.ClientSecret
// Current time -> Calculated in Milli-Second
timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
// Ref) https://github.com/NaverCloudPlatform/ncloud-sdk-go-v2/blob/master/services/vserver/api_client.go line 269 ~ 270
signer := hmac.NewSigner(secretKey, crypto.SHA256)
signature, _ := signer.Sign("GET", fullURL, accessKey, timestamp) // Caution!! : Different from the general signature format.
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
newErr := fmt.Errorf("Failed to Create Request : [%v]", err)
cblogger.Error(newErr.Error())
return nil, newErr
}
req.Header.Set("x-ncp-apigw-timestamp", timestamp)
req.Header.Set("x-ncp-iam-access-key", accessKey)
req.Header.Set("x-ncp-apigw-signature-v2", signature)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
newErr := fmt.Errorf("Failed to Send Request : [%v]", err)
cblogger.Error(newErr.Error())
return nil, newErr
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
newErr := fmt.Errorf("Failed to Read Response Body : [%v]", err)
cblogger.Error(newErr.Error())
return nil, newErr
}
// log.Printf("### body")
// spew.Dump(body)
// ### Convert []byte format of date to []unit8 format
var dataInUint8 []uint8 = body // Caution!!
return dataInUint8, nil
}
// This is necessary because NCP GoSDK does not support these PriceInfo APIs.
func (priceInfoHandler *NcpPriceInfoHandler) GetRequestBodyWithProductCode(regionCode string, callURL string, productCode string, filterList []irs.KeyValue) ([]uint8, error) {
cblogger.Info("NCP Classic Cloud driver: called GetRequestBodyWithProductCode()!!")
// ### Ref for Auth.) https://api.ncloud-docs.com/docs/common-ncpapi
// Set Query Parameters
// NCP API Call URL Ex) : GET {API_URL}/product/getProductPriceList?regionCode=KR&productItemKindCode=VSVR&productName=6248R
params := url.Values{}
params.Add("responseFormatType", "json") // Note!! : 'json' or 'xml'
params.Add("regionCode", regionCode)
params.Add("productItemKindCode", productCode) // Ex) SVR or VSVR, ...
if len(filterList) == 0 {
filterList = nil
} else {
for _, filter := range filterList {
params.Add(filter.Key, filter.Value)
}
}
// Add Query Parameters to BaseURL
fullURL := BaseURL + callURL + "?" + params.Encode()
accessKey := priceInfoHandler.CredentialInfo.ClientId
secretKey := priceInfoHandler.CredentialInfo.ClientSecret
// Current time -> Calculated in Milli-Second
timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
// Ref) https://github.com/NaverCloudPlatform/ncloud-sdk-go-v2/blob/master/services/vserver/api_client.go line 269 ~ 270
signer := hmac.NewSigner(secretKey, crypto.SHA256)
signature, _ := signer.Sign("GET", fullURL, accessKey, timestamp) // Caution!! : Different from the general signature format.
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
newErr := fmt.Errorf("Failed to Create Request : [%v]", err)
cblogger.Error(newErr.Error())
return nil, newErr
}
req.Header.Set("x-ncp-apigw-timestamp", timestamp)
req.Header.Set("x-ncp-iam-access-key", accessKey)
req.Header.Set("x-ncp-apigw-signature-v2", signature)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
newErr := fmt.Errorf("Failed to Send Request : [%v]", err)
cblogger.Error(newErr.Error())
return nil, newErr
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
newErr := fmt.Errorf("Failed to Read Response Body : [%v]", err)
cblogger.Error(newErr.Error())
return nil, newErr
}
// log.Printf("### body")
// spew.Dump(body)
// ### Convert []byte format of date to []unit8 format
var dataInUint8 []uint8 = body // Caution!!
return dataInUint8, nil
}