github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/ignition/resource_ignition_systemd_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 resourceSystemdUnit() *schema.Resource {
     9  	return &schema.Resource{
    10  		Create: resourceSystemdUnitCreate,
    11  		Delete: resourceSystemdUnitDelete,
    12  		Exists: resourceSystemdUnitExists,
    13  		Read:   resourceSystemdUnitRead,
    14  		Schema: map[string]*schema.Schema{
    15  			"name": &schema.Schema{
    16  				Type:     schema.TypeString,
    17  				Required: true,
    18  				ForceNew: true,
    19  			},
    20  			"enable": &schema.Schema{
    21  				Type:     schema.TypeBool,
    22  				Optional: true,
    23  				Default:  true,
    24  				ForceNew: true,
    25  			},
    26  			"mask": &schema.Schema{
    27  				Type:     schema.TypeBool,
    28  				Optional: true,
    29  				ForceNew: true,
    30  			},
    31  			"content": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Optional: true,
    34  				ForceNew: true,
    35  			},
    36  			"dropin": &schema.Schema{
    37  				Type:     schema.TypeList,
    38  				Optional: true,
    39  				ForceNew: true,
    40  				Elem: &schema.Resource{
    41  					Schema: map[string]*schema.Schema{
    42  						"name": &schema.Schema{
    43  							Type:     schema.TypeString,
    44  							Required: true,
    45  							ForceNew: true,
    46  						},
    47  						"content": &schema.Schema{
    48  							Type:     schema.TypeString,
    49  							Optional: true,
    50  							ForceNew: true,
    51  						},
    52  					},
    53  				},
    54  			},
    55  		},
    56  	}
    57  }
    58  
    59  func resourceSystemdUnitCreate(d *schema.ResourceData, meta interface{}) error {
    60  	id, err := buildSystemdUnit(d, meta.(*cache))
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	d.SetId(id)
    66  	return nil
    67  }
    68  
    69  func resourceSystemdUnitDelete(d *schema.ResourceData, meta interface{}) error {
    70  	d.SetId("")
    71  	return nil
    72  }
    73  
    74  func resourceSystemdUnitExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    75  	id, err := buildSystemdUnit(d, meta.(*cache))
    76  	if err != nil {
    77  		return false, err
    78  	}
    79  
    80  	return id == d.Id(), nil
    81  }
    82  
    83  func resourceSystemdUnitRead(d *schema.ResourceData, meta interface{}) error {
    84  	return nil
    85  }
    86  
    87  func buildSystemdUnit(d *schema.ResourceData, c *cache) (string, error) {
    88  	var dropins []types.SystemdUnitDropIn
    89  	for _, raw := range d.Get("dropin").([]interface{}) {
    90  		value := raw.(map[string]interface{})
    91  
    92  		if err := validateUnitContent(value["content"].(string)); err != nil {
    93  			return "", err
    94  		}
    95  
    96  		dropins = append(dropins, types.SystemdUnitDropIn{
    97  			Name:     types.SystemdUnitDropInName(value["name"].(string)),
    98  			Contents: value["content"].(string),
    99  		})
   100  	}
   101  
   102  	if err := validateUnitContent(d.Get("content").(string)); err != nil {
   103  		if err != errEmptyUnit || (err == errEmptyUnit && len(dropins) == 0) {
   104  			return "", err
   105  		}
   106  	}
   107  
   108  	return c.addSystemdUnit(&types.SystemdUnit{
   109  		Name:     types.SystemdUnitName(d.Get("name").(string)),
   110  		Contents: d.Get("content").(string),
   111  		Enable:   d.Get("enable").(bool),
   112  		Mask:     d.Get("mask").(bool),
   113  		DropIns:  dropins,
   114  	}), nil
   115  }