Skip to content

Commit d18c5cb

Browse files
mulpuriHarness
authored andcommitted
[feat]: [CCM-27581]: Map all azure instances in empty category to General Purpose (#20)
* cdf987 CCM-26375: Map all azure instances in empty category to General Purpose
1 parent 25b710f commit d18c5cb

File tree

2 files changed

+88
-14
lines changed

2 files changed

+88
-14
lines changed

internal/cloudinfo/cloudinfo.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,24 @@ func (cpi *cloudInfo) GetSeriesDetails(provider, service, region string) (map[st
186186
return nil, nil, errors.NewWithDetails("VMs Information not yet cached", "provider", provider, "service", service, "region", region)
187187
}
188188

189+
// For Azure, map empty categories to "General purpose"
190+
isAzure := strings.ToLower(provider) == "azure"
191+
189192
categorySeriesMap := map[string]map[string][]string{}
190193
for _, vm := range vms {
191-
if _, ok := categorySeriesMap[vm.Category]; !ok {
192-
categorySeriesMap[vm.Category] = map[string][]string{}
194+
category := vm.Category
195+
// Map empty category to "General purpose" for Azure instances
196+
if isAzure && category == "" {
197+
category = types.CategoryGeneral
198+
}
199+
200+
if _, ok := categorySeriesMap[category]; !ok {
201+
categorySeriesMap[category] = map[string][]string{}
193202
}
194-
if _, ok := categorySeriesMap[vm.Category][vm.Series]; !ok {
195-
categorySeriesMap[vm.Category][vm.Series] = make([]string, 0)
203+
if _, ok := categorySeriesMap[category][vm.Series]; !ok {
204+
categorySeriesMap[category][vm.Series] = make([]string, 0)
196205
}
197-
categorySeriesMap[vm.Category][vm.Series] = append(categorySeriesMap[vm.Category][vm.Series], vm.Type)
206+
categorySeriesMap[category][vm.Series] = append(categorySeriesMap[category][vm.Series], vm.Type)
198207
}
199208

200209
var seriesDetails []types.SeriesDetails

internal/cloudinfo/cloudinfo_test.go

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import (
2525
// DummyCloudInfoStore type implements the CloudInfoStore interface for mockig of external calls
2626
// the struct is to be extended according to the needs of test cases
2727
type DummyCloudInfoStore struct {
28-
TcId string
28+
TcId string
29+
customVMs []types.VMInfo
2930
// implement the interface
3031
CloudInfoStore
3132
}
@@ -53,6 +54,10 @@ func (dcis *DummyCloudInfoStore) GetVm(provider, service, region string) ([]type
5354
case notCached:
5455
return nil, false
5556
default:
57+
// If customVMs is provided, use it; otherwise use default VMs
58+
if dcis.customVMs != nil {
59+
return dcis.customVMs, true
60+
}
5661
return []types.VMInfo{
5762
{Category: types.CategoryGeneral, Series: "series0", Type: "instanceType00"},
5863
{Category: types.CategoryCompute, Series: "series1", Type: "instanceType10"},
@@ -212,13 +217,15 @@ func TestCachingCloudInfo_GetRegions(t *testing.T) {
212217

213218
func TestCloudInfo_GetSeries(t *testing.T) {
214219
tests := []struct {
215-
name string
216-
ciStore CloudInfoStore
217-
checker func(categorySeriesMap map[string]map[string][]string, seriesDetails []types.SeriesDetails, err error)
220+
name string
221+
provider string
222+
ciStore CloudInfoStore
223+
checker func(categorySeriesMap map[string]map[string][]string, seriesDetails []types.SeriesDetails, err error)
218224
}{
219225
{
220-
name: "successfully retrieved the series",
221-
ciStore: &DummyCloudInfoStore{},
226+
name: "successfully retrieved the series",
227+
provider: "dummyProvider",
228+
ciStore: &DummyCloudInfoStore{},
222229
checker: func(categorySeriesMap map[string]map[string][]string, seriesDetails []types.SeriesDetails, err error) {
223230
assert.Nil(t, err, "the error should be nil")
224231

@@ -242,19 +249,77 @@ func TestCloudInfo_GetSeries(t *testing.T) {
242249
},
243250
},
244251
{
245-
name: "failed to retrieve series",
246-
ciStore: &DummyCloudInfoStore{TcId: notCached},
252+
name: "failed to retrieve series",
253+
provider: "dummyProvider",
254+
ciStore: &DummyCloudInfoStore{TcId: notCached},
247255
checker: func(categorySeriesMap map[string]map[string][]string, seriesDetails []types.SeriesDetails, err error) {
248256
assert.Nil(t, seriesDetails, "the seriesDetails should be nil")
249257
assert.EqualError(t, err, "VMs Information not yet cached")
250258
},
251259
},
260+
{
261+
name: "Azure instances with empty category mapped to General purpose",
262+
provider: "azure",
263+
ciStore: &DummyCloudInfoStore{
264+
customVMs: []types.VMInfo{
265+
{Category: "", Series: "DASv5", Type: "Standard_D16as_v5"},
266+
{Category: "", Series: "DASv5", Type: "Standard_D2as_v5"},
267+
{Category: types.CategoryMemory, Series: "Ev3", Type: "Standard_E16_v3"},
268+
},
269+
},
270+
checker: func(categorySeriesMap map[string]map[string][]string, seriesDetails []types.SeriesDetails, err error) {
271+
assert.Nil(t, err, "the error should be nil")
272+
273+
// Empty categories should be mapped to "General purpose" for Azure
274+
assert.Equal(t, categorySeriesMap, map[string]map[string][]string{
275+
types.CategoryGeneral: {
276+
"DASv5": []string{"Standard_D16as_v5", "Standard_D2as_v5"},
277+
},
278+
types.CategoryMemory: {
279+
"Ev3": []string{"Standard_E16_v3"},
280+
},
281+
})
282+
283+
assert.ElementsMatch(t, seriesDetails, []types.SeriesDetails{
284+
{Series: "DASv5", Category: types.CategoryGeneral, InstanceTypes: []string{"Standard_D16as_v5", "Standard_D2as_v5"}},
285+
{Series: "Ev3", Category: types.CategoryMemory, InstanceTypes: []string{"Standard_E16_v3"}},
286+
})
287+
},
288+
},
289+
{
290+
name: "non-Azure instances with empty category remain empty",
291+
provider: "amazon",
292+
ciStore: &DummyCloudInfoStore{
293+
customVMs: []types.VMInfo{
294+
{Category: "", Series: "t3", Type: "t3.micro"},
295+
{Category: types.CategoryGeneral, Series: "m5", Type: "m5.large"},
296+
},
297+
},
298+
checker: func(categorySeriesMap map[string]map[string][]string, seriesDetails []types.SeriesDetails, err error) {
299+
assert.Nil(t, err, "the error should be nil")
300+
301+
// Empty categories should remain empty for non-Azure providers
302+
assert.Equal(t, categorySeriesMap, map[string]map[string][]string{
303+
"": {
304+
"t3": []string{"t3.micro"},
305+
},
306+
types.CategoryGeneral: {
307+
"m5": []string{"m5.large"},
308+
},
309+
})
310+
311+
assert.ElementsMatch(t, seriesDetails, []types.SeriesDetails{
312+
{Series: "t3", Category: "", InstanceTypes: []string{"t3.micro"}},
313+
{Series: "m5", Category: types.CategoryGeneral, InstanceTypes: []string{"m5.large"}},
314+
})
315+
},
316+
},
252317
}
253318
for _, test := range tests {
254319
t.Run(test.name, func(t *testing.T) {
255320
info, _ := NewCloudInfo([]string{}, &DummyCloudInfoStore{}, cloudinfoLogger)
256321
info.cloudInfoStore = test.ciStore
257-
test.checker(info.GetSeriesDetails("dummyProvider", "dummyService", "dummyRegion"))
322+
test.checker(info.GetSeriesDetails(test.provider, "dummyService", "dummyRegion"))
258323
})
259324
}
260325
}

0 commit comments

Comments
 (0)