Skip to content

Commit

Permalink
Enhance Supported VM Spec Info. and, Deduplicate VM Spec info list
Browse files Browse the repository at this point in the history
  • Loading branch information
innodreamer committed Feb 5, 2025
1 parent f8e07de commit 9675bad
Showing 1 changed file with 89 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"strconv"
// "github.com/davecgh/go-spew/spew"

"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
Expand Down Expand Up @@ -76,11 +77,12 @@ func (vmSpecHandler *NcpVMSpecHandler) ListVMSpec() ([]*irs.VMSpecInfo, error) {
}

// Note : var vmProductList []*server.Product //NCP Product(Spec) Info.
var vmSpecInfoList []*irs.VMSpecInfo //Cloud-Barista Spec Info.

var vmSpecInfoMap = make(map[string]*irs.VMSpecInfo) // Map to track unique VMSpec Info.
var vmSpecInfoList []*irs.VMSpecInfo // List to return unique VMSpec Info.

for _, image := range imageListResult {
cblogger.Infof("# 기준 NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)

cblogger.Infof("# Lookup by NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)
vmSpecReq := server.GetServerProductListRequest{
RegionNo: regionNo,
ZoneNo: zoneNo,
Expand All @@ -94,15 +96,36 @@ func (vmSpecHandler *NcpVMSpecHandler) ListVMSpec() ([]*irs.VMSpecInfo, error) {
return nil, err
} else {
cblogger.Info("Succeeded in Getting VMSpec list!!")
cblogger.Infof("기준 NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)
cblogger.Infof("에 대해 조회된 VMSpec 정보 수 : [%d]", len(result.ProductList))
cblogger.Infof("Lookup by NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)
cblogger.Infof("Number of VMSpec info looked up : [%d]", len(result.ProductList))
}

for _, product := range result.ProductList {
vmSpecInfo := MappingVMSpecInfo(ncpRegion, image.IId.SystemId, *product)
vmSpecInfoList = append(vmSpecInfoList, &vmSpecInfo)
vmSpecInfo := mappingVMSpecInfo(ncpRegion, *product)
if existingSpec, exists := vmSpecInfoMap[vmSpecInfo.Name]; exists {
// If the VMSpec already exists, add the image ID to the related images list in KeyValueList
for i, kv := range existingSpec.KeyValueList {
if kv.Key == "RelatedImages" {
existingSpec.KeyValueList[i].Value += "," + image.IId.SystemId
break
}
}
} else {
// If the VMSpec is new, add it to the map and initialize the related images list in KeyValueList
vmSpecInfo.KeyValueList = append(vmSpecInfo.KeyValueList, irs.KeyValue{
Key: "RelatedImages",
Value: image.IId.SystemId,
})
vmSpecInfoMap[vmSpecInfo.Name] = &vmSpecInfo
}
}
}

// Convert the map to a list
for _, specInfo := range vmSpecInfoMap {
vmSpecInfoList = append(vmSpecInfoList, specInfo)
}

cblogger.Infof("# 총 VMSpec 수 : [%d]", len(vmSpecInfoList))
return vmSpecInfoList, err
}
Expand Down Expand Up @@ -144,7 +167,7 @@ func (vmSpecHandler *NcpVMSpecHandler) GetVMSpec(Name string) (irs.VMSpecInfo, e

imgListResult, err := imgHandler.ListImage()
if err != nil {
cblogger.Infof("Failed to Find Image list!! : ", err)
cblogger.Infof("Failed to Find Image list!! : [%v]", err)
return irs.VMSpecInfo{}, errors.New("Failed to Find Image list!!")
} else {
cblogger.Info("Succeeded in Getting Image list!!")
Expand All @@ -154,7 +177,7 @@ func (vmSpecHandler *NcpVMSpecHandler) GetVMSpec(Name string) (irs.VMSpecInfo, e
}

for _, image := range imgListResult {
cblogger.Infof("# 기준 NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)
cblogger.Infof("# Lookup by NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)

specReq := server.GetServerProductListRequest{
RegionNo: regionNo,
Expand All @@ -175,7 +198,7 @@ func (vmSpecHandler *NcpVMSpecHandler) GetVMSpec(Name string) (irs.VMSpecInfo, e
// spew.Dump(result)

if len(result.ProductList) > 0 {
specInfo := MappingVMSpecInfo(ncpRegion, image.IId.SystemId, *result.ProductList[0])
specInfo := mappingVMSpecInfo(ncpRegion, *result.ProductList[0])
return specInfo, nil
}
}
Expand Down Expand Up @@ -226,10 +249,12 @@ func (vmSpecHandler *NcpVMSpecHandler) ListOrgVMSpec() (string, error) {
// spew.Dump(imageListResult)
}

var productList []*server.Product //NCP Product(Spec) Info.
var vmSpecInfoMap = make(map[string]*irs.VMSpecInfo) // Map to track unique VMSpec Info.
var vmSpecInfoList []*irs.VMSpecInfo // List to return unique VMSpec Info.

for _, image := range imageListResult {
cblogger.Infof("# 기준 NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)

cblogger.Infof("# Lookup by NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)
vmSpecReq := server.GetServerProductListRequest{
RegionNo: regionNo,
ZoneNo: zoneNo,
Expand All @@ -243,17 +268,38 @@ func (vmSpecHandler *NcpVMSpecHandler) ListOrgVMSpec() (string, error) {
return "", err
} else {
cblogger.Info("Succeeded in Getting VMSpec list!!")
cblogger.Infof("기준 NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)
cblogger.Infof("에 대해 조회된 VMSpec 정보 수 : [%d]", len(result.ProductList))
}

cblogger.Infof("Lookup by NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)
cblogger.Infof("Number of VMSpec info looked up : [%d]", len(result.ProductList))
}
for _, product := range result.ProductList {
productList = append(productList, product)
vmSpecInfo := mappingVMSpecInfo(ncpRegion, *product)
if existingSpec, exists := vmSpecInfoMap[vmSpecInfo.Name]; exists {
// If the VMSpec already exists, add the image ID to the related images list in KeyValueList
for i, kv := range existingSpec.KeyValueList {
if kv.Key == "RelatedImages" {
existingSpec.KeyValueList[i].Value += "," + image.IId.SystemId
break
}
}
} else {
// If the VMSpec is new, add it to the map and initialize the related images list in KeyValueList
vmSpecInfo.KeyValueList = append(vmSpecInfo.KeyValueList, irs.KeyValue{
Key: "RelatedImages",
Value: image.IId.SystemId,
})
vmSpecInfoMap[vmSpecInfo.Name] = &vmSpecInfo
}
}
}
cblogger.Infof("# 총 VMSpec 수 : [%d]", len(productList))

// Convert the map to a list
for _, specInfo := range vmSpecInfoMap {
vmSpecInfoList = append(vmSpecInfoList, specInfo)
}
// cblogger.Infof("# VMSpec Count : [%d]", len(vmSpecInfoList))

jsonString, jsonErr := ConvertJsonString(productList)
jsonString, jsonErr := ConvertJsonString(vmSpecInfoList)
if jsonErr != nil {
cblogger.Error(jsonErr)
return "", jsonErr
Expand Down Expand Up @@ -308,7 +354,7 @@ func (vmSpecHandler *NcpVMSpecHandler) GetOrgVMSpec(Name string) (string, error)
}

for _, image := range imgListResult {
cblogger.Infof("# 기준 NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)
cblogger.Infof("# Lookup by NCP Image ID(ImageProductCode) : [%s]", image.IId.SystemId)

specReq := server.GetServerProductListRequest{
RegionNo: regionNo,
Expand Down Expand Up @@ -341,39 +387,35 @@ func (vmSpecHandler *NcpVMSpecHandler) GetOrgVMSpec(Name string) (string, error)
return "", nil
}

func MappingVMSpecInfo(Region string, ImageId string, NcpVMSpec server.Product) irs.VMSpecInfo {
func mappingVMSpecInfo(region string, vmSpec server.Product) irs.VMSpecInfo {
// server ProductList type : []*Product
cblogger.Infof("*** Mapping VMSpecInfo : Region: [%s] / SpecName: [%s]", Region, *NcpVMSpec.ProductCode)
cblogger.Infof("*** Mapping VMSpecInfo : Region: [%s] / SpecName: [%s]", region, *vmSpec.ProductCode)
// spew.Dump(vmSpec)

// vmSpec에 리전 정보는 없기 때문에 받은 리전 정보로 기입
// NOTE 주의 : vmSpec.ProductCode -> specName 으로
// Since there is no region information in vmSpec, use the region information provided to the function
// NOTE: Caution: vmSpec.ProductCode -> specName
vmSpecInfo := irs.VMSpecInfo{
Region: Region,
//Name: *NcpVMSpec.ProductName,
Name: *NcpVMSpec.ProductCode,
// int32 to string 변환 : String(), int64 to string 변환 : strconv.Itoa()
VCpu: irs.VCpuInfo{Count: String(*NcpVMSpec.CpuCount), Clock: "N/A"},
Region: region,
// Name: *vmSpec.ProductName,
Name: *vmSpec.ProductCode,
// int32 to string : String(), int64 to string : strconv.Itoa()
VCpu: irs.VCpuInfo{Count: String(*vmSpec.CpuCount), Clock: "-1"},

// server.Product에 GPU 정보는 없음.
Gpu: []irs.GpuInfo{{Count: "N/A", Model: "N/A"}},
// 'server.Product' does not contain GPU information.
Gpu: []irs.GpuInfo{{Count: "-1", Mfr: "NA", Model: "NA", Mem: "-1"}},
Mem: strconv.FormatFloat(float64(*vmSpec.MemorySize)/(1024*1024), 'f', 0, 64),
Disk: strconv.FormatFloat(float64(*vmSpec.BaseBlockStorageSize)/(1024*1024*1024), 'f', 0, 64),

KeyValueList: []irs.KeyValue{
{Key: "ProductType", Value: *vmSpec.ProductType.CodeName},
{Key: "InfraResourceType", Value: *vmSpec.InfraResourceType.CodeName},
{Key: "DiskType", Value: *vmSpec.DiskType.CodeName},
{Key: "ProductDescription", Value: *vmSpec.ProductDescription},
{Key: "GenerationCode", Value: *vmSpec.GenerationCode},
// {Key: "ProductName", Value: *vmSpec.ProductName}, //This is same to 'ProductDescription'.
{Key: "ProductType", Value: *NcpVMSpec.ProductType.CodeName},
{Key: "InfraResourceType", Value: *NcpVMSpec.InfraResourceType.CodeName},
// {Key: "PlatformType", Value: *NcpVMSpec.PlatformType.CodeName}, //This makes "invalid memory address or nil pointer dereference" error
{Key: "BaseBlockStorageSize(GB)", Value: strconv.FormatFloat(float64(*NcpVMSpec.BaseBlockStorageSize)/(1024*1024*1024), 'f', 0, 64)},
{Key: "DiskType", Value: *NcpVMSpec.DiskType.CodeName},
{Key: "ProductDescription", Value: *NcpVMSpec.ProductDescription},
{Key: "SupportingImageSystemId", Value: ImageId},
{Key: "NCPGenerationCode", Value: *NcpVMSpec.GenerationCode},
{Key: "NCP Region", Value: Region},
// {Key: "PlatformType", Value: *vmSpec.PlatformType.CodeName}, //This makes "invalid memory address or nil pointer dereference" error
},
}

// vmSpecInfo.Mem = strconv.FormatFloat(float64(*vmSpec.MemorySize)*1024, 'f', 0, 64) // GB->MB로 변환
vmSpecInfo.Mem = strconv.FormatFloat(float64(*NcpVMSpec.MemorySize)/(1024*1024), 'f', 0, 64)

// Mem : strconv.FormatFloat(float64(*vmSpec.MemorySize)*1024, 'f', 0, 64) // GB -> MB
return vmSpecInfo
}

0 comments on commit 9675bad

Please sign in to comment.