This repository has been archived by the owner on Dec 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
resource_ignition_systemd_unit.go
109 lines (95 loc) · 2.23 KB
/
resource_ignition_systemd_unit.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
package ignition
import (
"github.com/coreos/ignition/config/v2_1/types"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceSystemdUnit() *schema.Resource {
return &schema.Resource{
Exists: resourceSystemdUnitExists,
Read: resourceSystemdUnitRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
ForceNew: true,
},
"mask": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"dropin": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
},
},
}
}
func resourceSystemdUnitRead(d *schema.ResourceData, meta interface{}) error {
id, err := buildSystemdUnit(d, globalCache)
if err != nil {
return err
}
d.SetId(id)
return nil
}
func resourceSystemdUnitExists(d *schema.ResourceData, meta interface{}) (bool, error) {
id, err := buildSystemdUnit(d, globalCache)
if err != nil {
return false, err
}
return id == d.Id(), nil
}
func buildSystemdUnit(d *schema.ResourceData, c *cache) (string, error) {
enabled := d.Get("enabled").(bool)
unit := &types.Unit{
Name: d.Get("name").(string),
Contents: d.Get("content").(string),
Enabled: &enabled,
Mask: d.Get("mask").(bool),
}
if err := handleReport(unit.ValidateName()); err != nil {
return "", err
}
if err := handleReport(unit.ValidateContents()); err != nil {
return "", err
}
for _, raw := range d.Get("dropin").([]interface{}) {
value := raw.(map[string]interface{})
d := types.Dropin{
Name: value["name"].(string),
Contents: value["content"].(string),
}
if err := handleReport(d.Validate()); err != nil {
return "", err
}
unit.Dropins = append(unit.Dropins, d)
}
return c.addSystemdUnit(unit), nil
}