This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
/
domain.go
208 lines (166 loc) · 5.39 KB
/
domain.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/wayfair/terraform-provider-utils/log"
)
const (
DomainEndpointPrefix = "domains"
)
// -----------------------------------------------------------------------------
// Struct Definition and Helpers
// -----------------------------------------------------------------------------
// The ForemanDomain API model represents the domain name. Domains serve as an
// identification string that defines autonomy, authority, or control for
// a portion of a network.
type ForemanDomain struct {
// Inherits the base object's attributes
ForemanObject
// Fully qualified domain name
Fullname string `json:"fullname"`
}
// -----------------------------------------------------------------------------
// CRUD Implementation
// -----------------------------------------------------------------------------
// CreateDomain creates a new ForemanDomain with the attributes of the supplied
// ForemanDomain reference and returns the created ForemanDomain reference.
// The returned reference will have its ID and other API default values set by
// this function.
func (c *Client) CreateDomain(d *ForemanDomain) (*ForemanDomain, error) {
log.Tracef("foreman/api/domain.go#Create")
reqEndpoint := fmt.Sprintf("/%s", DomainEndpointPrefix)
domainJSONBytes, jsonEncErr := json.Marshal(d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
log.Debugf("domainJSONBytes: [%s]", domainJSONBytes)
req, reqErr := c.NewRequest(
http.MethodPost,
reqEndpoint,
bytes.NewBuffer(domainJSONBytes),
)
if reqErr != nil {
return nil, reqErr
}
var createdDomain ForemanDomain
sendErr := c.SendAndParse(req, &createdDomain)
if sendErr != nil {
return nil, sendErr
}
log.Debugf("createdDomain: [%+v]", createdDomain)
return &createdDomain, nil
}
// ReadDomain reads the attributes of a ForemanDomain identified by the
// supplied ID and returns a ForemanDomain reference.
func (c *Client) ReadDomain(id int) (*ForemanDomain, error) {
log.Tracef("foreman/api/domain.go#Read")
reqEndpoint := fmt.Sprintf("/%s/%d", DomainEndpointPrefix, id)
req, reqErr := c.NewRequest(
http.MethodGet,
reqEndpoint,
nil,
)
if reqErr != nil {
return nil, reqErr
}
var readDomain ForemanDomain
sendErr := c.SendAndParse(req, &readDomain)
if sendErr != nil {
return nil, sendErr
}
log.Debugf("readDomain: [%+v]", readDomain)
return &readDomain, nil
}
// UpdateDomain updates a ForemanDomain's attributes. The domain with the ID
// of the supplied ForemanDomain will be updated. A new ForemanDomain reference
// is returned with the attributes from the result of the update operation.
func (c *Client) UpdateDomain(d *ForemanDomain) (*ForemanDomain, error) {
log.Tracef("foreman/api/domain.go#Update")
reqEndpoint := fmt.Sprintf("/%s/%d", DomainEndpointPrefix, d.Id)
domainJSONBytes, jsonEncErr := json.Marshal(d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
log.Debugf("domainJSONBytes: [%s]", domainJSONBytes)
req, reqErr := c.NewRequest(
http.MethodPut,
reqEndpoint,
bytes.NewBuffer(domainJSONBytes),
)
if reqErr != nil {
return nil, reqErr
}
var updatedDomain ForemanDomain
sendErr := c.SendAndParse(req, &updatedDomain)
if sendErr != nil {
return nil, sendErr
}
log.Debugf("updatedDomain: [%+v]", updatedDomain)
return &updatedDomain, nil
}
// DeleteDomain deletes the ForemanDomain identified by the supplied ID
func (c *Client) DeleteDomain(id int) error {
log.Tracef("foreman/api/domain.go#Delete")
reqEndpoint := fmt.Sprintf("/%s/%d", DomainEndpointPrefix, id)
req, reqErr := c.NewRequest(
http.MethodDelete,
reqEndpoint,
nil,
)
if reqErr != nil {
return reqErr
}
return c.SendAndParse(req, nil)
}
// -----------------------------------------------------------------------------
// Query Implementation
// -----------------------------------------------------------------------------
// QueryDomain queries for a ForemanDomain based on the attributes of the
// supplied ForemanDomain reference and returns a QueryResponse struct
// containing query/response metadata and the matching domains.
func (c *Client) QueryDomain(d *ForemanDomain) (QueryResponse, error) {
log.Tracef("foreman/api/domain.go#Search")
queryResponse := QueryResponse{}
reqEndpoint := fmt.Sprintf("/%s", DomainEndpointPrefix)
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 := `"` + d.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 []ForemanDomain for
// the results
results := []ForemanDomain{}
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 []ForemanDomain 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
}