Skip to content

Commit 40902f3

Browse files
mcuadrosstack72
authored andcommitted
provider/ignition: allowing empty systemd.content when a dropin is provided (hashicorp#11216)
1 parent a866da4 commit 40902f3

6 files changed

Lines changed: 61 additions & 25 deletions

File tree

builtin/providers/ignition/provider.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/hex"
77
"encoding/json"
88
"fmt"
9-
"io"
109
"net/url"
1110
"sync"
1211

@@ -166,23 +165,20 @@ func getUInt(d *schema.ResourceData, key string) *uint {
166165
return uid
167166
}
168167

169-
func validateUnit(content string) error {
170-
r := bytes.NewBuffer([]byte(content))
168+
var errEmptyUnit = fmt.Errorf("invalid or empty unit content")
171169

172-
u, err := unit.Deserialize(r)
173-
if len(u) == 0 {
174-
return fmt.Errorf("invalid or empty unit content")
175-
}
176-
177-
if err == nil {
178-
return nil
170+
func validateUnitContent(content string) error {
171+
c := bytes.NewBufferString(content)
172+
unit, err := unit.Deserialize(c)
173+
if err != nil {
174+
return fmt.Errorf("invalid unit content: %s", err)
179175
}
180176

181-
if err == io.EOF {
182-
return fmt.Errorf("unexpected EOF reading unit content")
177+
if len(unit) == 0 {
178+
return errEmptyUnit
183179
}
184180

185-
return err
181+
return nil
186182
}
187183

188184
func buildURL(raw string) (types.Url, error) {

builtin/providers/ignition/provider_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ func TestProvider(t *testing.T) {
1818
}
1919

2020
func TestValidateUnit(t *testing.T) {
21-
if err := validateUnit(""); err == nil {
21+
if err := validateUnitContent(""); err == nil {
2222
t.Fatalf("error not found, expected error")
2323
}
2424

25-
if err := validateUnit("[foo]qux"); err == nil {
25+
if err := validateUnitContent("[foo]qux"); err == nil {
2626
t.Fatalf("error not found, expected error")
2727
}
2828

29-
if err := validateUnit("[foo]\nqux=foo\nfoo"); err == nil {
29+
if err := validateUnitContent("[foo]\nqux=foo\nfoo"); err == nil {
3030
t.Fatalf("error not found, expected error")
3131
}
3232
}

builtin/providers/ignition/resource_ignition_networkd_unit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func resourceNetworkdUnitRead(d *schema.ResourceData, meta interface{}) error {
5555
}
5656

5757
func buildNetworkdUnit(d *schema.ResourceData, c *cache) (string, error) {
58-
if err := validateUnit(d.Get("content").(string)); err != nil {
58+
if err := validateUnitContent(d.Get("content").(string)); err != nil {
5959
return "", err
6060
}
6161

builtin/providers/ignition/resource_ignition_systemd_unit.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func resourceSystemdUnit() *schema.Resource {
3030
},
3131
"content": &schema.Schema{
3232
Type: schema.TypeString,
33-
Required: true,
33+
Optional: true,
3434
ForceNew: true,
3535
},
3636
"dropin": &schema.Schema{
@@ -85,15 +85,11 @@ func resourceSystemdUnitRead(d *schema.ResourceData, meta interface{}) error {
8585
}
8686

8787
func buildSystemdUnit(d *schema.ResourceData, c *cache) (string, error) {
88-
if err := validateUnit(d.Get("content").(string)); err != nil {
89-
return "", err
90-
}
91-
9288
var dropins []types.SystemdUnitDropIn
9389
for _, raw := range d.Get("dropin").([]interface{}) {
9490
value := raw.(map[string]interface{})
9591

96-
if err := validateUnit(value["content"].(string)); err != nil {
92+
if err := validateUnitContent(value["content"].(string)); err != nil {
9793
return "", err
9894
}
9995

@@ -103,6 +99,12 @@ func buildSystemdUnit(d *schema.ResourceData, c *cache) (string, error) {
10399
})
104100
}
105101

102+
if err := validateUnitContent(d.Get("content").(string)); err != nil {
103+
if err != errEmptyUnit || (err == errEmptyUnit && len(dropins) == 0) {
104+
return "", err
105+
}
106+
}
107+
106108
return c.addSystemdUnit(&types.SystemdUnit{
107109
Name: types.SystemdUnitName(d.Get("name").(string)),
108110
Contents: d.Get("content").(string),

builtin/providers/ignition/resource_ignition_systemd_unit_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,41 @@ func TestIngnitionSystemdUnit(t *testing.T) {
5656
return nil
5757
})
5858
}
59+
60+
func TestIngnitionSystemdUnitEmptyContentWithDropIn(t *testing.T) {
61+
testIgnition(t, `
62+
resource "ignition_systemd_unit" "foo" {
63+
name = "foo.service"
64+
dropin {
65+
name = "foo.conf"
66+
content = "[Match]\nName=eth0\n\n[Network]\nAddress=10.0.1.7\n"
67+
}
68+
}
69+
70+
resource "ignition_config" "test" {
71+
systemd = [
72+
"${ignition_systemd_unit.foo.id}",
73+
]
74+
}
75+
`, func(c *types.Config) error {
76+
if len(c.Systemd.Units) != 1 {
77+
return fmt.Errorf("systemd, found %d", len(c.Systemd.Units))
78+
}
79+
80+
u := c.Systemd.Units[0]
81+
82+
if u.Name != "foo.service" {
83+
return fmt.Errorf("name, found %q", u.Name)
84+
}
85+
86+
if u.Contents != "" {
87+
return fmt.Errorf("content, found %q", u.Contents)
88+
}
89+
90+
if len(u.DropIns) != 1 {
91+
return fmt.Errorf("dropins, found %q", u.DropIns)
92+
}
93+
94+
return nil
95+
})
96+
}

website/source/docs/providers/ignition/r/systemd_unit.html.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ The following arguments are supported:
2929

3030
* `mask` - (Optional) Whether or not the service shall be masked. When true, the service is masked by symlinking it to _/dev/null_.
3131

32-
* `content` - (Required) The contents of the unit.
32+
* `content` - (Required) The contents of the unit. Optional when a dropin is provided.
3333

3434
* `dropin` - (Optional) The list of drop-ins for the unit.
3535

3636
The `dropin` block supports:
37-
37+
3838
* `name` - (Required) The name of the drop-in. This must be suffixed with _.conf_.
3939

4040
* `content` - (Optional) The contents of the drop-in.

0 commit comments

Comments
 (0)