github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/ignition/resource_ignition_networkd_unit.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 resourceNetworkdUnit() *schema.Resource {
     9  	return &schema.Resource{
    10  		Create: resourceNetworkdUnitCreate,
    11  		Delete: resourceNetworkdUnitDelete,
    12  		Exists: resourceNetworkdUnitExists,
    13  		Read:   resourceNetworkdUnitRead,
    14  		Schema: map[string]*schema.Schema{
    15  			"name": &schema.Schema{
    16  				Type:     schema.TypeString,
    17  				Required: true,
    18  				ForceNew: true,
    19  			},
    20  			"content": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Optional: true,
    23  				ForceNew: true,
    24  			},
    25  		},
    26  	}
    27  }
    28  
    29  func resourceNetworkdUnitCreate(d *schema.ResourceData, meta interface{}) error {
    30  	id, err := buildNetworkdUnit(d, meta.(*cache))
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	d.SetId(id)
    36  	return nil
    37  }
    38  
    39  func resourceNetworkdUnitDelete(d *schema.ResourceData, meta interface{}) error {
    40  	d.SetId("")
    41  	return nil
    42  }
    43  
    44  func resourceNetworkdUnitExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    45  	id, err := buildNetworkdUnit(d, meta.(*cache))
    46  	if err != nil {
    47  		return false, err
    48  	}
    49  
    50  	return id == d.Id(), nil
    51  }
    52  
    53  func resourceNetworkdUnitRead(d *schema.ResourceData, meta interface{}) error {
    54  	return nil
    55  }
    56  
    57  func buildNetworkdUnit(d *schema.ResourceData, c *cache) (string, error) {
    58  	if err := validateUnitContent(d.Get("content").(string)); err != nil {
    59  		return "", err
    60  	}
    61  
    62  	return c.addNetworkdUnit(&types.NetworkdUnit{
    63  		Name:     types.NetworkdUnitName(d.Get("name").(string)),
    64  		Contents: d.Get("content").(string),
    65  	}), nil
    66  }