|
| 1 | +package proxmox |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/sp-yduck/proxmox-go/api" |
| 8 | +) |
| 9 | + |
| 10 | +type Pool struct { |
| 11 | + service *Service |
| 12 | + Pool *api.ResourcePool |
| 13 | +} |
| 14 | + |
| 15 | +func (s *Service) Pools(ctx context.Context) ([]*api.ResourcePool, error) { |
| 16 | + return s.restclient.GetResourcePools(ctx) |
| 17 | +} |
| 18 | + |
| 19 | +func (s *Service) Pool(ctx context.Context, id string) (*Pool, error) { |
| 20 | + pool, err := s.restclient.GetResourcPool(ctx, id) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + return &Pool{service: s, Pool: pool}, nil |
| 25 | +} |
| 26 | + |
| 27 | +func (s *Service) CreatePool(ctx context.Context, pool api.ResourcePool) (*Pool, error) { |
| 28 | + if err := s.restclient.CreateResourcePool(ctx, pool); err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + return s.Pool(ctx, pool.PoolID) |
| 32 | +} |
| 33 | + |
| 34 | +func (s *Service) DeletePool(ctx context.Context, id string) error { |
| 35 | + return s.restclient.DeleteResourcePool(ctx, id) |
| 36 | +} |
| 37 | + |
| 38 | +func (p *Pool) AddVMs(ctx context.Context, vmids []int) error { |
| 39 | + opts := api.UpdateResourcePoolOption{ |
| 40 | + PoolID: p.Pool.PoolID, |
| 41 | + VMs: itoaSlice(vmids), |
| 42 | + } |
| 43 | + return p.service.restclient.UpdateResourcePool(ctx, opts) |
| 44 | +} |
| 45 | + |
| 46 | +func (p *Pool) RemoveVMs(ctx context.Context, vmids []int) error { |
| 47 | + opts := api.UpdateResourcePoolOption{ |
| 48 | + PoolID: p.Pool.PoolID, |
| 49 | + VMs: itoaSlice(vmids), |
| 50 | + Delete: true, |
| 51 | + } |
| 52 | + return p.service.restclient.UpdateResourcePool(ctx, opts) |
| 53 | +} |
| 54 | + |
| 55 | +func (p *Pool) AddStorages(ctx context.Context, storageNames []string) error { |
| 56 | + opts := api.UpdateResourcePoolOption{ |
| 57 | + PoolID: p.Pool.PoolID, |
| 58 | + Storage: storageNames, |
| 59 | + } |
| 60 | + return p.service.restclient.UpdateResourcePool(ctx, opts) |
| 61 | +} |
| 62 | + |
| 63 | +func (p *Pool) RemoveStorages(ctx context.Context, storageNames []string) error { |
| 64 | + opts := api.UpdateResourcePoolOption{ |
| 65 | + PoolID: p.Pool.PoolID, |
| 66 | + Storage: storageNames, |
| 67 | + Delete: true, |
| 68 | + } |
| 69 | + return p.service.restclient.UpdateResourcePool(ctx, opts) |
| 70 | +} |
| 71 | + |
| 72 | +func itoaSlice(i []int) []string { |
| 73 | + a := []string{} |
| 74 | + for _, x := range i { |
| 75 | + a = append(a, fmt.Sprintf("%d", x)) |
| 76 | + } |
| 77 | + return a |
| 78 | +} |
0 commit comments