github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/ignition/provider.go (about) 1 package ignition 2 3 import ( 4 "bytes" 5 "crypto/sha256" 6 "encoding/hex" 7 "encoding/json" 8 "fmt" 9 "net/url" 10 "sync" 11 12 "github.com/coreos/go-systemd/unit" 13 "github.com/coreos/ignition/config/types" 14 "github.com/hashicorp/terraform/helper/schema" 15 "github.com/hashicorp/terraform/terraform" 16 ) 17 18 func Provider() terraform.ResourceProvider { 19 return &schema.Provider{ 20 ResourcesMap: map[string]*schema.Resource{ 21 "ignition_config": resourceConfig(), 22 "ignition_disk": resourceDisk(), 23 "ignition_raid": resourceRaid(), 24 "ignition_filesystem": resourceFilesystem(), 25 "ignition_file": resourceFile(), 26 "ignition_systemd_unit": resourceSystemdUnit(), 27 "ignition_networkd_unit": resourceNetworkdUnit(), 28 "ignition_user": resourceUser(), 29 "ignition_group": resourceGroup(), 30 }, 31 ConfigureFunc: func(*schema.ResourceData) (interface{}, error) { 32 return &cache{ 33 disks: make(map[string]*types.Disk, 0), 34 arrays: make(map[string]*types.Raid, 0), 35 filesystems: make(map[string]*types.Filesystem, 0), 36 files: make(map[string]*types.File, 0), 37 systemdUnits: make(map[string]*types.SystemdUnit, 0), 38 networkdUnits: make(map[string]*types.NetworkdUnit, 0), 39 users: make(map[string]*types.User, 0), 40 groups: make(map[string]*types.Group, 0), 41 }, nil 42 }, 43 } 44 } 45 46 type cache struct { 47 disks map[string]*types.Disk 48 arrays map[string]*types.Raid 49 filesystems map[string]*types.Filesystem 50 files map[string]*types.File 51 systemdUnits map[string]*types.SystemdUnit 52 networkdUnits map[string]*types.NetworkdUnit 53 users map[string]*types.User 54 groups map[string]*types.Group 55 56 sync.Mutex 57 } 58 59 func (c *cache) addDisk(g *types.Disk) string { 60 c.Lock() 61 defer c.Unlock() 62 63 id := id(g) 64 c.disks[id] = g 65 66 return id 67 } 68 69 func (c *cache) addRaid(r *types.Raid) string { 70 c.Lock() 71 defer c.Unlock() 72 73 id := id(r) 74 c.arrays[id] = r 75 76 return id 77 } 78 79 func (c *cache) addFilesystem(f *types.Filesystem) string { 80 c.Lock() 81 defer c.Unlock() 82 83 id := id(f) 84 c.filesystems[id] = f 85 86 return id 87 } 88 89 func (c *cache) addFile(f *types.File) string { 90 c.Lock() 91 defer c.Unlock() 92 93 id := id(f) 94 c.files[id] = f 95 96 return id 97 } 98 99 func (c *cache) addSystemdUnit(u *types.SystemdUnit) string { 100 c.Lock() 101 defer c.Unlock() 102 103 id := id(u) 104 c.systemdUnits[id] = u 105 106 return id 107 } 108 109 func (c *cache) addNetworkdUnit(u *types.NetworkdUnit) string { 110 c.Lock() 111 defer c.Unlock() 112 113 id := id(u) 114 c.networkdUnits[id] = u 115 116 return id 117 } 118 119 func (c *cache) addUser(u *types.User) string { 120 c.Lock() 121 defer c.Unlock() 122 123 id := id(u) 124 c.users[id] = u 125 126 return id 127 } 128 129 func (c *cache) addGroup(g *types.Group) string { 130 c.Lock() 131 defer c.Unlock() 132 133 id := id(g) 134 c.groups[id] = g 135 136 return id 137 } 138 139 func id(input interface{}) string { 140 b, _ := json.Marshal(input) 141 return hash(string(b)) 142 } 143 144 func hash(s string) string { 145 sha := sha256.Sum256([]byte(s)) 146 return hex.EncodeToString(sha[:]) 147 } 148 149 func castSliceInterface(i []interface{}) []string { 150 var o []string 151 for _, value := range i { 152 o = append(o, value.(string)) 153 } 154 155 return o 156 } 157 158 func getUInt(d *schema.ResourceData, key string) *uint { 159 var uid *uint 160 if value, ok := d.GetOk(key); ok { 161 u := uint(value.(int)) 162 uid = &u 163 } 164 165 return uid 166 } 167 168 var errEmptyUnit = fmt.Errorf("invalid or empty unit content") 169 170 func validateUnitContent(content string) error { 171 c := bytes.NewBufferString(content) 172 unit, err := unit.Deserialize(c) 173 if err != nil { 174 return fmt.Errorf("invalid unit content: %s", err) 175 } 176 177 if len(unit) == 0 { 178 return errEmptyUnit 179 } 180 181 return nil 182 } 183 184 func buildURL(raw string) (types.Url, error) { 185 u, err := url.Parse(raw) 186 if err != nil { 187 return types.Url{}, err 188 } 189 190 return types.Url(*u), nil 191 } 192 193 func buildHash(raw string) (types.Hash, error) { 194 h := types.Hash{} 195 err := h.UnmarshalJSON([]byte(fmt.Sprintf("%q", raw))) 196 197 return h, err 198 }