This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
/
provisioningtemplate.go
359 lines (303 loc) · 11.8 KB
/
provisioningtemplate.go
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/wayfair/terraform-provider-utils/log"
)
const (
ProvisioningTemplateEndpointPrefix = "provisioning_templates"
)
// -----------------------------------------------------------------------------
// Struct Definition and Helpers
// -----------------------------------------------------------------------------
// The ForemanProvisioningTemplate API model represents a provisioning template.
// Provisioning templates are scripts used to describe how to boostrap and
// install the operating system on a host.
type ForemanProvisioningTemplate struct {
// Inherits the base object's attributes
ForemanObject
// The markup and code of the provisioning template
Template string
// Whether or not the provisioning template is a snippet to be embedded
// and used by other templates
Snippet bool
// Notes and comments for auditing purposes
AuditComment string
// Whether or not the template is locked for editing
Locked bool
// ID of the template kind which categorizes the provisioning template.
// Optional for snippets, otherwise required.
TemplateKindId int
// IDs of operating systems associated with this provisioning template
OperatingSystemIds []int
// How templates are determined:
//
// When editing a template, you must assign a list of operating systems
// which this template can be used with. Optionally, you can restrict
// a template to a list of host groups and/or environments.
//
// When a host requests a template, Foreman will select the best match
// from the available templates of that type in the following order:
//
// 1. host group and environment
// 2. host group only
// 3. environment only
// 4. operating system default
//
// Template combinations attributes contains an array of hostgroup IDs
// and environment ID combinations so they can be used in the
// provisioning template selection described above.
TemplateCombinationsAttributes []ForemanTemplateCombinationAttribute `json:"template_combinations_attributes"`
}
// See the comment in ForemanProvisioningTemplate.TemplateCombinationsAttributes
type ForemanTemplateCombinationAttribute struct {
// Unique identifier of the template combination
Id int `json:"id,omitempty"`
// Hostgroup ID associated with the template combination
HostgroupId int `json:"hostgroup_id,omitempty"`
// Environment ID associated with the template combination
EnvironmentId int `json:"environment_id,omitempty"`
// NOTE(ALL): Each of the template combinations receives a unique identifier
// on creation. To modify the list of template combinations, the supplied
// list to the API does NOT perform a replace operation. Adding new
// combinations to the list is rather trivial and just involves sending the
// new values to receive an ID. When removing one of the combinations from
// the set, a secret flag "_destroy" must be supplied as part of that
// combination. This is not documented as part of the Foreman API. We
// omit empty here, because we only want to pass the flag when "_destroy"
// is "true" to signal an item removal.
Destroy bool `json:"_destroy,omitempty"`
}
// ForemanProvisioningTemplate struct used for JSON decode. Foreman API returns
// the operating system ids back as a list of ForemanObjects with some of the
// attributes. For our purposes, we are only interested in the IDs.
type foremanProvisioningTemplateJSON struct {
OperatingSystems []ForemanObject `json:"operatingsystems"`
TemplateCombinationsAttributes []ForemanTemplateCombinationAttribute `json:"template_combinations"`
}
// Custom JSON marshal function for provisioning temmplates. The Foreman API
// expects all parameters to be enclosed in double quotes, with the exception
// of boolean and slice values.
func (ft ForemanProvisioningTemplate) MarshalJSON() ([]byte, error) {
log.Tracef("Provisioning template marshal")
// map structure representation of the passed ForemanProvisioningTemplate
// for ease of marshalling - essentially convert over to a map then call
// json.Marshal() on the mapstructure
ftMap := map[string]interface{}{}
ftMap["name"] = ft.Name
ftMap["template"] = ft.Template
ftMap["snippet"] = ft.Snippet
ftMap["audit_comment"] = ft.AuditComment
ftMap["locked"] = ft.Locked
ftMap["template_kind_id"] = intIdToJSONString(ft.TemplateKindId)
// always marshal the OS array - otherwise, when the array is updated
// from [1,2,3] to [], we would skip the marshalling and the OS id array
// would not get updated by the API
//
// Foreman API interprets the data of this field as a REPLACE operation
ftMap["operatingsystem_ids"] = ft.OperatingSystemIds
// only include the template combination attributes if it is set.
// The Foreman API will return "500: Internal Server Error" with the
// explanation "Expected Hash or Array got NilClass (nil)" if any of the
// following are supplied: [], null, "null"
if len(ft.TemplateCombinationsAttributes) > 0 {
ftMap["template_combinations_attributes"] = ft.TemplateCombinationsAttributes
}
log.Debugf("ftMap: [%v]", ftMap)
return json.Marshal(ftMap)
}
// Custom JSON unmarshal function. Unmarshal to the unexported JSON struct
// and then convert over to a ForemanProvisioningTemplate struct.
func (ft *ForemanProvisioningTemplate) UnmarshalJSON(b []byte) error {
var jsonDecErr error
// Unmarshal the common Foreman object properties
var fo ForemanObject
jsonDecErr = json.Unmarshal(b, &fo)
if jsonDecErr != nil {
return jsonDecErr
}
ft.ForemanObject = fo
// Unmarshal to temporary JSON struct to get the properties with differently
// named keys
var ftJSON foremanProvisioningTemplateJSON
jsonDecErr = json.Unmarshal(b, &ftJSON)
if jsonDecErr != nil {
return jsonDecErr
}
ft.OperatingSystemIds = foremanObjectArrayToIdIntArray(ftJSON.OperatingSystems)
ft.TemplateCombinationsAttributes = ftJSON.TemplateCombinationsAttributes
// Unmarshal into mapstructure and set the rest of the struct properties
var ftMap map[string]interface{}
jsonDecErr = json.Unmarshal(b, &ftMap)
if jsonDecErr != nil {
return jsonDecErr
}
var ok bool
if ft.Template, ok = ftMap["template"].(string); !ok {
ft.Template = ""
}
if ft.Snippet, ok = ftMap["snippet"].(bool); !ok {
ft.Snippet = false
}
if ft.AuditComment, ok = ftMap["audit_comment"].(string); !ok {
ft.AuditComment = ""
}
if ft.Locked, ok = ftMap["locked"].(bool); !ok {
ft.Locked = false
}
if ft.TemplateKindId, ok = ftMap["template_kind_id"].(int); !ok {
ft.TemplateKindId = 0
}
return nil
}
// -----------------------------------------------------------------------------
// CRUD Implementation
// -----------------------------------------------------------------------------
// CreateProvisioningTemplate creates a new ForemanProvisioningTemplate with
// the attributes of the supplied ForemanProvisioningTemplate reference and
// returns the created ForemanProvisioningTemplate reference. The returned
// reference will have its ID and other API default values set by this
// function.
func (c *Client) CreateProvisioningTemplate(t *ForemanProvisioningTemplate) (*ForemanProvisioningTemplate, error) {
log.Tracef("foreman/api/provisioningtemplate.go#Create")
reqEndpoint := fmt.Sprintf("/%s", ProvisioningTemplateEndpointPrefix)
tJSONBytes, jsonEncErr := json.Marshal(t)
if jsonEncErr != nil {
return nil, jsonEncErr
}
log.Debugf("templateJSONBytes: [%s]", tJSONBytes)
req, reqErr := c.NewRequest(
http.MethodPost,
reqEndpoint,
bytes.NewBuffer(tJSONBytes),
)
if reqErr != nil {
return nil, reqErr
}
var createdTemplate ForemanProvisioningTemplate
sendErr := c.SendAndParse(req, &createdTemplate)
if sendErr != nil {
return nil, sendErr
}
log.Debugf("createdTemplate: [%+v]", createdTemplate)
return &createdTemplate, nil
}
// ReadProvisioningTemplate reads the attributes of a
// ForemanProvisioningTemplate identified by the supplied ID and returns a
// ForemanProvisioningTemplate reference.
func (c *Client) ReadProvisioningTemplate(id int) (*ForemanProvisioningTemplate, error) {
log.Tracef("foreman/api/provisioningtemplate.go#Read")
reqEndpoint := fmt.Sprintf("/%s/%d", ProvisioningTemplateEndpointPrefix, id)
req, reqErr := c.NewRequest(
http.MethodGet,
reqEndpoint,
nil,
)
if reqErr != nil {
return nil, reqErr
}
var readTemplate ForemanProvisioningTemplate
sendErr := c.SendAndParse(req, &readTemplate)
if sendErr != nil {
return nil, sendErr
}
log.Debugf("readTemplate: [%+v]", readTemplate)
return &readTemplate, nil
}
// UpdateProvisioningTemplate updates a ForemanProvisioningTemplate's
// attributes. The template with the ID of the supplied
// ForemanProvisioningTemplate will be updated. A new
// ForemanProvisioningTemplate reference is returned with the attributes from
// the result of the update operation.
func (c *Client) UpdateProvisioningTemplate(t *ForemanProvisioningTemplate) (*ForemanProvisioningTemplate, error) {
log.Tracef("foreman/api/provisioningtemplate.go#Update")
reqEndpoint := fmt.Sprintf("/%s/%d", ProvisioningTemplateEndpointPrefix, t.Id)
tJSONBytes, jsonEncErr := json.Marshal(t)
if jsonEncErr != nil {
return nil, jsonEncErr
}
log.Debugf("templateJSONBytes: [%s]", tJSONBytes)
req, reqErr := c.NewRequest(
http.MethodPut,
reqEndpoint,
bytes.NewBuffer(tJSONBytes),
)
if reqErr != nil {
return nil, reqErr
}
var updatedTemplate ForemanProvisioningTemplate
sendErr := c.SendAndParse(req, &updatedTemplate)
if sendErr != nil {
return nil, sendErr
}
log.Debugf("updatedTemplate: [%+v]", updatedTemplate)
return &updatedTemplate, nil
}
// DeleteProvisioningTemplate deletes the ForemanProvisioningTemplate
// identified by the supplied ID
func (c *Client) DeleteProvisioningTemplate(id int) error {
log.Tracef("foreman/api/provisioningtemplate.go#Delete")
reqEndpoint := fmt.Sprintf("/%s/%d", ProvisioningTemplateEndpointPrefix, id)
req, reqErr := c.NewRequest(
http.MethodDelete,
reqEndpoint,
nil,
)
if reqErr != nil {
return reqErr
}
return c.SendAndParse(req, nil)
}
// -----------------------------------------------------------------------------
// Query Implementation
// -----------------------------------------------------------------------------
// QueryProvisioningTemplate queries for a ForemanProvisioningTemplate based on
// the attributes of the supplied ForemanProvisioningTemplate reference and
// returns a QueryResponse struct containing query/response metadata and the
// matching templates.
func (c *Client) QueryProvisioningTemplate(t *ForemanProvisioningTemplate) (QueryResponse, error) {
log.Tracef("foreman/api/provisioningtemplate.go#Query")
queryResponse := QueryResponse{}
reqEndpoint := fmt.Sprintf("/%s", ProvisioningTemplateEndpointPrefix)
req, reqErr := c.NewRequest(
http.MethodGet,
reqEndpoint,
nil,
)
if reqErr != nil {
return queryResponse, reqErr
}
// dynamically build the query based on the attributes
reqQuery := req.URL.Query()
name := "\"" + t.Name + "\""
reqQuery.Set("search", "name="+name)
req.URL.RawQuery = reqQuery.Encode()
sendErr := c.SendAndParse(req, &queryResponse)
if sendErr != nil {
return queryResponse, sendErr
}
log.Debugf("queryResponse: [%+v]", queryResponse)
// Results will be Unmarshaled into a []map[string]interface{}
//
// Encode back to JSON, then Unmarshal into []ForemanProvisioningTemplate for
// the results
results := []ForemanProvisioningTemplate{}
resultsBytes, jsonEncErr := json.Marshal(queryResponse.Results)
if jsonEncErr != nil {
return queryResponse, jsonEncErr
}
jsonDecErr := json.Unmarshal(resultsBytes, &results)
if jsonDecErr != nil {
return queryResponse, jsonDecErr
}
// convert the search results from []ForemanProvisioningTemplate to []interface
// and set the search results on the query
iArr := make([]interface{}, len(results))
for idx, val := range results {
iArr[idx] = val
}
queryResponse.Results = iArr
return queryResponse, nil
}