This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
virtid.go
109 lines (89 loc) · 2.39 KB
/
virtid.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
package uyuniapi
import (
"fmt"
"strconv"
)
// Virtual ID manager
type VIDManager struct {
umap map[string]int
urmap map[int]string
}
func NewVIDManager() *VIDManager {
vidman := new(VIDManager)
vidman.umap = make(map[string]int)
vidman.urmap = make(map[int]string)
return vidman
}
// Add context
func (vidman *VIDManager) AddContext(fqdn string) {
vidman.umap[fqdn] = len(vidman.umap) + 1
vidman.urmap[vidman.umap[fqdn]] = fqdn
}
// Get context
func (vidman *VIDManager) GetContext(fqdn string) *VIDContext {
var vid *VIDContext
if fqdn_id, exist := vidman.umap[fqdn]; exist {
vid = NewVIDContext(fqdn_id, len(vidman.umap))
} else {
panic("Context not found")
}
return vid
}
// Decode virtual ID and map to the FQDN of the context server
func (vidman *VIDManager) ToSystemId(vsysid int) (int, string) {
vids := strconv.Itoa(vsysid)[1:]
ctxdgt := len(strconv.Itoa(len(vidman.umap)))
ctx, _ := strconv.Atoi(vids[:ctxdgt])
sid, _ := strconv.Atoi(vids[ctxdgt:])
return sid, vidman.urmap[ctx]
}
// Return all known contexts to the VID manager
func (vidman *VIDManager) GetContextFQDNs() []string {
ctx := make([]string, len(vidman.umap))
idx := 0
for fqdn := range vidman.umap {
ctx[idx] = fqdn
idx++
}
return ctx
}
// ID calculations
type VIDContext struct {
max int
context int
}
// Constructor
func NewVIDContext(context int, max int) *VIDContext {
idc := new(VIDContext)
idc.context = context
idc.max = max
return idc
}
// Max width for context zero padding
func (idc *VIDContext) ctxDigits() string {
return strconv.Itoa(len(strconv.Itoa(idc.max)))
}
/*
Translation to the Virtual ID. The virtual ID consists of the following format:
1 [max] [System ID]
^ ^ ^
| | |
| | +--- A regular Uyuni's System ID
| +--------- Maximum number of Uyuni servers. Smaller numbers are padded with zero.
+------------ Always 1
*/
func (idc *VIDContext) ToVirtualId(systemid int) int {
ptn := "1%0" + idc.ctxDigits() + "d"
vid, _ := strconv.Atoi(fmt.Sprintf(ptn+"%d", idc.context, systemid))
return vid
}
// Extract system ID within the given context
func (idc *VIDContext) ToSystemId(vsysid int) int {
vids := strconv.Itoa(vsysid)[1:]
ctxIdx, _ := strconv.Atoi(fmt.Sprintf("%0"+idc.ctxDigits()+"d", idc.context))
if ctxIdx != idc.context {
panic("System ID does not belong to this context")
}
sid, _ := strconv.Atoi(vids[len(idc.ctxDigits()):])
return sid
}