github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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  						"create": &schema.Schema{
    38  							Type:     schema.TypeBool,
    39  							Optional: true,
    40  							ForceNew: true,
    41  						},
    42  						"force": &schema.Schema{
    43  							Type:     schema.TypeBool,
    44  							Optional: true,
    45  							ForceNew: true,
    46  						},
    47  						"options": &schema.Schema{
    48  							Type:     schema.TypeList,
    49  							Optional: true,
    50  							ForceNew: true,
    51  							Elem:     &schema.Schema{Type: schema.TypeString},
    52  						},
    53  					},
    54  				},
    55  			},
    56  			"path": &schema.Schema{
    57  				Type:     schema.TypeString,
    58  				Optional: true,
    59  				ForceNew: true,
    60  			},
    61  		},
    62  	}
    63  }
    64  
    65  func resourceFilesystemRead(d *schema.ResourceData, meta interface{}) error {
    66  	id, err := buildFilesystem(d, globalCache)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	d.SetId(id)
    72  	return nil
    73  }
    74  
    75  func resourceFilesystemExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    76  	id, err := buildFilesystem(d, globalCache)
    77  	if err != nil {
    78  		return false, err
    79  	}
    80  
    81  	return id == d.Id(), nil
    82  }
    83  
    84  func buildFilesystem(d *schema.ResourceData, c *cache) (string, error) {
    85  	var mount *types.FilesystemMount
    86  	if _, ok := d.GetOk("mount"); ok {
    87  		mount = &types.FilesystemMount{
    88  			Device: types.Path(d.Get("mount.0.device").(string)),
    89  			Format: types.FilesystemFormat(d.Get("mount.0.format").(string)),
    90  		}
    91  
    92  		create, hasCreate := d.GetOk("mount.0.create")
    93  		force, hasForce := d.GetOk("mount.0.force")
    94  		options, hasOptions := d.GetOk("mount.0.options")
    95  		if hasCreate || hasOptions || hasForce {
    96  			mount.Create = &types.FilesystemCreate{
    97  				Force:   force.(bool),
    98  				Options: castSliceInterface(options.([]interface{})),
    99  			}
   100  		}
   101  
   102  		if !create.(bool) && (hasForce || hasOptions) {
   103  			return "", fmt.Errorf("create should be true when force or options is used")
   104  		}
   105  	}
   106  
   107  	var path *types.Path
   108  	if p, ok := d.GetOk("path"); ok {
   109  		tp := types.Path(p.(string))
   110  		path = &tp
   111  	}
   112  
   113  	if mount != nil && path != nil {
   114  		return "", fmt.Errorf("mount and path are mutually exclusive")
   115  	}
   116  
   117  	return c.addFilesystem(&types.Filesystem{
   118  		Name:  d.Get("name").(string),
   119  		Mount: mount,
   120  		Path:  path,
   121  	}), nil
   122  }