Skip to content

Commit dbfb5a2

Browse files
SYN-3922: add support for downtime configurations (#22)
* SYN-3922: add support for downtime configurations * create test for integration test * test pagination
1 parent bf6a9b5 commit dbfb5a2

15 files changed

+791
-39
lines changed

coverage.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mode: set
1+
mode: set

syntheticsclientv2/common_models.go

+41-2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ type GetChecksV2Options struct {
170170
TestTypes []string `json:"testTypes"`
171171
}
172172

173+
type GetDowntimeConfigurationsV2Options struct {
174+
PerPage int `json:"perPage"`
175+
Page int `json:"page"`
176+
Search string `json:"search"`
177+
OrderBy string `json:"orderBy"`
178+
Rule []string `json:"rule"`
179+
Status []string `json:"status"`
180+
}
181+
173182
type Errors []struct {
174183
Title string `json:"title,omitempty"`
175184
Description string `json:"description,omitempty"`
@@ -190,6 +199,21 @@ type Variable struct {
190199
Value string `json:"value"`
191200
}
192201

202+
type DowntimeConfiguration struct {
203+
Createdat time.Time `json:"createdAt,omitempty"`
204+
Description string `json:"description,omitempty"`
205+
ID int `json:"id,omitempty"`
206+
Name string `json:"name"`
207+
Updatedat time.Time `json:"updatedAt,omitempty"`
208+
Rule string `json:"rule"`
209+
Starttime time.Time `json:"startTime"`
210+
Endtime time.Time `json:"endTime"`
211+
Status string `json:"status,omitempty"`
212+
Testsupdatedat time.Time `json:"testsUpdatedAt,omitempty"`
213+
Testcount int `json:"testCount,omitempty"`
214+
Testids []int `json:"testIds,omitempty"`
215+
}
216+
193217
type DeleteCheck struct {
194218
Result string `json:"result"`
195219
Message string `json:"message"`
@@ -213,6 +237,21 @@ type DevicesV2Response struct {
213237
Devices []Device `json:"devices"`
214238
}
215239

240+
type DowntimeConfigurationV2Response struct {
241+
DowntimeConfiguration `json:"downtimeConfiguration"`
242+
}
243+
244+
type DowntimeConfigurationV2Input struct {
245+
DowntimeConfiguration `json:"downtimeConfiguration"`
246+
}
247+
248+
type DowntimeConfigurationsV2Response struct {
249+
Page int `json:"nextPageLink"`
250+
Pagelimt int `json:"perPage"`
251+
Totalcount int `json:"totalCount"`
252+
Downtimeconfigurations []DowntimeConfiguration `json:"downtimeConfigurations"`
253+
}
254+
216255
type VariableV2Response struct {
217256
Variable `json:"variable"`
218257
}
@@ -307,7 +346,7 @@ type HttpCheckV2Response struct {
307346
Lastrunstatus string `json:"lastRunStatus"`
308347
Lastrunat time.Time `json:"lastRunAt"`
309348
Automaticretries int `json:"automaticRetries"`
310-
Port int `json:"port"`
349+
Port int `json:"port"`
311350
} `json:"test"`
312351
}
313352

@@ -329,7 +368,7 @@ type HttpCheckV2Input struct {
329368
Validations []Validations `json:"validations"`
330369
Customproperties []CustomProperties `json:"customProperties"`
331370
Automaticretries int `json:"automaticRetries"`
332-
Port int `json:"port"`
371+
Port int `json:"port"`
333372
} `json:"test"`
334373
}
335374

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2024 Splunk, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package syntheticsclientv2
16+
17+
import (
18+
"bytes"
19+
"encoding/json"
20+
)
21+
22+
func parseCreateDowntimeConfigurationV2Response(response string) (*DowntimeConfigurationV2Response, error) {
23+
24+
var createDowntimeConfigurationV2 DowntimeConfigurationV2Response
25+
JSONResponse := []byte(response)
26+
err := json.Unmarshal(JSONResponse, &createDowntimeConfigurationV2)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
return &createDowntimeConfigurationV2, err
32+
}
33+
34+
func (c Client) CreateDowntimeConfigurationV2(DowntimeConfigurationV2Details *DowntimeConfigurationV2Input) (*DowntimeConfigurationV2Response, *RequestDetails, error) {
35+
36+
body, err := json.Marshal(DowntimeConfigurationV2Details)
37+
if err != nil {
38+
return nil, nil, err
39+
}
40+
41+
details, err := c.makePublicAPICall("POST", "/downtime_configurations", bytes.NewBuffer(body), nil)
42+
if err != nil {
43+
return nil, details, err
44+
}
45+
46+
newDowntimeConfigurationV2, err := parseCreateDowntimeConfigurationV2Response(details.ResponseBody)
47+
if err != nil {
48+
return newDowntimeConfigurationV2, details, err
49+
}
50+
51+
return newDowntimeConfigurationV2, details, nil
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//go:build unit_tests
2+
// +build unit_tests
3+
4+
// Copyright 2024 Splunk, Inc.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
package syntheticsclientv2
19+
20+
import (
21+
"encoding/json"
22+
"fmt"
23+
"net/http"
24+
"reflect"
25+
"testing"
26+
)
27+
28+
var (
29+
createDowntimeConfigurationV2Body = `{"downtimeConfiguration":{"name":"dc test","description":"My super awesome test downtimeConfiguration","rule":"augment_data","testIds":[482],"startTime":"2024-05-16T20:23:00.000Z","endTime":"2024-05-16T20:38:00.000Z"}}`
30+
inputDowntimeConfigurationV2Data = DowntimeConfigurationV2Input{}
31+
)
32+
33+
func TestCreateDowntimeConfigurationV2(t *testing.T) {
34+
setup()
35+
defer teardown()
36+
37+
testMux.HandleFunc("/downtime_configurations", func(w http.ResponseWriter, r *http.Request) {
38+
testMethod(t, r, "POST")
39+
_, err := w.Write([]byte(createDowntimeConfigurationV2Body))
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
})
44+
45+
err := json.Unmarshal([]byte(createDowntimeConfigurationV2Body), &inputDowntimeConfigurationV2Data)
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
50+
resp, _, err := testClient.CreateDowntimeConfigurationV2(&inputDowntimeConfigurationV2Data)
51+
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
56+
fmt.Println(resp)
57+
58+
if !reflect.DeepEqual(resp.DowntimeConfiguration.ID, inputDowntimeConfigurationV2Data.DowntimeConfiguration.ID) {
59+
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.ID, inputDowntimeConfigurationV2Data.DowntimeConfiguration.ID)
60+
}
61+
62+
if !reflect.DeepEqual(resp.DowntimeConfiguration.Name, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Name) {
63+
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Name, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Name)
64+
}
65+
66+
if !reflect.DeepEqual(resp.DowntimeConfiguration.Description, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Description) {
67+
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Description, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Description)
68+
}
69+
70+
if !reflect.DeepEqual(resp.DowntimeConfiguration.Rule, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Rule) {
71+
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Rule, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Rule)
72+
}
73+
74+
if !reflect.DeepEqual(resp.DowntimeConfiguration.Starttime, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Starttime) {
75+
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Starttime, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Starttime)
76+
}
77+
78+
if !reflect.DeepEqual(resp.DowntimeConfiguration.Endtime, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Endtime) {
79+
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Endtime, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Endtime)
80+
}
81+
82+
}

syntheticsclientv2/create_httpcheckv2_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
var (
29-
createHttpCheckV2Body = `{"test":{"automaticRetries": 1, "customProperties": [{"key": "Test_Key", "value": "Test Custom Properties"}], "port": 443, "name":"morebeeps-test","type":"http","url":"https://www.splunk.com","location_ids":["aws-us-east-1"],"frequency":10,"scheduling_strategy":"round_robin","active":true,"request_method":"GET","body":null,"headers":[{"name":"boop","value":"beep"}]}}`
29+
createHttpCheckV2Body = `{"test":{"automaticRetries": 1, "customProperties": [{"key": "Test_Key", "value": "Test Custom Properties"}], "port": 443, "name":"morebeeps-test","type":"http","url":"https://www.splunk.com","location_ids":["aws-us-east-1"],"frequency":10,"scheduling_strategy":"round_robin","active":true,"request_method":"GET","body":null,"headers":[{"name":"boop","value":"beep"}]}}`
3030
inputHttpCheckV2Data = HttpCheckV2Input{}
3131
)
3232

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2024 Splunk, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package syntheticsclientv2
16+
17+
import (
18+
"bytes"
19+
"errors"
20+
"fmt"
21+
"strconv"
22+
)
23+
24+
func (c Client) DeleteDowntimeConfigurationV2(id int) (int, error) {
25+
requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/downtime_configurations/%d", id), bytes.NewBufferString("{}"), nil)
26+
if err != nil {
27+
return 1, err
28+
}
29+
var status = requestDetails.StatusCode
30+
31+
fmt.Println(status)
32+
33+
if status >= 300 || status < 200 {
34+
errorMsg := fmt.Sprintf("error: Response code %v. Expecting 2XX.", strconv.Itoa(status))
35+
return status, errors.New(errorMsg)
36+
}
37+
38+
return status, err
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//go:build unit_tests
2+
// +build unit_tests
3+
4+
// Copyright 2024 Splunk, Inc.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
package syntheticsclientv2
19+
20+
import (
21+
"fmt"
22+
"net/http"
23+
"testing"
24+
)
25+
26+
var (
27+
deleteDowntimeConfigurationV2RespBody = ``
28+
)
29+
30+
func TestDeleteDowntimeConfigurationV2(t *testing.T) {
31+
setup()
32+
defer teardown()
33+
34+
testMux.HandleFunc("/downtime_configurations/19", func(w http.ResponseWriter, r *http.Request) {
35+
testMethod(t, r, "DELETE")
36+
_, err := w.Write([]byte(deleteDowntimeConfigurationV2RespBody))
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
})
41+
42+
resp, err := testClient.DeleteDowntimeConfigurationV2(19)
43+
if err != nil {
44+
fmt.Println(resp)
45+
t.Fatal(err)
46+
}
47+
fmt.Println(resp)
48+
}

syntheticsclientv2/get_checksv2.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
"bytes"
1919
"encoding/json"
2020
"fmt"
21-
"strings"
2221
"strconv"
22+
"strings"
2323
)
2424

2525
func parseChecksV2Response(response string) (*ChecksV2Response, error) {
@@ -85,15 +85,15 @@ func (c Client) GetChecksV2(params *GetChecksV2Options) (*ChecksV2Response, *Req
8585
return check, details, nil
8686
}
8787

88-
func activeQueryParam(param *bool) (string) {
88+
func activeQueryParam(param *bool) string {
8989
if param != nil {
9090
boolString := strconv.FormatBool(*param)
9191
return fmt.Sprintf("&active=%s", boolString)
9292
}
9393
return ""
9494
}
9595

96-
func customPropsQueryParam(params []CustomProperties) (string) {
96+
func customPropsQueryParam(params []CustomProperties) string {
9797
if len(params) == 0 {
9898
return ""
9999
}
@@ -104,14 +104,14 @@ func customPropsQueryParam(params []CustomProperties) (string) {
104104
return result
105105
}
106106

107-
func integersQueryParam(params []int, queryParamName string) (string) {
107+
func integersQueryParam(params []int, queryParamName string) string {
108108
if len(params) == 0 {
109109
return ""
110110
}
111111
return queryParamName + strings.Trim(strings.Replace(fmt.Sprint(params), " ", queryParamName, -1), "[]")
112112
}
113113

114-
func stringsQueryParam(params []string, queryParamName string) (string) {
114+
func stringsQueryParam(params []string, queryParamName string) string {
115115
if len(params) == 0 {
116116
return ""
117117
}

0 commit comments

Comments
 (0)