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