Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SYN-3922: add support for downtime configurations #22

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coverage.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mode: set
mode: set
43 changes: 41 additions & 2 deletions syntheticsclientv2/common_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ type GetChecksV2Options struct {
TestTypes []string `json:"testTypes"`
}

type GetDowntimeConfigurationsV2Options struct {
PerPage int `json:"perPage"`
Page int `json:"page"`
Search string `json:"search"`
OrderBy string `json:"orderBy"`
Rule []string `json:"rule"`
Status []string `json:"status"`
}

type Errors []struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Expand All @@ -190,6 +199,21 @@ type Variable struct {
Value string `json:"value"`
}

type DowntimeConfiguration struct {
Createdat time.Time `json:"createdAt,omitempty"`
Description string `json:"description,omitempty"`
ID int `json:"id,omitempty"`
Name string `json:"name"`
Updatedat time.Time `json:"updatedAt,omitempty"`
Rule string `json:"rule"`
Starttime time.Time `json:"startTime"`
Endtime time.Time `json:"endTime"`
Status string `json:"status,omitempty"`
Testsupdatedat time.Time `json:"testsUpdatedAt,omitempty"`
Testcount int `json:"testCount,omitempty"`
Testids []int `json:"testIds,omitempty"`
}

type DeleteCheck struct {
Result string `json:"result"`
Message string `json:"message"`
Expand All @@ -213,6 +237,21 @@ type DevicesV2Response struct {
Devices []Device `json:"devices"`
}

type DowntimeConfigurationV2Response struct {
DowntimeConfiguration `json:"downtimeConfiguration"`
}

type DowntimeConfigurationV2Input struct {
DowntimeConfiguration `json:"downtimeConfiguration"`
}

type DowntimeConfigurationsV2Response struct {
Page int `json:"nextPageLink"`
Pagelimt int `json:"perPage"`
Totalcount int `json:"totalCount"`
Downtimeconfigurations []DowntimeConfiguration `json:"downtimeConfigurations"`
}

type VariableV2Response struct {
Variable `json:"variable"`
}
Expand Down Expand Up @@ -307,7 +346,7 @@ type HttpCheckV2Response struct {
Lastrunstatus string `json:"lastRunStatus"`
Lastrunat time.Time `json:"lastRunAt"`
Automaticretries int `json:"automaticRetries"`
Port int `json:"port"`
Port int `json:"port"`
} `json:"test"`
}

Expand All @@ -329,7 +368,7 @@ type HttpCheckV2Input struct {
Validations []Validations `json:"validations"`
Customproperties []CustomProperties `json:"customProperties"`
Automaticretries int `json:"automaticRetries"`
Port int `json:"port"`
Port int `json:"port"`
} `json:"test"`
}

Expand Down
52 changes: 52 additions & 0 deletions syntheticsclientv2/create_downtimeconfigurationv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2024 Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package syntheticsclientv2

import (
"bytes"
"encoding/json"
)

func parseCreateDowntimeConfigurationV2Response(response string) (*DowntimeConfigurationV2Response, error) {

var createDowntimeConfigurationV2 DowntimeConfigurationV2Response
JSONResponse := []byte(response)
err := json.Unmarshal(JSONResponse, &createDowntimeConfigurationV2)
if err != nil {
return nil, err
}

return &createDowntimeConfigurationV2, err
}

func (c Client) CreateDowntimeConfigurationV2(DowntimeConfigurationV2Details *DowntimeConfigurationV2Input) (*DowntimeConfigurationV2Response, *RequestDetails, error) {

body, err := json.Marshal(DowntimeConfigurationV2Details)
if err != nil {
return nil, nil, err
}

details, err := c.makePublicAPICall("POST", "/downtime_configurations", bytes.NewBuffer(body), nil)
if err != nil {
return nil, details, err
}

newDowntimeConfigurationV2, err := parseCreateDowntimeConfigurationV2Response(details.ResponseBody)
if err != nil {
return newDowntimeConfigurationV2, details, err
}

return newDowntimeConfigurationV2, details, nil
}
82 changes: 82 additions & 0 deletions syntheticsclientv2/create_downtimeconfigurationv2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//go:build unit_tests
// +build unit_tests

// Copyright 2024 Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package syntheticsclientv2

import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)

var (
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"}}`
inputDowntimeConfigurationV2Data = DowntimeConfigurationV2Input{}
)

func TestCreateDowntimeConfigurationV2(t *testing.T) {
setup()
defer teardown()

testMux.HandleFunc("/downtime_configurations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
_, err := w.Write([]byte(createDowntimeConfigurationV2Body))
if err != nil {
t.Fatal(err)
}
})

err := json.Unmarshal([]byte(createDowntimeConfigurationV2Body), &inputDowntimeConfigurationV2Data)
if err != nil {
t.Fatal(err)
}

resp, _, err := testClient.CreateDowntimeConfigurationV2(&inputDowntimeConfigurationV2Data)

if err != nil {
t.Fatal(err)
}

fmt.Println(resp)

if !reflect.DeepEqual(resp.DowntimeConfiguration.ID, inputDowntimeConfigurationV2Data.DowntimeConfiguration.ID) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.ID, inputDowntimeConfigurationV2Data.DowntimeConfiguration.ID)
}

if !reflect.DeepEqual(resp.DowntimeConfiguration.Name, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Name) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Name, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Name)
}

if !reflect.DeepEqual(resp.DowntimeConfiguration.Description, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Description) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Description, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Description)
}

if !reflect.DeepEqual(resp.DowntimeConfiguration.Rule, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Rule) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Rule, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Rule)
}

if !reflect.DeepEqual(resp.DowntimeConfiguration.Starttime, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Starttime) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Starttime, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Starttime)
}

if !reflect.DeepEqual(resp.DowntimeConfiguration.Endtime, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Endtime) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Endtime, inputDowntimeConfigurationV2Data.DowntimeConfiguration.Endtime)
}

}
2 changes: 1 addition & 1 deletion syntheticsclientv2/create_httpcheckv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

var (
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"}]}}`
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"}]}}`
inputHttpCheckV2Data = HttpCheckV2Input{}
)

Expand Down
39 changes: 39 additions & 0 deletions syntheticsclientv2/delete_downtimeconfigurationv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package syntheticsclientv2

import (
"bytes"
"errors"
"fmt"
"strconv"
)

func (c Client) DeleteDowntimeConfigurationV2(id int) (int, error) {
requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/downtime_configurations/%d", id), bytes.NewBufferString("{}"), nil)
if err != nil {
return 1, err
}
var status = requestDetails.StatusCode

fmt.Println(status)

if status >= 300 || status < 200 {
errorMsg := fmt.Sprintf("error: Response code %v. Expecting 2XX.", strconv.Itoa(status))
return status, errors.New(errorMsg)
}

return status, err
}
48 changes: 48 additions & 0 deletions syntheticsclientv2/delete_downtimeconfigurationv2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//go:build unit_tests
// +build unit_tests

// Copyright 2024 Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package syntheticsclientv2

import (
"fmt"
"net/http"
"testing"
)

var (
deleteDowntimeConfigurationV2RespBody = ``
)

func TestDeleteDowntimeConfigurationV2(t *testing.T) {
setup()
defer teardown()

testMux.HandleFunc("/downtime_configurations/19", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
_, err := w.Write([]byte(deleteDowntimeConfigurationV2RespBody))
if err != nil {
t.Fatal(err)
}
})

resp, err := testClient.DeleteDowntimeConfigurationV2(19)
if err != nil {
fmt.Println(resp)
t.Fatal(err)
}
fmt.Println(resp)
}
10 changes: 5 additions & 5 deletions syntheticsclientv2/get_checksv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"
"strconv"
"strings"
)

func parseChecksV2Response(response string) (*ChecksV2Response, error) {
Expand Down Expand Up @@ -85,15 +85,15 @@ func (c Client) GetChecksV2(params *GetChecksV2Options) (*ChecksV2Response, *Req
return check, details, nil
}

func activeQueryParam(param *bool) (string) {
func activeQueryParam(param *bool) string {
if param != nil {
boolString := strconv.FormatBool(*param)
return fmt.Sprintf("&active=%s", boolString)
}
return ""
}

func customPropsQueryParam(params []CustomProperties) (string) {
func customPropsQueryParam(params []CustomProperties) string {
if len(params) == 0 {
return ""
}
Expand All @@ -104,14 +104,14 @@ func customPropsQueryParam(params []CustomProperties) (string) {
return result
}

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

func stringsQueryParam(params []string, queryParamName string) (string) {
func stringsQueryParam(params []string, queryParamName string) string {
if len(params) == 0 {
return ""
}
Expand Down
Loading
Loading