/
client_data_source.go
236 lines (189 loc) · 8.71 KB
/
client_data_source.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
package dsfhub
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
const endpointDsfDataSource = "/data-sources"
// CreateDSFDataSource adds a DSF data source to be monitored DSF
func (c *Client) CreateDSFDataSource(dsfDataSource ResourceWrapper) (*ResourceWrapper, error) {
log.Printf("[INFO] Adding DSFDataSource: %s to gateway: %s\n", dsfDataSource.Data.ServerType, dsfDataSource.Data.GatewayID)
//dsfDataSource := DSFDataSource{}
dsfDataSourceJSON, err := json.Marshal(dsfDataSource)
log.Printf("[DEBUG] Adding DSFDataSource dsfDataSourceJSON: %s\n", dsfDataSourceJSON)
if err != nil {
return nil, fmt.Errorf("failed to JSON marshal dsfDataSource: %s", err)
}
resp, err := c.MakeCall(http.MethodPost, endpointDsfDataSource, dsfDataSourceJSON)
if err != nil {
log.Printf("[INFO] err.Error(): %s\n", err.Error())
return nil, fmt.Errorf("error adding DSFDataSource for serverType: %s and gatewayId: %s | err: %s", dsfDataSource.Data.ServerType, dsfDataSource.Data.GatewayID, err)
}
// Read the body
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
// Dump JSON
log.Printf("[DEBUG] Add DSFDataSource JSON response: %s\n", string(responseBody))
// Parse the JSON
var createDSFDataSourceResponse ResourceWrapper
err = json.Unmarshal([]byte(responseBody), &createDSFDataSourceResponse)
if err != nil {
return nil, fmt.Errorf("error parsing add DSFDataSource JSON response serverType: %s and gatewayId: %s | err: %s", dsfDataSource.Data.ServerType, dsfDataSource.Data.GatewayID, err)
}
if createDSFDataSourceResponse.Errors != nil {
return nil, fmt.Errorf("errors found in json response: %s", responseBody)
}
return &createDSFDataSourceResponse, nil
}
// ReadDSFDataSource gets the DSF data source by ID
func (c *Client) ReadDSFDataSource(dataSourceId string) (*ResourceWrapper, error) {
log.Printf("[INFO] Getting DSFDataSource for dataSourceId: %s\n", dataSourceId)
reqURL := fmt.Sprintf(endpointDsfDataSource+"/%s", url.PathEscape(dataSourceId))
resp, err := c.MakeCall(http.MethodGet, reqURL, nil)
if err != nil {
return nil, fmt.Errorf("error reading DSFDataSource for dataSourceId: %s | err: %s", dataSourceId, err)
}
// Read the body
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
// Dump JSON
log.Printf("[DEBUG] ReadDSFDataSource JSON response: %s\n", string(responseBody))
// Parse the JSON
var readDSFDataSourceDataResponse ResourceWrapper
err = json.Unmarshal([]byte(responseBody), &readDSFDataSourceDataResponse)
if err != nil {
return nil, fmt.Errorf("error parsing DSFDataSource JSON response for dataSourceId: %s | dsfDataSource: %s err: %s", dataSourceId, responseBody, err)
}
if readDSFDataSourceDataResponse.Errors != nil {
return nil, fmt.Errorf("errors found in json response: %s", responseBody)
}
return &readDSFDataSourceDataResponse, nil
}
// ReadDSFDataSources all DSFDataSources
func (c *Client) ReadDSFDataSources() (*ResourcesWrapper, error) {
log.Printf("[INFO] Getting DSFDataSources\n")
resp, err := c.MakeCall(http.MethodGet, endpointDsfDataSource, nil)
if err != nil {
return nil, fmt.Errorf("error reading DSFDataSources | err: %s", err)
}
// Read the body
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
// Dump JSON
log.Printf("[DEBUG] ReadDSFDataSources JSON response: %s\n", string(responseBody))
// Parse the JSON
var readDSFDataSourcesDataResponse ResourcesWrapper
err = json.Unmarshal([]byte(responseBody), &readDSFDataSourcesDataResponse)
if err != nil {
return nil, fmt.Errorf("error parsing DSFDataSource JSON response: %v err: %s", responseBody, err)
}
if readDSFDataSourcesDataResponse.Errors != nil {
return nil, fmt.Errorf("errors found in json response: %s", responseBody)
}
return &readDSFDataSourcesDataResponse, nil
}
// UpdateDSFDataSource will update a specific data source in DSF referenced by the dataSourceId
func (c *Client) UpdateDSFDataSource(dataSourceId string, dsfDataSourceData ResourceWrapper) (*ResourceWrapper, error) {
log.Printf("[INFO] Getting DSF data source for dataSourceId: %s)\n", dataSourceId)
//dsfDataSource := DSFDataSource{}
dsfDataSourceJSON, err := json.Marshal(dsfDataSourceData)
log.Printf("[DEBUG] Updating DSFDataSource dsfDataSourceJSON: %s\n", dsfDataSourceJSON)
if err != nil {
return nil, fmt.Errorf("failed to JSON marshal dsfDataSource: %s", err)
}
reqURL := fmt.Sprintf(endpointDsfDataSource+"/%s", url.PathEscape(dataSourceId))
resp, err := c.MakeCall(http.MethodPut, reqURL, dsfDataSourceJSON)
if err != nil {
return nil, fmt.Errorf("error updating DSFDataSource for dataSourceId: %s | err: %s", dataSourceId, err)
}
// Read the body
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
// Dump JSON
log.Printf("[DEBUG] Update DSFDataSource JSON response: %s\n", string(responseBody))
// Parse the JSON
var updateDSFDataSourceDataResponse ResourceWrapper
err = json.Unmarshal([]byte(responseBody), &updateDSFDataSourceDataResponse)
if err != nil {
return nil, fmt.Errorf("error parsing update DSFDataSource JSON response for dataSourceId: %s | err: %s", dataSourceId, err)
}
if updateDSFDataSourceDataResponse.Errors != nil {
return nil, fmt.Errorf("errors found in json response: %s", responseBody)
}
return &updateDSFDataSourceDataResponse, nil
}
// DeleteDSFDataSource deletes a data source in DSF
func (c *Client) DeleteDSFDataSource(dataSourceId string) (*ResourceResponse, error) {
log.Printf("[INFO] Deleting DSFDataSource with dataSourceId: %s\n", dataSourceId)
reqURL := fmt.Sprintf(endpointDsfDataSource+"/%s", url.PathEscape(dataSourceId))
resp, err := c.MakeCall(http.MethodDelete, reqURL, nil)
if err != nil {
return nil, fmt.Errorf("error deleting DSFDataSource for dataSourceId: %s, %s", dataSourceId, err)
}
// Read the body
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
// Dump JSON
log.Printf("[DEBUG] Delete DSFDataSource with JSON response: %s\n", string(responseBody))
// Parse the JSON
var deleteDSFDataSourceResponse ResourceResponse
err = json.Unmarshal([]byte(responseBody), &deleteDSFDataSourceResponse)
if err != nil {
return nil, fmt.Errorf("error parsing delete DSFDataSource JSON response for dataSourceId: %s, %s", dataSourceId, err)
}
if deleteDSFDataSourceResponse.Errors != nil {
return nil, fmt.Errorf("errors found in json response: %s", responseBody)
}
return &deleteDSFDataSourceResponse, nil
}
// EnableAuditDSFDataSource enables logging for a DSF data source
func (c *Client) EnableAuditDSFDataSource(dataSourceId string) (*UpdateAuditResponse, error) {
log.Printf("[INFO] Enabling audit for dataSourceId: %v\n", dataSourceId)
reqURL := fmt.Sprintf(endpointDsfDataSource+"/%s/operations/enable-audit-collection", url.PathEscape(dataSourceId))
resp, err := c.MakeCall(http.MethodPost, reqURL, nil)
if err != nil {
return nil, fmt.Errorf("error enabling audit for dataSourceId: %s | err: %s\n", dataSourceId, err)
}
// Read the body
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
// Dump JSON
log.Printf("[DEBUG] Enable audit for DSFDataSource '%v' JSON response: %s\n", dataSourceId, string(responseBody))
// Parse the JSON
var enableAuditResponse UpdateAuditResponse
err = json.Unmarshal([]byte(responseBody), &enableAuditResponse)
if err != nil {
return nil, fmt.Errorf("error parsing enable audit DSFDataSource JSON response dataSourceId: %s | err: %s\n", dataSourceId, err)
}
if enableAuditResponse.Errors != nil {
return nil, fmt.Errorf("errors found in json response: %s", responseBody)
}
return &enableAuditResponse, nil
}
// DisableAuditDSFDataSource enables logging for a DSF data source
func (c *Client) DisableAuditDSFDataSource(dataSourceId string) (*UpdateAuditResponse, error) {
log.Printf("[INFO] Disabling audit for dataSourceId: %v\n", dataSourceId)
reqURL := fmt.Sprintf(endpointDsfDataSource+"/%s/operations/disable-audit-collection", url.PathEscape(dataSourceId))
resp, err := c.MakeCall(http.MethodPost, reqURL, nil)
if err != nil {
return nil, fmt.Errorf("error disabling audit for dataSourceId: %s | err: %s\n", dataSourceId, err)
}
// Read the body
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
// Dump JSON
log.Printf("[DEBUG] Disable audit for DSFDataSource '%v' JSON response: %s\n", dataSourceId, string(responseBody))
// Parse the JSON
var disableAuditResponse UpdateAuditResponse
err = json.Unmarshal([]byte(responseBody), &disableAuditResponse)
if err != nil {
return nil, fmt.Errorf("error parsing disable audit DSFDataSource JSON response dataSourceId: %s | err: %s\n", dataSourceId, err)
}
if disableAuditResponse.Errors != nil {
return nil, fmt.Errorf("errors found in json response: %s", responseBody)
}
return &disableAuditResponse, nil
}