GetGroupVMs retrieves VMs from all connected profiles in the group. Each VM's SourceProfile field is set to identify which profile it came from. Returns a combined list of all VMs across all profiles.
(ctx context.Context)
| 203 | // Each VM's SourceProfile field is set to identify which profile it came from. |
| 204 | // Returns a combined list of all VMs across all profiles. |
| 205 | func (m *GroupClientManager) GetGroupVMs(ctx context.Context) ([]*VM, error) { |
| 206 | operation := func(profileName string, client *Client) (interface{}, error) { |
| 207 | // Get cluster status to retrieve nodes with their VMs |
| 208 | cluster, err := client.GetClusterStatus() |
| 209 | if err != nil { |
| 210 | return nil, fmt.Errorf("failed to get cluster status: %w", err) |
| 211 | } |
| 212 | |
| 213 | // Collect all VMs from all nodes |
| 214 | var vms []*VM |
| 215 | for _, node := range cluster.Nodes { |
| 216 | if node != nil && node.VMs != nil { |
| 217 | for _, vm := range node.VMs { |
| 218 | if vm != nil { |
| 219 | vm.SourceProfile = profileName |
| 220 | vms = append(vms, vm) |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return vms, nil |
| 227 | } |
| 228 | |
| 229 | groupFunc := func(results []ProfileResult) (interface{}, error) { |
| 230 | var allVMs []*VM |
| 231 | |
| 232 | for _, result := range results { |
| 233 | if vms, ok := result.Data.([]*VM); ok { |
| 234 | allVMs = append(allVMs, vms...) |
| 235 | } |
| 236 | } |
| 237 | allVMs = deduplicateGroupVMs(allVMs) |
| 238 | |
| 239 | // Sort VMs by profile name, then by node, then by ID |
| 240 | sort.Slice(allVMs, func(i, j int) bool { |
| 241 | if allVMs[i] == nil || allVMs[j] == nil { |
| 242 | return allVMs[i] != nil |
| 243 | } |
| 244 | // Sort by profile name first |
| 245 | if allVMs[i].SourceProfile != allVMs[j].SourceProfile { |
| 246 | return allVMs[i].SourceProfile < allVMs[j].SourceProfile |
| 247 | } |
| 248 | // Then by node name |
| 249 | if allVMs[i].Node != allVMs[j].Node { |
| 250 | return allVMs[i].Node < allVMs[j].Node |
| 251 | } |
| 252 | // Finally by VM ID |
| 253 | return allVMs[i].ID < allVMs[j].ID |
| 254 | }) |
| 255 | |
| 256 | return allVMs, nil |
| 257 | } |
| 258 | |
| 259 | data, err := m.GetGroupData(ctx, operation, groupFunc) |
| 260 | if err != nil { |
| 261 | return nil, err |
| 262 | } |
no test coverage detected