diff --git a/resources/virt.go b/resources/virt.go
index 87a474f3..540e10c9 100644
--- a/resources/virt.go
+++ b/resources/virt.go
@@ -998,7 +998,7 @@ func (d *diskDevice) GetXML(idx int) string {
b += ""
b += fmt.Sprintf("", d.Type)
b += fmt.Sprintf("", source)
- b += fmt.Sprintf("", (string)(idx+97)) // TODO: 26, 27... should be 'aa', 'ab'...
+ b += fmt.Sprintf("", numToAlpha(idx))
b += ""
return b
}
@@ -1009,7 +1009,7 @@ func (d *cdRomDevice) GetXML(idx int) string {
b += ""
b += fmt.Sprintf("", d.Type)
b += fmt.Sprintf("", source)
- b += fmt.Sprintf("", (string)(idx+97)) // TODO: 26, 27... should be 'aa', 'ab'...
+ b += fmt.Sprintf("", numToAlpha(idx))
b += ""
b += ""
return b
@@ -1195,3 +1195,12 @@ func expandHome(p string) (string, error) {
return p, nil
}
+
+func numToAlpha(idx int) string {
+ var mod = idx % 26
+ var div = idx / 26
+ if div > 0 {
+ return numToAlpha(div-1) + string(rune(mod+int('a')))
+ }
+ return string(rune(mod + int('a')))
+}
diff --git a/resources/virt_test.go b/resources/virt_test.go
index 2dac409f..8af59b58 100644
--- a/resources/virt_test.go
+++ b/resources/virt_test.go
@@ -46,3 +46,25 @@ func TestExpandHome(t *testing.T) {
}
}
}
+
+func TestNumToAlpha(t *testing.T) {
+ var numToAlphaTests = []struct {
+ number int
+ result string
+ }{
+ {0, "a"},
+ {25, "z"},
+ {26, "aa"},
+ {27, "ab"},
+ {702, "aaa"},
+ {703, "aab"},
+ {63269, "cool"},
+ }
+
+ for _, test := range numToAlphaTests {
+ actual := numToAlpha(test.number)
+ if actual != test.result {
+ t.Errorf("numToAlpha(%d): expected %s, actual %s", test.number, test.result, actual)
+ }
+ }
+}