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