provider/ignition: allowing empty systemd.content when a dropin is provided (#11216)

This commit is contained in:
Máximo Cuadros 2017-01-17 12:49:45 +01:00 committed by Paul Stack
parent a866da4938
commit 40902f3e40
6 changed files with 61 additions and 25 deletions

View File

@ -6,7 +6,6 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/url" "net/url"
"sync" "sync"
@ -166,23 +165,20 @@ func getUInt(d *schema.ResourceData, key string) *uint {
return uid return uid
} }
func validateUnit(content string) error { var errEmptyUnit = fmt.Errorf("invalid or empty unit content")
r := bytes.NewBuffer([]byte(content))
u, err := unit.Deserialize(r) func validateUnitContent(content string) error {
if len(u) == 0 { c := bytes.NewBufferString(content)
return fmt.Errorf("invalid or empty unit content") unit, err := unit.Deserialize(c)
if err != nil {
return fmt.Errorf("invalid unit content: %s", err)
} }
if err == nil { if len(unit) == 0 {
return nil return errEmptyUnit
} }
if err == io.EOF { return nil
return fmt.Errorf("unexpected EOF reading unit content")
}
return err
} }
func buildURL(raw string) (types.Url, error) { func buildURL(raw string) (types.Url, error) {

View File

@ -18,15 +18,15 @@ func TestProvider(t *testing.T) {
} }
func TestValidateUnit(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") 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") 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") t.Fatalf("error not found, expected error")
} }
} }

View File

@ -55,7 +55,7 @@ func resourceNetworkdUnitRead(d *schema.ResourceData, meta interface{}) error {
} }
func buildNetworkdUnit(d *schema.ResourceData, c *cache) (string, 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 return "", err
} }

View File

@ -30,7 +30,7 @@ func resourceSystemdUnit() *schema.Resource {
}, },
"content": &schema.Schema{ "content": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
ForceNew: true, ForceNew: true,
}, },
"dropin": &schema.Schema{ "dropin": &schema.Schema{
@ -85,15 +85,11 @@ func resourceSystemdUnitRead(d *schema.ResourceData, meta interface{}) error {
} }
func buildSystemdUnit(d *schema.ResourceData, c *cache) (string, 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 var dropins []types.SystemdUnitDropIn
for _, raw := range d.Get("dropin").([]interface{}) { for _, raw := range d.Get("dropin").([]interface{}) {
value := raw.(map[string]interface{}) value := raw.(map[string]interface{})
if err := validateUnit(value["content"].(string)); err != nil { if err := validateUnitContent(value["content"].(string)); err != nil {
return "", err 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{ return c.addSystemdUnit(&types.SystemdUnit{
Name: types.SystemdUnitName(d.Get("name").(string)), Name: types.SystemdUnitName(d.Get("name").(string)),
Contents: d.Get("content").(string), Contents: d.Get("content").(string),

View File

@ -56,3 +56,41 @@ func TestIngnitionSystemdUnit(t *testing.T) {
return nil 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
})
}

View File

@ -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_. * `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. * `dropin` - (Optional) The list of drop-ins for the unit.
The `dropin` block supports: The `dropin` block supports:
* `name` - (Required) The name of the drop-in. This must be suffixed with _.conf_. * `name` - (Required) The name of the drop-in. This must be suffixed with _.conf_.
* `content` - (Optional) The contents of the drop-in. * `content` - (Optional) The contents of the drop-in.