-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdatabase_common.go
More file actions
252 lines (243 loc) · 7.31 KB
/
database_common.go
File metadata and controls
252 lines (243 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package database
import (
"fmt"
"regexp"
"time"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func schemaDatabaseCommon() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Description: "Name of the service. The name is used as a prefix for the logical hostname. Must be unique within an account",
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateFunc: validation.StringLenBetween(3, 30),
},
"components": schemaDatabaseComponents(),
"maintenance_window_dow": {
Description: "Maintenance window day of week. Lower case weekday name (monday, tuesday, ...)",
Type: schema.TypeString,
Computed: true,
Optional: true,
RequiredWith: []string{"maintenance_window_time"},
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}, false)),
},
"maintenance_window_time": {
Description: "Maintenance window UTC time in hh:mm:ss format",
Type: schema.TypeString,
Computed: true,
Optional: true,
RequiredWith: []string{"maintenance_window_dow"},
ValidateDiagFunc: func(i interface{}, _ cty.Path) diag.Diagnostics {
if _, err := time.Parse("15:04:05", i.(string)); err != nil {
return diag.FromErr(fmt.Errorf("maintenance_window_time format must be HH:MM:SS"))
}
return nil
},
},
"network": schemaDatabaseNetwork(),
"node_states": schemaDatabaseNodeStates(),
"plan": {
Description: "Service plan to use. This determines how much resources the instance will have. You can list available plans with `upctl database plans <type>`.",
Type: schema.TypeString,
Required: true,
},
"powered": {
Description: "The administrative power state of the service",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"service_uri": {
Description: "URI to the service instance",
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"service_host": {
Description: "Hostname to the service instance",
Type: schema.TypeString,
Computed: true,
},
"service_port": {
Description: "Port to the service instance",
Type: schema.TypeString,
Computed: true,
},
"service_username": {
Description: "Primary username to the service instance",
Type: schema.TypeString,
Computed: true,
},
"service_password": {
Description: "Primary username's password to the service instance",
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"state": {
Description: "State of the service",
Type: schema.TypeString,
Computed: true,
},
"title": {
Description: "Title of a managed database instance",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(0, 255),
},
"type": {
Description: "Type of the service",
Type: schema.TypeString,
Computed: true,
},
"zone": {
Description: "Zone where the instance resides, e.g. `de-fra1`. You can list available zones with `upctl zone list`.",
Type: schema.TypeString,
Required: true,
},
"primary_database": {
Description: "Primary database name",
Type: schema.TypeString,
Computed: true,
},
}
}
func schemaDatabaseComponents() *schema.Schema {
return &schema.Schema{
Description: "Service component information",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
EnableLegacyTypeSystemApplyErrors: true,
EnableLegacyTypeSystemPlanErrors: true,
Schema: map[string]*schema.Schema{
"component": {
Description: "Type of the component",
Type: schema.TypeString,
Computed: true,
},
"host": {
Description: "Hostname of the component",
Type: schema.TypeString,
Computed: true,
},
"port": {
Description: "Port number of the component",
Type: schema.TypeInt,
Computed: true,
},
"route": {
Description: "Component network route type",
Type: schema.TypeString,
Computed: true,
},
"usage": {
Description: "Usage of the component",
Type: schema.TypeString,
Computed: true,
},
},
},
}
}
func schemaDatabaseNetwork() *schema.Schema {
return &schema.Schema{
Description: "Private networks attached to the managed database",
Type: schema.TypeSet,
Optional: true,
MaxItems: 8,
MinItems: 1,
Elem: &schema.Resource{
EnableLegacyTypeSystemApplyErrors: true,
EnableLegacyTypeSystemPlanErrors: true,
Schema: map[string]*schema.Schema{
"name": {
Description: "The name of the network. Must be unique within the service.",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.All(
validation.StringLenBetween(0, 65),
validation.StringMatch(regexp.MustCompile("^[a-zA-Z0-9_-]+$"), ""),
)),
},
"type": {
Description: "The type of the network. Must be private.",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(
validation.StringInSlice([]string{
string(upcloud.LoadBalancerNetworkTypePrivate),
}, false),
),
},
"family": {
Description: "Network family. Currently only `IPv4` is supported.",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(
validation.StringInSlice([]string{
string(upcloud.LoadBalancerAddressFamilyIPv4),
}, false),
),
},
"uuid": {
Description: "Private network UUID. Must reside in the same zone as the database.",
Type: schema.TypeString,
Required: true,
},
},
},
}
}
func networksFromResourceData(d *schema.ResourceData) []upcloud.ManagedDatabaseNetwork {
req := make([]upcloud.ManagedDatabaseNetwork, 0)
if networks, ok := d.GetOk("network"); ok {
for _, network := range networks.(*schema.Set).List() {
n := network.(map[string]interface{})
uuid := n["uuid"].(string)
r := upcloud.ManagedDatabaseNetwork{
Name: n["name"].(string),
Type: n["type"].(string),
Family: n["family"].(string),
UUID: &uuid,
}
req = append(req, r)
}
}
return req
}
func schemaDatabaseNodeStates() *schema.Schema {
return &schema.Schema{
Description: "Information about nodes providing the managed service",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
EnableLegacyTypeSystemApplyErrors: true,
EnableLegacyTypeSystemPlanErrors: true,
Schema: map[string]*schema.Schema{
"name": {
Description: "Name plus a node iteration",
Type: schema.TypeString,
Computed: true,
},
"role": {
Description: "Role of the node",
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"state": {
Description: "State of the node",
Type: schema.TypeString,
Computed: true,
},
},
},
}
}