github.com/simonswine/terraform@v0.9.0-beta2/builtin/providers/ignition/resource_ignition_filesystem.go (about) 1 package ignition 2 3 import ( 4 "fmt" 5 6 "github.com/coreos/ignition/config/types" 7 "github.com/hashicorp/terraform/helper/schema" 8 ) 9 10 func resourceFilesystem() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourceFilesystemCreate, 13 Delete: resourceFilesystemDelete, 14 Exists: resourceFilesystemExists, 15 Read: resourceFilesystemRead, 16 Schema: map[string]*schema.Schema{ 17 "name": &schema.Schema{ 18 Type: schema.TypeString, 19 Optional: true, 20 ForceNew: true, 21 }, 22 "mount": &schema.Schema{ 23 Type: schema.TypeList, 24 Optional: true, 25 ForceNew: true, 26 MaxItems: 1, 27 Elem: &schema.Resource{ 28 Schema: map[string]*schema.Schema{ 29 "device": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 "format": &schema.Schema{ 35 Type: schema.TypeString, 36 Required: true, 37 ForceNew: true, 38 }, 39 "force": &schema.Schema{ 40 Type: schema.TypeBool, 41 Optional: true, 42 ForceNew: true, 43 }, 44 "options": &schema.Schema{ 45 Type: schema.TypeList, 46 Optional: true, 47 ForceNew: true, 48 Elem: &schema.Schema{Type: schema.TypeString}, 49 }, 50 }, 51 }, 52 }, 53 "path": &schema.Schema{ 54 Type: schema.TypeString, 55 Optional: true, 56 ForceNew: true, 57 }, 58 }, 59 } 60 } 61 62 func resourceFilesystemCreate(d *schema.ResourceData, meta interface{}) error { 63 id, err := buildFilesystem(d, meta.(*cache)) 64 if err != nil { 65 return err 66 } 67 68 d.SetId(id) 69 return nil 70 } 71 72 func resourceFilesystemDelete(d *schema.ResourceData, meta interface{}) error { 73 d.SetId("") 74 return nil 75 } 76 77 func resourceFilesystemExists(d *schema.ResourceData, meta interface{}) (bool, error) { 78 id, err := buildFilesystem(d, meta.(*cache)) 79 if err != nil { 80 return false, err 81 } 82 83 return id == d.Id(), nil 84 } 85 86 func resourceFilesystemRead(d *schema.ResourceData, meta interface{}) error { 87 return nil 88 } 89 90 func buildFilesystem(d *schema.ResourceData, c *cache) (string, error) { 91 var mount *types.FilesystemMount 92 if _, ok := d.GetOk("mount"); ok { 93 mount = &types.FilesystemMount{ 94 Device: types.Path(d.Get("mount.0.device").(string)), 95 Format: types.FilesystemFormat(d.Get("mount.0.format").(string)), 96 } 97 98 force, hasForce := d.GetOk("mount.0.force") 99 options, hasOptions := d.GetOk("mount.0.options") 100 if hasOptions || hasForce { 101 mount.Create = &types.FilesystemCreate{ 102 Force: force.(bool), 103 Options: castSliceInterface(options.([]interface{})), 104 } 105 } 106 } 107 108 var path *types.Path 109 if p, ok := d.GetOk("path"); ok { 110 tp := types.Path(p.(string)) 111 path = &tp 112 } 113 114 if mount != nil && path != nil { 115 return "", fmt.Errorf("mount and path are mutually exclusive") 116 } 117 118 return c.addFilesystem(&types.Filesystem{ 119 Name: d.Get("name").(string), 120 Mount: mount, 121 Path: path, 122 }), nil 123 }