/
resource_service_openrc.go
339 lines (279 loc) · 10 KB
/
resource_service_openrc.go
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package provider
import (
"context"
"errors"
"fmt"
"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"
"github.com/neuspaces/terraform-provider-system/internal/client"
"github.com/neuspaces/terraform-provider-system/internal/client/openrc"
"github.com/neuspaces/terraform-provider-system/internal/extlib/to"
)
const resourceServiceOpenrcName = "system_service_openrc"
const (
resourceServiceOpenrcAttrId = "id"
resourceServiceOpenrcAttrName = "name"
resourceServiceOpenrcAttrStatus = "status"
resourceServiceOpenrcAttrStatusStarted = "started"
resourceServiceOpenrcAttrStatusStopped = "stopped"
resourceServiceOpenrcAttrEnabled = "enabled"
resourceServiceOpenrcAttrRunlevel = "runlevel"
resourceServiceOpenrcAttrRestartOn = "restart_on"
resourceServiceOpenrcAttrReloadOn = "reload_on"
)
func resourceServiceOpenrc() *schema.Resource {
return &schema.Resource{
Description: fmt.Sprintf("`%s` manages an OpenRC service on the remote system.", resourceServiceOpenrcName),
CreateContext: resourceServiceOpenrcCreate,
ReadContext: resourceServiceOpenrcRead,
UpdateContext: resourceServiceOpenrcUpdate,
DeleteContext: resourceServiceOpenrcDelete,
// Importer is intentionally not configured
// Read will not fail if the service does not exist
// Create will implicitly import the service in the state
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
resourceServiceOpenrcAttrId: {
Description: "ID of the service",
Type: schema.TypeString,
Computed: true,
},
resourceServiceOpenrcAttrName: {
Description: "Name of the service. The service must exist.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
resourceServiceOpenrcAttrStatus: {
Description: fmt.Sprintf("Status of the service. If `%[1]s`, the service will be started. If `%[2]s`, the service will be stopped.", resourceServiceOpenrcAttrStatusStarted, resourceServiceOpenrcAttrStatusStopped),
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
resourceServiceOpenrcAttrStatusStarted,
resourceServiceOpenrcAttrStatusStopped,
}, false),
},
resourceServiceOpenrcAttrEnabled: {
Description: "If `true`, the service will be enabled on the provided runlevel. If not provided, the service will not be changed.",
Type: schema.TypeBool,
Optional: true,
Computed: true,
Default: nil,
},
resourceServiceOpenrcAttrRunlevel: {
Description: fmt.Sprintf("Runlevel to which the `enabled` attribute refers to. Defaults to `%[1]s`.", openrc.DefaultRunlevel),
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
DefaultFunc: func() (interface{}, error) {
return openrc.DefaultRunlevel, nil
},
},
resourceServiceOpenrcAttrRestartOn: {
Description: "Set of arbitrary strings which will trigger a restart of the service.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
resourceServiceOpenrcAttrReloadOn: {
Description: "Set of arbitrary strings which will trigger a reload of the service.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
internalDataSchemaKey: internalDataSchema(),
},
}
}
type resourceServiceOpenrcInternalData struct {
// PreStatus is the original status of the service before managed by the resource. This status will be applied when the resource is destroyed.
PreStatus string `json:"pre_status,omitempty"`
// PreEnabled is true if the service was enabled before managed by the resource. This activation will be applied when the resource is destroyed.
PreEnabled *bool `json:"pre_enabled,omitempty"`
}
func resourceServiceOpenrcGetResourceData(d *schema.ResourceData) (*client.Service, diag.Diagnostics) {
r := &client.Service{
Name: d.Get(resourceServiceOpenrcAttrName).(string),
Runlevel: d.Get(resourceServiceOpenrcAttrRunlevel).(string),
}
if val, ok := d.GetOk(resourceServiceOpenrcAttrStatus); ok {
r.Status = client.ServiceStatusPtr(resourceServiceOpenRcStatusToClientStatus(val.(string)))
}
// Use deprecated GetOkExists instead of HasChange because HasChange does not support optional bool attributes
// https://github.com/hashicorp/terraform-plugin-sdk/issues/817
if val, ok := d.GetOkExists(resourceServiceOpenrcAttrEnabled); ok {
r.Enabled = to.BoolPtr(val.(bool))
}
return r, nil
}
func resourceServiceOpenrcSetResourceData(r *client.Service, d *schema.ResourceData) diag.Diagnostics {
_ = d.Set(resourceServiceOpenrcAttrName, r.Name)
if r.Status != nil {
_ = d.Set(resourceServiceOpenrcAttrStatus, resourceServiceOpenRcStatusFromClientStatus(*r.Status))
}
if r.Enabled != nil {
_ = d.Set(resourceServiceOpenrcAttrEnabled, to.Bool(r.Enabled))
}
_ = d.Set(resourceServiceOpenrcAttrRunlevel, r.Runlevel)
return nil
}
func resourceServiceOpenrcNewClient(ctx context.Context, meta interface{}) (client.ServiceClient, diag.Diagnostics) {
p, diagErr := providerFromMeta(meta)
if diagErr != nil {
return nil, diagErr
}
c := client.NewOpenRcServiceClient(p.System)
return c, nil
}
func resourceServiceOpenrcCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, diagErr := resourceServiceOpenrcNewClient(ctx, meta)
if diagErr != nil {
return diagErr
}
r, diagErr := resourceServiceOpenrcGetResourceData(d)
if diagErr != nil {
return diagErr
}
preR, err := resourceServiceClientGet(ctx, c, client.ServiceGetArgs{Name: r.Name, Runlevel: r.Runlevel})
if err != nil {
return diag.FromErr(err)
}
// Require enabled and status properties
if preR.Enabled == nil {
return newDetailedDiagnostic(diag.Error, "unexpected enabled property", "enabled property could not be determined during create", nil)
}
if preR.Status == nil {
return newDetailedDiagnostic(diag.Error, "unexpected status property", "status property could not be determined during create", nil)
}
// Apply options
var applyOpts []client.ServiceApplyOption
// Handle reload trigger
if d.HasChange(resourceServiceOpenrcAttrReloadOn) {
applyOpts = append(applyOpts, client.ServiceReload())
}
// Handle restart trigger
if d.HasChange(resourceServiceOpenrcAttrRestartOn) {
applyOpts = append(applyOpts, client.ServiceRestart())
}
err = c.Apply(ctx, *r, applyOpts...)
if err != nil {
return diag.FromErr(err)
}
d.SetId(r.Name)
// Store status and activation before create in internal data
internalData := resourceServiceOpenrcInternalData{
PreStatus: resourceServiceOpenRcStatusFromClientStatus(*preR.Status),
PreEnabled: preR.Enabled,
}
diagErr = setInternalData(d, &internalData)
if diagErr != nil {
return diagErr
}
return resourceServiceOpenrcRead(ctx, d, meta)
}
func resourceServiceOpenrcRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, diagErr := resourceServiceOpenrcNewClient(ctx, meta)
if diagErr != nil {
return diagErr
}
r, diagErr := resourceServiceOpenrcGetResourceData(d)
if diagErr != nil {
return diagErr
}
r, err := resourceServiceClientGet(ctx, c, client.ServiceGetArgs{Name: r.Name, Runlevel: r.Runlevel})
if err != nil {
if errors.Is(err, client.ErrServiceNotFound) {
// Ignore if service does not exist in read operation
} else {
return diag.FromErr(err)
}
}
diagErr = resourceServiceOpenrcSetResourceData(r, d)
if diagErr != nil {
return diagErr
}
return nil
}
func resourceServiceOpenrcUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, diagErr := resourceServiceOpenrcNewClient(ctx, meta)
if diagErr != nil {
return diagErr
}
r, diagErr := resourceServiceOpenrcGetResourceData(d)
if diagErr != nil {
return diagErr
}
// Update of `runlevel` is handled by ForceNew
// Apply options
var applyOpts []client.ServiceApplyOption
// Handle reload trigger
if d.HasChange(resourceServiceOpenrcAttrReloadOn) {
applyOpts = append(applyOpts, client.ServiceReload())
}
// Handle restart trigger
if d.HasChange(resourceServiceOpenrcAttrRestartOn) {
applyOpts = append(applyOpts, client.ServiceRestart())
}
err := c.Apply(ctx, *r, applyOpts...)
if err != nil {
return diag.FromErr(err)
}
return resourceServiceOpenrcRead(ctx, d, meta)
}
func resourceServiceOpenrcDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, diagErr := resourceServiceOpenrcNewClient(ctx, meta)
if diagErr != nil {
return diagErr
}
r, diagErr := resourceServiceOpenrcGetResourceData(d)
if diagErr != nil {
return diagErr
}
// Apply the service status and activation to values before the resource has been created
preR := &client.Service{
Name: r.Name,
// Assume the runlevel attribute has not changed due to the ForceNew flag
Runlevel: r.Runlevel,
}
var internalData resourceServiceOpenrcInternalData
_, diagErr = getInternalData(d, &internalData)
if diagErr != nil {
return diagErr
}
if internalData.PreStatus != "" {
preR.Status = client.ServiceStatusPtr(resourceServiceOpenRcStatusToClientStatus(internalData.PreStatus))
}
if internalData.PreEnabled != nil {
preR.Enabled = internalData.PreEnabled
}
err := c.Apply(ctx, *preR)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceServiceOpenRcStatusToClientStatus(s string) client.ServiceStatus {
switch s {
case resourceServiceOpenrcAttrStatusStarted:
return client.ServiceStatusStarted
case resourceServiceOpenrcAttrStatusStopped:
return client.ServiceStatusStopped
}
return client.ServiceStatusUndefined
}
func resourceServiceOpenRcStatusFromClientStatus(s client.ServiceStatus) string {
switch s {
case client.ServiceStatusStarted:
return resourceServiceOpenrcAttrStatusStarted
case client.ServiceStatusStopped:
return resourceServiceOpenrcAttrStatusStopped
}
return ""
}