From 128893814abc3ce4a6649aab92e438728caac5c1 Mon Sep 17 00:00:00 2001 From: Boramey Chhay Date: Wed, 15 May 2024 17:04:22 -0600 Subject: [PATCH] SYN-3922: add support for downtime configurations --- coverage.txt | 2 +- syntheticsclientv2/common_models.go | 30 ++++ .../create_downtimeconfigurationv2.go | 52 ++++++ .../create_downtimeconfigurationv2_test.go | 82 +++++++++ .../delete_downtimeconfigurationv2.go | 39 ++++ .../delete_downtimeconfigurationv2_test.go | 48 +++++ .../get_downtimeconfigurationsv2.go | 81 +++++++++ .../get_downtimeconfigurationsv2_test.go | 168 ++++++++++++++++++ syntheticsclientv2/integration_test.go | 103 +++++++++++ .../update_downtimeconfigurationv2.go | 53 ++++++ .../update_downtimeconfigurationv2_test.go | 80 +++++++++ 11 files changed, 737 insertions(+), 1 deletion(-) create mode 100644 syntheticsclientv2/create_downtimeconfigurationv2.go create mode 100644 syntheticsclientv2/create_downtimeconfigurationv2_test.go create mode 100644 syntheticsclientv2/delete_downtimeconfigurationv2.go create mode 100644 syntheticsclientv2/delete_downtimeconfigurationv2_test.go create mode 100644 syntheticsclientv2/get_downtimeconfigurationsv2.go create mode 100644 syntheticsclientv2/get_downtimeconfigurationsv2_test.go create mode 100644 syntheticsclientv2/update_downtimeconfigurationv2.go create mode 100644 syntheticsclientv2/update_downtimeconfigurationv2_test.go diff --git a/coverage.txt b/coverage.txt index 5f02b11..4dd54a3 100644 --- a/coverage.txt +++ b/coverage.txt @@ -1 +1 @@ -mode: set +mode: set \ No newline at end of file diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go index 26febe0..c1e211b 100644 --- a/syntheticsclientv2/common_models.go +++ b/syntheticsclientv2/common_models.go @@ -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"` @@ -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"` } diff --git a/syntheticsclientv2/create_downtimeconfigurationv2.go b/syntheticsclientv2/create_downtimeconfigurationv2.go new file mode 100644 index 0000000..b92e257 --- /dev/null +++ b/syntheticsclientv2/create_downtimeconfigurationv2.go @@ -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 +} diff --git a/syntheticsclientv2/create_downtimeconfigurationv2_test.go b/syntheticsclientv2/create_downtimeconfigurationv2_test.go new file mode 100644 index 0000000..a1c0c4d --- /dev/null +++ b/syntheticsclientv2/create_downtimeconfigurationv2_test.go @@ -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) + } + +} diff --git a/syntheticsclientv2/delete_downtimeconfigurationv2.go b/syntheticsclientv2/delete_downtimeconfigurationv2.go new file mode 100644 index 0000000..5401310 --- /dev/null +++ b/syntheticsclientv2/delete_downtimeconfigurationv2.go @@ -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 +} diff --git a/syntheticsclientv2/delete_downtimeconfigurationv2_test.go b/syntheticsclientv2/delete_downtimeconfigurationv2_test.go new file mode 100644 index 0000000..b96ad92 --- /dev/null +++ b/syntheticsclientv2/delete_downtimeconfigurationv2_test.go @@ -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) +} diff --git a/syntheticsclientv2/get_downtimeconfigurationsv2.go b/syntheticsclientv2/get_downtimeconfigurationsv2.go new file mode 100644 index 0000000..c2c4bbf --- /dev/null +++ b/syntheticsclientv2/get_downtimeconfigurationsv2.go @@ -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 +} diff --git a/syntheticsclientv2/get_downtimeconfigurationsv2_test.go b/syntheticsclientv2/get_downtimeconfigurationsv2_test.go new file mode 100644 index 0000000..8ae5550 --- /dev/null +++ b/syntheticsclientv2/get_downtimeconfigurationsv2_test.go @@ -0,0 +1,168 @@ +//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" + "net/http" + "reflect" + "testing" +) + +var ( + getDowntimeConfigurationV2Body = `{"downtimeConfiguration":{"id":329,"name":"dc test","description":"My super awesome test downtimeConfiguration","rule":"pause_tests","startTime":"2024-05-16T20:23:00.000Z","endTime":"2024-05-16T20:38:00.000Z","status":"scheduled","createdAt":"2024-05-15T20:24:07.541Z","updatedAt":"2024-05-15T20:25:44.211Z","testsUpdatedAt":"2024-05-15T20:24:07.541Z","testIds":[29976]}}` + inputGetDowntimeConfigurationV2 = verifyDowntimeConfigurationV2Input(string(getDowntimeConfigurationV2Body)) + getDowntimeConfigurationsV2Body = `{"downtimeConfigurations":[{"id":329,"name":"dc test","description":"My super awesome test downtimeConfiguration","rule":"pause_tests","startTime":"2024-05-16T20:23:00.000Z","endTime":"2024-05-16T20:38:00.000Z","status":"scheduled","createdAt":"2024-05-15T20:24:07.541Z","updatedAt":"2024-05-15T20:25:44.211Z","testsUpdatedAt":"2024-05-15T20:24:07.541Z","testCount":1}],"page":1,"pageLimit":1,"totalCount":1}` + inputGetDowntimeConfigurationsV2 = verifyDowntimeConfigurationsV2Input(string(getDowntimeConfigurationsV2Body)) +) + +func TestGetDowntimeConfigurationV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/downtime_configurations/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + _, err := w.Write([]byte(getDowntimeConfigurationV2Body)) + if err != nil { + t.Fatal(err) + } + }) + + resp, _, err := testClient.GetDowntimeConfigurationV2(1) + + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(resp.DowntimeConfiguration.ID, inputGetDowntimeConfigurationV2.DowntimeConfiguration.ID) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.ID, inputGetDowntimeConfigurationV2.DowntimeConfiguration.ID) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Name, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Name, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Name) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Description, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Description) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Description, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Description) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Rule, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Rule) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Rule, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Rule) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Starttime, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Starttime) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Starttime, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Starttime) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Endtime, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Endtime) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Endtime, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Endtime) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Status, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Status) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Status, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Status) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Createdat, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Createdat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Createdat, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Createdat) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Updatedat, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Updatedat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Updatedat, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Updatedat) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Testsupdatedat, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Testsupdatedat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Testsupdatedat, inputGetDowntimeConfigurationV2.DowntimeConfiguration.Testsupdatedat) + } + +} + +func verifyDowntimeConfigurationV2Input(stringInput string) *DowntimeConfigurationV2Response { + check := &DowntimeConfigurationV2Response{} + err := json.Unmarshal([]byte(stringInput), check) + if err != nil { + panic(err) + } + return check +} + +func TestGetDowntimeConfigurationsV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/downtime_configurations", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + _, err := w.Write([]byte(getDowntimeConfigurationsV2Body)) + if err != nil { + t.Fatal(err) + } + }) + + resp, _, err := testClient.GetDowntimeConfigurationsV2() + + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(resp.Downtimeconfigurations[0].ID, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].ID) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Downtimeconfigurations[0].ID, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].ID) + } + + if !reflect.DeepEqual(resp.Downtimeconfigurations[0].Name, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Downtimeconfigurations[0].Name, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Name) + } + + if !reflect.DeepEqual(resp.Downtimeconfigurations[0].Description, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Description) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Downtimeconfigurations[0].Description, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Description) + } + + if !reflect.DeepEqual(resp.Downtimeconfigurations[0].Rule, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Rule) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Downtimeconfigurations[0].Rule, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Rule) + } + + if !reflect.DeepEqual(resp.Downtimeconfigurations[0].Starttime, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Starttime) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Downtimeconfigurations[0].Starttime, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Starttime) + } + + if !reflect.DeepEqual(resp.Downtimeconfigurations[0].Endtime, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Endtime) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Downtimeconfigurations[0].Endtime, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Endtime) + } + + if !reflect.DeepEqual(resp.Downtimeconfigurations[0].Status, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Status) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Downtimeconfigurations[0].Status, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Status) + } + + if !reflect.DeepEqual(resp.Downtimeconfigurations[0].Createdat, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Createdat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Downtimeconfigurations[0].Createdat, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Createdat) + } + + if !reflect.DeepEqual(resp.Downtimeconfigurations[0].Updatedat, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Updatedat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Downtimeconfigurations[0].Updatedat, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Updatedat) + } + + if !reflect.DeepEqual(resp.Downtimeconfigurations[0].Testsupdatedat, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Testsupdatedat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Downtimeconfigurations[0].Testsupdatedat, inputGetDowntimeConfigurationsV2.Downtimeconfigurations[0].Testsupdatedat) + } + +} + +func verifyDowntimeConfigurationsV2Input(stringInput string) *DowntimeConfigurationsV2Response { + check := &DowntimeConfigurationsV2Response{} + err := json.Unmarshal([]byte(stringInput), check) + if err != nil { + panic(err) + } + return check +} diff --git a/syntheticsclientv2/integration_test.go b/syntheticsclientv2/integration_test.go index 7dbda5c..0f548d8 100644 --- a/syntheticsclientv2/integration_test.go +++ b/syntheticsclientv2/integration_test.go @@ -54,6 +54,8 @@ var ( inputApiCheckV2Data = ApiCheckV2Input{} updateApiCheckV2Body = `{"test":{"active":true,"deviceId":1,"frequency":5, "automaticRetries": 1, "customProperties": [{"key": "Test_Key", "value": "Test Custom Properties"}],"locationIds":["aws-us-east-1"],"name":"a-API-boop-test","schedulingStrategy":"round_robin","requests":[{"configuration":{"name":"Get-Test","requestMethod": "GET","url":"https://api.us1.signalfx.com/v2/synthetics/tests/api/4892","headers":{"X-SF-TOKEN":"jinglebellsbatmanshells", "beep":"boop"},"body":null},"setup":[{"name":"Extract from response body","type":"extract_json","source":"{{response.body}}","extractor":"$.requests","variable":"custom-varz"}],"validations":[{"name":"Assert response code equals 200","type":"assert_numeric","actual":"{{response.code}}","expected":"200","comparator":"equals"}]}]}}` updateApiCheckV2Data = ApiCheckV2Input{} + inputDowntimeConfigurationV2Data = DowntimeConfigurationV2Input{} + updateDowntimeConfigurationV2Data = DowntimeConfigurationV2Input{} ) // You will need to fill in values for the get and delete tests @@ -839,3 +841,104 @@ func TestLiveDeleteLocationV2(t *testing.T) { } } + +func TestLiveDowntimeConfigurationCreateUpdateAndDeleteV2(t *testing.T) { + + //Create your client with the token + c := NewClient(token, realm) + var err error + + //There are restrictions on startTime and endTime for a downtime_configuration so we set the startTime to 10 days + //in the future and the endTime to be 1 hour after the startTime + year, month, day := time.Now().Add(10 * time.Day).Date() + startTime := fmt.Sprintf("%s-%s-%sT20:00:00.000Z", year, int(month), day) + endTime := fmt.Sprintf("%s-%s-%sT21:00:00.000Z", year, int(month), day) + + createDowntimeConfigurationV2Body := fmt.Sprintf("{\"downtimeConfiguration\":{\"name\":\"dc test\",\"description\":\"My super awesome test downtimeConfiguration\",\"rule\":\"augment_data\",\"testIds\":[1111],\"startTime\":\"%s\",\"endTime\":\"%s\"}}", startTime, endTime) + + downtimeConfigId, err := CreateDowntimeConfigurationV2(createDowntimeConfigurationV2Body, c) + if err != nil { + t.Fatal(err) + } + + err = GetDowntimeConfigurationV2(downtimeConfigId, c) + if err != nil { + t.Fatal(err) + } + + updateDowntimeConfigurationV2Body := fmt.Sprintf("{\"downtimeConfiguration\":{\"name\":\"dc test\",\"description\":\"My super awesome test downtimeConfiguration\",\"rule\":\"pause_tests\",\"testIds\":[1111],\"startTime\":\"%s\",\"endTime\":\"%s\"}}", startTime, endTime) + + err = UpdateDowntimeConfigurationV2(downtimeConfigId, updateDowntimeConfigurationV2Body, c) + if err != nil { + t.Fatal(err) + } + + err = GetDowntimeConfigurationV2(downtimeConfigId, c) + if err != nil { + t.Fatal(err) + } + + err = DeleteDowntimeConfigurationV2(downtimeConfigId, c) + if err != nil { + t.Fatal(err) + } + +} + +func CreateDowntimeConfigurationV2(downtimeConfiguration string, c *Client) (int, error) { + + err := json.Unmarshal([]byte(downtimeConfiguration), &inputDowntimeConfigurationV2Data) + if err != nil { + return 0, err + } + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateDowntimeConfigurationV2(&inputDowntimeConfigurationV2Data) + if err != nil { + return 0, err + } + fmt.Println(reqDetail) + JsonPrint(res) + + return res.DowntimeConfiguration.ID, nil +} + +func UpdateDowntimeConfigurationV2(downtimeConfigId int, downtimeConfiguration string, c *Client) error { + + err := json.Unmarshal([]byte(downtimeConfiguration), &updateDowntimeConfigurationV2Data) + if err != nil { + return err + } + + // Make the request with your check settings and print result + res, reqDetail, err := c.UpdateDowntimeConfigurationV2(downtimeConfigId, &updateDowntimeConfigurationV2Data) + if err != nil { + return err + } + fmt.Println(reqDetail) + JsonPrint(res) + + return nil +} + +func GetDowntimeConfigurationV2(downtimeConfigId int, c *Client) error { + // Make the request with your check settings and print result + res, _, err := c.GetDowntimeConfigurationV2(downtimeConfigId) + if err != nil { + return err + } + JsonPrint(res) + + return nil +} + +func DeleteDowntimeConfigurationV2(downtimeConfigId int, c *Client) error { + // Make the request with your check settings and print result + res, err := c.DeleteDowntimeConfigurationV2(downtimeConfigId) + if err != nil { + return err + } + JsonPrint(res) + + return nil +} diff --git a/syntheticsclientv2/update_downtimeconfigurationv2.go b/syntheticsclientv2/update_downtimeconfigurationv2.go new file mode 100644 index 0000000..0e6e2c4 --- /dev/null +++ b/syntheticsclientv2/update_downtimeconfigurationv2.go @@ -0,0 +1,53 @@ +// 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 parseUpdateDowntimeConfigurationV2Response(response string) (*DowntimeConfigurationV2Response, error) { + var updateDowntimeConfigurationV2 DowntimeConfigurationV2Response + if response != "" { + err := json.Unmarshal([]byte(response), &updateDowntimeConfigurationV2) + if err != nil { + return nil, err + } + return &updateDowntimeConfigurationV2, err + } + return &updateDowntimeConfigurationV2, nil +} + +func (c Client) UpdateDowntimeConfigurationV2(id int, DowntimeConfigurationV2Details *DowntimeConfigurationV2Input) (*DowntimeConfigurationV2Response, *RequestDetails, error) { + + body, err := json.Marshal(DowntimeConfigurationV2Details) + if err != nil { + return nil, nil, err + } + + requestDetails, err := c.makePublicAPICall("PUT", fmt.Sprintf("/downtime_configurations/%d", id), bytes.NewBuffer(body), nil) + if err != nil { + return nil, requestDetails, err + } + + updateDowntimeConfigurationV2, err := parseUpdateDowntimeConfigurationV2Response(requestDetails.ResponseBody) + if err != nil { + return updateDowntimeConfigurationV2, requestDetails, err + } + + return updateDowntimeConfigurationV2, requestDetails, nil +} diff --git a/syntheticsclientv2/update_downtimeconfigurationv2_test.go b/syntheticsclientv2/update_downtimeconfigurationv2_test.go new file mode 100644 index 0000000..5e85801 --- /dev/null +++ b/syntheticsclientv2/update_downtimeconfigurationv2_test.go @@ -0,0 +1,80 @@ +//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 ( + updateDowntimeConfigurationV2Body = `{"downtimeConfiguration":{"name":"dc test","description":"My super awesome test downtimeConfiguration","rule":"pause_tests","testIds":[29976],"startTime":"2024-05-16T20:23:00.000Z","endTime":"2024-05-16T20:38:00.000Z"}}` + inputDowntimeConfigurationV2Update = DowntimeConfigurationV2Input{} +) + +func TestUpdateDowntimeConfigurationV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/downtime_configurations/10", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + _, err := w.Write([]byte(updateDowntimeConfigurationV2Body)) + if err != nil { + t.Fatal(err) + } + }) + + err := json.Unmarshal([]byte(updateDowntimeConfigurationV2Body), &inputDowntimeConfigurationV2Update) + if err != nil { + t.Fatal(err) + } + + resp, _, err := testClient.UpdateDowntimeConfigurationV2(10, &inputDowntimeConfigurationV2Update) + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Name, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Name, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Name) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Description, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Description) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Description, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Description) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Rule, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Rule) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Rule, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Rule) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Testids, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Testids) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Testids, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Testids) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Starttime, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Starttime) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Starttime, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Starttime) + } + + if !reflect.DeepEqual(resp.DowntimeConfiguration.Endtime, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Endtime) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.DowntimeConfiguration.Endtime, inputDowntimeConfigurationV2Update.DowntimeConfiguration.Endtime) + } +}