Skip to content

Commit ebd353a

Browse files
committed
feat(storage): integrate backup's block
Signed-off-by: James Neill <[email protected]>
1 parent 9025a70 commit ebd353a

13 files changed

+175
-78
lines changed

fwprovider/storage/model_backups.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
// BackupModel maps the backup block schema.
88
type BackupModel struct {
99
MaxProtectedBackups types.Int64 `tfsdk:"max_protected_backups"`
10+
KeepAll types.Bool `tfsdk:"keep_all"`
1011
KeepLast types.Int64 `tfsdk:"keep_last"`
1112
KeepHourly types.Int64 `tfsdk:"keep_hourly"`
1213
KeepDaily types.Int64 `tfsdk:"keep_daily"`

fwprovider/storage/model_directory.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type DirectoryStorageModel struct {
1212
StorageModelBase
1313
Path types.String `tfsdk:"path"`
1414
Preallocation types.String `tfsdk:"preallocation"`
15+
Backups *BackupModel `tfsdk:"backups"`
1516
}
1617

1718
func (m *DirectoryStorageModel) GetStorageType() types.String {

fwprovider/storage/resource_directory.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (r *directoryStorageResource) Metadata(_ context.Context, req resource.Meta
3939
}
4040

4141
func (r *directoryStorageResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
42-
specificAttributes := map[string]schema.Attribute{
42+
attributes := map[string]schema.Attribute{
4343
"path": schema.StringAttribute{
4444
Description: "The path to the directory on the Proxmox node.",
4545
Required: true,
@@ -59,8 +59,11 @@ func (r *directoryStorageResource) Schema(_ context.Context, _ resource.SchemaRe
5959
},
6060
}
6161

62-
resp.Schema = storageSchemaFactory(specificAttributes)
63-
resp.Schema.Description = "Manages a directory-based storage in Proxmox VE."
62+
factory := NewStorageSchemaFactory()
63+
factory.WithAttributes(attributes)
64+
factory.WithDescription("Manages directory-based storage in Proxmox VE.")
65+
factory.WithBackupBlock()
66+
resp.Schema = *factory.Schema
6467
}
6568

6669
// Configure adds the provider configured client to the resource.

fwprovider/storage/resource_lvm.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func (r *lvmPoolStorageResource) Schema(_ context.Context, _ resource.SchemaRequ
5959
Computed: true,
6060
},
6161
}
62-
s := storageSchemaFactory(attributes)
63-
s.Description = "Manages LVM-based storage in Proxmox VE."
64-
resp.Schema = s
62+
factory := NewStorageSchemaFactory()
63+
factory.WithAttributes(attributes)
64+
factory.WithDescription("Manages LVM-based storage in Proxmox VE.")
65+
resp.Schema = *factory.Schema
6566
}

fwprovider/storage/resource_lvm_thin.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func (r *lvmThinPoolStorageResource) Schema(_ context.Context, _ resource.Schema
5757
Computed: true,
5858
},
5959
}
60-
s := storageSchemaFactory(attributes)
61-
s.Description = "Manages LVM-based storage in Proxmox VE."
62-
resp.Schema = s
60+
factory := NewStorageSchemaFactory()
61+
factory.WithAttributes(attributes)
62+
factory.WithDescription("Manages thin LVM-based storage in Proxmox VE.")
63+
resp.Schema = *factory.Schema
6364
}

fwprovider/storage/resource_nfs.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ func (r *nfsStorageResource) Schema(_ context.Context, _ resource.SchemaRequest,
7878
Default: booldefault.StaticBool(true),
7979
},
8080
}
81-
s := storageSchemaFactory(attributes)
82-
s.Description = "Manages an NFS-based storage in Proxmox VE."
83-
resp.Schema = s
81+
82+
factory := NewStorageSchemaFactory()
83+
factory.WithAttributes(attributes)
84+
factory.WithDescription("Manages an NFS-based storage in Proxmox VE.")
85+
resp.Schema = *factory.Schema
8486
}

fwprovider/storage/resource_pbs.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ func (r *pbsStorageResource) Schema(_ context.Context, _ resource.SchemaRequest,
164164
Computed: true,
165165
},
166166
}
167-
resp.Schema = storageSchemaFactory(attributes)
168-
resp.Schema.Description = "Manages a Proxmox Backup Server (PBS) storage in Proxmox VE."
167+
factory := NewStorageSchemaFactory()
168+
factory.WithAttributes(attributes)
169+
factory.WithDescription("Manages a Proxmox Backup Server (PBS) storage in Proxmox VE.")
170+
resp.Schema = *factory.Schema
169171
}

fwprovider/storage/resource_zfs.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ func (r *zfsPoolStorageResource) Schema(_ context.Context, _ resource.SchemaRequ
6060
Default: booldefault.StaticBool(false),
6161
},
6262
}
63-
s := storageSchemaFactory(attributes)
64-
s.Description = "Manages ZFS-based storage in Proxmox VE."
65-
resp.Schema = s
63+
factory := NewStorageSchemaFactory()
64+
factory.WithAttributes(attributes)
65+
factory.WithDescription("Manages ZFS-based storage in Proxmox VE.")
66+
resp.Schema = *factory.Schema
6667
}

fwprovider/storage/schema_factory.go

Lines changed: 122 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,142 @@
11
package storage
22

33
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
45
"github.com/hashicorp/terraform-plugin-framework/attr"
56
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
67
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
78
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
89
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault"
910
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
11+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1012
"github.com/hashicorp/terraform-plugin-framework/types"
1113
)
1214

13-
// storageSchemaFactory generates the schema for a storage resource.
14-
func storageSchemaFactory(specificAttributes map[string]schema.Attribute) schema.Schema {
15-
attributes := map[string]schema.Attribute{
16-
"id": schema.StringAttribute{
17-
Description: "The unique identifier of the storage.",
18-
Required: true,
19-
PlanModifiers: []planmodifier.String{
20-
stringplanmodifier.RequiresReplace(),
15+
type StorageSchemaFactory struct {
16+
Schema *schema.Schema
17+
18+
description string
19+
}
20+
21+
func NewStorageSchemaFactory() *StorageSchemaFactory {
22+
s := &schema.Schema{
23+
Attributes: map[string]schema.Attribute{
24+
"id": schema.StringAttribute{
25+
Description: "The unique identifier of the storage.",
26+
Required: true,
27+
PlanModifiers: []planmodifier.String{
28+
stringplanmodifier.RequiresReplace(),
29+
},
30+
},
31+
"nodes": schema.SetAttribute{
32+
Description: "A list of nodes where this storage is available.",
33+
ElementType: types.StringType,
34+
Optional: true,
35+
Computed: true,
36+
Default: setdefault.StaticValue(
37+
types.SetValueMust(types.StringType, []attr.Value{}),
38+
),
39+
},
40+
"content": schema.SetAttribute{
41+
Description: "The content types that can be stored on this storage.",
42+
ElementType: types.StringType,
43+
Optional: true,
44+
Computed: true,
45+
Default: setdefault.StaticValue(
46+
types.SetValueMust(types.StringType, []attr.Value{}),
47+
),
48+
},
49+
"disable": schema.BoolAttribute{
50+
Description: "Whether the storage is disabled.",
51+
Optional: true,
52+
Default: booldefault.StaticBool(false),
53+
Computed: true,
2154
},
2255
},
23-
"nodes": schema.SetAttribute{
24-
Description: "A list of nodes where this storage is available.",
25-
ElementType: types.StringType,
26-
Optional: true,
27-
Computed: true,
28-
Default: setdefault.StaticValue(
29-
types.SetValueMust(types.StringType, []attr.Value{}),
30-
),
31-
},
32-
"content": schema.SetAttribute{
33-
Description: "The content types that can be stored on this storage.",
34-
ElementType: types.StringType,
35-
Optional: true,
36-
Computed: true,
37-
Default: setdefault.StaticValue(
38-
types.SetValueMust(types.StringType, []attr.Value{}),
39-
),
40-
},
41-
"disable": schema.BoolAttribute{
42-
Description: "Whether the storage is disabled.",
43-
Optional: true,
44-
Default: booldefault.StaticBool(false),
45-
Computed: true,
46-
},
56+
Blocks: map[string]schema.Block{},
4757
}
58+
return &StorageSchemaFactory{
59+
Schema: s,
60+
}
61+
}
4862

49-
// Merge provided attributes for the given storage type
50-
for k, v := range specificAttributes {
51-
attributes[k] = v
63+
func (s *StorageSchemaFactory) WithDescription(description string) *StorageSchemaFactory {
64+
s.Schema.Description = description
65+
return s
66+
}
67+
68+
func (s *StorageSchemaFactory) WithAttributes(attributes map[string]schema.Attribute) *StorageSchemaFactory {
69+
for k, v := range attributes {
70+
s.Schema.Attributes[k] = v
5271
}
72+
return s
73+
}
5374

54-
return schema.Schema{
55-
Attributes: attributes,
75+
func (s *StorageSchemaFactory) WithBlocks(blocks map[string]schema.Block) *StorageSchemaFactory {
76+
for k, v := range blocks {
77+
s.Schema.Blocks[k] = v
5678
}
79+
return s
80+
}
81+
82+
func (s *StorageSchemaFactory) WithBackupBlock() *StorageSchemaFactory {
83+
return s.WithBlocks(map[string]schema.Block{
84+
"backups": schema.SingleNestedBlock{
85+
Attributes: map[string]schema.Attribute{
86+
"max_protected_backups": schema.Int64Attribute{
87+
Description: "The maximum number of protected backups per guest. Use '-1' for unlimited.",
88+
Optional: true,
89+
},
90+
"keep_last": schema.Int64Attribute{
91+
Description: "Specifies the number of the most recent backups to keep, regardless of their age.",
92+
Optional: true,
93+
Validators: []validator.Int64{
94+
int64validator.AtLeast(0),
95+
},
96+
},
97+
"keep_hourly": schema.Int64Attribute{
98+
Description: "The number of hourly backups to keep. Older backups will be removed.",
99+
Optional: true,
100+
Validators: []validator.Int64{
101+
int64validator.AtLeast(0),
102+
},
103+
},
104+
"keep_daily": schema.Int64Attribute{
105+
Description: "The number of daily backups to keep. Older backups will be removed.",
106+
Optional: true,
107+
Validators: []validator.Int64{
108+
int64validator.AtLeast(0),
109+
},
110+
},
111+
"keep_weekly": schema.Int64Attribute{
112+
Description: "The number of weekly backups to keep. Older backups will be removed.",
113+
Optional: true,
114+
Validators: []validator.Int64{
115+
int64validator.AtLeast(0),
116+
},
117+
},
118+
"keep_monthly": schema.Int64Attribute{
119+
Description: "The number of monthly backups to keep. Older backups will be removed.",
120+
Optional: true,
121+
Validators: []validator.Int64{
122+
int64validator.AtLeast(0),
123+
},
124+
},
125+
"keep_yearly": schema.Int64Attribute{
126+
Description: "The number of yearly backups to keep. Older backups will be removed.",
127+
Optional: true,
128+
Validators: []validator.Int64{
129+
int64validator.AtLeast(0),
130+
},
131+
},
132+
"keep_all": schema.BoolAttribute{
133+
Description: "Specifies if all backups should be kept, regardless of their age.",
134+
Optional: true,
135+
Computed: true,
136+
Default: booldefault.StaticBool(false),
137+
},
138+
},
139+
Description: "Configure backup retention settings for the storage type.",
140+
},
141+
})
57142
}

proxmox/storage/directory_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "github.com/bpg/terraform-provider-proxmox/proxmox/types"
55
// DirectoryStorageMutableFields defines the mutable attributes for 'dir' type storage.
66
type DirectoryStorageMutableFields struct {
77
DataStoreCommonMutableFields
8+
DataStoreWithBackups
89
Preallocation *string `json:"preallocation,omitempty" url:"preallocation,omitempty"`
910
SnapshotsAsVolumeChain types.CustomBool `json:"snapshot-as-volume-chain,omitempty" url:"snapshot-as-volume-chain,omitempty"`
1011
Shared *types.CustomBool `json:"shared,omitempty" url:"shared,omitempty,int"`

0 commit comments

Comments
 (0)