|
| 1 | +package framework |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/sp-yduck/proxmox-go/api" |
| 7 | + "github.com/sp-yduck/proxmox-go/proxmox" |
| 8 | +) |
| 9 | + |
| 10 | +type Status struct { |
| 11 | + code int |
| 12 | + reasons []string |
| 13 | + err error |
| 14 | + failedPlugin string |
| 15 | +} |
| 16 | + |
| 17 | +func NewStatus() *Status { |
| 18 | + return &Status{code: 0} |
| 19 | +} |
| 20 | + |
| 21 | +func (s *Status) Code() int { |
| 22 | + return s.code |
| 23 | +} |
| 24 | + |
| 25 | +func (s *Status) SetCode(code int) { |
| 26 | + s.code = code |
| 27 | +} |
| 28 | + |
| 29 | +func (s *Status) Reasons() []string { |
| 30 | + if s.err != nil { |
| 31 | + return append([]string{s.err.Error()}, s.reasons...) |
| 32 | + } |
| 33 | + return s.reasons |
| 34 | +} |
| 35 | + |
| 36 | +func (s *Status) FailedPlugin() string { |
| 37 | + return s.failedPlugin |
| 38 | +} |
| 39 | + |
| 40 | +func (s *Status) SetFailedPlugin(name string) { |
| 41 | + s.failedPlugin = name |
| 42 | +} |
| 43 | + |
| 44 | +func (s *Status) IsSuccess() bool { |
| 45 | + return s.code == 0 |
| 46 | +} |
| 47 | + |
| 48 | +func (s *Status) Error() error { |
| 49 | + return s.err |
| 50 | +} |
| 51 | + |
| 52 | +// NodeInfo is node level aggregated information |
| 53 | +type NodeInfo struct { |
| 54 | + node *api.Node |
| 55 | + |
| 56 | + // qemus assigned to the node |
| 57 | + qemus []*api.VirtualMachine |
| 58 | +} |
| 59 | + |
| 60 | +func GetNodeInfoList(ctx context.Context, client *proxmox.Service) ([]*NodeInfo, error) { |
| 61 | + nodes, err := client.Nodes(ctx) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + nodeInfos := []*NodeInfo{} |
| 66 | + for _, node := range nodes { |
| 67 | + qemus, err := client.RESTClient().GetVirtualMachines(ctx, node.Node) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + nodeInfos = append(nodeInfos, &NodeInfo{node: node, qemus: qemus}) |
| 72 | + } |
| 73 | + return nodeInfos, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (n NodeInfo) Node() *api.Node { |
| 77 | + return n.node |
| 78 | +} |
| 79 | + |
| 80 | +func (n NodeInfo) QEMUs() []*api.VirtualMachine { |
| 81 | + return n.qemus |
| 82 | +} |
| 83 | + |
| 84 | +// NodeScoreList declares a list of nodes and their scores. |
| 85 | +type NodeScoreList []NodeScore |
| 86 | + |
| 87 | +// NodeScore is a struct with node name and score. |
| 88 | +type NodeScore struct { |
| 89 | + Name string |
| 90 | + Score int64 |
| 91 | +} |
0 commit comments