2017-01-03 12:29:14 +01:00
|
|
|
package ignition
|
|
|
|
|
|
|
|
import (
|
2017-01-27 20:28:42 +01:00
|
|
|
"fmt"
|
|
|
|
|
2017-01-03 12:29:14 +01:00
|
|
|
"github.com/coreos/ignition/config/types"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceFilesystem() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Exists: resourceFilesystemExists,
|
|
|
|
Read: resourceFilesystemRead,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"mount": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
MaxItems: 1,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"device": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"format": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-03-23 10:02:54 +01:00
|
|
|
"create": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-01-03 12:29:14 +01:00
|
|
|
"force": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"options": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:23:04 +01:00
|
|
|
func resourceFilesystemRead(d *schema.ResourceData, meta interface{}) error {
|
2017-01-03 12:29:14 +01:00
|
|
|
id, err := buildFilesystem(d, meta.(*cache))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFilesystemExists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
|
|
|
id, err := buildFilesystem(d, meta.(*cache))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id == d.Id(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildFilesystem(d *schema.ResourceData, c *cache) (string, error) {
|
|
|
|
var mount *types.FilesystemMount
|
|
|
|
if _, ok := d.GetOk("mount"); ok {
|
|
|
|
mount = &types.FilesystemMount{
|
|
|
|
Device: types.Path(d.Get("mount.0.device").(string)),
|
|
|
|
Format: types.FilesystemFormat(d.Get("mount.0.format").(string)),
|
|
|
|
}
|
|
|
|
|
2017-03-23 10:02:54 +01:00
|
|
|
create, hasCreate := d.GetOk("mount.0.create")
|
2017-01-03 12:29:14 +01:00
|
|
|
force, hasForce := d.GetOk("mount.0.force")
|
|
|
|
options, hasOptions := d.GetOk("mount.0.options")
|
2017-03-23 10:02:54 +01:00
|
|
|
if hasCreate || hasOptions || hasForce {
|
2017-01-03 12:29:14 +01:00
|
|
|
mount.Create = &types.FilesystemCreate{
|
|
|
|
Force: force.(bool),
|
|
|
|
Options: castSliceInterface(options.([]interface{})),
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 10:02:54 +01:00
|
|
|
|
|
|
|
if !create.(bool) && (hasForce || hasOptions) {
|
|
|
|
return "", fmt.Errorf("create should be true when force or options is used")
|
|
|
|
}
|
2017-01-03 12:29:14 +01:00
|
|
|
}
|
|
|
|
|
2017-01-27 20:28:42 +01:00
|
|
|
var path *types.Path
|
|
|
|
if p, ok := d.GetOk("path"); ok {
|
|
|
|
tp := types.Path(p.(string))
|
|
|
|
path = &tp
|
|
|
|
}
|
|
|
|
|
|
|
|
if mount != nil && path != nil {
|
|
|
|
return "", fmt.Errorf("mount and path are mutually exclusive")
|
|
|
|
}
|
2017-01-03 12:29:14 +01:00
|
|
|
|
|
|
|
return c.addFilesystem(&types.Filesystem{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Mount: mount,
|
2017-01-27 20:28:42 +01:00
|
|
|
Path: path,
|
2017-01-03 12:29:14 +01:00
|
|
|
}), nil
|
|
|
|
}
|