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 2 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
30 changes: 30 additions & 0 deletions syntheticsclientv2/common_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,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 +228,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
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)
}

}
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)
}
81 changes: 81 additions & 0 deletions syntheticsclientv2/get_downtimeconfigurationsv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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"
"fmt"
)

func parseDowntimeConfigurationV2Response(response string) (*DowntimeConfigurationV2Response, error) {
// Parse the response and return the check object
var check DowntimeConfigurationV2Response
err := json.Unmarshal([]byte(response), &check)
if err != nil {
return nil, err
}

return &check, err
}

func (c Client) GetDowntimeConfigurationV2(id int) (*DowntimeConfigurationV2Response, *RequestDetails, error) {

details, err := c.makePublicAPICall("GET",
fmt.Sprintf("/downtime_configurations/%d", id),
bytes.NewBufferString("{}"),
nil)

if err != nil {
return nil, details, err
}

check, err := parseDowntimeConfigurationV2Response(details.ResponseBody)
if err != nil {
return check, details, err
}

return check, details, nil
}

func parseDowntimeConfigurationsV2Response(response string) (*DowntimeConfigurationsV2Response, error) {
// Parse the response and return the check object
var check DowntimeConfigurationsV2Response
err := json.Unmarshal([]byte(response), &check)
if err != nil {
return nil, err
}

return &check, err
}

func (c Client) GetDowntimeConfigurationsV2() (*DowntimeConfigurationsV2Response, *RequestDetails, error) {

details, err := c.makePublicAPICall("GET",
"/downtime_configurations",
bytes.NewBufferString("{}"),
nil)

if err != nil {
return nil, details, err
}

check, err := parseDowntimeConfigurationsV2Response(details.ResponseBody)
if err != nil {
return check, details, err
}

return check, details, nil
}
Loading
Loading