/
product.go
215 lines (175 loc) · 6.16 KB
/
product.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
package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/HanseMerkur/terraform-provider-utils/log"
)
const (
// KatelloProductEndpointPrefix api endpoint prefix for katello products
// 'katello/ will be removed, it's a marker to detect talking with katello api
KatelloProductEndpointPrefix = "katello/products"
)
// -----------------------------------------------------------------------------
// Struct Definition and Helpers
// -----------------------------------------------------------------------------
// ForemanKatelloProduct API model representing a product.
type ForemanKatelloProduct struct {
// Inherits the base object's attributes
ForemanObject
Description string `json:"description"`
GpgKeyId int `json:"gpg_key_id"`
/* SslCaCertId int `json:"ssl_ca_cert_id"`
SslClientCertId int `json:"ssl_client_cert_id"`
SslClientKeyId int `json:"ssl_client_key_id"` */
SyncPlanId int `json:"sync_plan_id"`
Label string `json:"label"`
}
// -----------------------------------------------------------------------------
// CRUD Implementation
// -----------------------------------------------------------------------------
// CreateKatelloProduct creates a new ForemanKatelloProduct with the attributes of the
// supplied ForemanKatelloProduct reference and returns the created
// ForemanKatelloProduct reference. The returned reference will have its ID and
// other API default values set by this function.
func (c *Client) CreateKatelloProduct(ctx context.Context, p *ForemanKatelloProduct) (*ForemanKatelloProduct, error) {
log.Tracef("foreman/api/product.go#Create")
sJSONBytes, jsonEncErr := c.WrapJSON(nil, p)
if jsonEncErr != nil {
return nil, jsonEncErr
}
log.Debugf("KatelloProductJSONBytes: [%s]", sJSONBytes)
req, reqErr := c.NewRequestWithContext(
ctx,
http.MethodPost,
KatelloProductEndpointPrefix,
bytes.NewBuffer(sJSONBytes),
)
if reqErr != nil {
return nil, reqErr
}
var createdKatelloProduct ForemanKatelloProduct
sendErr := c.SendAndParse(req, &createdKatelloProduct)
if sendErr != nil {
return nil, sendErr
}
log.Debugf("createdKatelloProduct: [%+v]", createdKatelloProduct)
return &createdKatelloProduct, nil
}
// ReadKatelloProduct reads the attributes of a ForemanKatelloProduct identified by the
// supplied ID and returns a ForemanKatelloProduct reference.
func (c *Client) ReadKatelloProduct(ctx context.Context, id int) (*ForemanKatelloProduct, error) {
log.Tracef("foreman/api/product.go#Read")
reqEndpoint := fmt.Sprintf("%s/%d", KatelloProductEndpointPrefix, id)
req, reqErr := c.NewRequestWithContext(
ctx,
http.MethodGet,
reqEndpoint,
nil,
)
if reqErr != nil {
return nil, reqErr
}
var readKatelloProduct ForemanKatelloProduct
sendErr := c.SendAndParse(req, &readKatelloProduct)
if sendErr != nil {
return nil, sendErr
}
log.Debugf("readKatelloProduct: [%+v]", readKatelloProduct)
return &readKatelloProduct, nil
}
// UpdateKatelloProduct updates a ForemanKatelloProduct's attributes. The sync plan
// with the ID of the supplied ForemanKatelloProduct will be updated. A new
// ForemanKatelloProduct reference is returned with the attributes from the result
// of the update operation.
func (c *Client) UpdateKatelloProduct(ctx context.Context, p *ForemanKatelloProduct) (*ForemanKatelloProduct, error) {
log.Tracef("foreman/api/product.go#Update")
reqEndpoint := fmt.Sprintf("%s/%d", KatelloProductEndpointPrefix, p.Id)
sJSONBytes, jsonEncErr := c.WrapJSON(nil, p)
if jsonEncErr != nil {
return nil, jsonEncErr
}
log.Debugf("KatelloProductJSONBytes: [%s]", sJSONBytes)
req, reqErr := c.NewRequestWithContext(
ctx,
http.MethodPut,
reqEndpoint,
bytes.NewBuffer(sJSONBytes),
)
if reqErr != nil {
return nil, reqErr
}
var updatedKatelloProduct ForemanKatelloProduct
sendErr := c.SendAndParse(req, &updatedKatelloProduct)
if sendErr != nil {
return nil, sendErr
}
log.Debugf("updatedKatelloProduct: [%+v]", updatedKatelloProduct)
return &updatedKatelloProduct, nil
}
// DeleteKatelloProduct deletes the ForemanKatelloProduct identified by the supplied ID
func (c *Client) DeleteKatelloProduct(ctx context.Context, id int) error {
log.Tracef("foreman/api/product.go#Delete")
reqEndpoint := fmt.Sprintf("%s/%d", KatelloProductEndpointPrefix, id)
req, reqErr := c.NewRequestWithContext(
ctx,
http.MethodDelete,
reqEndpoint,
nil,
)
if reqErr != nil {
return reqErr
}
return c.SendAndParse(req, nil)
}
// -----------------------------------------------------------------------------
// Query Implementation
// -----------------------------------------------------------------------------
// QueryKatelloProduct queries for a ForemanKatelloProduct based on the attributes of
// the supplied ForemanKatelloProduct reference and returns a QueryResponse struct
// containing query/response metadata and the matching sync plan.
func (c *Client) QueryKatelloProduct(ctx context.Context, p *ForemanKatelloProduct) (QueryResponse, error) {
log.Tracef("foreman/api/product.go#Search")
queryResponse := QueryResponse{}
req, reqErr := c.NewRequestWithContext(
ctx,
http.MethodGet,
KatelloProductEndpointPrefix,
nil,
)
if reqErr != nil {
return queryResponse, reqErr
}
// dynamically build the query based on the attributes
reqQuery := req.URL.Query()
name := `"` + p.Name + `"`
reqQuery.Set("search", "name="+name)
req.URL.RawQuery = reqQuery.Encode()
sendErr := c.SendAndParse(req, &queryResponse)
if sendErr != nil {
return queryResponse, sendErr
}
// Results will be Unmarshaled into a []map[string]interface{}
//
// Encode back to JSON, then Unmarshal into []ForemanKatelloProduct for
// the results
results := []ForemanKatelloProduct{}
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 []ForemanKatelloProduct 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
}