|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/awlsring/proxmox-go/proxmox" |
| 8 | + "github.com/awlsring/terraform-provider-proxmox/internal/service/errors" |
| 9 | +) |
| 10 | + |
| 11 | +type LVMThinpool struct { |
| 12 | + Name string |
| 13 | + Node string |
| 14 | + Size int64 |
| 15 | + MetadataSize int64 |
| 16 | + VolumeGroup string |
| 17 | + Device string |
| 18 | +} |
| 19 | + |
| 20 | +func (c *Proxmox) ListLVMThinpools(ctx context.Context, node string) ([]LVMThinpool, error) { |
| 21 | + request := c.client.ListLVMThins(ctx, node) |
| 22 | + pools, h, err := c.client.ListLVMThinsExecute(request) |
| 23 | + if err != nil { |
| 24 | + return nil, errors.ApiError(h, err) |
| 25 | + } |
| 26 | + |
| 27 | + lvms, err := c.ListLVMs(ctx, node) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + mapping := map[string]LVM{} |
| 33 | + for _, l := range lvms { |
| 34 | + mapping[l.Name] = l |
| 35 | + } |
| 36 | + |
| 37 | + thins := []LVMThinpool{} |
| 38 | + for _, p := range pools.Data { |
| 39 | + |
| 40 | + var vg LVM |
| 41 | + if v, ok := mapping[p.Vg]; ok { |
| 42 | + vg = v |
| 43 | + } |
| 44 | + |
| 45 | + thin := LVMThinpool{ |
| 46 | + Name: p.Lv, |
| 47 | + Node: node, |
| 48 | + Size: int64(p.LvSize), |
| 49 | + MetadataSize: int64(p.MetadataSize), |
| 50 | + VolumeGroup: p.Vg, |
| 51 | + Device: vg.Device, |
| 52 | + } |
| 53 | + thins = append(thins, thin) |
| 54 | + } |
| 55 | + |
| 56 | + return thins, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (c *Proxmox) GetLVMThinpool(ctx context.Context, node string, pool string) (*LVMThinpool, error) { |
| 60 | + pools, err := c.ListLVMThinpools(ctx, node) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + for _, p := range pools { |
| 66 | + if p.Name == pool { |
| 67 | + return &p, nil |
| 68 | + } |
| 69 | + } |
| 70 | + return nil, fmt.Errorf("LVM thinpool not found") |
| 71 | +} |
| 72 | + |
| 73 | +type CreateLVMThinpoolInput struct { |
| 74 | + Node string |
| 75 | + Name string |
| 76 | + Device string |
| 77 | +} |
| 78 | + |
| 79 | +func (c *Proxmox) CreateLVMThinpool(ctx context.Context, input *CreateLVMThinpoolInput) error { |
| 80 | + request := c.client.CreateLVMThin(ctx, input.Node) |
| 81 | + addStorage := float32(0) |
| 82 | + request = request.CreateLVMThinRequestContent(proxmox.CreateLVMThinRequestContent{ |
| 83 | + Device: input.Device, |
| 84 | + Name: input.Name, |
| 85 | + AddStorage: &addStorage, |
| 86 | + }) |
| 87 | + _, h, err := c.client.CreateLVMThinExecute(request) |
| 88 | + if err != nil { |
| 89 | + return errors.ApiError(h, err) |
| 90 | + } |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func (c *Proxmox) DeleteLVMThinpool(ctx context.Context, node string, pool string, vg string) error { |
| 95 | + request := c.client.DeleteLVMThin(ctx, node, pool) |
| 96 | + request = request.VolumeGroup(vg) |
| 97 | + request = request.CleanupDisks(1) |
| 98 | + _, h, err := c.client.DeleteLVMThinExecute(request) |
| 99 | + if err != nil { |
| 100 | + return errors.ApiError(h, err) |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
0 commit comments