Skip to content

Commit 403abd5

Browse files
author
awlsring
committed
feat: lvm, lvmthin data source and resources
1 parent 3bfa8b6 commit 403abd5

File tree

18 files changed

+1135
-6
lines changed

18 files changed

+1135
-6
lines changed

docs/data-sources/lvm_thinpools.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "proxmox_lvm_thinpools Data Source - terraform-provider-proxmox"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# proxmox_lvm_thinpools (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Optional
19+
20+
- `filters` (Attributes List) (see [below for nested schema](#nestedatt--filters))
21+
22+
### Read-Only
23+
24+
- `lvm_thinpools` (Attributes List) (see [below for nested schema](#nestedatt--lvm_thinpools))
25+
26+
<a id="nestedatt--filters"></a>
27+
### Nested Schema for `filters`
28+
29+
Required:
30+
31+
- `name` (String) The name of the attribute to filter on.
32+
- `values` (List of String) The value(s) to be used in the filter.
33+
34+
35+
<a id="nestedatt--lvm_thinpools"></a>
36+
### Nested Schema for `lvm_thinpools`
37+
38+
Read-Only:
39+
40+
- `device` (String) The device used to create the LVM thinpool.`
41+
- `id` (String) The id of the LVM thinpool. Formatted as `{node}/{name}`.
42+
- `metadata_size` (Number) The size of the LVM thinpool metadata lv in bytes.
43+
- `name` (String) The name of the LVM thinpool.
44+
- `node` (String) The node the LVM thinpool is on.
45+
- `size` (Number) The size of the LVM thinpool in bytes.
46+
- `volume_group` (String) The associated volume group. Formatted as `{node}/{volume_group}`
47+
48+

docs/data-sources/lvms.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "proxmox_lvms Data Source - terraform-provider-proxmox"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# proxmox_lvms (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Optional
19+
20+
- `filters` (Attributes List) (see [below for nested schema](#nestedatt--filters))
21+
22+
### Read-Only
23+
24+
- `lvms` (Attributes List) (see [below for nested schema](#nestedatt--lvms))
25+
26+
<a id="nestedatt--filters"></a>
27+
### Nested Schema for `filters`
28+
29+
Required:
30+
31+
- `name` (String) The name of the attribute to filter on.
32+
- `values` (List of String) The value(s) to be used in the filter.
33+
34+
35+
<a id="nestedatt--lvms"></a>
36+
### Nested Schema for `lvms`
37+
38+
Read-Only:
39+
40+
- `device` (String) Device the LVM is on.
41+
- `id` (String) The id of the LVM. Formatted as `{node}/{name}`.
42+
- `name` (String) The name of the LVM.
43+
- `node` (String) The node the LVM is on.
44+
- `size` (Number) The size of the LVM in bytes.
45+
46+

docs/resources/lvm.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "proxmox_lvm Resource - terraform-provider-proxmox"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# proxmox_lvm (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `device` (String) Device the LVM is on.
21+
- `name` (String) The name of the LVM.
22+
- `node` (String) The node the LVM is on.
23+
24+
### Read-Only
25+
26+
- `id` (String) The id of the LVM. Formatted as `{node}/{name}`.
27+
- `size` (Number) The size of the LVM in bytes.
28+
29+

docs/resources/lvm_thinpool.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "proxmox_lvm_thinpool Resource - terraform-provider-proxmox"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# proxmox_lvm_thinpool (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `device` (String) The device to create the LVM thinpool on.
21+
- `name` (String) The name of the LVM thinpool.
22+
- `node` (String) The node the LVM thinpool is on.
23+
24+
### Read-Only
25+
26+
- `id` (String) The id of the LVM thinpool. Formatted as `{node}/{name}`.
27+
- `metadata_size` (Number) The size of the LVM thinpool metadata lv in bytes.
28+
- `size` (Number) The size of the LVM thinpool in bytes.
29+
- `volume_group` (String) The associated volume group id. Formatted as `{node}/{volume_group}`
30+
31+

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
)
3939

4040
require (
41-
github.com/awlsring/proxmox-go v0.37.0
41+
github.com/awlsring/proxmox-go v0.42.0
4242
github.com/fatih/color v1.14.1 // indirect
4343
github.com/golang/protobuf v1.5.2 // indirect
4444
github.com/hashicorp/errwrap v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
2121
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
2222
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
2323
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
24-
github.com/awlsring/proxmox-go v0.37.0 h1:Bp0SSvffO6HGmctt9EsfO8odsMOpb66N0l3O+4eEpn4=
25-
github.com/awlsring/proxmox-go v0.37.0/go.mod h1:S0kNoKsDa1cZrOfidxvC+X150eO+M+fVnq2Iu8ZC8ZI=
24+
github.com/awlsring/proxmox-go v0.42.0 h1:1lrNfSr5c+3eSqQ9DLuG49w6TNVB+1d82gKw+aBckIc=
25+
github.com/awlsring/proxmox-go v0.42.0/go.mod h1:S0kNoKsDa1cZrOfidxvC+X150eO+M+fVnq2Iu8ZC8ZI=
2626
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
2727
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
2828
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=

internal/service/lvm_thinpools.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

internal/service/lvms.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package service
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/awlsring/proxmox-go/proxmox"
9+
"github.com/awlsring/terraform-provider-proxmox/internal/service/errors"
10+
)
11+
12+
type LVM struct {
13+
Name string
14+
Node string
15+
Size int64
16+
Device string
17+
}
18+
19+
func (c *Proxmox) ListLVMs(ctx context.Context, node string) ([]LVM, error) {
20+
request := c.client.ListLVMs(ctx, node)
21+
resp, h, err := c.client.ListLVMsExecute(request)
22+
if err != nil {
23+
return nil, errors.ApiError(h, err)
24+
}
25+
26+
lvms := []LVM{}
27+
for _, c := range resp.Data.Children {
28+
var d string
29+
if c.HasChildren() {
30+
for _, ch := range c.Children {
31+
if isDiskChild(ch) {
32+
d = *ch.Name
33+
}
34+
}
35+
}
36+
37+
lvm := LVM{
38+
Name: PtrStringToString(c.Name),
39+
Node: node,
40+
Size: PtrFloatToInt64(c.Size),
41+
Device: d,
42+
}
43+
lvms = append(lvms, lvm)
44+
}
45+
46+
return lvms, nil
47+
}
48+
49+
func isDiskChild(c proxmox.LVMChild) bool {
50+
if c.Name == nil {
51+
return false
52+
}
53+
if strings.Contains(*c.Name, "/dev/") {
54+
return true
55+
}
56+
return false
57+
}
58+
59+
func (c *Proxmox) GetLVM(ctx context.Context, node string, pool string) (*LVM, error) {
60+
pools, err := c.ListLVMs(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 CreateLVMInput struct {
74+
Node string
75+
Name string
76+
Device string
77+
}
78+
79+
func (c *Proxmox) CreateLVM(ctx context.Context, input *CreateLVMInput) error {
80+
request := c.client.CreateLVM(ctx, input.Node)
81+
addStorage := float32(0)
82+
request = request.CreateLVMRequestContent(proxmox.CreateLVMRequestContent{
83+
Device: input.Device,
84+
Name: input.Name,
85+
AddStorage: &addStorage,
86+
})
87+
_, h, err := c.client.CreateLVMExecute(request)
88+
if err != nil {
89+
return errors.ApiError(h, err)
90+
}
91+
return nil
92+
}
93+
94+
func (c *Proxmox) DeleteLVM(ctx context.Context, node string, pool string) error {
95+
request := c.client.DeleteLVM(ctx, node, pool)
96+
request = request.CleanupDisks(1)
97+
_, h, err := c.client.DeleteLVMExecute(request)
98+
if err != nil {
99+
return errors.ApiError(h, err)
100+
}
101+
return nil
102+
}

internal/service/zfs_pool.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ func (c *Proxmox) CreateZFSPool(ctx context.Context, input *CreateZFSPoolInput)
131131

132132
func (c *Proxmox) DeleteZFSPool(ctx context.Context, node string, pool string) error {
133133
request := c.client.DeleteZFSPool(ctx, node, pool)
134-
request.CleanupDisks(1)
135-
// request.CleanupConfig(1)
134+
request = request.CleanupDisks(1)
136135
_, h, err := c.client.DeleteZFSPoolExecute(request)
137136
if err != nil {
138137
return errors.ApiError(h, err)

0 commit comments

Comments
 (0)