github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/ignition/resource_ignition_raid.go (about)

     1  package ignition
     2  
     3  import (
     4  	"github.com/coreos/ignition/config/types"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  )
     7  
     8  func resourceRaid() *schema.Resource {
     9  	return &schema.Resource{
    10  		Exists: resourceRaidExists,
    11  		Read:   resourceRaidRead,
    12  		Schema: map[string]*schema.Schema{
    13  			"name": &schema.Schema{
    14  				Type:     schema.TypeString,
    15  				Required: true,
    16  				ForceNew: true,
    17  			},
    18  			"level": &schema.Schema{
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  				ForceNew: true,
    22  			},
    23  			"devices": &schema.Schema{
    24  				Type:     schema.TypeList,
    25  				Optional: true,
    26  				ForceNew: true,
    27  				Elem:     &schema.Schema{Type: schema.TypeString},
    28  			},
    29  			"spares": &schema.Schema{
    30  				Type:     schema.TypeInt,
    31  				Optional: true,
    32  				ForceNew: true,
    33  			},
    34  		},
    35  	}
    36  }
    37  
    38  func resourceRaidRead(d *schema.ResourceData, meta interface{}) error {
    39  	id, err := buildRaid(d, globalCache)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	d.SetId(id)
    45  	return nil
    46  }
    47  
    48  func resourceRaidExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    49  	id, err := buildRaid(d, globalCache)
    50  	if err != nil {
    51  		return false, err
    52  	}
    53  
    54  	return id == d.Id(), nil
    55  }
    56  
    57  func buildRaid(d *schema.ResourceData, c *cache) (string, error) {
    58  	var devices []types.Path
    59  	for _, value := range d.Get("devices").([]interface{}) {
    60  		devices = append(devices, types.Path(value.(string)))
    61  	}
    62  
    63  	return c.addRaid(&types.Raid{
    64  		Name:    d.Get("name").(string),
    65  		Level:   d.Get("level").(string),
    66  		Devices: devices,
    67  		Spares:  d.Get("spares").(int),
    68  	}), nil
    69  }