|
1 | 1 | package storage
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" |
4 | 5 | "github.com/hashicorp/terraform-plugin-framework/attr"
|
5 | 6 | "github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
6 | 7 | "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
|
7 | 8 | "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
8 | 9 | "github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault"
|
9 | 10 | "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
| 11 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
10 | 12 | "github.com/hashicorp/terraform-plugin-framework/types"
|
11 | 13 | )
|
12 | 14 |
|
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, |
21 | 54 | },
|
22 | 55 | },
|
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{}, |
47 | 57 | }
|
| 58 | + return &StorageSchemaFactory{ |
| 59 | + Schema: s, |
| 60 | + } |
| 61 | +} |
48 | 62 |
|
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 |
52 | 71 | }
|
| 72 | + return s |
| 73 | +} |
53 | 74 |
|
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 |
56 | 78 | }
|
| 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 | + }) |
57 | 142 | }
|
0 commit comments