provider/ignition: allowing empty systemd.content when a dropin is provided (#11216)
This commit is contained in:
parent
a866da4938
commit
40902f3e40
|
@ -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")
|
||||
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 == nil {
|
||||
return nil
|
||||
if len(unit) == 0 {
|
||||
return errEmptyUnit
|
||||
}
|
||||
|
||||
if err == io.EOF {
|
||||
return fmt.Errorf("unexpected EOF reading unit content")
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildURL(raw string) (types.Url, error) {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue