2017-01-03 12:29:14 +01:00
|
|
|
package ignition
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/coreos/ignition/config/types"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceSystemdUnit() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Exists: resourceSystemdUnitExists,
|
|
|
|
Read: resourceSystemdUnitRead,
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-21 06:28:04 +01:00
|
|
|
"name": {
|
2017-01-03 12:29:14 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-01-21 06:28:04 +01:00
|
|
|
"enable": {
|
2017-01-03 12:29:14 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-01-21 06:28:04 +01:00
|
|
|
"mask": {
|
2017-01-03 12:29:14 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-01-21 06:28:04 +01:00
|
|
|
"content": {
|
2017-01-03 12:29:14 +01:00
|
|
|
Type: schema.TypeString,
|
2017-01-17 12:49:45 +01:00
|
|
|
Optional: true,
|
2017-01-03 12:29:14 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-01-21 06:28:04 +01:00
|
|
|
"dropin": {
|
2017-01-03 12:29:14 +01:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-21 06:28:04 +01:00
|
|
|
"name": {
|
2017-01-03 12:29:14 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-01-21 06:28:04 +01:00
|
|
|
"content": {
|
2017-01-03 12:29:14 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:23:04 +01:00
|
|
|
func resourceSystemdUnitRead(d *schema.ResourceData, meta interface{}) error {
|
2017-01-03 12:29:14 +01:00
|
|
|
id, err := buildSystemdUnit(d, meta.(*cache))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceSystemdUnitExists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
|
|
|
id, err := buildSystemdUnit(d, meta.(*cache))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id == d.Id(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildSystemdUnit(d *schema.ResourceData, c *cache) (string, error) {
|
|
|
|
var dropins []types.SystemdUnitDropIn
|
|
|
|
for _, raw := range d.Get("dropin").([]interface{}) {
|
|
|
|
value := raw.(map[string]interface{})
|
|
|
|
|
2017-01-17 12:49:45 +01:00
|
|
|
if err := validateUnitContent(value["content"].(string)); err != nil {
|
2017-01-03 12:29:14 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
dropins = append(dropins, types.SystemdUnitDropIn{
|
|
|
|
Name: types.SystemdUnitDropInName(value["name"].(string)),
|
|
|
|
Contents: value["content"].(string),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-17 12:49:45 +01:00
|
|
|
if err := validateUnitContent(d.Get("content").(string)); err != nil {
|
2017-01-21 06:28:04 +01:00
|
|
|
if err != errEmptyUnit {
|
2017-01-17 12:49:45 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 12:29:14 +01:00
|
|
|
return c.addSystemdUnit(&types.SystemdUnit{
|
|
|
|
Name: types.SystemdUnitName(d.Get("name").(string)),
|
|
|
|
Contents: d.Get("content").(string),
|
|
|
|
Enable: d.Get("enable").(bool),
|
|
|
|
Mask: d.Get("mask").(bool),
|
|
|
|
DropIns: dropins,
|
|
|
|
}), nil
|
|
|
|
}
|