Skip to content

Commit 5f0fa2e

Browse files
author
awlsring
committed
feat: content type validation
1 parent 89fe76d commit 5f0fa2e

File tree

4 files changed

+26
-42
lines changed

4 files changed

+26
-42
lines changed

proxmox/storage-class/common.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@ import (
44
"context"
55

66
"github.com/awlsring/terraform-provider-proxmox/internal/service"
7+
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
8+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
9+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
710
"github.com/hashicorp/terraform-plugin-framework/types"
811
"github.com/hashicorp/terraform-plugin-log/tflog"
912
)
1013

14+
func ContentTypeValidator(t ...string) []validator.List {
15+
return []validator.List{
16+
listvalidator.UniqueValues(),
17+
listvalidator.ValueStringsAre(
18+
stringvalidator.OneOf(t...),
19+
),
20+
}
21+
}
22+
1123
func DetermineContentTypes(ctx context.Context, c types.List, defaults []string) ([]string, error) {
1224
if c.IsNull() || c.IsUnknown() {
1325
return defaults, nil

proxmox/storage-class/nfs/schema.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nfs
22

33
import (
4+
"github.com/awlsring/terraform-provider-proxmox/proxmox/storage-class"
45
ds "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
56
rs "github.com/hashicorp/terraform-plugin-framework/resource/schema"
67
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
@@ -81,6 +82,14 @@ var resourceSchema = rs.Schema{
8182
PlanModifiers: []planmodifier.List{
8283
listplanmodifier.UseStateForUnknown(),
8384
},
85+
Validators: storage.ContentTypeValidator(
86+
"images",
87+
"rootdir",
88+
"vztmpl",
89+
"backup",
90+
"iso",
91+
"snippets",
92+
),
8493
},
8594
"mount": rs.StringAttribute{
8695
Computed: true,

proxmox/storage-class/zfs/resource.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/awlsring/terraform-provider-proxmox/proxmox/utils"
1010
"github.com/hashicorp/terraform-plugin-framework/path"
1111
"github.com/hashicorp/terraform-plugin-framework/resource"
12-
"github.com/hashicorp/terraform-plugin-framework/types"
1312
"github.com/hashicorp/terraform-plugin-log/tflog"
1413
)
1514

@@ -142,47 +141,6 @@ func (r *zfsResource) readstorageClassZfsModel(ctx context.Context, name string)
142141
return ZFSStorageClassToModel(s), nil
143142
}
144143

145-
func (r *zfsResource) determineNodes(ctx context.Context, n types.List) ([]string, error) {
146-
if n.IsNull() || n.IsUnknown() {
147-
tflog.Debug(ctx, "nodes is null or unknown")
148-
nodes, err := r.client.ListNodesNames(ctx)
149-
if err != nil {
150-
return nil, err
151-
}
152-
return nodes, nil
153-
}
154-
tflog.Debug(ctx, "nodes is not null or unknown")
155-
nodes := []string{}
156-
for _, node := range n.Elements() {
157-
v, err := node.ToTerraformValue(ctx)
158-
if err != nil {
159-
return nil, err
160-
}
161-
var n string
162-
v.As(&n)
163-
nodes = append(nodes, n)
164-
}
165-
return nodes, nil
166-
}
167-
168-
func (r *zfsResource) determineContentTypes(ctx context.Context, c types.List) ([]string, error) {
169-
if c.IsNull() || c.IsUnknown() {
170-
return []string{"images", "rootdir"}, nil
171-
}
172-
tflog.Debug(ctx, "content types is not null or unknown")
173-
contentTypes := []string{}
174-
for _, contentType := range c.Elements() {
175-
v, err := contentType.ToTerraformValue(ctx)
176-
if err != nil {
177-
return nil, err
178-
}
179-
var c string
180-
v.As(&c)
181-
contentTypes = append(contentTypes, c)
182-
}
183-
return contentTypes, nil
184-
}
185-
186144
func (r *zfsResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
187145
tflog.Debug(ctx, "Update bridge method")
188146
var plan storageClassZfsModel

proxmox/storage-class/zfs/schema.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package zfs
22

33
import (
4+
"github.com/awlsring/terraform-provider-proxmox/proxmox/storage-class"
45
ds "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
56
rs "github.com/hashicorp/terraform-plugin-framework/resource/schema"
67
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
@@ -84,6 +85,10 @@ var resourceSchema = rs.Schema{
8485
PlanModifiers: []planmodifier.List{
8586
listplanmodifier.UseStateForUnknown(),
8687
},
88+
Validators: storage.ContentTypeValidator(
89+
"images",
90+
"rootdir",
91+
),
8792
},
8893
},
8994
}

0 commit comments

Comments
 (0)