From 40902f3e403fb97898e4e659a5f6213844e86546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Tue, 17 Jan 2017 12:49:45 +0100 Subject: [PATCH] provider/ignition: allowing empty systemd.content when a dropin is provided (#11216) --- builtin/providers/ignition/provider.go | 22 +++++------ builtin/providers/ignition/provider_test.go | 6 +-- .../resource_ignition_networkd_unit.go | 2 +- .../resource_ignition_systemd_unit.go | 14 ++++--- .../resource_ignition_systemd_unit_test.go | 38 +++++++++++++++++++ .../providers/ignition/r/systemd_unit.html.md | 4 +- 6 files changed, 61 insertions(+), 25 deletions(-) diff --git a/builtin/providers/ignition/provider.go b/builtin/providers/ignition/provider.go index d6b4372f3f..71bfc8fa41 100644 --- a/builtin/providers/ignition/provider.go +++ b/builtin/providers/ignition/provider.go @@ -6,7 +6,6 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io" "net/url" "sync" @@ -166,23 +165,20 @@ func getUInt(d *schema.ResourceData, key string) *uint { return uid } -func validateUnit(content string) error { - r := bytes.NewBuffer([]byte(content)) +var errEmptyUnit = fmt.Errorf("invalid or empty unit content") - u, err := unit.Deserialize(r) - if len(u) == 0 { - return fmt.Errorf("invalid or empty unit content") - } - - if err == nil { - return nil +func validateUnitContent(content string) error { + c := bytes.NewBufferString(content) + unit, err := unit.Deserialize(c) + if err != nil { + return fmt.Errorf("invalid unit content: %s", err) } - if err == io.EOF { - return fmt.Errorf("unexpected EOF reading unit content") + if len(unit) == 0 { + return errEmptyUnit } - return err + return nil } func buildURL(raw string) (types.Url, error) { diff --git a/builtin/providers/ignition/provider_test.go b/builtin/providers/ignition/provider_test.go index d7e381471b..6ee665bb51 100644 --- a/builtin/providers/ignition/provider_test.go +++ b/builtin/providers/ignition/provider_test.go @@ -18,15 +18,15 @@ func TestProvider(t *testing.T) { } func TestValidateUnit(t *testing.T) { - if err := validateUnit(""); err == nil { + if err := validateUnitContent(""); err == nil { t.Fatalf("error not found, expected error") } - if err := validateUnit("[foo]qux"); err == nil { + if err := validateUnitContent("[foo]qux"); err == nil { t.Fatalf("error not found, expected error") } - if err := validateUnit("[foo]\nqux=foo\nfoo"); err == nil { + if err := validateUnitContent("[foo]\nqux=foo\nfoo"); err == nil { t.Fatalf("error not found, expected error") } } diff --git a/builtin/providers/ignition/resource_ignition_networkd_unit.go b/builtin/providers/ignition/resource_ignition_networkd_unit.go index 5b2518d64d..2ce2a7d286 100644 --- a/builtin/providers/ignition/resource_ignition_networkd_unit.go +++ b/builtin/providers/ignition/resource_ignition_networkd_unit.go @@ -55,7 +55,7 @@ func resourceNetworkdUnitRead(d *schema.ResourceData, meta interface{}) error { } func buildNetworkdUnit(d *schema.ResourceData, c *cache) (string, error) { - if err := validateUnit(d.Get("content").(string)); err != nil { + if err := validateUnitContent(d.Get("content").(string)); err != nil { return "", err } diff --git a/builtin/providers/ignition/resource_ignition_systemd_unit.go b/builtin/providers/ignition/resource_ignition_systemd_unit.go index 1b2a94c288..819d6331f4 100644 --- a/builtin/providers/ignition/resource_ignition_systemd_unit.go +++ b/builtin/providers/ignition/resource_ignition_systemd_unit.go @@ -30,7 +30,7 @@ func resourceSystemdUnit() *schema.Resource { }, "content": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, ForceNew: true, }, "dropin": &schema.Schema{ @@ -85,15 +85,11 @@ func resourceSystemdUnitRead(d *schema.ResourceData, meta interface{}) error { } func buildSystemdUnit(d *schema.ResourceData, c *cache) (string, error) { - if err := validateUnit(d.Get("content").(string)); err != nil { - return "", err - } - var dropins []types.SystemdUnitDropIn for _, raw := range d.Get("dropin").([]interface{}) { value := raw.(map[string]interface{}) - if err := validateUnit(value["content"].(string)); err != nil { + if err := validateUnitContent(value["content"].(string)); err != nil { return "", err } @@ -103,6 +99,12 @@ func buildSystemdUnit(d *schema.ResourceData, c *cache) (string, error) { }) } + if err := validateUnitContent(d.Get("content").(string)); err != nil { + if err != errEmptyUnit || (err == errEmptyUnit && len(dropins) == 0) { + return "", err + } + } + return c.addSystemdUnit(&types.SystemdUnit{ Name: types.SystemdUnitName(d.Get("name").(string)), Contents: d.Get("content").(string), diff --git a/builtin/providers/ignition/resource_ignition_systemd_unit_test.go b/builtin/providers/ignition/resource_ignition_systemd_unit_test.go index 8375b0ab8e..01b7666077 100644 --- a/builtin/providers/ignition/resource_ignition_systemd_unit_test.go +++ b/builtin/providers/ignition/resource_ignition_systemd_unit_test.go @@ -56,3 +56,41 @@ func TestIngnitionSystemdUnit(t *testing.T) { return nil }) } + +func TestIngnitionSystemdUnitEmptyContentWithDropIn(t *testing.T) { + testIgnition(t, ` + resource "ignition_systemd_unit" "foo" { + name = "foo.service" + dropin { + name = "foo.conf" + content = "[Match]\nName=eth0\n\n[Network]\nAddress=10.0.1.7\n" + } + } + + resource "ignition_config" "test" { + systemd = [ + "${ignition_systemd_unit.foo.id}", + ] + } + `, func(c *types.Config) error { + if len(c.Systemd.Units) != 1 { + return fmt.Errorf("systemd, found %d", len(c.Systemd.Units)) + } + + u := c.Systemd.Units[0] + + if u.Name != "foo.service" { + return fmt.Errorf("name, found %q", u.Name) + } + + if u.Contents != "" { + return fmt.Errorf("content, found %q", u.Contents) + } + + if len(u.DropIns) != 1 { + return fmt.Errorf("dropins, found %q", u.DropIns) + } + + return nil + }) +} diff --git a/website/source/docs/providers/ignition/r/systemd_unit.html.md b/website/source/docs/providers/ignition/r/systemd_unit.html.md index 98494515d5..9ac6a86fa2 100644 --- a/website/source/docs/providers/ignition/r/systemd_unit.html.md +++ b/website/source/docs/providers/ignition/r/systemd_unit.html.md @@ -29,12 +29,12 @@ The following arguments are supported: * `mask` - (Optional) Whether or not the service shall be masked. When true, the service is masked by symlinking it to _/dev/null_. -* `content` - (Required) The contents of the unit. +* `content` - (Required) The contents of the unit. Optional when a dropin is provided. * `dropin` - (Optional) The list of drop-ins for the unit. The `dropin` block supports: - + * `name` - (Required) The name of the drop-in. This must be suffixed with _.conf_. * `content` - (Optional) The contents of the drop-in.