github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/ignition/resource_ignition_disk.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 resourceDisk() *schema.Resource {
     9  	return &schema.Resource{
    10  		Exists: resourceDiskExists,
    11  		Read:   resourceDiskRead,
    12  		Schema: map[string]*schema.Schema{
    13  			"device": &schema.Schema{
    14  				Type:     schema.TypeString,
    15  				Required: true,
    16  				ForceNew: true,
    17  			},
    18  			"wipe_table": &schema.Schema{
    19  				Type:     schema.TypeBool,
    20  				Optional: true,
    21  				ForceNew: true,
    22  			},
    23  			"partition": &schema.Schema{
    24  				Type:     schema.TypeList,
    25  				Optional: true,
    26  				ForceNew: true,
    27  				Elem: &schema.Resource{
    28  					Schema: map[string]*schema.Schema{
    29  						"label": &schema.Schema{
    30  							Type:     schema.TypeString,
    31  							Optional: true,
    32  							ForceNew: true,
    33  						},
    34  						"number": &schema.Schema{
    35  							Type:     schema.TypeInt,
    36  							Optional: true,
    37  							ForceNew: true,
    38  						},
    39  						"size": &schema.Schema{
    40  							Type:     schema.TypeInt,
    41  							Optional: true,
    42  							ForceNew: true,
    43  						},
    44  						"start": &schema.Schema{
    45  							Type:     schema.TypeInt,
    46  							Optional: true,
    47  							ForceNew: true,
    48  						},
    49  						"type_guid": &schema.Schema{
    50  							Type:     schema.TypeString,
    51  							Optional: true,
    52  							ForceNew: true,
    53  						},
    54  					},
    55  				},
    56  			},
    57  		},
    58  	}
    59  }
    60  
    61  func resourceDiskRead(d *schema.ResourceData, meta interface{}) error {
    62  	id, err := buildDisk(d, globalCache)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	d.SetId(id)
    68  	return nil
    69  }
    70  
    71  func resourceDiskExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    72  	id, err := buildDisk(d, globalCache)
    73  	if err != nil {
    74  		return false, err
    75  	}
    76  
    77  	return id == d.Id(), nil
    78  }
    79  
    80  func buildDisk(d *schema.ResourceData, c *cache) (string, error) {
    81  	var partitions []types.Partition
    82  	for _, raw := range d.Get("partition").([]interface{}) {
    83  		v := raw.(map[string]interface{})
    84  
    85  		partitions = append(partitions, types.Partition{
    86  			Label:    types.PartitionLabel(v["label"].(string)),
    87  			Number:   v["number"].(int),
    88  			Size:     types.PartitionDimension(v["size"].(int)),
    89  			Start:    types.PartitionDimension(v["start"].(int)),
    90  			TypeGUID: types.PartitionTypeGUID(v["type_guid"].(string)),
    91  		})
    92  	}
    93  
    94  	return c.addDisk(&types.Disk{
    95  		Device:     types.Path(d.Get("device").(string)),
    96  		WipeTable:  d.Get("wipe_table").(bool),
    97  		Partitions: partitions,
    98  	}), nil
    99  }