summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/terraform/builtin/providers/ignition/provider.go
blob: 81462e3614277d40640dcd09589893fe4b94db85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package ignition

import (
	"bytes"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"net/url"
	"sync"

	"github.com/coreos/go-systemd/unit"
	"github.com/coreos/ignition/config/types"
	"github.com/hashicorp/terraform/helper/schema"
	"github.com/hashicorp/terraform/terraform"
)

// globalCache keeps the instances of the internal types of ignition generated
// by the different data resources with the goal to be reused by the
// ignition_config data resource. The key of the maps are a hash of the types
// calculated on the type serialized to JSON.
var globalCache = &cache{
	disks:         make(map[string]*types.Disk, 0),
	arrays:        make(map[string]*types.Raid, 0),
	filesystems:   make(map[string]*types.Filesystem, 0),
	files:         make(map[string]*types.File, 0),
	systemdUnits:  make(map[string]*types.SystemdUnit, 0),
	networkdUnits: make(map[string]*types.NetworkdUnit, 0),
	users:         make(map[string]*types.User, 0),
	groups:        make(map[string]*types.Group, 0),
}

func Provider() terraform.ResourceProvider {
	return &schema.Provider{
		DataSourcesMap: map[string]*schema.Resource{
			"ignition_config":        resourceConfig(),
			"ignition_disk":          resourceDisk(),
			"ignition_raid":          resourceRaid(),
			"ignition_filesystem":    resourceFilesystem(),
			"ignition_file":          resourceFile(),
			"ignition_systemd_unit":  resourceSystemdUnit(),
			"ignition_networkd_unit": resourceNetworkdUnit(),
			"ignition_user":          resourceUser(),
			"ignition_group":         resourceGroup(),
		},
		ResourcesMap: map[string]*schema.Resource{
			"ignition_config": schema.DataSourceResourceShim(
				"ignition_config",
				resourceConfig(),
			),
			"ignition_disk": schema.DataSourceResourceShim(
				"ignition_disk",
				resourceDisk(),
			),
			"ignition_raid": schema.DataSourceResourceShim(
				"ignition_raid",
				resourceRaid(),
			),
			"ignition_filesystem": schema.DataSourceResourceShim(
				"ignition_filesystem",
				resourceFilesystem(),
			),
			"ignition_file": schema.DataSourceResourceShim(
				"ignition_file",
				resourceFile(),
			),
			"ignition_systemd_unit": schema.DataSourceResourceShim(
				"ignition_systemd_unit",
				resourceSystemdUnit(),
			),
			"ignition_networkd_unit": schema.DataSourceResourceShim(
				"ignition_networkd_unit",
				resourceNetworkdUnit(),
			),
			"ignition_user": schema.DataSourceResourceShim(
				"ignition_user",
				resourceUser(),
			),
			"ignition_group": schema.DataSourceResourceShim(
				"ignition_group",
				resourceGroup(),
			),
		},
	}
}

type cache struct {
	disks         map[string]*types.Disk
	arrays        map[string]*types.Raid
	filesystems   map[string]*types.Filesystem
	files         map[string]*types.File
	systemdUnits  map[string]*types.SystemdUnit
	networkdUnits map[string]*types.NetworkdUnit
	users         map[string]*types.User
	groups        map[string]*types.Group

	sync.Mutex
}

func (c *cache) addDisk(g *types.Disk) string {
	c.Lock()
	defer c.Unlock()

	id := id(g)
	c.disks[id] = g

	return id
}

func (c *cache) addRaid(r *types.Raid) string {
	c.Lock()
	defer c.Unlock()

	id := id(r)
	c.arrays[id] = r

	return id
}

func (c *cache) addFilesystem(f *types.Filesystem) string {
	c.Lock()
	defer c.Unlock()

	id := id(f)
	c.filesystems[id] = f

	return id
}

func (c *cache) addFile(f *types.File) string {
	c.Lock()
	defer c.Unlock()

	id := id(f)
	c.files[id] = f

	return id
}

func (c *cache) addSystemdUnit(u *types.SystemdUnit) string {
	c.Lock()
	defer c.Unlock()

	id := id(u)
	c.systemdUnits[id] = u

	return id
}

func (c *cache) addNetworkdUnit(u *types.NetworkdUnit) string {
	c.Lock()
	defer c.Unlock()

	id := id(u)
	c.networkdUnits[id] = u

	return id
}

func (c *cache) addUser(u *types.User) string {
	c.Lock()
	defer c.Unlock()

	id := id(u)
	c.users[id] = u

	return id
}

func (c *cache) addGroup(g *types.Group) string {
	c.Lock()
	defer c.Unlock()

	id := id(g)
	c.groups[id] = g

	return id
}

func id(input interface{}) string {
	b, _ := json.Marshal(input)
	return hash(string(b))
}

func hash(s string) string {
	sha := sha256.Sum256([]byte(s))
	return hex.EncodeToString(sha[:])
}

func castSliceInterface(i []interface{}) []string {
	var o []string
	for _, value := range i {
		o = append(o, value.(string))
	}

	return o
}

func getUInt(d *schema.ResourceData, key string) *uint {
	var uid *uint
	if value, ok := d.GetOk(key); ok {
		u := uint(value.(int))
		uid = &u
	}

	return uid
}

var errEmptyUnit = fmt.Errorf("invalid or empty unit content")

func validateUnitContent(content string) error {
	c := bytes.NewBufferString(content)
	unit, err := unit.Deserialize(c)
	if err != nil {
		return fmt.Errorf("invalid unit content: %s", err)
	}

	if len(unit) == 0 {
		return errEmptyUnit
	}

	return nil
}

func buildURL(raw string) (types.Url, error) {
	u, err := url.Parse(raw)
	if err != nil {
		return types.Url{}, err
	}

	return types.Url(*u), nil
}

func buildHash(raw string) (types.Hash, error) {
	h := types.Hash{}
	err := h.UnmarshalJSON([]byte(fmt.Sprintf("%q", raw)))

	return h, err
}