From 8537c0db4a9220ddf9a481432a5b3517aeb01555 Mon Sep 17 00:00:00 2001 From: jeremyh Date: Thu, 17 Nov 2022 09:39:46 -0500 Subject: [PATCH 01/15] update the client and models --- syntheticsclientv2/common_models.go | 528 ++++++++++++++++++++++++++ syntheticsclientv2/synthetics.go | 155 ++++++++ syntheticsclientv2/synthetics_test.go | 94 +++++ 3 files changed, 777 insertions(+) create mode 100644 syntheticsclientv2/common_models.go create mode 100644 syntheticsclientv2/synthetics.go create mode 100644 syntheticsclientv2/synthetics_test.go diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go new file mode 100644 index 0000000..e2437aa --- /dev/null +++ b/syntheticsclientv2/common_models.go @@ -0,0 +1,528 @@ +// Copyright 2021 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 ( + "time" +) + +// Common and shared struct models used for more complex requests +type Links struct { + Self string `json:"self,omitempty"` + SelfHTML string `json:"self_html,omitempty"` + Metrics string `json:"metrics,omitempty"` + LastRun string `json:"last_run,omitempty"` +} + +type Tags []struct { + ID int `json:"id,omitempty"` + Name string `json:"name,omitempty"` +} + +type Status struct { + LastCode int `json:"last_code,omitempty"` + LastMessage string `json:"last_message,omitempty"` + LastResponseTime int `json:"last_response_time,omitempty"` + LastRunAt string `json:"last_run_at,omitempty"` + LastFailureAt string `json:"last_failure_at,omitempty"` + LastAlertAt string `json:"last_alert_at,omitempty"` + HasFailure bool `json:"has_failure,omitempty"` + HasLocationFailure bool `json:"has_location_failure,omitempty"` +} + +type NotifyWho struct { + Sms bool `json:"sms,omitempty"` + Call bool `json:"call,omitempty"` + Email bool `json:"email,omitempty"` + CustomUserEmail string `json:"custom_email"` + Type string `json:"type,omitempty"` + Links Links `json:"links,omitempty"` + ID int `json:"id,omitempty"` +} + +type NotificationWindows []struct { + StartTime string `json:"start_time,omitempty"` + EndTime string `json:"end_time,omitempty"` + DurationInMinutes int `json:"duration_in_minutes,omitempty"` + TimeZone string `json:"time_zone,omitempty"` +} + +type NotificationWindow struct { + StartTime string `json:"start_time,omitempty"` + EndTime string `json:"end_time,omitempty"` + DurationInMinutes int `json:"duration_in_minutes,omitempty"` + TimeZone string `json:"time_zone,omitempty"` +} + +type Escalations struct { + Sms bool `json:"sms,omitempty"` + Email bool `json:"email,omitempty"` + Call bool `json:"call,omitempty"` + AfterMinutes int `json:"after_minutes,omitempty"` + NotifyWho []NotifyWho `json:"notify_who,omitempty"` + IsRepeat bool `json:"is_repeat,omitempty"` + NotificationWindow NotificationWindow `json:"notification_window,omitempty"` +} + +type Notifications struct { + Sms bool `json:"sms,omitempty"` + Email bool `json:"email,omitempty"` + Call bool `json:"call,omitempty"` + NotifyWho []NotifyWho `json:"notify_who,omitempty"` + NotifyAfterFailureCount int `json:"notify_after_failure_count,omitempty"` + NotifyOnLocationFailure bool `json:"notify_on_location_failure,omitempty"` + NotificationWindows NotificationWindows `json:"notification_windows,omitempty"` + Escalations []Escalations `json:"escalations,omitempty"` + Muted bool `json:"muted,omitempty"` +} + +type SuccessCriteria struct { + ActionType string `json:"action_type,omitempty"` + ComparisonString string `json:"comparison_string,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` +} + +type Connection struct { + UploadBandwidth int `json:"upload_bandwidth,omitempty"` + DownloadBandwidth int `json:"download_bandwidth,omitempty"` + Latency int `json:"latency,omitempty"` + PacketLoss float64 `json:"packet_loss,omitempty"` +} + +type Locations []struct { + ID int `json:"id,omitempty"` + Name string `json:"name,omitempty"` + WorldRegion string `json:"world_region,omitempty"` + RegionCode string `json:"region_code,omitempty"` +} + +type Integrations []struct { + ID int `json:"id,omitempty"` + Name string `json:"name,omitempty"` +} + +type HTTPRequestHeaders struct { + UserAgent string `json:"User-Agent,omitempty"` +} + +type Browser struct { + Label string `json:"label,omitempty"` + Code string `json:"code,omitempty"` +} + +type Steps struct { + ItemMethod string `json:"item_method,omitempty"` + Value string `json:"value,omitempty"` + How string `json:"how,omitempty"` + What string `json:"what,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + VariableName string `json:"variable_name,omitempty"` + Name string `json:"name,omitempty"` + Position int `json:"position,omitempty"` +} + +type Cookies struct { + Key string `json:"key,omitempty"` + Value string `json:"value,omitempty"` + Domain string `json:"domain,omitempty"` + Path string `json:"path,omitempty"` +} + +type JavascriptFiles struct { + ID int `json:"id,omitempty"` + Name string `json:"name,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` + Links Links `json:"links,omitempty"` +} + +type ExcludedFiles struct { + ExclusionType string `json:"exclusion_type,omitempty"` + PresetName string `json:"preset_name,omitempty"` + URL string `json:"url,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` +} + +type BlackoutPeriods []struct { + StartDate string `json:"start_date,omitempty"` + EndDate string `json:"end_date,omitempty"` + Timezone string `json:"timezone,omitempty"` + StartTime string `json:"start_time,omitempty"` + EndTime string `json:"end_time,omitempty"` + RepeatType string `json:"repeat_type,omitempty"` + DurationInMinutes int `json:"duration_in_minutes,omitempty"` + IsRepeat bool `json:"is_repeat,omitempty"` + MonthlyRepeatType string `json:"monthly_repeat_type,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` +} + +type Viewport struct { + Height int `json:"height,omitempty"` + Width int `json:"width,omitempty"` +} + +type ThresholdMonitors struct { + Matcher string `json:"matcher,omitempty"` + MetricName string `json:"metric_name,omitempty"` + ComparisonType string `json:"comparison_type,omitempty"` + Value int `json:"value,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` +} + +type DNSOverrides struct { + OriginalDomainCom string `json:"original.domain.com,omitempty"` + OriginalHostCom string `json:"original.host.com,omitempty"` +} + +type Networkconnection struct { + Description string `json:"description,omitempty"` + Downloadbandwidth int `json:"downloadBandwidth,omitempty"` + Latency int `json:"latency,omitempty"` + Packetloss int `json:"packetLoss,omitempty"` + Uploadbandwidth int `json:"uploadBandwidth,omitempty"` +} + +type Advancedsettings struct { + Authentication `json:"authentication"` + Cookiesv2 `json:"cookies"` + BrowserHeaders `json:"headers,omitempty"` + Verifycertificates bool `json:"verifyCertificates,omitempty"` +} + +type Authentication struct { + Password string `json:"password"` + Username string `json:"username"` +} + +type Cookiesv2 []struct { + Key string `json:"key"` + Value string `json:"value"` + Domain string `json:"domain"` + Path string `json:"path"` +} + +type BrowserHeaders []struct { + Name string `json:"name"` + Value string `json:"value"` + Domain string `json:"domain"` +} + +type Transactions []struct { + Name string `json:"name"` + Stepsv2 `json:"steps"` +} + +type BusinessTransactions []struct { + Name string `json:"name"` + BusinessTransactionStepsV2 `json:"steps"` +} + +type Stepsv2 []struct { + Name string `json:"name"` + Selector string `json:"selector,omitempty"` + Selectortype string `json:"selectorType,omitempty"` + Type string `json:"type,omitempty"` + Waitfornav bool `json:"waitForNav,omitempty"` +} + +type BusinessTransactionStepsV2 []struct { + Name string `json:"name"` + Type string `json:"type"` + URL string `json:"url,omitempty"` + Action string `json:"action,omitempty"` + WaitForNav bool `json:"wait_for_nav"` + SelectorType string `json:"selector_type,omitempty"` + Selector string `json:"selector,omitempty"` +} + +type Device struct { + ID int `json:"id,omitempty"` + Label string `json:"label,omitempty"` + Networkconnection `json:"networkConnection,omitempty"` + Viewportheight int `json:"viewportHeight,omitempty"` + Viewportwidth int `json:"viewportWidth,omitempty"` +} + +type Requests []struct { + Configuration `json:"configuration,omitempty"` + Setup `json:"setup,omitempty"` + Validations `json:"validations,omitempty"` +} + +type Configuration struct { + Body string `json:"body"` + Headers `json:"headers,omitempty"` + Name string `json:"name,omitempty"` + Requestmethod string `json:"requestMethod,omitempty"` + URL string `json:"url,omitempty"` +} + +type Headers map[string]interface{} + +type Setup []struct { + Extractor string `json:"extractor,omitempty"` + Name string `json:"name,omitempty"` + Source string `json:"source,omitempty"` + Type string `json:"type,omitempty"` + Variable string `json:"variable,omitempty"` +} + +type Validations []struct { + Actual string `json:"actual,omitempty"` + Comparator string `json:"comparator,omitempty"` + Expected string `json:"expected,omitempty"` + Name string `json:"name,omitempty"` + Type string `json:"type,omitempty"` +} + +type Checks []struct { + ID int `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + Frequency int `json:"frequency"` + Paused bool `json:"paused"` + Muted bool `json:"muted"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + Links Links `json:"links"` + Status Status `json:"status"` + Tags Tags `json:"tags"` +} + +type Tests []struct { + Active bool `json:"active"` + Createdat time.Time `json:"createdAt"` + Frequency int `json:"frequency"` + ID int `json:"id"` + Locationids []string `json:"locationIds"` + Name string `json:"name"` + Schedulingstrategy string `json:"schedulingStrategy"` + Type string `json:"type"` + Updatedat time.Time `json:"updatedAt"` +} + +type Errors []struct { + Title string `json:"title,omitempty"` + Description string `json:"description,omitempty"` +} + +type DeleteCheck struct { + Result string `json:"result"` + Message string `json:"message"` + Errors Errors `json:"errors"` +} + +type ChecksV2Response struct { + Nextpagelink int `json:"nextPageLink"` + Perpage int `json:"perPage"` + Tests `json:"tests"` + Totalcount int `json:"totalCount"` +} + +type ApiCheckV2Input struct { + Test struct { + Active bool `json:"active"` + Deviceid int `json:"device_id"` + Frequency int `json:"frequency"` + Locationids []string `json:"location_ids"` + Name string `json:"name"` + Requests `json:"requests"` + Schedulingstrategy string `json:"schedulingStrategy"` + } `json:"test"` +} + +type ApiCheckV2Response struct { + Test struct { + Active bool `json:"active,omitempty"` + Createdat time.Time `json:"createdAt,omitempty"` + Device `json:"device,omitempty"` + Frequency int `json:"frequency,omitempty"` + ID int `json:"id,omitempty"` + Locationids []string `json:"location_ids,omitempty"` + Name string `json:"name,omitempty"` + Requests `json:"requests,omitempty"` + Schedulingstrategy string `json:"schedulingStrategy,omitempty"` + Type string `json:"type,omitempty"` + Updatedat time.Time `json:"updatedAt,omitempty"` + } +} + +type BrowserCheckV2Input struct { + Test struct { + Name string `json:"name"` + BusinessTransactions `json:"business_transactions"` + Urlprotocol string `json:"urlProtocol"` + Starturl string `json:"startUrl"` + LocationIds []string `json:"location_ids"` + DeviceID int `json:"device_id"` + Frequency int `json:"frequency"` + Schedulingstrategy string `json:"scheduling_strategy"` + Active bool `json:"active"` + Advancedsettings `json:"advanced_settings"` + } `json:"test"` +} + +type BrowserCheckV2Response struct { + Test struct { + Active bool `json:"active"` + Advancedsettings `json:"advanced_settings"` + BusinessTransactions `json:"business_transactions"` + Createdat time.Time `json:"createdAt"` + Device `json:"device"` + Frequency int `json:"frequency"` + ID int `json:"id"` + Locationids []string `json:"location_ids"` + Name string `json:"name"` + Schedulingstrategy string `json:"scheduling_strategy"` + Transactions `json:"transactions"` + Type string `json:"type"` + Updatedat time.Time `json:"updatedAt"` + } `json:"test"` +} + +type HttpCheckInput struct { + ID int `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Type string `json:"type,omitempty"` + Frequency int `json:"frequency,omitempty"` + Paused bool `json:"paused,omitempty"` + Muted bool `json:"muted,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` + Links Links `json:"links,omitempty"` + Tags []string `json:"tags"` + Status Status `json:"status,omitempty"` + RoundRobin bool `json:"round_robin,omitempty"` + AutoRetry bool `json:"auto_retry,omitempty"` + Enabled bool `json:"enabled,omitempty"` + BlackoutPeriods BlackoutPeriods `json:"blackout_periods,omitempty"` + Locations []int `json:"locations,omitempty"` + Integrations []int `json:"integrations,omitempty"` + HTTPRequestHeaders HTTPRequestHeaders `json:"http_request_headers,omitempty"` + HTTPRequestBody string `json:"http_request_body,omitempty"` + Notifications Notifications `json:"notifications,omitempty"` + URL string `json:"url,omitempty"` + HTTPMethod string `json:"http_method,omitempty"` + SuccessCriteria []SuccessCriteria `json:"success_criteria,omitempty"` + Connection Connection `json:"connection,omitempty"` +} + +type HttpCheckResponse struct { + ID int `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Type string `json:"type,omitempty"` + Frequency int `json:"frequency,omitempty"` + Paused bool `json:"paused,omitempty"` + Muted bool `json:"muted,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` + Links Links `json:"links,omitempty"` + Tags Tags `json:"tags,omitempty"` + Status Status `json:"status,omitempty"` + RoundRobin bool `json:"round_robin,omitempty"` + AutoRetry bool `json:"auto_retry,omitempty"` + Enabled bool `json:"enabled,omitempty"` + BlackoutPeriods BlackoutPeriods `json:"blackout_periods,omitempty"` + Locations Locations `json:"locations,omitempty"` + Integrations Integrations `json:"integrations,omitempty"` + HTTPRequestHeaders HTTPRequestHeaders `json:"http_request_headers,omitempty"` + HTTPRequestBody string `json:"http_request_body,omitempty"` + Notifications Notifications `json:"notifications,omitempty"` + URL string `json:"url,omitempty"` + HTTPMethod string `json:"http_method,omitempty"` + SuccessCriteria []SuccessCriteria `json:"success_criteria,omitempty"` + Connection Connection `json:"connection,omitempty"` +} + +type BrowserCheckInput struct { + ID int `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Type string `json:"type,omitempty"` + Frequency int `json:"frequency,omitempty"` + Paused bool `json:"paused,omitempty"` + Muted bool `json:"muted,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` + Links Links `json:"links,omitempty"` + Tags []string `json:"tags"` + Status Status `json:"status,omitempty"` + RoundRobin bool `json:"round_robin,omitempty"` + AutoRetry bool `json:"auto_retry,omitempty"` + Enabled bool `json:"enabled,omitempty"` + BlackoutPeriods BlackoutPeriods `json:"blackout_periods,omitempty"` + Locations []int `json:"locations,omitempty"` + Integrations []int `json:"integrations,omitempty"` + HTTPRequestHeaders HTTPRequestHeaders `json:"http_request_headers,omitempty"` + HTTPRequestBody string `json:"http_request_body,omitempty"` + HTTPMethod string `json:"http_method,omitempty"` + Notifications Notifications `json:"notifications,omitempty"` + URL string `json:"url,omitempty"` + UserAgent string `json:"user_agent,omitempty"` + AutoUpdateUserAgent bool `json:"auto_update_user_agent,omitempty"` + Browser Browser `json:"browser,omitempty"` + Steps []Steps `json:"steps,omitempty"` + Cookies []Cookies `json:"cookies,omitempty"` + JavascriptFiles []JavascriptFiles `json:"javascript_files,omitempty"` + ExcludedFiles []ExcludedFiles `json:"excluded_files,omitempty"` + Viewport Viewport `json:"viewport,omitempty"` + EnforceSslValidation bool `json:"enforce_ssl_validation,omitempty"` + ThresholdMonitors []ThresholdMonitors `json:"threshold_monitors,omitempty"` + DNSOverrides DNSOverrides `json:"dns_overrides,omitempty"` + Connection Connection `json:"connection,omitempty"` + WaitForFullMetrics bool `json:"wait_for_full_metrics,omitempty"` +} + +type BrowserCheckResponse struct { + ID int `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Type string `json:"type,omitempty"` + Frequency int `json:"frequency,omitempty"` + Paused bool `json:"paused,omitempty"` + Muted bool `json:"muted,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` + Links Links `json:"links,omitempty"` + Tags Tags `json:"tags,omitempty"` + Status Status `json:"status,omitempty"` + RoundRobin bool `json:"round_robin,omitempty"` + AutoRetry bool `json:"auto_retry,omitempty"` + Enabled bool `json:"enabled,omitempty"` + BlackoutPeriods BlackoutPeriods `json:"blackout_periods,omitempty"` + Locations Locations `json:"locations,omitempty"` + Integrations Integrations `json:"integrations,omitempty"` + HTTPRequestHeaders HTTPRequestHeaders `json:"http_request_headers,omitempty"` + HTTPRequestBody string `json:"http_request_body,omitempty"` + HTTPMethod string `json:"http_method,omitempty"` + Notifications Notifications `json:"notifications,omitempty"` + URL string `json:"url,omitempty"` + UserAgent string `json:"user_agent,omitempty"` + AutoUpdateUserAgent bool `json:"auto_update_user_agent,omitempty"` + Browser Browser `json:"browser,omitempty"` + Steps []Steps `json:"steps,omitempty"` + Cookies []Cookies `json:"cookies,omitempty"` + JavascriptFiles []JavascriptFiles `json:"javascript_files,omitempty"` + ExcludedFiles []ExcludedFiles `json:"excluded_files,omitempty"` + Viewport Viewport `json:"viewport,omitempty"` + EnforceSslValidation bool `json:"enforce_ssl_validation,omitempty"` + ThresholdMonitors []ThresholdMonitors `json:"threshold_monitors,omitempty"` + DNSOverrides DNSOverrides `json:"dns_overrides,omitempty"` + Connection Connection `json:"connection,omitempty"` + WaitForFullMetrics bool `json:"wait_for_full_metrics,omitempty"` +} diff --git a/syntheticsclientv2/synthetics.go b/syntheticsclientv2/synthetics.go new file mode 100644 index 0000000..70331dd --- /dev/null +++ b/syntheticsclientv2/synthetics.go @@ -0,0 +1,155 @@ +// Copyright 2021 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" + "errors" + "fmt" + "io" + "io/ioutil" + "net/http" + "net/http/httputil" + "time" +) + +type Client struct { + publicBaseURL string + apiKey string + realm string + httpClient http.Client +} + +type ClientArgs struct { + timeoutSeconds int + publicBaseUrl string +} + +type RequestDetails struct { + StatusCode int + ResponseBody string + RequestBody string + RawResponse *http.Response + RawRequest *http.Request +} + +type errorResponse struct { + Status string `json:"status,omitempty"` + Error string `json:"error,omitempty"` + Result string `json:"result,omitempty"` + Message string `json:"message,omitempty"` + Errors Errors `json:"errors,omitempty"` +} + +func (c Client) String() string { + return fmt.Sprintf("Splunk Synthetics Client: URL: %s ", c.publicBaseURL) +} + +func (c Client) makePublicAPICall(method string, endpoint string, requestBody io.Reader, queryParams map[string]string) (*RequestDetails, error) { + details := RequestDetails{} + // Create the request + req, err := http.NewRequest(method, c.publicBaseURL+endpoint, requestBody) + if err != nil { + return &details, err + } + + // Set the auth headers needed for the public api + req.Header.Set("X-SF-TOKEN", c.apiKey) + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + + // Set the query params + q := req.URL.Query() + for key, value := range queryParams { + q.Add(key, value) + } + req.URL.RawQuery = q.Encode() + + // Add the request to the details + details.RawRequest = req + requestDump, err := httputil.DumpRequestOut(req, true) + if err != nil { + return &details, err + } + details.RequestBody = string(requestDump) + fmt.Println("************") + fmt.Println(details.RequestBody) + fmt.Println("************") + + // Make the request + resp, err := c.httpClient.Do(req) + if err != nil { + return &details, err + } + + if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest { + var errRes errorResponse + if err = json.NewDecoder(resp.Body).Decode(&errRes); err == nil { + errorField, err2 := json.Marshal(errRes) + if err2 != nil { + return &details, fmt.Errorf("unknown issue while parsing API error response, status code: %d", resp.StatusCode) + } + return &details, errors.New("Status Code: " + resp.Status + "\n" + string(errorField)) + } + return &details, fmt.Errorf("unknown error, status code: %d", resp.StatusCode) + } + + responseBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + return &details, err + } + + details.StatusCode = resp.StatusCode + details.ResponseBody = string(responseBody) + details.RawResponse = resp + + return &details, nil +} + +func NewClient(apiKey string, realm string) *Client { + args := ClientArgs{timeoutSeconds: 30} + return NewConfigurableClient(apiKey, realm, args) +} + +func NewConfigurableClient(apiKey string, realm string, args ClientArgs) *Client { + client := Client{ + apiKey: apiKey, + httpClient: http.Client{Timeout: time.Duration(args.timeoutSeconds) * time.Second}, + } + if args.publicBaseUrl == "" { + client.publicBaseURL = "https://api." + realm + ".signalfx.com/v2/synthetics/" + } else { + client.publicBaseURL = args.publicBaseUrl + } + + return &client +} + +// GetHTTPClient returns http client for the purpose of test +func (c Client) GetHTTPClient() *http.Client { + return &c.httpClient +} + +// Helper for tests and output +func JsonPrint(data interface{}) { + var p []byte + p, err := json.Marshal(data) + if err != nil { + fmt.Println(err) + return + } + fmt.Printf("%s \n", p) +} diff --git a/syntheticsclientv2/synthetics_test.go b/syntheticsclientv2/synthetics_test.go new file mode 100644 index 0000000..6684a4e --- /dev/null +++ b/syntheticsclientv2/synthetics_test.go @@ -0,0 +1,94 @@ +// Copyright 2021 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 ( + "log" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +var ( + // mux is the HTTP request multiplexer used with the test server. + testMux *http.ServeMux + + testClient *Client + + // server is a test HTTP server used to provide mock API responses. + testServer *httptest.Server +) + +func setup() { + testMux = http.NewServeMux() + testServer = httptest.NewServer(testMux) + + testClient = NewConfigurableClient("apiKey", "realm", ClientArgs{ + publicBaseUrl: testServer.URL, + }) + log.Printf("Client instantiated: %s", testClient.publicBaseURL) +} + +func teardown() { + testServer.Close() +} + +func testMethod(t *testing.T, r *http.Request, want string) { + if got := r.Method; got != want { + t.Errorf("Request method: %v, want %v", got, want) + } +} + +func TestConfigurableClient(t *testing.T) { + testMux = http.NewServeMux() + testServer = httptest.NewServer(testMux) + args := ClientArgs{ + timeoutSeconds: 30, + publicBaseUrl: testServer.URL, + } + + testConfigurableClient := NewConfigurableClient("snakedonut", "realm-o", args) + log.Printf("Client instantiated: %s", testServer.URL) + if testConfigurableClient.GetHTTPClient() == nil { + t.Errorf("http client is nil") + } + if testConfigurableClient.apiKey != "snakedonut" { + t.Errorf("apiKey was not correctly passed") + } + if testConfigurableClient.apiKey != "realm-o" { + t.Errorf("apiKey was not correctly passed") + } +} + +func TestConfigurableClientTimeout(t *testing.T) { + testMux = http.NewServeMux() + testServer = httptest.NewServer(testMux) + + testMux.HandleFunc("/v2/checks/12", func(w http.ResponseWriter, r *http.Request) { + time.Sleep(2 * time.Second) + }) + + testConfigurableClient := NewConfigurableClient("apiKey", "realm", ClientArgs{ + timeoutSeconds: 1, + publicBaseUrl: testServer.URL, + }) + log.Printf("Client instantiated: %s", testServer.URL) + _, _, err := testConfigurableClient.GetCheck(12) + if !strings.Contains(err.Error(), "context deadline exceeded (Client.Timeout exceeded while awaiting headers)") { + t.Errorf("expected to see timeout error, but saw: %s", err.Error()) + } +} From 02b1f0941b1b7bf3e0d50ea41722c12fa5e92206 Mon Sep 17 00:00:00 2001 From: jeremyh Date: Thu, 17 Nov 2022 09:40:22 -0500 Subject: [PATCH 02/15] api checks v2 --- syntheticsclientv2/create_apicheckv2.go | 53 +++++++++ syntheticsclientv2/create_apicheckv2_test.go | 107 +++++++++++++++++ syntheticsclientv2/delete_apicheckv2.go | 40 +++++++ syntheticsclientv2/delete_apicheckv2_test.go | 72 ++++++++++++ syntheticsclientv2/get_apicheckv2.go | 52 +++++++++ syntheticsclientv2/get_apicheckv2_test.go | 116 +++++++++++++++++++ syntheticsclientv2/update_apicheckv2.go | 51 ++++++++ syntheticsclientv2/update_apicheckv2_test.go | 73 ++++++++++++ 8 files changed, 564 insertions(+) create mode 100644 syntheticsclientv2/create_apicheckv2.go create mode 100644 syntheticsclientv2/create_apicheckv2_test.go create mode 100644 syntheticsclientv2/delete_apicheckv2.go create mode 100644 syntheticsclientv2/delete_apicheckv2_test.go create mode 100644 syntheticsclientv2/get_apicheckv2.go create mode 100644 syntheticsclientv2/get_apicheckv2_test.go create mode 100644 syntheticsclientv2/update_apicheckv2.go create mode 100644 syntheticsclientv2/update_apicheckv2_test.go diff --git a/syntheticsclientv2/create_apicheckv2.go b/syntheticsclientv2/create_apicheckv2.go new file mode 100644 index 0000000..14ea93f --- /dev/null +++ b/syntheticsclientv2/create_apicheckv2.go @@ -0,0 +1,53 @@ +// Copyright 2021 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 parseCreateApiCheckV2Response(response string) (*ApiCheckV2Response, error) { + + var createApiCheckV2 ApiCheckV2Response + JSONResponse := []byte(response) + err := json.Unmarshal(JSONResponse, &createApiCheckV2) + if err != nil { + return nil, err + } + + return &createApiCheckV2, err +} + +func (c Client) CreateApiCheckV2(ApiCheckV2Details *ApiCheckV2Input) (*ApiCheckV2Response, *RequestDetails, error) { + + body, err := json.Marshal(ApiCheckV2Details) + if err != nil { + return nil, nil, err + } + + details, err := c.makePublicAPICall("POST", "/tests/api", bytes.NewBuffer(body), nil) + if err != nil { + return nil, details, err + } + + newApiCheckV2, err := parseCreateApiCheckV2Response(details.ResponseBody) + if err != nil { + return newApiCheckV2, details, err + } + + return newApiCheckV2, details, nil +} diff --git a/syntheticsclientv2/create_apicheckv2_test.go b/syntheticsclientv2/create_apicheckv2_test.go new file mode 100644 index 0000000..56c410f --- /dev/null +++ b/syntheticsclientv2/create_apicheckv2_test.go @@ -0,0 +1,107 @@ +// Copyright 2021 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 ( + "net/http" + "reflect" + "testing" + "fmt" + "encoding/json" + "os" +) + +var ( + createApiV2Body = `{"test":{"active":true,"device_id":1,"frequency":5,"location_ids":["aws-us-east-1"],"name":"boop-test","scheduling_strategy":"round_robin","requests":[{"configuration":{"name":"Get-Test","requestMethod":"GET","url":"https://api.us1.signalfx.com/v2/synthetics/tests/api/489","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"}]}]}}` + inputData = ApiCheckV2Input{} +) + +func TestCreateApiCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/api", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + w.Write([]byte(createApiV2Body)) + }) + + json.Unmarshal([]byte(createApiV2Body), &inputData) + + resp, _, err := testClient.CreateApiCheckV2(&inputData) + + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.Test.Name, inputData.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputData.Test.Name) + } + + if !reflect.DeepEqual(resp.Test.Active, inputData.Test.Active) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputData.Test.Active) + } + + if !reflect.DeepEqual(resp.Test.Locationids, inputData.Test.Locationids) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Locationids, inputData.Test.Locationids) + } + + if !reflect.DeepEqual(resp.Test.Frequency, inputData.Test.Frequency) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputData.Test.Frequency) + } + + if !reflect.DeepEqual(resp.Test.Requests, inputData.Test.Requests) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Requests, inputData.Test.Requests) + } + + if !reflect.DeepEqual(resp.Test.Schedulingstrategy, inputData.Test.Schedulingstrategy) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Schedulingstrategy, inputData.Test.Schedulingstrategy) + } + +} + + +func TestLiveCreateApiCheckV2(t *testing.T) { + setup() + defer teardown() + + json.Unmarshal([]byte(createApiV2Body), &inputData) + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateApiCheckV2(&inputData) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} \ No newline at end of file diff --git a/syntheticsclientv2/delete_apicheckv2.go b/syntheticsclientv2/delete_apicheckv2.go new file mode 100644 index 0000000..dd3a7cd --- /dev/null +++ b/syntheticsclientv2/delete_apicheckv2.go @@ -0,0 +1,40 @@ +// Copyright 2021 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) DeleteApiCheckV2(id int) (int, error) { + requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/tests/api/%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_apicheckv2_test.go b/syntheticsclientv2/delete_apicheckv2_test.go new file mode 100644 index 0000000..7f373e9 --- /dev/null +++ b/syntheticsclientv2/delete_apicheckv2_test.go @@ -0,0 +1,72 @@ +// Copyright 2021 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 ( + "net/http" + "testing" + "os" + "fmt" +) + +var ( + deleteApiCheckV2RespBody = `` +) + +func TestDeleteApiCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/api/19", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.Write([]byte(deleteApiCheckV2RespBody)) + }) + + resp, err := testClient.DeleteApiCheckV2(19) + if err != nil { + fmt.Println(resp) + t.Fatal(err) + } + fmt.Println(resp) +} + +func TestLiveDeleteApiCheckV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, err := c.DeleteApiCheckV2(1093) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} \ No newline at end of file diff --git a/syntheticsclientv2/get_apicheckv2.go b/syntheticsclientv2/get_apicheckv2.go new file mode 100644 index 0000000..8493af7 --- /dev/null +++ b/syntheticsclientv2/get_apicheckv2.go @@ -0,0 +1,52 @@ +// Copyright 2021 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 parseApiCheckV2Response(response string) (*ApiCheckV2Response, error) { + // Parse the response and return the check object + var check ApiCheckV2Response + err := json.Unmarshal([]byte(response), &check) + if err != nil { + return nil, err + } + + return &check, err +} + +func (c Client) GetApiCheckV2(id int) (*ApiCheckV2Response, *RequestDetails, error) { + + details, err := c.makePublicAPICall("GET", + fmt.Sprintf("/tests/api/%d", id), + bytes.NewBufferString("{}"), + nil) + + if err != nil { + return nil, details, err + } + + check, err := parseApiCheckV2Response(details.ResponseBody) + if err != nil { + return check, details, err + } + + return check, details, nil +} diff --git a/syntheticsclientv2/get_apicheckv2_test.go b/syntheticsclientv2/get_apicheckv2_test.go new file mode 100644 index 0000000..feb5ab1 --- /dev/null +++ b/syntheticsclientv2/get_apicheckv2_test.go @@ -0,0 +1,116 @@ +// Copyright 2021 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" + "os" + "fmt" +) + +var ( + getApiCheckV2Body = `{"test":{"active":true,"createdAt":"0001-01-01T00:00:00Z","device":{"id":1,"label":"Desktop","networkConnection":{}},"frequency":5,"id":489,"name":"Appinspect login API","requests":[{"configuration":{"body":"","headers":{},"name":"Login","requestMethod":"GET","url":"https://api.splunk.com/2.0/rest/login/splunk"}}],"type":"api","updatedAt":"0001-01-01T00:00:00Z"}}` + inputGetApiCheckV2 = verifyApiCheckV2Input(string(getApiCheckV2Body)) +) + +func TestGetApiCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/api/489", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.Write([]byte(getApiCheckV2Body)) + }) + + resp, _, err := testClient.GetApiCheckV2(489) + + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(resp.Test.ID, inputGetApiCheckV2.Test.ID) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.ID, inputGetApiCheckV2.Test.ID) + } + + if !reflect.DeepEqual(resp.Test.Name, inputGetApiCheckV2.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputGetApiCheckV2.Test.Name) + } + + if !reflect.DeepEqual(resp.Test.Type, inputGetApiCheckV2.Test.Type) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Type, inputGetApiCheckV2.Test.Type) + } + + if !reflect.DeepEqual(resp.Test.Frequency, inputGetApiCheckV2.Test.Frequency) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputGetApiCheckV2.Test.Frequency) + } + + if !reflect.DeepEqual(resp.Test.Active, inputGetApiCheckV2.Test.Active) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputGetApiCheckV2.Test.Active) + } + + if !reflect.DeepEqual(resp.Test.Createdat, inputGetApiCheckV2.Test.Createdat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Createdat, inputGetApiCheckV2.Test.Createdat) + } + + if !reflect.DeepEqual(resp.Test.Updatedat, inputGetApiCheckV2.Test.Updatedat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Updatedat, inputGetApiCheckV2.Test.Updatedat) + } + + if !reflect.DeepEqual(resp.Test.Device, inputGetApiCheckV2.Test.Device) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Device, inputGetApiCheckV2.Test.Device) + } + + if !reflect.DeepEqual(resp.Test.Requests, inputGetApiCheckV2.Test.Requests) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Requests, inputGetApiCheckV2.Test.Requests) + } + +} + +func verifyApiCheckV2Input(stringInput string) *ApiCheckV2Response { + check := &ApiCheckV2Response{} + err := json.Unmarshal([]byte(stringInput), check) + if err != nil { + panic(err) + } + return check +} + +func TestLiveGetApiCheckV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetApiCheckV2(489) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} diff --git a/syntheticsclientv2/update_apicheckv2.go b/syntheticsclientv2/update_apicheckv2.go new file mode 100644 index 0000000..e914542 --- /dev/null +++ b/syntheticsclientv2/update_apicheckv2.go @@ -0,0 +1,51 @@ +// Copyright 2021 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 parseUpdateApiCheckV2Response(response string) (*ApiCheckV2Response, error) { + var updateApiCheckV2 ApiCheckV2Response + err := json.Unmarshal([]byte(response), &updateApiCheckV2) + if err != nil { + return nil, err + } + + return &updateApiCheckV2, err +} + +func (c Client) UpdateApiCheckV2(id int, ApiCheckV2Details *ApiCheckV2Input) (*ApiCheckV2Response, *RequestDetails, error) { + + body, err := json.Marshal(ApiCheckV2Details) + if err != nil { + return nil, nil, err + } + + requestDetails, err := c.makePublicAPICall("PUT", fmt.Sprintf("/tests/api/%d", id), bytes.NewBuffer(body), nil) + if err != nil { + return nil, requestDetails, err + } + + updateApiCheckV2, err := parseUpdateApiCheckV2Response(requestDetails.ResponseBody) + if err != nil { + return updateApiCheckV2, requestDetails, err + } + + return updateApiCheckV2, requestDetails, nil +} diff --git a/syntheticsclientv2/update_apicheckv2_test.go b/syntheticsclientv2/update_apicheckv2_test.go new file mode 100644 index 0000000..a6f197b --- /dev/null +++ b/syntheticsclientv2/update_apicheckv2_test.go @@ -0,0 +1,73 @@ +// Copyright 2021 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 ( + updateApiCheckV2Body = `{"test":{"active":true,"device_id":4,"frequency":5,"location_ids":["aws-us-east-1","aws-ap-northeast-1"],"name":"boop-test","scheduling_strategy":"round_robin","requests":[{"configuration":{"name":"Get-Test","requestMethod":"GET","url":"https://api.us1.signalfx.com/v2/synthetics/tests/api/489","headers":{"beep":"boop","X-SF-TOKEN":"jinglebellsbatmanshells"},"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"}]}]}}` + inputApiCheckV2Update = ApiCheckV2Input{} +) + +func TestUpdateApiCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/api/10", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + w.Write([]byte(updateApiCheckV2Body)) + }) + + json.Unmarshal([]byte(updateApiCheckV2Body), &inputApiCheckV2Update) + + resp, _, err := testClient.UpdateApiCheckV2(10, &inputApiCheckV2Update) + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.Test.Name, inputApiCheckV2Update.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputApiCheckV2Update.Test.Name) + } + + if !reflect.DeepEqual(resp.Test.Active, inputApiCheckV2Update.Test.Active) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputApiCheckV2Update.Test.Active) + } + + if !reflect.DeepEqual(resp.Test.Locationids, inputApiCheckV2Update.Test.Locationids) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Locationids, inputApiCheckV2Update.Test.Locationids) + } + + if !reflect.DeepEqual(resp.Test.Frequency, inputApiCheckV2Update.Test.Frequency) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputApiCheckV2Update.Test.Frequency) + } + + if !reflect.DeepEqual(resp.Test.Requests, inputApiCheckV2Update.Test.Requests) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Requests, inputApiCheckV2Update.Test.Requests) + } + + if !reflect.DeepEqual(resp.Test.Schedulingstrategy, inputApiCheckV2Update.Test.Schedulingstrategy) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Schedulingstrategy, inputApiCheckV2Update.Test.Schedulingstrategy) + } + +} + From f0f4669a73f68b97e8be7bdde8d0c5bbd6b08f36 Mon Sep 17 00:00:00 2001 From: jeremyh Date: Thu, 17 Nov 2022 09:40:44 -0500 Subject: [PATCH 03/15] browser checks v2 --- syntheticsclientv2/create_browsercheckv2.go | 52 ++++++++ .../create_browsercheckv2_test.go | 111 ++++++++++++++++ syntheticsclientv2/delete_browsercheckv2.go | 40 ++++++ .../delete_browsercheckv2_test.go | 72 +++++++++++ syntheticsclientv2/get_browsercheckv2.go | 48 +++++++ syntheticsclientv2/get_browsercheckv2_test.go | 120 ++++++++++++++++++ syntheticsclientv2/update_browsercheckv2.go | 51 ++++++++ .../update_browsercheckv2_test.go | 53 ++++++++ 8 files changed, 547 insertions(+) create mode 100644 syntheticsclientv2/create_browsercheckv2.go create mode 100644 syntheticsclientv2/create_browsercheckv2_test.go create mode 100644 syntheticsclientv2/delete_browsercheckv2.go create mode 100644 syntheticsclientv2/delete_browsercheckv2_test.go create mode 100644 syntheticsclientv2/get_browsercheckv2.go create mode 100644 syntheticsclientv2/get_browsercheckv2_test.go create mode 100644 syntheticsclientv2/update_browsercheckv2.go create mode 100644 syntheticsclientv2/update_browsercheckv2_test.go diff --git a/syntheticsclientv2/create_browsercheckv2.go b/syntheticsclientv2/create_browsercheckv2.go new file mode 100644 index 0000000..f6ebdd6 --- /dev/null +++ b/syntheticsclientv2/create_browsercheckv2.go @@ -0,0 +1,52 @@ +// Copyright 2021 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 parseCreateBrowserCheckV2Response(response string) (*BrowserCheckV2Response, error) { + + var createBrowserCheckV2 BrowserCheckV2Response + JSONResponse := []byte(response) + err := json.Unmarshal(JSONResponse, &createBrowserCheckV2) + if err != nil { + return nil, err + } + + return &createBrowserCheckV2, err +} + +func (c Client) CreateBrowserCheckV2(browserCheckV2Details *BrowserCheckV2Input) (*BrowserCheckV2Response, *RequestDetails, error) { + + body, err := json.Marshal(browserCheckV2Details) + if err != nil { + return nil, nil, err + } + + details, err := c.makePublicAPICall("POST", "/tests/browser", bytes.NewBuffer(body), nil) + if err != nil { + return nil, details, err + } + + newBrowserCheckV2, err := parseCreateBrowserCheckV2Response(details.ResponseBody) + if err != nil { + return newBrowserCheckV2, details, err + } + + return newBrowserCheckV2, details, nil +} diff --git a/syntheticsclientv2/create_browsercheckv2_test.go b/syntheticsclientv2/create_browsercheckv2_test.go new file mode 100644 index 0000000..c6777dd --- /dev/null +++ b/syntheticsclientv2/create_browsercheckv2_test.go @@ -0,0 +1,111 @@ +// Copyright 2021 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 ( + "net/http" + "reflect" + "testing" + "fmt" + "encoding/json" + "os" +) + +var ( + createBrowserCheckV2Body = `{"test":{"name":"browser-beep-test","business_transactions":[{"name":"Synthetic transaction 1","steps":[{"name":"Go to URL","type":"go_to_url","url":"https://www.splunk.com","action":"go_to_url","wait_for_nav":true},{"name":"Nexter step","type":"click_element","selector_type":"id","wait_for_nav":false,"selector":"free-splunk-click-desktop"}]}],"urlProtocol":"https://","startUrl":"www.splunk.com","location_ids":["aws-us-east-1"],"device_id":1,"frequency":5,"scheduling_strategy":"round_robin","active":true,"advanced_settings":{"verify_certificates":true,"authentication":{"username":"boopuser","password":"{{env.beep-var}}"},"headers":[{"name":"batman","value":"Agentoz","domain":"www.batmansagent.com"}],"cookies":[{"key":"super","value":"duper","domain":"www.batmansagent.com","path":"/boom/goes/beep"}]}}}` + inputBrowserCheckV2Data = BrowserCheckV2Input{} +) + +func TestCreateBrowserCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/browser", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + w.Write([]byte(createBrowserCheckV2Body)) + }) + + json.Unmarshal([]byte(createBrowserCheckV2Body), &inputBrowserCheckV2Data) + + resp, _, err := testClient.CreateBrowserCheckV2(&inputBrowserCheckV2Data) + + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.Test.Name, inputBrowserCheckV2Data.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputBrowserCheckV2Data.Test.Name) + } + + if !reflect.DeepEqual(resp.Test.Active, inputBrowserCheckV2Data.Test.Active) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputBrowserCheckV2Data.Test.Active) + } + + if !reflect.DeepEqual(resp.Test.Locationids, inputBrowserCheckV2Data.Test.LocationIds) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Locationids, inputBrowserCheckV2Data.Test.LocationIds) + } + + if !reflect.DeepEqual(resp.Test.Frequency, inputBrowserCheckV2Data.Test.Frequency) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputBrowserCheckV2Data.Test.Frequency) + } + + if !reflect.DeepEqual(resp.Test.BusinessTransactions, inputBrowserCheckV2Data.Test.BusinessTransactions) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.BusinessTransactions, inputBrowserCheckV2Data.Test.BusinessTransactions) + } + + if !reflect.DeepEqual(resp.Test.Advancedsettings, inputBrowserCheckV2Data.Test.Advancedsettings) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Advancedsettings, inputBrowserCheckV2Data.Test.Advancedsettings) + } + + if !reflect.DeepEqual(resp.Test.Schedulingstrategy, inputBrowserCheckV2Data.Test.Schedulingstrategy) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Schedulingstrategy, inputBrowserCheckV2Data.Test.Schedulingstrategy) + } + +} + + +func TestLiveCreateBrowserCheckV2(t *testing.T) { + setup() + defer teardown() + + json.Unmarshal([]byte(createBrowserCheckV2Body), &inputBrowserCheckV2Data) + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputBrowserCheckV2Data) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateBrowserCheckV2(&inputBrowserCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} \ No newline at end of file diff --git a/syntheticsclientv2/delete_browsercheckv2.go b/syntheticsclientv2/delete_browsercheckv2.go new file mode 100644 index 0000000..8db02ec --- /dev/null +++ b/syntheticsclientv2/delete_browsercheckv2.go @@ -0,0 +1,40 @@ +// Copyright 2021 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) DeleteBrowserCheckV2(id int) (int, error) { + requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/tests/browser/%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_browsercheckv2_test.go b/syntheticsclientv2/delete_browsercheckv2_test.go new file mode 100644 index 0000000..e7f6d2c --- /dev/null +++ b/syntheticsclientv2/delete_browsercheckv2_test.go @@ -0,0 +1,72 @@ +// Copyright 2021 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 ( + "net/http" + "testing" + "os" + "fmt" +) + +var ( + deleteBrowserCheckV2RespBody = `` +) + +func TestDeleteBrowserCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/browser/19", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.Write([]byte(deleteBrowserCheckV2RespBody)) + }) + + resp, err := testClient.DeleteBrowserCheckV2(19) + if err != nil { + fmt.Println(resp) + t.Fatal(err) + } + fmt.Println(resp) +} + +func TestLiveDeleteBrowserCheckV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, err := c.DeleteBrowserCheckV2(1115) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} \ No newline at end of file diff --git a/syntheticsclientv2/get_browsercheckv2.go b/syntheticsclientv2/get_browsercheckv2.go new file mode 100644 index 0000000..f263b0b --- /dev/null +++ b/syntheticsclientv2/get_browsercheckv2.go @@ -0,0 +1,48 @@ +// Copyright 2021 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 parseGetBrowserCheckV2Response(response string) (*BrowserCheckV2Response, error) { + // Parse the response and return the user object + var BrowserCheckV2 BrowserCheckV2Response + err := json.Unmarshal([]byte(response), &BrowserCheckV2) + if err != nil { + return nil, err + } + + return &BrowserCheckV2, err +} + +func (c Client) GetBrowserCheckV2(id int) (*BrowserCheckV2Response, *RequestDetails, error) { + details, err := c.makePublicAPICall("GET", fmt.Sprintf("/tests/browser/%d", id), bytes.NewBufferString("{}"), nil) + + // Check for errors + if err != nil { + return nil, details, err + } + + BrowserCheckV2, err := parseGetBrowserCheckV2Response(details.ResponseBody) + if err != nil { + return BrowserCheckV2, details, err + } + + return BrowserCheckV2, details, nil +} diff --git a/syntheticsclientv2/get_browsercheckv2_test.go b/syntheticsclientv2/get_browsercheckv2_test.go new file mode 100644 index 0000000..ad47354 --- /dev/null +++ b/syntheticsclientv2/get_browsercheckv2_test.go @@ -0,0 +1,120 @@ +// Copyright 2021 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" + "os" + "fmt" +) + +var ( + getBrowserCheckV2Body = `{"test":{"active":true,"advancedSettings":{"authentication":{"password":"password123","username":"myuser"},"cookies":[{"key":"qux","value":"qux","domain":"splunk.com","path":"/qux"}],"headers":[{"name":"Accept","value":"application/json","domain":"splunk.com"}],"verifyCertificates":true},"createdAt":"2022-09-14T14:35:37.801Z","device":{"id":1,"label":"iPhone","networkConnection":{"description":"Mobile LTE","downloadBandwidth":12000,"latency":70,"packetLoss":0,"uploadBandwidth":12000},"viewportHeight":844,"viewportWidth":375},"frequency":5,"id":1,"locationIds":["na-us-virginia"],"name":"My Test","schedulingStrategy":"round_robin","transactions":[{"name":"Example transaction","steps":[{"name":"element step","selector":".main","selectorType":"css","type":"click_element","waitForNav":true}]}],"type":"browser","updatedAt":"2022-09-14T14:35:38.099Z"}}` + inputGetBrowserCheckV2 = verifyBrowserCheckV2Input(string(getBrowserCheckV2Body)) +) + +func TestGetBrowserCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/browser/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.Write([]byte(getBrowserCheckV2Body)) + }) + + resp, _, err := testClient.GetBrowserCheckV2(1) + + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(resp.Test.ID, inputGetBrowserCheckV2.Test.ID) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.ID, inputGetBrowserCheckV2.Test.ID) + } + + if !reflect.DeepEqual(resp.Test.Name, inputGetBrowserCheckV2.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputGetBrowserCheckV2.Test.Name) + } + + if !reflect.DeepEqual(resp.Test.Type, inputGetBrowserCheckV2.Test.Type) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Type, inputGetBrowserCheckV2.Test.Type) + } + + if !reflect.DeepEqual(resp.Test.Frequency, inputGetBrowserCheckV2.Test.Frequency) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputGetBrowserCheckV2.Test.Frequency) + } + + if !reflect.DeepEqual(resp.Test.Active, inputGetBrowserCheckV2.Test.Active) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputGetBrowserCheckV2.Test.Active) + } + + if !reflect.DeepEqual(resp.Test.Createdat, inputGetBrowserCheckV2.Test.Createdat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Createdat, inputGetBrowserCheckV2.Test.Createdat) + } + + if !reflect.DeepEqual(resp.Test.Updatedat, inputGetBrowserCheckV2.Test.Updatedat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Updatedat, inputGetBrowserCheckV2.Test.Updatedat) + } + + if !reflect.DeepEqual(resp.Test.Device, inputGetBrowserCheckV2.Test.Device) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Device, inputGetBrowserCheckV2.Test.Device) + } + + if !reflect.DeepEqual(resp.Test.Advancedsettings, inputGetBrowserCheckV2.Test.Advancedsettings) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Advancedsettings, inputGetBrowserCheckV2.Test.Advancedsettings) + } + + if !reflect.DeepEqual(resp.Test.Transactions, inputGetBrowserCheckV2.Test.Transactions) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Transactions, inputGetBrowserCheckV2.Test.Transactions) + } + +} + +func verifyBrowserCheckV2Input(stringInput string) *BrowserCheckV2Response { + check := &BrowserCheckV2Response{} + err := json.Unmarshal([]byte(stringInput), check) + if err != nil { + panic(err) + } + return check +} + +func TestLiveGetBrowserCheckV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetBrowserCheckV2(495) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} diff --git a/syntheticsclientv2/update_browsercheckv2.go b/syntheticsclientv2/update_browsercheckv2.go new file mode 100644 index 0000000..8ff5148 --- /dev/null +++ b/syntheticsclientv2/update_browsercheckv2.go @@ -0,0 +1,51 @@ +// Copyright 2021 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 parseUpdateBrowserCheckV2Response(response string) (*BrowserCheckV2Response, error) { + var updateBrowserCheckV2 BrowserCheckV2Response + err := json.Unmarshal([]byte(response), &updateBrowserCheckV2) + if err != nil { + return nil, err + } + + return &updateBrowserCheckV2, err +} + +func (c Client) UpdateBrowserCheckV2(id int, BrowserCheckV2Details *BrowserCheckV2Input) (*BrowserCheckV2Response, *RequestDetails, error) { + + body, err := json.Marshal(BrowserCheckV2Details) + if err != nil { + return nil, nil, err + } + + requestDetails, err := c.makePublicAPICall("PUT", fmt.Sprintf("/tests/browser/%d", id), bytes.NewBuffer(body), nil) + if err != nil { + return nil, requestDetails, err + } + + updateBrowserCheckV2, err := parseUpdateBrowserCheckV2Response(requestDetails.ResponseBody) + if err != nil { + return updateBrowserCheckV2, requestDetails, err + } + + return updateBrowserCheckV2, requestDetails, nil +} diff --git a/syntheticsclientv2/update_browsercheckv2_test.go b/syntheticsclientv2/update_browsercheckv2_test.go new file mode 100644 index 0000000..bf3ac84 --- /dev/null +++ b/syntheticsclientv2/update_browsercheckv2_test.go @@ -0,0 +1,53 @@ +// Copyright 2021 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 ( + updateBrowserCheckV2Body = `{"test":{"name":"browser-beep-test","business_transactions":[{"name":"Synthetic transaction 1","steps":[{"name":"Go to URL","type":"go_to_url","url":"https://www.splunk.com","action":"go_to_url","options":{"url":"https://www.splunk.com"},"wait_for_nav":true},{"name":"Nexter step","type":"click_element","selector_type":"id","selector":"free-splunk-click-desktop","wait_for_nav":false}]}],"urlProtocol":"https://","startUrl":"www.splunk.com","location_ids":["aws-us-east-1"],"device_id":2,"frequency":5,"scheduling_strategy":"round_robin","active":true,"advanced_settings":{"authentication":{"username":"boopuser","password":"{{env.beep-var}}"},"cookies":[{"key":"super","value":"duper","domain":"www.batmansagent.com","path":"/boom/goes/beep"}],"headers":[{"name":"batman","value":"Agentoz","domain":"www.batmansagent.com"}],"host_overrides":[],"user_agent":"Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X; Splunk Synthetics) AppleWebKit/604.1.25 (KHTML, like Gecko) Version/11.0 Mobile/15A5304j Safari/604.1","verify_certificates":true}}}` + inputBrowserCheckV2Update = BrowserCheckV2Input{} +) + +func TestUpdateBrowserCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/browser/10", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + w.Write([]byte(updateBrowserCheckV2Body)) + }) + + json.Unmarshal([]byte(updateBrowserCheckV2Body), &inputBrowserCheckV2Update) + + resp, _, err := testClient.UpdateBrowserCheckV2(10, &inputBrowserCheckV2Update) + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.Test.Name, inputBrowserCheckV2Update.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputBrowserCheckV2Update.Test.Name) + } + +} + From 82d235cc8bba1c562a5ad51b6330d601bbd7296e Mon Sep 17 00:00:00 2001 From: jeremyh Date: Thu, 17 Nov 2022 09:41:31 -0500 Subject: [PATCH 04/15] get checks --- syntheticsclientv2/get_checksv2.go | 74 +++++++++++++++ syntheticsclientv2/get_checksv2_test.go | 120 ++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 syntheticsclientv2/get_checksv2.go create mode 100644 syntheticsclientv2/get_checksv2_test.go diff --git a/syntheticsclientv2/get_checksv2.go b/syntheticsclientv2/get_checksv2.go new file mode 100644 index 0000000..b601bb9 --- /dev/null +++ b/syntheticsclientv2/get_checksv2.go @@ -0,0 +1,74 @@ +// Copyright 2021 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" +) + +// Leaving off "Enabled" filter setting. Can be added later if required. +type GetChecksV2Options struct { + Type string `json:"type"` + PerPage int `json:"per_page"` + Page int `json:"page"` + Search string `json:"search"` + OrderBy string `json:"orderBy"` +} + +func parseChecksV2Response(response string) (*GetChecksV2, error) { + // Parse the response and return the check object + var checks GetChecksV2 + err := json.Unmarshal([]byte(response), &checks) + if err != nil { + return nil, err + } + + return &checks, err +} + +// GetChecks returns all checks +func (c Client) GetChecks(params *GetChecksV2Options) (*GetChecksV2, *RequestDetails, error) { + // Check for default params + if params.Type == "" { + params.Type = "all" + } + if params.Page == 0 { + params.Page = int(1) + } + if params.PerPage == 0 { + params.PerPage = int(50) + } + + // Make the request + details, err := c.makePublicAPICall( + "GET", + fmt.Sprintf("/tests?testType=%s&page=%d&perPage=%d&orderBy=%s&search=%s", params.Type, params.Page, params.PerPage, params.OrderBy, params.Search), + bytes.NewBufferString("{}"), + nil) + + // Check for errors + if err != nil { + return nil, details, err + } + + check, err := parseChecksV2Response(details.ResponseBody) + if err != nil { + return check, details, err + } + + return check, details, nil +} diff --git a/syntheticsclientv2/get_checksv2_test.go b/syntheticsclientv2/get_checksv2_test.go new file mode 100644 index 0000000..6913e00 --- /dev/null +++ b/syntheticsclientv2/get_checksv2_test.go @@ -0,0 +1,120 @@ +// Copyright 2021 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" + "os" + "fmt" +) + +var ( + getChecksV2Body = `` + inputGetChecksV2 = verifyChecksV2Input(string(getChecksV2Body)) +) + +// func TestGetChecksV2(t *testing.T) { +// setup() +// defer teardown() + +// testMux.HandleFunc("/tests/browser/1", func(w http.ResponseWriter, r *http.Request) { +// testMethod(t, r, "GET") +// w.Write([]byte(getChecksV2Body)) +// }) + +// resp, _, err := testClient.GetChecksV2(1) + +// if err != nil { +// t.Fatal(err) +// } +// if !reflect.DeepEqual(resp.Test.ID, inputGetChecksV2.Test.ID) { +// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.ID, inputGetChecksV2.Test.ID) +// } + +// if !reflect.DeepEqual(resp.Test.Name, inputGetChecksV2.Test.Name) { +// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputGetChecksV2.Test.Name) +// } + +// if !reflect.DeepEqual(resp.Test.Type, inputGetChecksV2.Test.Type) { +// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Type, inputGetChecksV2.Test.Type) +// } + +// if !reflect.DeepEqual(resp.Test.Frequency, inputGetChecksV2.Test.Frequency) { +// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputGetChecksV2.Test.Frequency) +// } + +// if !reflect.DeepEqual(resp.Test.Active, inputGetChecksV2.Test.Active) { +// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputGetChecksV2.Test.Active) +// } + +// if !reflect.DeepEqual(resp.Test.Createdat, inputGetChecksV2.Test.Createdat) { +// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Createdat, inputGetChecksV2.Test.Createdat) +// } + +// if !reflect.DeepEqual(resp.Test.Updatedat, inputGetChecksV2.Test.Updatedat) { +// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Updatedat, inputGetChecksV2.Test.Updatedat) +// } + +// if !reflect.DeepEqual(resp.Test.Device, inputGetChecksV2.Test.Device) { +// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Device, inputGetChecksV2.Test.Device) +// } + +// if !reflect.DeepEqual(resp.Test.Advancedsettings, inputGetChecksV2.Test.Advancedsettings) { +// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Advancedsettings, inputGetChecksV2.Test.Advancedsettings) +// } + +// if !reflect.DeepEqual(resp.Test.Transactions, inputGetChecksV2.Test.Transactions) { +// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Transactions, inputGetChecksV2.Test.Transactions) +// } + +// } + +func verifyChecksV2Input(stringInput string) *ChecksV2Response { + check := &ChecksV2Response{} + err := json.Unmarshal([]byte(stringInput), check) + if err != nil { + panic(err) + } + return check +} + +func TestLiveGetChecksV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetChecksV2(495) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} From b8ce9187f7102784e6b9d6e2b42e5842e3d615df Mon Sep 17 00:00:00 2001 From: jeremyh Date: Thu, 17 Nov 2022 10:18:41 -0500 Subject: [PATCH 05/15] get checks --- syntheticsclientv2/common_models.go | 8 +++ syntheticsclientv2/get_checksv2.go | 23 +++----- syntheticsclientv2/get_checksv2_test.go | 76 +++++++------------------ syntheticsclientv2/synthetics_test.go | 2 +- 4 files changed, 40 insertions(+), 69 deletions(-) diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go index e2437aa..eeea263 100644 --- a/syntheticsclientv2/common_models.go +++ b/syntheticsclientv2/common_models.go @@ -318,6 +318,14 @@ type Tests []struct { Updatedat time.Time `json:"updatedAt"` } +type GetChecksV2Options struct { + TestType string `json:"testType"` + PerPage int `json:"perPage"` + Page int `json:"page"` + Search string `json:"search"` + OrderBy string `json:"orderBy"` +} + type Errors []struct { Title string `json:"title,omitempty"` Description string `json:"description,omitempty"` diff --git a/syntheticsclientv2/get_checksv2.go b/syntheticsclientv2/get_checksv2.go index b601bb9..1f3c9de 100644 --- a/syntheticsclientv2/get_checksv2.go +++ b/syntheticsclientv2/get_checksv2.go @@ -20,18 +20,10 @@ import ( "fmt" ) -// Leaving off "Enabled" filter setting. Can be added later if required. -type GetChecksV2Options struct { - Type string `json:"type"` - PerPage int `json:"per_page"` - Page int `json:"page"` - Search string `json:"search"` - OrderBy string `json:"orderBy"` -} -func parseChecksV2Response(response string) (*GetChecksV2, error) { +func parseChecksV2Response(response string) (*ChecksV2Response, error) { // Parse the response and return the check object - var checks GetChecksV2 + var checks ChecksV2Response err := json.Unmarshal([]byte(response), &checks) if err != nil { return nil, err @@ -41,10 +33,13 @@ func parseChecksV2Response(response string) (*GetChecksV2, error) { } // GetChecks returns all checks -func (c Client) GetChecks(params *GetChecksV2Options) (*GetChecksV2, *RequestDetails, error) { +func (c Client) GetChecksV2(params *GetChecksV2Options) (*ChecksV2Response, *RequestDetails, error) { // Check for default params - if params.Type == "" { - params.Type = "all" + if params.TestType == "" { + params.TestType = "" + } + if params.Search == "" { + params.Search = "" } if params.Page == 0 { params.Page = int(1) @@ -56,7 +51,7 @@ func (c Client) GetChecks(params *GetChecksV2Options) (*GetChecksV2, *RequestDet // Make the request details, err := c.makePublicAPICall( "GET", - fmt.Sprintf("/tests?testType=%s&page=%d&perPage=%d&orderBy=%s&search=%s", params.Type, params.Page, params.PerPage, params.OrderBy, params.Search), + fmt.Sprintf("/tests?testType=%s&page=%d&perPage=%d&orderBy=%s&search=%s", params.TestType, params.Page, params.PerPage, params.OrderBy, params.Search), bytes.NewBufferString("{}"), nil) diff --git a/syntheticsclientv2/get_checksv2_test.go b/syntheticsclientv2/get_checksv2_test.go index 6913e00..c53a110 100644 --- a/syntheticsclientv2/get_checksv2_test.go +++ b/syntheticsclientv2/get_checksv2_test.go @@ -24,68 +24,36 @@ import ( ) var ( - getChecksV2Body = `` + getChecksV2Body = `{"testType":"","page":1,"perPage":50,"search":"","orderBy":"id"}` inputGetChecksV2 = verifyChecksV2Input(string(getChecksV2Body)) + getChecksV2Output = `{"tests":[{"id":482,"name":"Test of Splunk.com","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-15T16:05:25.815Z","updated_at":"2022-09-29T19:13:13.853Z","location_ids":["aws-us-east-1"],"type":"browser"},{"id":489,"name":"Appinspect login API","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-16T15:47:43.730Z","updated_at":"2022-08-16T15:47:43.741Z","location_ids":["aws-us-east-1"],"type":"api"},{"id":490,"name":"Arch Linux Packages","active":true,"frequency":10,"scheduling_strategy":"round_robin","created_at":"2022-08-16T16:48:42.119Z","updated_at":"2022-08-16T16:48:42.131Z","location_ids":["aws-us-east-1"],"type":"http"},{"id":492,"name":"Test of Splunkbase","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-16T19:35:54.014Z","updated_at":"2022-09-29T19:13:13.907Z","location_ids":["aws-us-east-1"],"type":"browser"},{"id":493,"name":"Brewery API","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-16T19:44:15.626Z","updated_at":"2022-08-16T19:44:15.635Z","location_ids":["aws-us-east-1"],"type":"api"},{"id":495,"name":"Multi-step test of legacy Splunkbase","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-17T01:24:44.579Z","updated_at":"2022-09-29T19:13:13.203Z","location_ids":["aws-us-east-1"],"type":"browser"},{"id":496,"name":"Multi-step Test of new Splunkbase","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-17T01:33:27.771Z","updated_at":"2022-09-29T19:13:13.997Z","location_ids":["aws-us-east-1"],"type":"browser"},{"id":935,"name":"This test does test stuff","active":true,"frequency":30,"scheduling_strategy":"round_robin","created_at":"2022-10-26T14:48:36.026Z","updated_at":"2022-10-26T14:48:36.037Z","location_ids":["aws-us-east-1"],"type":"api"},{"id":1116,"name":"boop-test","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-11-16T19:18:59.603Z","updated_at":"2022-11-16T19:20:58.911Z","location_ids":["aws-us-east-1","aws-ap-northeast-1"],"type":"api"},{"id":1128,"name":"boopbeep","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-11-17T14:19:49.564Z","updated_at":"2022-11-17T14:19:49.571Z","location_ids":["aws-us-east-1"],"type":"browser"}],"page":1,"per_page":50,"next_page_link":null,"total_count":10}` + output = &ChecksV2Response{} ) -// func TestGetChecksV2(t *testing.T) { -// setup() -// defer teardown() - -// testMux.HandleFunc("/tests/browser/1", func(w http.ResponseWriter, r *http.Request) { -// testMethod(t, r, "GET") -// w.Write([]byte(getChecksV2Body)) -// }) - -// resp, _, err := testClient.GetChecksV2(1) - -// if err != nil { -// t.Fatal(err) -// } -// if !reflect.DeepEqual(resp.Test.ID, inputGetChecksV2.Test.ID) { -// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.ID, inputGetChecksV2.Test.ID) -// } - -// if !reflect.DeepEqual(resp.Test.Name, inputGetChecksV2.Test.Name) { -// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputGetChecksV2.Test.Name) -// } - -// if !reflect.DeepEqual(resp.Test.Type, inputGetChecksV2.Test.Type) { -// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Type, inputGetChecksV2.Test.Type) -// } - -// if !reflect.DeepEqual(resp.Test.Frequency, inputGetChecksV2.Test.Frequency) { -// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputGetChecksV2.Test.Frequency) -// } - -// if !reflect.DeepEqual(resp.Test.Active, inputGetChecksV2.Test.Active) { -// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputGetChecksV2.Test.Active) -// } - -// if !reflect.DeepEqual(resp.Test.Createdat, inputGetChecksV2.Test.Createdat) { -// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Createdat, inputGetChecksV2.Test.Createdat) -// } +func TestGetChecksV2(t *testing.T) { + setup() + defer teardown() -// if !reflect.DeepEqual(resp.Test.Updatedat, inputGetChecksV2.Test.Updatedat) { -// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Updatedat, inputGetChecksV2.Test.Updatedat) -// } + testMux.HandleFunc("/tests", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.Write([]byte(getChecksV2Output)) + }) -// if !reflect.DeepEqual(resp.Test.Device, inputGetChecksV2.Test.Device) { -// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Device, inputGetChecksV2.Test.Device) -// } + json.Unmarshal([]byte(getChecksV2Output), output) -// if !reflect.DeepEqual(resp.Test.Advancedsettings, inputGetChecksV2.Test.Advancedsettings) { -// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Advancedsettings, inputGetChecksV2.Test.Advancedsettings) -// } + resp, _, err := testClient.GetChecksV2(inputGetChecksV2) -// if !reflect.DeepEqual(resp.Test.Transactions, inputGetChecksV2.Test.Transactions) { -// t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Transactions, inputGetChecksV2.Test.Transactions) -// } + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(resp.Tests, output.Tests) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Tests, output.Tests) + } -// } +} -func verifyChecksV2Input(stringInput string) *ChecksV2Response { - check := &ChecksV2Response{} +func verifyChecksV2Input(stringInput string) *GetChecksV2Options { + check := &GetChecksV2Options{} err := json.Unmarshal([]byte(stringInput), check) if err != nil { panic(err) @@ -106,7 +74,7 @@ func TestLiveGetChecksV2(t *testing.T) { c := NewClient(token, realm) // Make the request with your check settings and print result - res, _, err := c.GetChecksV2(495) + res, _, err := c.GetChecksV2(inputGetChecksV2) if err != nil { fmt.Println(err) } else { diff --git a/syntheticsclientv2/synthetics_test.go b/syntheticsclientv2/synthetics_test.go index 6684a4e..29532ca 100644 --- a/syntheticsclientv2/synthetics_test.go +++ b/syntheticsclientv2/synthetics_test.go @@ -87,7 +87,7 @@ func TestConfigurableClientTimeout(t *testing.T) { publicBaseUrl: testServer.URL, }) log.Printf("Client instantiated: %s", testServer.URL) - _, _, err := testConfigurableClient.GetCheck(12) + _, _, err := testConfigurableClient.GetBrowserCheckV2(12) if !strings.Contains(err.Error(), "context deadline exceeded (Client.Timeout exceeded while awaiting headers)") { t.Errorf("expected to see timeout error, but saw: %s", err.Error()) } From 35d77573e69849f2603f89c629676480abff4785 Mon Sep 17 00:00:00 2001 From: jeremyh Date: Fri, 18 Nov 2022 10:23:17 -0500 Subject: [PATCH 06/15] add parts for variables --- syntheticsclientv2/common_models.go | 56 +++++++++ syntheticsclientv2/create_variablev2.go | 52 +++++++++ syntheticsclientv2/create_variablev2_test.go | 103 +++++++++++++++++ syntheticsclientv2/delete_variablesv2_test.go | 72 ++++++++++++ syntheticsclientv2/delete_variablev2.go | 40 +++++++ syntheticsclientv2/get_variablev2.go | 52 +++++++++ syntheticsclientv2/get_variablev2_test.go | 108 ++++++++++++++++++ syntheticsclientv2/synthetics.go | 1 + syntheticsclientv2/update_variablesv2_test.go | 65 +++++++++++ syntheticsclientv2/update_variablev2.go | 51 +++++++++ 10 files changed, 600 insertions(+) create mode 100644 syntheticsclientv2/create_variablev2.go create mode 100644 syntheticsclientv2/create_variablev2_test.go create mode 100644 syntheticsclientv2/delete_variablesv2_test.go create mode 100644 syntheticsclientv2/delete_variablev2.go create mode 100644 syntheticsclientv2/get_variablev2.go create mode 100644 syntheticsclientv2/get_variablev2_test.go create mode 100644 syntheticsclientv2/update_variablesv2_test.go create mode 100644 syntheticsclientv2/update_variablev2.go diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go index eeea263..8c4a17d 100644 --- a/syntheticsclientv2/common_models.go +++ b/syntheticsclientv2/common_models.go @@ -331,12 +331,35 @@ type Errors []struct { Description string `json:"description,omitempty"` } +type HttpHeaders []struct { + Name string `json:"name,omitempty"` + Value string `json:"value,omitempty"` +} + +type Variable struct { + Createdat time.Time `json:"createdAt,omitempty"` + Description string `json:"description,omitempty"` + ID int `json:"id,omitempty"` + Name string `json:"name"` + Secret bool `json:"secret"` + Updatedat time.Time `json:"updatedAt,omitempty"` + Value string `json:"value"` +} + type DeleteCheck struct { Result string `json:"result"` Message string `json:"message"` Errors Errors `json:"errors"` } +type VariableV2Response struct { + Variable `json:"variable"` +} + +type VariableV2Input struct { + Variable `json:"variable"` +} + type ChecksV2Response struct { Nextpagelink int `json:"nextPageLink"` Perpage int `json:"perPage"` @@ -344,6 +367,39 @@ type ChecksV2Response struct { Totalcount int `json:"totalCount"` } +type HttpCheckV2Response struct { + Test struct { + ID int `json:"id"` + Name string `json:"name"` + Active bool `json:"active"` + Frequency int `json:"frequency"` + SchedulingStrategy string `json:"scheduling_strategy"` + CreatedAt time.Time `json:"created_at,omitempty"` + UpdatedAt time.Time `json:"updated_at,omitempty"` + LocationIds []string `json:"location_ids"` + Type string `json:"type"` + URL string `json:"url"` + RequestMethod string `json:"request_method"` + Body string `json:"body,omitempty"` + HttpHeaders `json:"headers,omitempty"` +} `json:"test"` +} + +type HttpCheckV2Input struct { + Test struct { + Name string `json:"name"` + Type string `json:"type"` + URL string `json:"url"` + LocationIds []string `json:"location_ids"` + Frequency int `json:"frequency"` + SchedulingStrategy string `json:"scheduling_strategy"` + Active bool `json:"active"` + RequestMethod string `json:"request_method"` + Body string `json:"body,omitempty"` + HttpHeaders `json:"headers,omitempty"` + } `json:"test"` +} + type ApiCheckV2Input struct { Test struct { Active bool `json:"active"` diff --git a/syntheticsclientv2/create_variablev2.go b/syntheticsclientv2/create_variablev2.go new file mode 100644 index 0000000..05c43f3 --- /dev/null +++ b/syntheticsclientv2/create_variablev2.go @@ -0,0 +1,52 @@ +// Copyright 2021 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 parseCreateVariableV2Response(response string) (*VariableV2Response, error) { + + var createVariableV2 VariableV2Response + JSONResponse := []byte(response) + err := json.Unmarshal(JSONResponse, &createVariableV2) + if err != nil { + return nil, err + } + + return &createVariableV2, err +} + +func (c Client) CreateVariableV2(VariableV2Details *VariableV2Input) (*VariableV2Response, *RequestDetails, error) { + + body, err := json.Marshal(VariableV2Details) + if err != nil { + return nil, nil, err + } + + details, err := c.makePublicAPICall("POST", "/variables", bytes.NewBuffer(body), nil) + if err != nil { + return nil, details, err + } + + newVariableV2, err := parseCreateVariableV2Response(details.ResponseBody) + if err != nil { + return newVariableV2, details, err + } + + return newVariableV2, details, nil +} diff --git a/syntheticsclientv2/create_variablev2_test.go b/syntheticsclientv2/create_variablev2_test.go new file mode 100644 index 0000000..aa8afae --- /dev/null +++ b/syntheticsclientv2/create_variablev2_test.go @@ -0,0 +1,103 @@ +// Copyright 2021 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 ( + "net/http" + "reflect" + "testing" + "fmt" + "encoding/json" + "os" +) + +var ( + createVariableV2Body = `{"variable":{"description":"My super awesome test variable","name":"food","secret":false,"value":"bar"}}` + inputVariableV2Data = VariableV2Input{} +) + +func TestCreateVariableV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/variables", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + w.Write([]byte(createVariableV2Body)) + }) + + json.Unmarshal([]byte(createVariableV2Body), &inputVariableV2Data) + + resp, _, err := testClient.CreateVariableV2(&inputVariableV2Data) + + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.Variable.ID, inputVariableV2Data.Variable.ID) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.ID, inputVariableV2Data.Variable.ID) + } + + if !reflect.DeepEqual(resp.Variable.Name, inputVariableV2Data.Variable.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Name, inputVariableV2Data.Variable.Name) + } + + if !reflect.DeepEqual(resp.Variable.Description, inputVariableV2Data.Variable.Description) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Description, inputVariableV2Data.Variable.Description) + } + + if !reflect.DeepEqual(resp.Variable.Value, inputVariableV2Data.Variable.Value) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Value, inputVariableV2Data.Variable.Value) + } + + if !reflect.DeepEqual(resp.Variable.Secret, inputVariableV2Data.Variable.Secret) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Secret, inputVariableV2Data.Variable.Secret) + } + +} + + +func TestLiveCreateVariableV2(t *testing.T) { + setup() + defer teardown() + + json.Unmarshal([]byte(createVariableV2Body), &inputVariableV2Data) + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputVariableV2Data) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateVariableV2(&inputVariableV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} \ No newline at end of file diff --git a/syntheticsclientv2/delete_variablesv2_test.go b/syntheticsclientv2/delete_variablesv2_test.go new file mode 100644 index 0000000..40cc3fd --- /dev/null +++ b/syntheticsclientv2/delete_variablesv2_test.go @@ -0,0 +1,72 @@ +// Copyright 2021 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 ( + "net/http" + "testing" + "os" + "fmt" +) + +var ( + deleteVariableV2RespBody = `` +) + +func TestDeleteVariableV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/variables/19", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.Write([]byte(deleteVariableV2RespBody)) + }) + + resp, err := testClient.DeleteVariableV2(19) + if err != nil { + fmt.Println(resp) + t.Fatal(err) + } + fmt.Println(resp) +} + +func TestLiveDeleteVariableV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, err := c.DeleteVariableV2(254) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} \ No newline at end of file diff --git a/syntheticsclientv2/delete_variablev2.go b/syntheticsclientv2/delete_variablev2.go new file mode 100644 index 0000000..75890dc --- /dev/null +++ b/syntheticsclientv2/delete_variablev2.go @@ -0,0 +1,40 @@ +// Copyright 2021 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) DeleteVariableV2(id int) (int, error) { + requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/variables/%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/get_variablev2.go b/syntheticsclientv2/get_variablev2.go new file mode 100644 index 0000000..37cda8d --- /dev/null +++ b/syntheticsclientv2/get_variablev2.go @@ -0,0 +1,52 @@ +// Copyright 2021 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 parseVariableV2Response(response string) (*VariableV2Response, error) { + // Parse the response and return the check object + var check VariableV2Response + err := json.Unmarshal([]byte(response), &check) + if err != nil { + return nil, err + } + + return &check, err +} + +func (c Client) GetVariableV2(id int) (*VariableV2Response, *RequestDetails, error) { + + details, err := c.makePublicAPICall("GET", + fmt.Sprintf("/variables/%d", id), + bytes.NewBufferString("{}"), + nil) + + if err != nil { + return nil, details, err + } + + check, err := parseVariableV2Response(details.ResponseBody) + if err != nil { + return check, details, err + } + + return check, details, nil +} diff --git a/syntheticsclientv2/get_variablev2_test.go b/syntheticsclientv2/get_variablev2_test.go new file mode 100644 index 0000000..d1c9ed7 --- /dev/null +++ b/syntheticsclientv2/get_variablev2_test.go @@ -0,0 +1,108 @@ +// Copyright 2021 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" + "os" + "fmt" +) + +var ( + getVariableV2Body = `{"variable":{"id":1,"name":"beep-var","description":"","value":"","secret":true,"created_at":"2022-11-16T14:55:09.480Z","updated_at":"2022-11-16T14:55:09.480Z"}}` + inputGetVariableV2 = verifyVariableV2Input(string(getVariableV2Body)) +) + +func TestGetVariableV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/variables/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.Write([]byte(getVariableV2Body)) + }) + + resp, _, err := testClient.GetVariableV2(1) + + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(resp.Variable.ID, inputGetVariableV2.Variable.ID) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.ID, inputGetVariableV2.Variable.ID) + } + + if !reflect.DeepEqual(resp.Variable.Name, inputGetVariableV2.Variable.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Name, inputGetVariableV2.Variable.Name) + } + + if !reflect.DeepEqual(resp.Variable.Description, inputGetVariableV2.Variable.Description) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Description, inputGetVariableV2.Variable.Description) + } + + if !reflect.DeepEqual(resp.Variable.Value, inputGetVariableV2.Variable.Value) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Value, inputGetVariableV2.Variable.Value) + } + + if !reflect.DeepEqual(resp.Variable.Secret, inputGetVariableV2.Variable.Secret) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Secret, inputGetVariableV2.Variable.Secret) + } + + if !reflect.DeepEqual(resp.Variable.Createdat, inputGetVariableV2.Variable.Createdat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Createdat, inputGetVariableV2.Variable.Createdat) + } + + if !reflect.DeepEqual(resp.Variable.Updatedat, inputGetVariableV2.Variable.Updatedat) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Updatedat, inputGetVariableV2.Variable.Updatedat) + } + +} + +func verifyVariableV2Input(stringInput string) *VariableV2Response { + check := &VariableV2Response{} + err := json.Unmarshal([]byte(stringInput), check) + if err != nil { + panic(err) + } + return check +} + +func TestLiveGetVariableV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetVariableV2(246) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} diff --git a/syntheticsclientv2/synthetics.go b/syntheticsclientv2/synthetics.go index 70331dd..ca95e2d 100644 --- a/syntheticsclientv2/synthetics.go +++ b/syntheticsclientv2/synthetics.go @@ -127,6 +127,7 @@ func NewClient(apiKey string, realm string) *Client { func NewConfigurableClient(apiKey string, realm string, args ClientArgs) *Client { client := Client{ apiKey: apiKey, + realm: realm, httpClient: http.Client{Timeout: time.Duration(args.timeoutSeconds) * time.Second}, } if args.publicBaseUrl == "" { diff --git a/syntheticsclientv2/update_variablesv2_test.go b/syntheticsclientv2/update_variablesv2_test.go new file mode 100644 index 0000000..95fbe3a --- /dev/null +++ b/syntheticsclientv2/update_variablesv2_test.go @@ -0,0 +1,65 @@ +// Copyright 2021 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 ( + updateVariableV2Body = `{"variable":{"description":"My super awesome test variable","name":"foo2","secret":false,"value":"bar"}}` + inputVariableV2Update = VariableV2Input{} +) + +func TestUpdateVariableV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/variables/10", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + w.Write([]byte(updateVariableV2Body)) + }) + + json.Unmarshal([]byte(updateVariableV2Body), &inputVariableV2Update) + + resp, _, err := testClient.UpdateVariableV2(10, &inputVariableV2Update) + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.Variable.Name, inputVariableV2Update.Variable.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Name, inputVariableV2Update.Variable.Name) + } + + if !reflect.DeepEqual(resp.Variable.Description, inputVariableV2Update.Variable.Description) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Description, inputVariableV2Update.Variable.Description) + } + + if !reflect.DeepEqual(resp.Variable.Value, inputVariableV2Update.Variable.Value) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Value, inputVariableV2Update.Variable.Value) + } + + if !reflect.DeepEqual(resp.Variable.Secret, inputVariableV2Update.Variable.Secret) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Variable.Secret, inputVariableV2Update.Variable.Secret) + } + +} + diff --git a/syntheticsclientv2/update_variablev2.go b/syntheticsclientv2/update_variablev2.go new file mode 100644 index 0000000..2c59455 --- /dev/null +++ b/syntheticsclientv2/update_variablev2.go @@ -0,0 +1,51 @@ +// Copyright 2021 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 parseUpdateVariableV2Response(response string) (*VariableV2Response, error) { + var updateVariableV2 VariableV2Response + err := json.Unmarshal([]byte(response), &updateVariableV2) + if err != nil { + return nil, err + } + + return &updateVariableV2, err +} + +func (c Client) UpdateVariableV2(id int, VariableV2Details *VariableV2Input) (*VariableV2Response, *RequestDetails, error) { + + body, err := json.Marshal(VariableV2Details) + if err != nil { + return nil, nil, err + } + + requestDetails, err := c.makePublicAPICall("PUT", fmt.Sprintf("/variables/%d", id), bytes.NewBuffer(body), nil) + if err != nil { + return nil, requestDetails, err + } + + updateVariableV2, err := parseUpdateVariableV2Response(requestDetails.ResponseBody) + if err != nil { + return updateVariableV2, requestDetails, err + } + + return updateVariableV2, requestDetails, nil +} From 6ad30f316c98e65dc51ada9acbd9434eeef6f641 Mon Sep 17 00:00:00 2001 From: jeremyh Date: Mon, 21 Nov 2022 10:06:07 -0500 Subject: [PATCH 07/15] add httpchecksv2 --- syntheticsclientv2/common_models.go | 180 +++++++++--------- syntheticsclientv2/create_httpcheckv2.go | 52 +++++ syntheticsclientv2/create_httpcheckv2_test.go | 110 +++++++++++ syntheticsclientv2/delete_httpcheckv2.go | 40 ++++ syntheticsclientv2/delete_httpcheckv2_test.go | 72 +++++++ syntheticsclientv2/get_httpcheckv2.go | 48 +++++ syntheticsclientv2/get_httpcheckv2_test.go | 132 +++++++++++++ syntheticsclientv2/synthetics.go | 4 +- syntheticsclientv2/update_httpcheckv2.go | 51 +++++ syntheticsclientv2/update_httpcheckv2_test.go | 53 ++++++ 10 files changed, 650 insertions(+), 92 deletions(-) create mode 100644 syntheticsclientv2/create_httpcheckv2.go create mode 100644 syntheticsclientv2/create_httpcheckv2_test.go create mode 100644 syntheticsclientv2/delete_httpcheckv2.go create mode 100644 syntheticsclientv2/delete_httpcheckv2_test.go create mode 100644 syntheticsclientv2/get_httpcheckv2.go create mode 100644 syntheticsclientv2/get_httpcheckv2_test.go create mode 100644 syntheticsclientv2/update_httpcheckv2.go create mode 100644 syntheticsclientv2/update_httpcheckv2_test.go diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go index 8c4a17d..f4c8682 100644 --- a/syntheticsclientv2/common_models.go +++ b/syntheticsclientv2/common_models.go @@ -200,16 +200,16 @@ type Networkconnection struct { } type Advancedsettings struct { - Authentication `json:"authentication"` - Cookiesv2 `json:"cookies"` - BrowserHeaders `json:"headers,omitempty"` + Authentication `json:"authentication"` + Cookiesv2 `json:"cookies"` + BrowserHeaders `json:"headers,omitempty"` Verifycertificates bool `json:"verifyCertificates,omitempty"` -} +} type Authentication struct { Password string `json:"password"` Username string `json:"username"` -} +} type Cookiesv2 []struct { Key string `json:"key"` @@ -225,13 +225,13 @@ type BrowserHeaders []struct { } type Transactions []struct { - Name string `json:"name"` - Stepsv2 `json:"steps"` + Name string `json:"name"` + Stepsv2 `json:"steps"` } type BusinessTransactions []struct { - Name string `json:"name"` - BusinessTransactionStepsV2 `json:"steps"` + Name string `json:"name"` + BusinessTransactionStepsV2 `json:"steps"` } type Stepsv2 []struct { @@ -256,19 +256,19 @@ type Device struct { ID int `json:"id,omitempty"` Label string `json:"label,omitempty"` Networkconnection `json:"networkConnection,omitempty"` - Viewportheight int `json:"viewportHeight,omitempty"` - Viewportwidth int `json:"viewportWidth,omitempty"` + Viewportheight int `json:"viewportHeight,omitempty"` + Viewportwidth int `json:"viewportWidth,omitempty"` } type Requests []struct { Configuration `json:"configuration,omitempty"` - Setup `json:"setup,omitempty"` - Validations `json:"validations,omitempty"` + Setup `json:"setup,omitempty"` + Validations `json:"validations,omitempty"` } type Configuration struct { - Body string `json:"body"` - Headers `json:"headers,omitempty"` + Body string `json:"body"` + Headers `json:"headers,omitempty"` Name string `json:"name,omitempty"` Requestmethod string `json:"requestMethod,omitempty"` URL string `json:"url,omitempty"` @@ -290,7 +290,7 @@ type Validations []struct { Expected string `json:"expected,omitempty"` Name string `json:"name,omitempty"` Type string `json:"type,omitempty"` -} +} type Checks []struct { ID int `json:"id"` @@ -306,7 +306,7 @@ type Checks []struct { Tags Tags `json:"tags"` } -type Tests []struct { +type Tests []struct { Active bool `json:"active"` Createdat time.Time `json:"createdAt"` Frequency int `json:"frequency"` @@ -319,11 +319,11 @@ type Tests []struct { } type GetChecksV2Options struct { - TestType string `json:"testType"` - PerPage int `json:"perPage"` - Page int `json:"page"` - Search string `json:"search"` - OrderBy string `json:"orderBy"` + TestType string `json:"testType"` + PerPage int `json:"perPage"` + Page int `json:"page"` + Search string `json:"search"` + OrderBy string `json:"orderBy"` } type Errors []struct { @@ -332,8 +332,8 @@ type Errors []struct { } type HttpHeaders []struct { - Name string `json:"name,omitempty"` - Value string `json:"value,omitempty"` + Name string `json:"name,omitempty"` + Value string `json:"value,omitempty"` } type Variable struct { @@ -364,100 +364,100 @@ type ChecksV2Response struct { Nextpagelink int `json:"nextPageLink"` Perpage int `json:"perPage"` Tests `json:"tests"` - Totalcount int `json:"totalCount"` + Totalcount int `json:"totalCount"` } type HttpCheckV2Response struct { Test struct { - ID int `json:"id"` - Name string `json:"name"` - Active bool `json:"active"` - Frequency int `json:"frequency"` - SchedulingStrategy string `json:"scheduling_strategy"` - CreatedAt time.Time `json:"created_at,omitempty"` - UpdatedAt time.Time `json:"updated_at,omitempty"` - LocationIds []string `json:"location_ids"` - Type string `json:"type"` - URL string `json:"url"` - RequestMethod string `json:"request_method"` - Body string `json:"body,omitempty"` - HttpHeaders `json:"headers,omitempty"` -} `json:"test"` + ID int `json:"id"` + Name string `json:"name"` + Active bool `json:"active"` + Frequency int `json:"frequency"` + SchedulingStrategy string `json:"scheduling_strategy"` + CreatedAt time.Time `json:"created_at,omitempty"` + UpdatedAt time.Time `json:"updated_at,omitempty"` + LocationIds []string `json:"location_ids"` + Type string `json:"type"` + URL string `json:"url"` + RequestMethod string `json:"request_method"` + Body string `json:"body,omitempty"` + HttpHeaders `json:"headers,omitempty"` + } `json:"test"` } type HttpCheckV2Input struct { Test struct { - Name string `json:"name"` - Type string `json:"type"` - URL string `json:"url"` - LocationIds []string `json:"location_ids"` - Frequency int `json:"frequency"` - SchedulingStrategy string `json:"scheduling_strategy"` - Active bool `json:"active"` - RequestMethod string `json:"request_method"` - Body string `json:"body,omitempty"` - HttpHeaders `json:"headers,omitempty"` + Name string `json:"name"` + Type string `json:"type"` + URL string `json:"url"` + LocationIds []string `json:"location_ids"` + Frequency int `json:"frequency"` + SchedulingStrategy string `json:"scheduling_strategy"` + Active bool `json:"active"` + RequestMethod string `json:"request_method"` + Body string `json:"body,omitempty"` + HttpHeaders `json:"headers,omitempty"` } `json:"test"` } type ApiCheckV2Input struct { Test struct { - Active bool `json:"active"` - Deviceid int `json:"device_id"` - Frequency int `json:"frequency"` - Locationids []string `json:"location_ids"` - Name string `json:"name"` - Requests `json:"requests"` + Active bool `json:"active"` + Deviceid int `json:"device_id"` + Frequency int `json:"frequency"` + Locationids []string `json:"location_ids"` + Name string `json:"name"` + Requests `json:"requests"` Schedulingstrategy string `json:"schedulingStrategy"` } `json:"test"` } type ApiCheckV2Response struct { Test struct { - Active bool `json:"active,omitempty"` - Createdat time.Time `json:"createdAt,omitempty"` - Device `json:"device,omitempty"` - Frequency int `json:"frequency,omitempty"` - ID int `json:"id,omitempty"` - Locationids []string `json:"location_ids,omitempty"` - Name string `json:"name,omitempty"` - Requests `json:"requests,omitempty"` - Schedulingstrategy string `json:"schedulingStrategy,omitempty"` - Type string `json:"type,omitempty"` - Updatedat time.Time `json:"updatedAt,omitempty"` - } + Active bool `json:"active,omitempty"` + Createdat time.Time `json:"createdAt,omitempty"` + Device `json:"device,omitempty"` + Frequency int `json:"frequency,omitempty"` + ID int `json:"id,omitempty"` + Locationids []string `json:"location_ids,omitempty"` + Name string `json:"name,omitempty"` + Requests `json:"requests,omitempty"` + Schedulingstrategy string `json:"schedulingStrategy,omitempty"` + Type string `json:"type,omitempty"` + Updatedat time.Time `json:"updatedAt,omitempty"` + } } type BrowserCheckV2Input struct { Test struct { - Name string `json:"name"` - BusinessTransactions `json:"business_transactions"` - Urlprotocol string `json:"urlProtocol"` - Starturl string `json:"startUrl"` - LocationIds []string `json:"location_ids"` - DeviceID int `json:"device_id"` - Frequency int `json:"frequency"` - Schedulingstrategy string `json:"scheduling_strategy"` - Active bool `json:"active"` - Advancedsettings `json:"advanced_settings"` + Name string `json:"name"` + BusinessTransactions `json:"business_transactions"` + Urlprotocol string `json:"urlProtocol"` + Starturl string `json:"startUrl"` + LocationIds []string `json:"location_ids"` + DeviceID int `json:"device_id"` + Frequency int `json:"frequency"` + Schedulingstrategy string `json:"scheduling_strategy"` + Active bool `json:"active"` + Advancedsettings `json:"advanced_settings"` } `json:"test"` } type BrowserCheckV2Response struct { Test struct { - Active bool `json:"active"` - Advancedsettings `json:"advanced_settings"` - BusinessTransactions `json:"business_transactions"` - Createdat time.Time `json:"createdAt"` - Device `json:"device"` - Frequency int `json:"frequency"` - ID int `json:"id"` - Locationids []string `json:"location_ids"` - Name string `json:"name"` - Schedulingstrategy string `json:"scheduling_strategy"` - Transactions `json:"transactions"` - Type string `json:"type"` - Updatedat time.Time `json:"updatedAt"` + Active bool `json:"active"` + Advancedsettings `json:"advanced_settings"` + BusinessTransactions `json:"business_transactions"` + Createdat time.Time `json:"createdAt"` + Device `json:"device"` + Frequency int `json:"frequency"` + ID int `json:"id"` + Locationids []string `json:"location_ids"` + Name string `json:"name"` + Schedulingstrategy string `json:"scheduling_strategy"` + Transactions `json:"transactions"` + Type string `json:"type"` + Updatedat time.Time `json:"updatedAt"` } `json:"test"` } diff --git a/syntheticsclientv2/create_httpcheckv2.go b/syntheticsclientv2/create_httpcheckv2.go new file mode 100644 index 0000000..218efda --- /dev/null +++ b/syntheticsclientv2/create_httpcheckv2.go @@ -0,0 +1,52 @@ +// Copyright 2021 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 parseCreateHttpCheckV2Response(response string) (*HttpCheckV2Response, error) { + + var createHttpCheckV2 HttpCheckV2Response + JSONResponse := []byte(response) + err := json.Unmarshal(JSONResponse, &createHttpCheckV2) + if err != nil { + return nil, err + } + + return &createHttpCheckV2, err +} + +func (c Client) CreateHttpCheckV2(HttpCheckV2Details *HttpCheckV2Input) (*HttpCheckV2Response, *RequestDetails, error) { + + body, err := json.Marshal(HttpCheckV2Details) + if err != nil { + return nil, nil, err + } + + details, err := c.makePublicAPICall("POST", "/tests/http", bytes.NewBuffer(body), nil) + if err != nil { + return nil, details, err + } + + newHttpCheckV2, err := parseCreateHttpCheckV2Response(details.ResponseBody) + if err != nil { + return newHttpCheckV2, details, err + } + + return newHttpCheckV2, details, nil +} diff --git a/syntheticsclientv2/create_httpcheckv2_test.go b/syntheticsclientv2/create_httpcheckv2_test.go new file mode 100644 index 0000000..3dd4a00 --- /dev/null +++ b/syntheticsclientv2/create_httpcheckv2_test.go @@ -0,0 +1,110 @@ +// Copyright 2021 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" + "os" + "reflect" + "testing" +) + +var ( + createHttpCheckV2Body = `{"test":{"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{} +) + +func TestCreateHttpCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/http", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + w.Write([]byte(createHttpCheckV2Body)) + }) + + json.Unmarshal([]byte(createHttpCheckV2Body), &inputHttpCheckV2Data) + + resp, _, err := testClient.CreateHttpCheckV2(&inputHttpCheckV2Data) + + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.Test.Name, inputHttpCheckV2Data.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputHttpCheckV2Data.Test.Name) + } + + if !reflect.DeepEqual(resp.Test.Active, inputHttpCheckV2Data.Test.Active) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputHttpCheckV2Data.Test.Active) + } + + if !reflect.DeepEqual(resp.Test.LocationIds, inputHttpCheckV2Data.Test.LocationIds) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.LocationIds, inputHttpCheckV2Data.Test.LocationIds) + } + + if !reflect.DeepEqual(resp.Test.Frequency, inputHttpCheckV2Data.Test.Frequency) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputHttpCheckV2Data.Test.Frequency) + } + + if !reflect.DeepEqual(resp.Test.RequestMethod, inputHttpCheckV2Data.Test.RequestMethod) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.RequestMethod, inputHttpCheckV2Data.Test.RequestMethod) + } + + if !reflect.DeepEqual(resp.Test.Body, inputHttpCheckV2Data.Test.Body) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Body, inputHttpCheckV2Data.Test.Body) + } + + if !reflect.DeepEqual(resp.Test.SchedulingStrategy, inputHttpCheckV2Data.Test.SchedulingStrategy) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.SchedulingStrategy, inputHttpCheckV2Data.Test.SchedulingStrategy) + } + +} + +func TestLiveCreateHttpCheckV2(t *testing.T) { + setup() + defer teardown() + + json.Unmarshal([]byte(createHttpCheckV2Body), &inputHttpCheckV2Data) + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputHttpCheckV2Data) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateHttpCheckV2(&inputHttpCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} diff --git a/syntheticsclientv2/delete_httpcheckv2.go b/syntheticsclientv2/delete_httpcheckv2.go new file mode 100644 index 0000000..1451eea --- /dev/null +++ b/syntheticsclientv2/delete_httpcheckv2.go @@ -0,0 +1,40 @@ +// Copyright 2021 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) DeleteHttpCheckV2(id int) (int, error) { + requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/tests/http/%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_httpcheckv2_test.go b/syntheticsclientv2/delete_httpcheckv2_test.go new file mode 100644 index 0000000..925e159 --- /dev/null +++ b/syntheticsclientv2/delete_httpcheckv2_test.go @@ -0,0 +1,72 @@ +// Copyright 2021 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 ( + "net/http" + "testing" + "os" + "fmt" +) + +var ( + deleteHttpCheckV2RespBody = `` +) + +func TestDeleteHttpCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/http/19", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.Write([]byte(deleteHttpCheckV2RespBody)) + }) + + resp, err := testClient.DeleteHttpCheckV2(19) + if err != nil { + fmt.Println(resp) + t.Fatal(err) + } + fmt.Println(resp) +} + +func TestLiveDeleteHttpCheckV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, err := c.DeleteHttpCheckV2(1528) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} \ No newline at end of file diff --git a/syntheticsclientv2/get_httpcheckv2.go b/syntheticsclientv2/get_httpcheckv2.go new file mode 100644 index 0000000..f0517ff --- /dev/null +++ b/syntheticsclientv2/get_httpcheckv2.go @@ -0,0 +1,48 @@ +// Copyright 2021 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 parseGetHttpCheckV2Response(response string) (*HttpCheckV2Response, error) { + // Parse the response and return the user object + var HttpCheckV2 HttpCheckV2Response + err := json.Unmarshal([]byte(response), &HttpCheckV2) + if err != nil { + return nil, err + } + + return &HttpCheckV2, err +} + +func (c Client) GetHttpCheckV2(id int) (*HttpCheckV2Response, *RequestDetails, error) { + details, err := c.makePublicAPICall("GET", fmt.Sprintf("/tests/http/%d", id), bytes.NewBufferString("{}"), nil) + + // Check for errors + if err != nil { + return nil, details, err + } + + HttpCheckV2, err := parseGetHttpCheckV2Response(details.ResponseBody) + if err != nil { + return HttpCheckV2, details, err + } + + return HttpCheckV2, details, nil +} diff --git a/syntheticsclientv2/get_httpcheckv2_test.go b/syntheticsclientv2/get_httpcheckv2_test.go new file mode 100644 index 0000000..514cfa1 --- /dev/null +++ b/syntheticsclientv2/get_httpcheckv2_test.go @@ -0,0 +1,132 @@ +// Copyright 2021 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" + "os" + "reflect" + "testing" +) + +var ( + getHttpCheckV2Body = `{"test":{"active":true,"advancedSettings":{"authentication":{"password":"password123","username":"myuser"},"cookies":[{"key":"qux","value":"qux","domain":"splunk.com","path":"/qux"}],"headers":[{"name":"Accept","value":"application/json","domain":"splunk.com"}],"verifyCertificates":true},"createdAt":"2022-09-14T14:35:37.801Z","device":{"id":1,"label":"iPhone","networkConnection":{"description":"Mobile LTE","downloadBandwidth":12000,"latency":70,"packetLoss":0,"uploadBandwidth":12000},"viewportHeight":844,"viewportWidth":375},"frequency":5,"id":1,"locationIds":["na-us-virginia"],"name":"My Test","schedulingStrategy":"round_robin","transactions":[{"name":"Example transaction","steps":[{"name":"element step","selector":".main","selectorType":"css","type":"click_element","waitForNav":true}]}],"type":"browser","updatedAt":"2022-09-14T14:35:38.099Z"}}` + inputGetHttpCheckV2 = verifyHttpCheckV2Input(string(getHttpCheckV2Body)) +) + +func TestGetHttpCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/http/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.Write([]byte(getHttpCheckV2Body)) + }) + + resp, _, err := testClient.GetHttpCheckV2(1) + + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(resp.Test.ID, inputGetHttpCheckV2.Test.ID) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.ID, inputGetHttpCheckV2.Test.ID) + } + + if !reflect.DeepEqual(resp.Test.Name, inputGetHttpCheckV2.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputGetHttpCheckV2.Test.Name) + } + + if !reflect.DeepEqual(resp.Test.Type, inputGetHttpCheckV2.Test.Type) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Type, inputGetHttpCheckV2.Test.Type) + } + + if !reflect.DeepEqual(resp.Test.Frequency, inputGetHttpCheckV2.Test.Frequency) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputGetHttpCheckV2.Test.Frequency) + } + + if !reflect.DeepEqual(resp.Test.Active, inputGetHttpCheckV2.Test.Active) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputGetHttpCheckV2.Test.Active) + } + + if !reflect.DeepEqual(resp.Test.HttpHeaders, inputGetHttpCheckV2.Test.HttpHeaders) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.HttpHeaders, inputGetHttpCheckV2.Test.HttpHeaders) + } + + if !reflect.DeepEqual(resp.Test.Body, inputGetHttpCheckV2.Test.Body) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Body, inputGetHttpCheckV2.Test.Body) + } + + if !reflect.DeepEqual(resp.Test.URL, inputGetHttpCheckV2.Test.URL) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.URL, inputGetHttpCheckV2.Test.URL) + } + + if !reflect.DeepEqual(resp.Test.SchedulingStrategy, inputGetHttpCheckV2.Test.SchedulingStrategy) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.SchedulingStrategy, inputGetHttpCheckV2.Test.SchedulingStrategy) + } + + if !reflect.DeepEqual(resp.Test.RequestMethod, inputGetHttpCheckV2.Test.RequestMethod) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.RequestMethod, inputGetHttpCheckV2.Test.RequestMethod) + } + + if !reflect.DeepEqual(resp.Test.LocationIds, inputGetHttpCheckV2.Test.LocationIds) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.LocationIds, inputGetHttpCheckV2.Test.LocationIds) + } + + if !reflect.DeepEqual(resp.Test.UpdatedAt, inputGetHttpCheckV2.Test.UpdatedAt) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.UpdatedAt, inputGetHttpCheckV2.Test.UpdatedAt) + } + + if !reflect.DeepEqual(resp.Test.CreatedAt, inputGetHttpCheckV2.Test.CreatedAt) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.CreatedAt, inputGetHttpCheckV2.Test.CreatedAt) + } + +} + +func verifyHttpCheckV2Input(stringInput string) *HttpCheckV2Response { + check := &HttpCheckV2Response{} + err := json.Unmarshal([]byte(stringInput), check) + if err != nil { + panic(err) + } + return check +} + +func TestLiveGetHttpCheckV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetHttpCheckV2(1528) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} diff --git a/syntheticsclientv2/synthetics.go b/syntheticsclientv2/synthetics.go index ca95e2d..08d5bf7 100644 --- a/syntheticsclientv2/synthetics.go +++ b/syntheticsclientv2/synthetics.go @@ -28,7 +28,7 @@ import ( type Client struct { publicBaseURL string apiKey string - realm string + realm string httpClient http.Client } @@ -127,7 +127,7 @@ func NewClient(apiKey string, realm string) *Client { func NewConfigurableClient(apiKey string, realm string, args ClientArgs) *Client { client := Client{ apiKey: apiKey, - realm: realm, + realm: realm, httpClient: http.Client{Timeout: time.Duration(args.timeoutSeconds) * time.Second}, } if args.publicBaseUrl == "" { diff --git a/syntheticsclientv2/update_httpcheckv2.go b/syntheticsclientv2/update_httpcheckv2.go new file mode 100644 index 0000000..abf2860 --- /dev/null +++ b/syntheticsclientv2/update_httpcheckv2.go @@ -0,0 +1,51 @@ +// Copyright 2021 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 parseUpdateHttpCheckV2Response(response string) (*HttpCheckV2Response, error) { + var updateHttpCheckV2 HttpCheckV2Response + err := json.Unmarshal([]byte(response), &updateHttpCheckV2) + if err != nil { + return nil, err + } + + return &updateHttpCheckV2, err +} + +func (c Client) UpdateHttpCheckV2(id int, HttpCheckV2Details *HttpCheckV2Input) (*HttpCheckV2Response, *RequestDetails, error) { + + body, err := json.Marshal(HttpCheckV2Details) + if err != nil { + return nil, nil, err + } + + requestDetails, err := c.makePublicAPICall("PUT", fmt.Sprintf("/tests/http/%d", id), bytes.NewBuffer(body), nil) + if err != nil { + return nil, requestDetails, err + } + + updateHttpCheckV2, err := parseUpdateHttpCheckV2Response(requestDetails.ResponseBody) + if err != nil { + return updateHttpCheckV2, requestDetails, err + } + + return updateHttpCheckV2, requestDetails, nil +} diff --git a/syntheticsclientv2/update_httpcheckv2_test.go b/syntheticsclientv2/update_httpcheckv2_test.go new file mode 100644 index 0000000..5532307 --- /dev/null +++ b/syntheticsclientv2/update_httpcheckv2_test.go @@ -0,0 +1,53 @@ +// Copyright 2021 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 ( + updateHttpCheckV2Body = `{"test":{"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"}]}}` + inputHttpCheckV2Update = HttpCheckV2Input{} +) + +func TestUpdateHttpCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/http/10", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + w.Write([]byte(updateHttpCheckV2Body)) + }) + + json.Unmarshal([]byte(updateHttpCheckV2Body), &inputHttpCheckV2Update) + + resp, _, err := testClient.UpdateHttpCheckV2(10, &inputHttpCheckV2Update) + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.Test.Name, inputHttpCheckV2Update.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputHttpCheckV2Update.Test.Name) + } + +} + From 370890ff3b11e706488124f71772b882dc169769 Mon Sep 17 00:00:00 2001 From: jeremyh Date: Mon, 21 Nov 2022 11:06:21 -0500 Subject: [PATCH 08/15] port checks --- syntheticsclientv2/common_models.go | 32 +++++ syntheticsclientv2/create_portcheckv2.go | 52 ++++++++ syntheticsclientv2/create_portcheckv2_test.go | 110 ++++++++++++++++ syntheticsclientv2/delete_portcheckv2.go | 39 ++++++ syntheticsclientv2/delete_portcheckv2_test.go | 72 +++++++++++ syntheticsclientv2/get_portcheckv2.go | 48 +++++++ syntheticsclientv2/get_portcheckv2_test.go | 120 ++++++++++++++++++ syntheticsclientv2/update_portcheckv2.go | 51 ++++++++ syntheticsclientv2/update_portcheckv2_test.go | 52 ++++++++ 9 files changed, 576 insertions(+) create mode 100644 syntheticsclientv2/create_portcheckv2.go create mode 100644 syntheticsclientv2/create_portcheckv2_test.go create mode 100644 syntheticsclientv2/delete_portcheckv2.go create mode 100644 syntheticsclientv2/delete_portcheckv2_test.go create mode 100644 syntheticsclientv2/get_portcheckv2.go create mode 100644 syntheticsclientv2/get_portcheckv2_test.go create mode 100644 syntheticsclientv2/update_portcheckv2.go create mode 100644 syntheticsclientv2/update_portcheckv2_test.go diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go index f4c8682..f582931 100644 --- a/syntheticsclientv2/common_models.go +++ b/syntheticsclientv2/common_models.go @@ -367,6 +367,38 @@ type ChecksV2Response struct { Totalcount int `json:"totalCount"` } +type PortCheckV2Response struct { + Test struct { + ID int `json:"id"` + Name string `json:"name"` + Active bool `json:"active"` + Frequency int `json:"frequency"` + SchedulingStrategy string `json:"scheduling_strategy"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + LocationIds []string `json:"location_ids"` + Type string `json:"type"` + Protocol string `json:"protocol"` + Host string `json:"host"` + Port int `json:"port"` + } `json:"test"` +} + +type PortCheckV2Input struct { + Test struct { + Name string `json:"name"` + Type string `json:"type"` + URL string `json:"url"` + Port int `json:"port"` + Protocol string `json:"protocol"` + Host string `json:"host"` + LocationIds []string `json:"location_ids"` + Frequency int `json:"frequency"` + SchedulingStrategy string `json:"scheduling_strategy"` + Active bool `json:"active"` + } `json:"test"` +} + type HttpCheckV2Response struct { Test struct { ID int `json:"id"` diff --git a/syntheticsclientv2/create_portcheckv2.go b/syntheticsclientv2/create_portcheckv2.go new file mode 100644 index 0000000..68506d2 --- /dev/null +++ b/syntheticsclientv2/create_portcheckv2.go @@ -0,0 +1,52 @@ +// Copyright 2021 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 parseCreatePortCheckV2Response(response string) (*PortCheckV2Response, error) { + + var createPortCheckV2 PortCheckV2Response + JSONResponse := []byte(response) + err := json.Unmarshal(JSONResponse, &createPortCheckV2) + if err != nil { + return nil, err + } + + return &createPortCheckV2, err +} + +func (c Client) CreatePortCheckV2(PortCheckV2Details *PortCheckV2Input) (*PortCheckV2Response, *RequestDetails, error) { + + body, err := json.Marshal(PortCheckV2Details) + if err != nil { + return nil, nil, err + } + + details, err := c.makePublicAPICall("POST", "/tests/port", bytes.NewBuffer(body), nil) + if err != nil { + return nil, details, err + } + + newPortCheckV2, err := parseCreatePortCheckV2Response(details.ResponseBody) + if err != nil { + return newPortCheckV2, details, err + } + + return newPortCheckV2, details, nil +} diff --git a/syntheticsclientv2/create_portcheckv2_test.go b/syntheticsclientv2/create_portcheckv2_test.go new file mode 100644 index 0000000..ac934d4 --- /dev/null +++ b/syntheticsclientv2/create_portcheckv2_test.go @@ -0,0 +1,110 @@ +// Copyright 2021 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" + "os" + "reflect" + "testing" +) + +var ( + createPortCheckV2Body = `{"test":{"name":"splunk - port 443","type":"port","url":"","port":443,"protocol":"tcp","host":"www.splunk.com","location_ids":["aws-us-east-1"],"frequency":10,"scheduling_strategy":"round_robin","active":true}}` + inputPortCheckV2Data = PortCheckV2Input{} +) + +func TestCreatePortCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/port", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + w.Write([]byte(createPortCheckV2Body)) + }) + + json.Unmarshal([]byte(createPortCheckV2Body), &inputPortCheckV2Data) + + resp, _, err := testClient.CreatePortCheckV2(&inputPortCheckV2Data) + + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.Test.Name, inputGetPortCheckV2.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputGetPortCheckV2.Test.Name) + } + + if !reflect.DeepEqual(resp.Test.Type, inputGetPortCheckV2.Test.Type) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Type, inputGetPortCheckV2.Test.Type) + } + + if !reflect.DeepEqual(resp.Test.Frequency, inputGetPortCheckV2.Test.Frequency) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputGetPortCheckV2.Test.Frequency) + } + + if !reflect.DeepEqual(resp.Test.Active, inputGetPortCheckV2.Test.Active) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputGetPortCheckV2.Test.Active) + } + + if !reflect.DeepEqual(resp.Test.Protocol, inputGetPortCheckV2.Test.Protocol) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Protocol, inputGetPortCheckV2.Test.Protocol) + } + + if !reflect.DeepEqual(resp.Test.Host, inputGetPortCheckV2.Test.Host) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Host, inputGetPortCheckV2.Test.Host) + } + + if !reflect.DeepEqual(resp.Test.Port, inputGetPortCheckV2.Test.Port) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Port, inputGetPortCheckV2.Test.Port) + } + +} + +func TestLiveCreatePortCheckV2(t *testing.T) { + setup() + defer teardown() + + json.Unmarshal([]byte(createPortCheckV2Body), &inputPortCheckV2Data) + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputPortCheckV2Data) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreatePortCheckV2(&inputPortCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} diff --git a/syntheticsclientv2/delete_portcheckv2.go b/syntheticsclientv2/delete_portcheckv2.go new file mode 100644 index 0000000..2860d1f --- /dev/null +++ b/syntheticsclientv2/delete_portcheckv2.go @@ -0,0 +1,39 @@ +// Copyright 2021 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) DeletePortCheckV2(id int) (int, error) { + requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/tests/port/%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_portcheckv2_test.go b/syntheticsclientv2/delete_portcheckv2_test.go new file mode 100644 index 0000000..e504ef7 --- /dev/null +++ b/syntheticsclientv2/delete_portcheckv2_test.go @@ -0,0 +1,72 @@ +// Copyright 2021 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" + "os" + "testing" +) + +var ( + deletePortCheckV2RespBody = `` +) + +func TestDeletePortCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/browser/19", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.Write([]byte(deletePortCheckV2RespBody)) + }) + + resp, err := testClient.DeletePortCheckV2(19) + if err != nil { + fmt.Println(resp) + t.Fatal(err) + } + fmt.Println(resp) +} + +func TestLiveDeletePortCheckV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, err := c.DeletePortCheckV2(1649) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} diff --git a/syntheticsclientv2/get_portcheckv2.go b/syntheticsclientv2/get_portcheckv2.go new file mode 100644 index 0000000..4655e08 --- /dev/null +++ b/syntheticsclientv2/get_portcheckv2.go @@ -0,0 +1,48 @@ +// Copyright 2021 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 parseGetPortCheckV2Response(response string) (*PortCheckV2Response, error) { + // Parse the response and return the user object + var PortCheckV2 PortCheckV2Response + err := json.Unmarshal([]byte(response), &PortCheckV2) + if err != nil { + return nil, err + } + + return &PortCheckV2, err +} + +func (c Client) GetPortCheckV2(id int) (*PortCheckV2Response, *RequestDetails, error) { + details, err := c.makePublicAPICall("GET", fmt.Sprintf("/tests/port/%d", id), bytes.NewBufferString("{}"), nil) + + // Check for errors + if err != nil { + return nil, details, err + } + + PortCheckV2, err := parseGetPortCheckV2Response(details.ResponseBody) + if err != nil { + return PortCheckV2, details, err + } + + return PortCheckV2, details, nil +} diff --git a/syntheticsclientv2/get_portcheckv2_test.go b/syntheticsclientv2/get_portcheckv2_test.go new file mode 100644 index 0000000..2c9b4ee --- /dev/null +++ b/syntheticsclientv2/get_portcheckv2_test.go @@ -0,0 +1,120 @@ +// Copyright 2021 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" + "os" + "reflect" + "testing" +) + +var ( + getPortCheckV2Body = `{"test":{"id":1647,"name":"splunk - port 443","active":true,"frequency":10,"scheduling_strategy":"round_robin","created_at":"2022-11-21T15:38:54.546Z","updated_at":"2022-11-21T15:38:54.554Z","location_ids":["aws-us-east-1"],"type":"port","protocol":"tcp","host":"www.splunk.com","port":443}}` + inputGetPortCheckV2 = verifyPortCheckV2Input(string(getPortCheckV2Body)) +) + +func TestGetPortCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/port/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.Write([]byte(getPortCheckV2Body)) + }) + + resp, _, err := testClient.GetPortCheckV2(1) + + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(resp.Test.ID, inputGetPortCheckV2.Test.ID) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.ID, inputGetPortCheckV2.Test.ID) + } + + if !reflect.DeepEqual(resp.Test.Name, inputGetPortCheckV2.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputGetPortCheckV2.Test.Name) + } + + if !reflect.DeepEqual(resp.Test.Type, inputGetPortCheckV2.Test.Type) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Type, inputGetPortCheckV2.Test.Type) + } + + if !reflect.DeepEqual(resp.Test.Frequency, inputGetPortCheckV2.Test.Frequency) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputGetPortCheckV2.Test.Frequency) + } + + if !reflect.DeepEqual(resp.Test.Active, inputGetPortCheckV2.Test.Active) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputGetPortCheckV2.Test.Active) + } + + if !reflect.DeepEqual(resp.Test.CreatedAt, inputGetPortCheckV2.Test.CreatedAt) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.CreatedAt, inputGetPortCheckV2.Test.CreatedAt) + } + + if !reflect.DeepEqual(resp.Test.UpdatedAt, inputGetPortCheckV2.Test.UpdatedAt) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.UpdatedAt, inputGetPortCheckV2.Test.UpdatedAt) + } + + if !reflect.DeepEqual(resp.Test.Protocol, inputGetPortCheckV2.Test.Protocol) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Protocol, inputGetPortCheckV2.Test.Protocol) + } + + if !reflect.DeepEqual(resp.Test.Host, inputGetPortCheckV2.Test.Host) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Host, inputGetPortCheckV2.Test.Host) + } + + if !reflect.DeepEqual(resp.Test.Port, inputGetPortCheckV2.Test.Port) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Port, inputGetPortCheckV2.Test.Port) + } + +} + +func verifyPortCheckV2Input(stringInput string) *PortCheckV2Response { + check := &PortCheckV2Response{} + err := json.Unmarshal([]byte(stringInput), check) + if err != nil { + panic(err) + } + return check +} + +func TestLiveGetPortCheckV2(t *testing.T) { + setup() + defer teardown() + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetPortCheckV2(1647) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} diff --git a/syntheticsclientv2/update_portcheckv2.go b/syntheticsclientv2/update_portcheckv2.go new file mode 100644 index 0000000..dfc1a0c --- /dev/null +++ b/syntheticsclientv2/update_portcheckv2.go @@ -0,0 +1,51 @@ +// Copyright 2021 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 parseUpdatePortCheckV2Response(response string) (*PortCheckV2Response, error) { + var updatePortCheckV2 PortCheckV2Response + err := json.Unmarshal([]byte(response), &updatePortCheckV2) + if err != nil { + return nil, err + } + + return &updatePortCheckV2, err +} + +func (c Client) UpdatePortCheckV2(id int, PortCheckV2Details *PortCheckV2Input) (*PortCheckV2Response, *RequestDetails, error) { + + body, err := json.Marshal(PortCheckV2Details) + if err != nil { + return nil, nil, err + } + + requestDetails, err := c.makePublicAPICall("PUT", fmt.Sprintf("/tests/port/%d", id), bytes.NewBuffer(body), nil) + if err != nil { + return nil, requestDetails, err + } + + updatePortCheckV2, err := parseUpdatePortCheckV2Response(requestDetails.ResponseBody) + if err != nil { + return updatePortCheckV2, requestDetails, err + } + + return updatePortCheckV2, requestDetails, nil +} diff --git a/syntheticsclientv2/update_portcheckv2_test.go b/syntheticsclientv2/update_portcheckv2_test.go new file mode 100644 index 0000000..58869ca --- /dev/null +++ b/syntheticsclientv2/update_portcheckv2_test.go @@ -0,0 +1,52 @@ +// Copyright 2021 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 ( + updatePortCheckV2Body = `{"test":{"name":"splunk - port 443","type":"port","url":"","port":80,"protocol":"tcp","host":"www.splunk.com","location_ids":["aws-us-east-1"],"frequency":10,"scheduling_strategy":"round_robin","active":true}}` + inputPortCheckV2Update = PortCheckV2Input{} +) + +func TestUpdatePortCheckV2(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/tests/port/1650", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + w.Write([]byte(updatePortCheckV2Body)) + }) + + json.Unmarshal([]byte(updatePortCheckV2Body), &inputPortCheckV2Update) + + resp, _, err := testClient.UpdatePortCheckV2(1650, &inputPortCheckV2Update) + if err != nil { + t.Fatal(err) + } + + fmt.Println(resp) + + if !reflect.DeepEqual(resp.Test.Name, inputPortCheckV2Update.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputPortCheckV2Update.Test.Name) + } + +} From 3ca93ec0d5a8a775a4a70784f2a6669679ae9c13 Mon Sep 17 00:00:00 2001 From: jeremyh Date: Mon, 21 Nov 2022 13:55:36 -0500 Subject: [PATCH 09/15] fix testing and move integration tests to own excluded file --- .gitignore | 5 +- Makefile | 12 +- syntheticsclientv2/create_apicheckv2.go | 1 - syntheticsclientv2/create_apicheckv2_test.go | 55 +- .../create_browsercheckv2_test.go | 55 +- syntheticsclientv2/create_httpcheckv2_test.go | 46 +- syntheticsclientv2/create_portcheckv2_test.go | 74 +-- syntheticsclientv2/create_variablev2_test.go | 55 +- syntheticsclientv2/delete_apicheckv2.go | 5 +- syntheticsclientv2/delete_apicheckv2_test.go | 40 +- syntheticsclientv2/delete_browsercheckv2.go | 5 +- .../delete_browsercheckv2_test.go | 40 +- syntheticsclientv2/delete_httpcheckv2.go | 5 +- syntheticsclientv2/delete_httpcheckv2_test.go | 40 +- syntheticsclientv2/delete_portcheckv2_test.go | 40 +- syntheticsclientv2/delete_variablesv2_test.go | 40 +- syntheticsclientv2/delete_variablev2.go | 5 +- syntheticsclientv2/get_apicheckv2.go | 1 - syntheticsclientv2/get_apicheckv2_test.go | 36 +- syntheticsclientv2/get_browsercheckv2_test.go | 36 +- syntheticsclientv2/get_checksv2.go | 1 - syntheticsclientv2/get_checksv2_test.go | 47 +- syntheticsclientv2/get_httpcheckv2_test.go | 36 +- syntheticsclientv2/get_portcheckv2_test.go | 36 +- syntheticsclientv2/get_variablev2.go | 1 - syntheticsclientv2/get_variablev2_test.go | 36 +- syntheticsclientv2/integration_test.go | 469 ++++++++++++++++++ syntheticsclientv2/synthetics_test.go | 16 +- syntheticsclientv2/update_apicheckv2_test.go | 16 +- .../update_browsercheckv2_test.go | 16 +- syntheticsclientv2/update_httpcheckv2_test.go | 16 +- syntheticsclientv2/update_portcheckv2_test.go | 13 +- syntheticsclientv2/update_variablesv2_test.go | 16 +- 33 files changed, 727 insertions(+), 588 deletions(-) create mode 100644 syntheticsclientv2/integration_test.go diff --git a/.gitignore b/.gitignore index f6cce3e..1ea71a6 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,7 @@ .history/ # Built Visual Studio Code Extensions -*.vsix \ No newline at end of file +*.vsix + +.DS_Store +.vscode/settings.json diff --git a/Makefile b/Makefile index 7267b6a..3c45ba5 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,14 @@ .PHONY: default all build clean test fmtcheck testacc sonarqube -PKG_NAME=syntheticsclient -FILES=./... +PKG_NAME=syntheticsclientv2 +FILES=./syntheticsclientv2/... -default: test +default: test all: clean build test build: fmtcheck - go build + go build -tags=unit_tests clean: @echo "==> Cleaning out old builds " @@ -28,11 +28,11 @@ fmtcheck: fmt lint test: fmtcheck @echo "==> Running all tests" - go test $(FILES) -v -timeout=30s -parallel=4 -cover + go test $(FILES) -v -tags=unit_tests -timeout=30s -parallel=4 -cover testacc: clean fmtcheck @echo "==> Running all tests" - go test $(FILES) -v -timeout=30s -parallel=8 -cover -coverprofile coverage.txt + go test $(FILES) -v -tags=unit_tests -timeout=30s -parallel=8 -cover -coverprofile coverage.txt sonarqube: testacc docker run -it -v "${PWD}:/usr/src" sonarsource/sonar-scanner-cli diff --git a/syntheticsclientv2/create_apicheckv2.go b/syntheticsclientv2/create_apicheckv2.go index 14ea93f..62da946 100644 --- a/syntheticsclientv2/create_apicheckv2.go +++ b/syntheticsclientv2/create_apicheckv2.go @@ -19,7 +19,6 @@ import ( "encoding/json" ) - func parseCreateApiCheckV2Response(response string) (*ApiCheckV2Response, error) { var createApiCheckV2 ApiCheckV2Response diff --git a/syntheticsclientv2/create_apicheckv2_test.go b/syntheticsclientv2/create_apicheckv2_test.go index 56c410f..9eb1e60 100644 --- a/syntheticsclientv2/create_apicheckv2_test.go +++ b/syntheticsclientv2/create_apicheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,17 +18,16 @@ package syntheticsclientv2 import ( + "encoding/json" + "fmt" "net/http" "reflect" "testing" - "fmt" - "encoding/json" - "os" ) var ( - createApiV2Body = `{"test":{"active":true,"device_id":1,"frequency":5,"location_ids":["aws-us-east-1"],"name":"boop-test","scheduling_strategy":"round_robin","requests":[{"configuration":{"name":"Get-Test","requestMethod":"GET","url":"https://api.us1.signalfx.com/v2/synthetics/tests/api/489","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"}]}]}}` - inputData = ApiCheckV2Input{} + createApiV2Body = `{"test":{"active":true,"device_id":1,"frequency":5,"location_ids":["aws-us-east-1"],"name":"boop-test","scheduling_strategy":"round_robin","requests":[{"configuration":{"name":"Get-Test","requestMethod":"GET","url":"https://api.us1.signalfx.com/v2/synthetics/tests/api/489","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"}]}]}}` + inputData = ApiCheckV2Input{} ) func TestCreateApiCheckV2(t *testing.T) { @@ -34,10 +36,16 @@ func TestCreateApiCheckV2(t *testing.T) { testMux.HandleFunc("/tests/api", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - w.Write([]byte(createApiV2Body)) + _, err := w.Write([]byte(createApiV2Body)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(createApiV2Body), &inputData) + err := json.Unmarshal([]byte(createApiV2Body), &inputData) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.CreateApiCheckV2(&inputData) @@ -72,36 +80,3 @@ func TestCreateApiCheckV2(t *testing.T) { } } - - -func TestLiveCreateApiCheckV2(t *testing.T) { - setup() - defer teardown() - - json.Unmarshal([]byte(createApiV2Body), &inputData) - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(c) - fmt.Println(inputData) - - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateApiCheckV2(&inputData) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} \ No newline at end of file diff --git a/syntheticsclientv2/create_browsercheckv2_test.go b/syntheticsclientv2/create_browsercheckv2_test.go index c6777dd..1bac9e1 100644 --- a/syntheticsclientv2/create_browsercheckv2_test.go +++ b/syntheticsclientv2/create_browsercheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,17 +18,16 @@ package syntheticsclientv2 import ( + "encoding/json" + "fmt" "net/http" "reflect" "testing" - "fmt" - "encoding/json" - "os" ) var ( - createBrowserCheckV2Body = `{"test":{"name":"browser-beep-test","business_transactions":[{"name":"Synthetic transaction 1","steps":[{"name":"Go to URL","type":"go_to_url","url":"https://www.splunk.com","action":"go_to_url","wait_for_nav":true},{"name":"Nexter step","type":"click_element","selector_type":"id","wait_for_nav":false,"selector":"free-splunk-click-desktop"}]}],"urlProtocol":"https://","startUrl":"www.splunk.com","location_ids":["aws-us-east-1"],"device_id":1,"frequency":5,"scheduling_strategy":"round_robin","active":true,"advanced_settings":{"verify_certificates":true,"authentication":{"username":"boopuser","password":"{{env.beep-var}}"},"headers":[{"name":"batman","value":"Agentoz","domain":"www.batmansagent.com"}],"cookies":[{"key":"super","value":"duper","domain":"www.batmansagent.com","path":"/boom/goes/beep"}]}}}` - inputBrowserCheckV2Data = BrowserCheckV2Input{} + createBrowserCheckV2Body = `{"test":{"name":"browser-beep-test","business_transactions":[{"name":"Synthetic transaction 1","steps":[{"name":"Go to URL","type":"go_to_url","url":"https://www.splunk.com","action":"go_to_url","wait_for_nav":true},{"name":"Nexter step","type":"click_element","selector_type":"id","wait_for_nav":false,"selector":"free-splunk-click-desktop"}]}],"urlProtocol":"https://","startUrl":"www.splunk.com","location_ids":["aws-us-east-1"],"device_id":1,"frequency":5,"scheduling_strategy":"round_robin","active":true,"advanced_settings":{"verify_certificates":true,"authentication":{"username":"boopuser","password":"{{env.beep-var}}"},"headers":[{"name":"batman","value":"Agentoz","domain":"www.batmansagent.com"}],"cookies":[{"key":"super","value":"duper","domain":"www.batmansagent.com","path":"/boom/goes/beep"}]}}}` + inputBrowserCheckV2Data = BrowserCheckV2Input{} ) func TestCreateBrowserCheckV2(t *testing.T) { @@ -34,10 +36,16 @@ func TestCreateBrowserCheckV2(t *testing.T) { testMux.HandleFunc("/tests/browser", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - w.Write([]byte(createBrowserCheckV2Body)) + _, err := w.Write([]byte(createBrowserCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(createBrowserCheckV2Body), &inputBrowserCheckV2Data) + err := json.Unmarshal([]byte(createBrowserCheckV2Body), &inputBrowserCheckV2Data) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.CreateBrowserCheckV2(&inputBrowserCheckV2Data) @@ -76,36 +84,3 @@ func TestCreateBrowserCheckV2(t *testing.T) { } } - - -func TestLiveCreateBrowserCheckV2(t *testing.T) { - setup() - defer teardown() - - json.Unmarshal([]byte(createBrowserCheckV2Body), &inputBrowserCheckV2Data) - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(c) - fmt.Println(inputBrowserCheckV2Data) - - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateBrowserCheckV2(&inputBrowserCheckV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} \ No newline at end of file diff --git a/syntheticsclientv2/create_httpcheckv2_test.go b/syntheticsclientv2/create_httpcheckv2_test.go index 3dd4a00..aebaf9c 100644 --- a/syntheticsclientv2/create_httpcheckv2_test.go +++ b/syntheticsclientv2/create_httpcheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,7 +21,6 @@ import ( "encoding/json" "fmt" "net/http" - "os" "reflect" "testing" ) @@ -34,10 +36,16 @@ func TestCreateHttpCheckV2(t *testing.T) { testMux.HandleFunc("/tests/http", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - w.Write([]byte(createHttpCheckV2Body)) + _, err := w.Write([]byte(createHttpCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(createHttpCheckV2Body), &inputHttpCheckV2Data) + err := json.Unmarshal([]byte(createHttpCheckV2Body), &inputHttpCheckV2Data) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.CreateHttpCheckV2(&inputHttpCheckV2Data) @@ -76,35 +84,3 @@ func TestCreateHttpCheckV2(t *testing.T) { } } - -func TestLiveCreateHttpCheckV2(t *testing.T) { - setup() - defer teardown() - - json.Unmarshal([]byte(createHttpCheckV2Body), &inputHttpCheckV2Data) - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(c) - fmt.Println(inputHttpCheckV2Data) - - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateHttpCheckV2(&inputHttpCheckV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} diff --git a/syntheticsclientv2/create_portcheckv2_test.go b/syntheticsclientv2/create_portcheckv2_test.go index ac934d4..fed9cab 100644 --- a/syntheticsclientv2/create_portcheckv2_test.go +++ b/syntheticsclientv2/create_portcheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,7 +21,6 @@ import ( "encoding/json" "fmt" "net/http" - "os" "reflect" "testing" ) @@ -34,10 +36,16 @@ func TestCreatePortCheckV2(t *testing.T) { testMux.HandleFunc("/tests/port", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - w.Write([]byte(createPortCheckV2Body)) + _, err := w.Write([]byte(createPortCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(createPortCheckV2Body), &inputPortCheckV2Data) + err := json.Unmarshal([]byte(createPortCheckV2Body), &inputPortCheckV2Data) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.CreatePortCheckV2(&inputPortCheckV2Data) @@ -47,64 +55,32 @@ func TestCreatePortCheckV2(t *testing.T) { fmt.Println(resp) - if !reflect.DeepEqual(resp.Test.Name, inputGetPortCheckV2.Test.Name) { - t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputGetPortCheckV2.Test.Name) - } - - if !reflect.DeepEqual(resp.Test.Type, inputGetPortCheckV2.Test.Type) { - t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Type, inputGetPortCheckV2.Test.Type) - } - - if !reflect.DeepEqual(resp.Test.Frequency, inputGetPortCheckV2.Test.Frequency) { - t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputGetPortCheckV2.Test.Frequency) + if !reflect.DeepEqual(resp.Test.Name, inputPortCheckV2Data.Test.Name) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Name, inputPortCheckV2Data.Test.Name) } - if !reflect.DeepEqual(resp.Test.Active, inputGetPortCheckV2.Test.Active) { - t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputGetPortCheckV2.Test.Active) + if !reflect.DeepEqual(resp.Test.Type, inputPortCheckV2Data.Test.Type) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Type, inputPortCheckV2Data.Test.Type) } - if !reflect.DeepEqual(resp.Test.Protocol, inputGetPortCheckV2.Test.Protocol) { - t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Protocol, inputGetPortCheckV2.Test.Protocol) + if !reflect.DeepEqual(resp.Test.Frequency, inputPortCheckV2Data.Test.Frequency) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Frequency, inputPortCheckV2Data.Test.Frequency) } - if !reflect.DeepEqual(resp.Test.Host, inputGetPortCheckV2.Test.Host) { - t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Host, inputGetPortCheckV2.Test.Host) + if !reflect.DeepEqual(resp.Test.Active, inputPortCheckV2Data.Test.Active) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Active, inputPortCheckV2Data.Test.Active) } - if !reflect.DeepEqual(resp.Test.Port, inputGetPortCheckV2.Test.Port) { - t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Port, inputGetPortCheckV2.Test.Port) + if !reflect.DeepEqual(resp.Test.Protocol, inputPortCheckV2Data.Test.Protocol) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Protocol, inputPortCheckV2Data.Test.Protocol) } -} - -func TestLiveCreatePortCheckV2(t *testing.T) { - setup() - defer teardown() - - json.Unmarshal([]byte(createPortCheckV2Body), &inputPortCheckV2Data) - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(c) - fmt.Println(inputPortCheckV2Data) - - // Make the request with your check settings and print result - res, reqDetail, err := c.CreatePortCheckV2(&inputPortCheckV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) + if !reflect.DeepEqual(resp.Test.Host, inputPortCheckV2Data.Test.Host) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Host, inputPortCheckV2Data.Test.Host) } - if err != nil { - t.Fatal(err) + if !reflect.DeepEqual(resp.Test.Port, inputPortCheckV2Data.Test.Port) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Port, inputPortCheckV2Data.Test.Port) } } diff --git a/syntheticsclientv2/create_variablev2_test.go b/syntheticsclientv2/create_variablev2_test.go index aa8afae..5964019 100644 --- a/syntheticsclientv2/create_variablev2_test.go +++ b/syntheticsclientv2/create_variablev2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,17 +18,16 @@ package syntheticsclientv2 import ( + "encoding/json" + "fmt" "net/http" "reflect" "testing" - "fmt" - "encoding/json" - "os" ) var ( - createVariableV2Body = `{"variable":{"description":"My super awesome test variable","name":"food","secret":false,"value":"bar"}}` - inputVariableV2Data = VariableV2Input{} + createVariableV2Body = `{"variable":{"description":"My super awesome test variable","name":"food","secret":false,"value":"bar"}}` + inputVariableV2Data = VariableV2Input{} ) func TestCreateVariableV2(t *testing.T) { @@ -34,10 +36,16 @@ func TestCreateVariableV2(t *testing.T) { testMux.HandleFunc("/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - w.Write([]byte(createVariableV2Body)) + _, err := w.Write([]byte(createVariableV2Body)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(createVariableV2Body), &inputVariableV2Data) + err := json.Unmarshal([]byte(createVariableV2Body), &inputVariableV2Data) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.CreateVariableV2(&inputVariableV2Data) @@ -68,36 +76,3 @@ func TestCreateVariableV2(t *testing.T) { } } - - -func TestLiveCreateVariableV2(t *testing.T) { - setup() - defer teardown() - - json.Unmarshal([]byte(createVariableV2Body), &inputVariableV2Data) - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(c) - fmt.Println(inputVariableV2Data) - - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateVariableV2(&inputVariableV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} \ No newline at end of file diff --git a/syntheticsclientv2/delete_apicheckv2.go b/syntheticsclientv2/delete_apicheckv2.go index dd3a7cd..0e17655 100644 --- a/syntheticsclientv2/delete_apicheckv2.go +++ b/syntheticsclientv2/delete_apicheckv2.go @@ -21,13 +21,12 @@ import ( "strconv" ) - func (c Client) DeleteApiCheckV2(id int) (int, error) { requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/tests/api/%d", id), bytes.NewBufferString("{}"), nil) if err != nil { return 1, err } - var status = requestDetails.StatusCode + var status = requestDetails.StatusCode fmt.Println(status) @@ -35,6 +34,6 @@ func (c Client) DeleteApiCheckV2(id int) (int, error) { 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_apicheckv2_test.go b/syntheticsclientv2/delete_apicheckv2_test.go index 7f373e9..066b8ee 100644 --- a/syntheticsclientv2/delete_apicheckv2_test.go +++ b/syntheticsclientv2/delete_apicheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,10 +18,9 @@ package syntheticsclientv2 import ( + "fmt" "net/http" "testing" - "os" - "fmt" ) var ( @@ -31,7 +33,10 @@ func TestDeleteApiCheckV2(t *testing.T) { testMux.HandleFunc("/tests/api/19", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - w.Write([]byte(deleteApiCheckV2RespBody)) + _, err := w.Write([]byte(deleteApiCheckV2RespBody)) + if err != nil { + t.Fatal(err) + } }) resp, err := testClient.DeleteApiCheckV2(19) @@ -41,32 +46,3 @@ func TestDeleteApiCheckV2(t *testing.T) { } fmt.Println(resp) } - -func TestLiveDeleteApiCheckV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(c) - fmt.Println(inputData) - - // Make the request with your check settings and print result - res, err := c.DeleteApiCheckV2(1093) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} \ No newline at end of file diff --git a/syntheticsclientv2/delete_browsercheckv2.go b/syntheticsclientv2/delete_browsercheckv2.go index 8db02ec..1bc7fe8 100644 --- a/syntheticsclientv2/delete_browsercheckv2.go +++ b/syntheticsclientv2/delete_browsercheckv2.go @@ -21,13 +21,12 @@ import ( "strconv" ) - func (c Client) DeleteBrowserCheckV2(id int) (int, error) { requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/tests/browser/%d", id), bytes.NewBufferString("{}"), nil) if err != nil { return 1, err } - var status = requestDetails.StatusCode + var status = requestDetails.StatusCode fmt.Println(status) @@ -35,6 +34,6 @@ func (c Client) DeleteBrowserCheckV2(id int) (int, error) { 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_browsercheckv2_test.go b/syntheticsclientv2/delete_browsercheckv2_test.go index e7f6d2c..764fa20 100644 --- a/syntheticsclientv2/delete_browsercheckv2_test.go +++ b/syntheticsclientv2/delete_browsercheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,10 +18,9 @@ package syntheticsclientv2 import ( + "fmt" "net/http" "testing" - "os" - "fmt" ) var ( @@ -31,7 +33,10 @@ func TestDeleteBrowserCheckV2(t *testing.T) { testMux.HandleFunc("/tests/browser/19", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - w.Write([]byte(deleteBrowserCheckV2RespBody)) + _, err := w.Write([]byte(deleteBrowserCheckV2RespBody)) + if err != nil { + t.Fatal(err) + } }) resp, err := testClient.DeleteBrowserCheckV2(19) @@ -41,32 +46,3 @@ func TestDeleteBrowserCheckV2(t *testing.T) { } fmt.Println(resp) } - -func TestLiveDeleteBrowserCheckV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(c) - fmt.Println(inputData) - - // Make the request with your check settings and print result - res, err := c.DeleteBrowserCheckV2(1115) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} \ No newline at end of file diff --git a/syntheticsclientv2/delete_httpcheckv2.go b/syntheticsclientv2/delete_httpcheckv2.go index 1451eea..3d21fba 100644 --- a/syntheticsclientv2/delete_httpcheckv2.go +++ b/syntheticsclientv2/delete_httpcheckv2.go @@ -21,13 +21,12 @@ import ( "strconv" ) - func (c Client) DeleteHttpCheckV2(id int) (int, error) { requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/tests/http/%d", id), bytes.NewBufferString("{}"), nil) if err != nil { return 1, err } - var status = requestDetails.StatusCode + var status = requestDetails.StatusCode fmt.Println(status) @@ -35,6 +34,6 @@ func (c Client) DeleteHttpCheckV2(id int) (int, error) { 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_httpcheckv2_test.go b/syntheticsclientv2/delete_httpcheckv2_test.go index 925e159..8e6221b 100644 --- a/syntheticsclientv2/delete_httpcheckv2_test.go +++ b/syntheticsclientv2/delete_httpcheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,10 +18,9 @@ package syntheticsclientv2 import ( + "fmt" "net/http" "testing" - "os" - "fmt" ) var ( @@ -31,7 +33,10 @@ func TestDeleteHttpCheckV2(t *testing.T) { testMux.HandleFunc("/tests/http/19", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - w.Write([]byte(deleteHttpCheckV2RespBody)) + _, err := w.Write([]byte(deleteHttpCheckV2RespBody)) + if err != nil { + t.Fatal(err) + } }) resp, err := testClient.DeleteHttpCheckV2(19) @@ -41,32 +46,3 @@ func TestDeleteHttpCheckV2(t *testing.T) { } fmt.Println(resp) } - -func TestLiveDeleteHttpCheckV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(c) - fmt.Println(inputData) - - // Make the request with your check settings and print result - res, err := c.DeleteHttpCheckV2(1528) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} \ No newline at end of file diff --git a/syntheticsclientv2/delete_portcheckv2_test.go b/syntheticsclientv2/delete_portcheckv2_test.go index e504ef7..00d1d24 100644 --- a/syntheticsclientv2/delete_portcheckv2_test.go +++ b/syntheticsclientv2/delete_portcheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,7 +20,6 @@ package syntheticsclientv2 import ( "fmt" "net/http" - "os" "testing" ) @@ -29,9 +31,12 @@ func TestDeletePortCheckV2(t *testing.T) { setup() defer teardown() - testMux.HandleFunc("/tests/browser/19", func(w http.ResponseWriter, r *http.Request) { + testMux.HandleFunc("/tests/port/19", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - w.Write([]byte(deletePortCheckV2RespBody)) + _, err := w.Write([]byte(deletePortCheckV2RespBody)) + if err != nil { + t.Fatal(err) + } }) resp, err := testClient.DeletePortCheckV2(19) @@ -41,32 +46,3 @@ func TestDeletePortCheckV2(t *testing.T) { } fmt.Println(resp) } - -func TestLiveDeletePortCheckV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(c) - fmt.Println(inputData) - - // Make the request with your check settings and print result - res, err := c.DeletePortCheckV2(1649) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} diff --git a/syntheticsclientv2/delete_variablesv2_test.go b/syntheticsclientv2/delete_variablesv2_test.go index 40cc3fd..29ac73d 100644 --- a/syntheticsclientv2/delete_variablesv2_test.go +++ b/syntheticsclientv2/delete_variablesv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,10 +18,9 @@ package syntheticsclientv2 import ( + "fmt" "net/http" "testing" - "os" - "fmt" ) var ( @@ -31,7 +33,10 @@ func TestDeleteVariableV2(t *testing.T) { testMux.HandleFunc("/variables/19", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - w.Write([]byte(deleteVariableV2RespBody)) + _, err := w.Write([]byte(deleteVariableV2RespBody)) + if err != nil { + t.Fatal(err) + } }) resp, err := testClient.DeleteVariableV2(19) @@ -41,32 +46,3 @@ func TestDeleteVariableV2(t *testing.T) { } fmt.Println(resp) } - -func TestLiveDeleteVariableV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(c) - fmt.Println(inputData) - - // Make the request with your check settings and print result - res, err := c.DeleteVariableV2(254) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} \ No newline at end of file diff --git a/syntheticsclientv2/delete_variablev2.go b/syntheticsclientv2/delete_variablev2.go index 75890dc..786071d 100644 --- a/syntheticsclientv2/delete_variablev2.go +++ b/syntheticsclientv2/delete_variablev2.go @@ -21,13 +21,12 @@ import ( "strconv" ) - func (c Client) DeleteVariableV2(id int) (int, error) { requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/variables/%d", id), bytes.NewBufferString("{}"), nil) if err != nil { return 1, err } - var status = requestDetails.StatusCode + var status = requestDetails.StatusCode fmt.Println(status) @@ -35,6 +34,6 @@ func (c Client) DeleteVariableV2(id int) (int, error) { errorMsg := fmt.Sprintf("error: Response code %v. Expecting 2XX.", strconv.Itoa(status)) return status, errors.New(errorMsg) } - + return status, err } diff --git a/syntheticsclientv2/get_apicheckv2.go b/syntheticsclientv2/get_apicheckv2.go index 8493af7..390f1a0 100644 --- a/syntheticsclientv2/get_apicheckv2.go +++ b/syntheticsclientv2/get_apicheckv2.go @@ -20,7 +20,6 @@ import ( "fmt" ) - func parseApiCheckV2Response(response string) (*ApiCheckV2Response, error) { // Parse the response and return the check object var check ApiCheckV2Response diff --git a/syntheticsclientv2/get_apicheckv2_test.go b/syntheticsclientv2/get_apicheckv2_test.go index feb5ab1..b1d9339 100644 --- a/syntheticsclientv2/get_apicheckv2_test.go +++ b/syntheticsclientv2/get_apicheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,8 +22,6 @@ import ( "net/http" "reflect" "testing" - "os" - "fmt" ) var ( @@ -34,7 +35,10 @@ func TestGetApiCheckV2(t *testing.T) { testMux.HandleFunc("/tests/api/489", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - w.Write([]byte(getApiCheckV2Body)) + _, err := w.Write([]byte(getApiCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) resp, _, err := testClient.GetApiCheckV2(489) @@ -88,29 +92,3 @@ func verifyApiCheckV2Input(stringInput string) *ApiCheckV2Response { } return check } - -func TestLiveGetApiCheckV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - // Make the request with your check settings and print result - res, _, err := c.GetApiCheckV2(489) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} diff --git a/syntheticsclientv2/get_browsercheckv2_test.go b/syntheticsclientv2/get_browsercheckv2_test.go index ad47354..cedcdc7 100644 --- a/syntheticsclientv2/get_browsercheckv2_test.go +++ b/syntheticsclientv2/get_browsercheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,8 +22,6 @@ import ( "net/http" "reflect" "testing" - "os" - "fmt" ) var ( @@ -34,7 +35,10 @@ func TestGetBrowserCheckV2(t *testing.T) { testMux.HandleFunc("/tests/browser/1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - w.Write([]byte(getBrowserCheckV2Body)) + _, err := w.Write([]byte(getBrowserCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) resp, _, err := testClient.GetBrowserCheckV2(1) @@ -92,29 +96,3 @@ func verifyBrowserCheckV2Input(stringInput string) *BrowserCheckV2Response { } return check } - -func TestLiveGetBrowserCheckV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - // Make the request with your check settings and print result - res, _, err := c.GetBrowserCheckV2(495) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} diff --git a/syntheticsclientv2/get_checksv2.go b/syntheticsclientv2/get_checksv2.go index 1f3c9de..fe825d5 100644 --- a/syntheticsclientv2/get_checksv2.go +++ b/syntheticsclientv2/get_checksv2.go @@ -20,7 +20,6 @@ import ( "fmt" ) - func parseChecksV2Response(response string) (*ChecksV2Response, error) { // Parse the response and return the check object var checks ChecksV2Response diff --git a/syntheticsclientv2/get_checksv2_test.go b/syntheticsclientv2/get_checksv2_test.go index c53a110..3e6eb17 100644 --- a/syntheticsclientv2/get_checksv2_test.go +++ b/syntheticsclientv2/get_checksv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,15 +22,13 @@ import ( "net/http" "reflect" "testing" - "os" - "fmt" ) var ( - getChecksV2Body = `{"testType":"","page":1,"perPage":50,"search":"","orderBy":"id"}` - inputGetChecksV2 = verifyChecksV2Input(string(getChecksV2Body)) + getChecksV2Body = `{"testType":"","page":1,"perPage":50,"search":"","orderBy":"id"}` + inputGetChecksV2 = verifyChecksV2Input(string(getChecksV2Body)) getChecksV2Output = `{"tests":[{"id":482,"name":"Test of Splunk.com","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-15T16:05:25.815Z","updated_at":"2022-09-29T19:13:13.853Z","location_ids":["aws-us-east-1"],"type":"browser"},{"id":489,"name":"Appinspect login API","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-16T15:47:43.730Z","updated_at":"2022-08-16T15:47:43.741Z","location_ids":["aws-us-east-1"],"type":"api"},{"id":490,"name":"Arch Linux Packages","active":true,"frequency":10,"scheduling_strategy":"round_robin","created_at":"2022-08-16T16:48:42.119Z","updated_at":"2022-08-16T16:48:42.131Z","location_ids":["aws-us-east-1"],"type":"http"},{"id":492,"name":"Test of Splunkbase","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-16T19:35:54.014Z","updated_at":"2022-09-29T19:13:13.907Z","location_ids":["aws-us-east-1"],"type":"browser"},{"id":493,"name":"Brewery API","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-16T19:44:15.626Z","updated_at":"2022-08-16T19:44:15.635Z","location_ids":["aws-us-east-1"],"type":"api"},{"id":495,"name":"Multi-step test of legacy Splunkbase","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-17T01:24:44.579Z","updated_at":"2022-09-29T19:13:13.203Z","location_ids":["aws-us-east-1"],"type":"browser"},{"id":496,"name":"Multi-step Test of new Splunkbase","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-17T01:33:27.771Z","updated_at":"2022-09-29T19:13:13.997Z","location_ids":["aws-us-east-1"],"type":"browser"},{"id":935,"name":"This test does test stuff","active":true,"frequency":30,"scheduling_strategy":"round_robin","created_at":"2022-10-26T14:48:36.026Z","updated_at":"2022-10-26T14:48:36.037Z","location_ids":["aws-us-east-1"],"type":"api"},{"id":1116,"name":"boop-test","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-11-16T19:18:59.603Z","updated_at":"2022-11-16T19:20:58.911Z","location_ids":["aws-us-east-1","aws-ap-northeast-1"],"type":"api"},{"id":1128,"name":"boopbeep","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-11-17T14:19:49.564Z","updated_at":"2022-11-17T14:19:49.571Z","location_ids":["aws-us-east-1"],"type":"browser"}],"page":1,"per_page":50,"next_page_link":null,"total_count":10}` - output = &ChecksV2Response{} + output = &ChecksV2Response{} ) func TestGetChecksV2(t *testing.T) { @@ -36,10 +37,16 @@ func TestGetChecksV2(t *testing.T) { testMux.HandleFunc("/tests", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - w.Write([]byte(getChecksV2Output)) + _, err := w.Write([]byte(getChecksV2Output)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(getChecksV2Output), output) + err := json.Unmarshal([]byte(getChecksV2Output), output) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.GetChecksV2(inputGetChecksV2) @@ -60,29 +67,3 @@ func verifyChecksV2Input(stringInput string) *GetChecksV2Options { } return check } - -func TestLiveGetChecksV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - // Make the request with your check settings and print result - res, _, err := c.GetChecksV2(inputGetChecksV2) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} diff --git a/syntheticsclientv2/get_httpcheckv2_test.go b/syntheticsclientv2/get_httpcheckv2_test.go index 514cfa1..b80f0bc 100644 --- a/syntheticsclientv2/get_httpcheckv2_test.go +++ b/syntheticsclientv2/get_httpcheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,9 +19,7 @@ package syntheticsclientv2 import ( "encoding/json" - "fmt" "net/http" - "os" "reflect" "testing" ) @@ -34,7 +35,10 @@ func TestGetHttpCheckV2(t *testing.T) { testMux.HandleFunc("/tests/http/1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - w.Write([]byte(getHttpCheckV2Body)) + _, err := w.Write([]byte(getHttpCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) resp, _, err := testClient.GetHttpCheckV2(1) @@ -104,29 +108,3 @@ func verifyHttpCheckV2Input(stringInput string) *HttpCheckV2Response { } return check } - -func TestLiveGetHttpCheckV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - // Make the request with your check settings and print result - res, _, err := c.GetHttpCheckV2(1528) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} diff --git a/syntheticsclientv2/get_portcheckv2_test.go b/syntheticsclientv2/get_portcheckv2_test.go index 2c9b4ee..af8781f 100644 --- a/syntheticsclientv2/get_portcheckv2_test.go +++ b/syntheticsclientv2/get_portcheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,9 +19,7 @@ package syntheticsclientv2 import ( "encoding/json" - "fmt" "net/http" - "os" "reflect" "testing" ) @@ -34,7 +35,10 @@ func TestGetPortCheckV2(t *testing.T) { testMux.HandleFunc("/tests/port/1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - w.Write([]byte(getPortCheckV2Body)) + _, err := w.Write([]byte(getPortCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) resp, _, err := testClient.GetPortCheckV2(1) @@ -92,29 +96,3 @@ func verifyPortCheckV2Input(stringInput string) *PortCheckV2Response { } return check } - -func TestLiveGetPortCheckV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - // Make the request with your check settings and print result - res, _, err := c.GetPortCheckV2(1647) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} diff --git a/syntheticsclientv2/get_variablev2.go b/syntheticsclientv2/get_variablev2.go index 37cda8d..5aa3493 100644 --- a/syntheticsclientv2/get_variablev2.go +++ b/syntheticsclientv2/get_variablev2.go @@ -20,7 +20,6 @@ import ( "fmt" ) - func parseVariableV2Response(response string) (*VariableV2Response, error) { // Parse the response and return the check object var check VariableV2Response diff --git a/syntheticsclientv2/get_variablev2_test.go b/syntheticsclientv2/get_variablev2_test.go index d1c9ed7..85d859a 100644 --- a/syntheticsclientv2/get_variablev2_test.go +++ b/syntheticsclientv2/get_variablev2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,8 +22,6 @@ import ( "net/http" "reflect" "testing" - "os" - "fmt" ) var ( @@ -34,7 +35,10 @@ func TestGetVariableV2(t *testing.T) { testMux.HandleFunc("/variables/1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - w.Write([]byte(getVariableV2Body)) + _, err := w.Write([]byte(getVariableV2Body)) + if err != nil { + t.Fatal(err) + } }) resp, _, err := testClient.GetVariableV2(1) @@ -80,29 +84,3 @@ func verifyVariableV2Input(stringInput string) *VariableV2Response { } return check } - -func TestLiveGetVariableV2(t *testing.T) { - setup() - defer teardown() - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) - - // Make the request with your check settings and print result - res, _, err := c.GetVariableV2(246) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } - -} diff --git a/syntheticsclientv2/integration_test.go b/syntheticsclientv2/integration_test.go new file mode 100644 index 0000000..73bb9cc --- /dev/null +++ b/syntheticsclientv2/integration_test.go @@ -0,0 +1,469 @@ +//go:build integration +// +build integration + +// Copyright 2021 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" + "os" + "testing" +) + +func TestLiveGetVariableV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetVariableV2(246) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveGetPortCheckV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetPortCheckV2(1647) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveGetHttpCheckV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetHttpCheckV2(1528) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveGetChecksV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetChecksV2(inputGetChecksV2) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveGetBrowserCheckV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetBrowserCheckV2(495) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveGetApiCheckV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + // Make the request with your check settings and print result + res, _, err := c.GetApiCheckV2(489) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveDeleteVariableV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, err := c.DeleteVariableV2(254) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveDeletePortCheckV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, err := c.DeletePortCheckV2(1649) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveDeleteApiCheckV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, err := c.DeleteApiCheckV2(1093) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveCreateVariableV2(t *testing.T) { + + err := json.Unmarshal([]byte(createVariableV2Body), &inputVariableV2Data) + if err != nil { + t.Fatal(err) + } + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputVariableV2Data) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateVariableV2(&inputVariableV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveCreateHttpCheckV2(t *testing.T) { + + err := json.Unmarshal([]byte(createHttpCheckV2Body), &inputHttpCheckV2Data) + if err != nil { + t.Fatal(err) + } + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputHttpCheckV2Data) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateHttpCheckV2(&inputHttpCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveCreateBrowserCheckV2(t *testing.T) { + + err := json.Unmarshal([]byte(createBrowserCheckV2Body), &inputBrowserCheckV2Data) + if err != nil { + t.Fatal(err) + } + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputBrowserCheckV2Data) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateBrowserCheckV2(&inputBrowserCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveCreateApiCheckV2(t *testing.T) { + + err := json.Unmarshal([]byte(createApiV2Body), &inputData) + if err != nil { + t.Fatal(err) + } + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateApiCheckV2(&inputData) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveCreatePortCheckV2(t *testing.T) { + + err := json.Unmarshal([]byte(createPortCheckV2Body), &inputPortCheckV2Data) + if err != nil { + t.Fatal(err) + } + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputPortCheckV2Data) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreatePortCheckV2(&inputPortCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveDeleteBrowserCheckV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, err := c.DeleteBrowserCheckV2(1115) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} + +func TestLiveDeleteHttpCheckV2(t *testing.T) { + + //Expects a token is available from the API_ACCESS_TOKEN environment variable + //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable + token := os.Getenv("API_ACCESS_TOKEN") + realm := os.Getenv("REALM") + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(c) + fmt.Println(inputData) + + // Make the request with your check settings and print result + res, err := c.DeleteHttpCheckV2(1528) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } + +} diff --git a/syntheticsclientv2/synthetics_test.go b/syntheticsclientv2/synthetics_test.go index 29532ca..8dc9b75 100644 --- a/syntheticsclientv2/synthetics_test.go +++ b/syntheticsclientv2/synthetics_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -54,6 +57,9 @@ func testMethod(t *testing.T, r *http.Request, want string) { } func TestConfigurableClient(t *testing.T) { + setup() + defer teardown() + testMux = http.NewServeMux() testServer = httptest.NewServer(testMux) args := ClientArgs{ @@ -61,16 +67,16 @@ func TestConfigurableClient(t *testing.T) { publicBaseUrl: testServer.URL, } - testConfigurableClient := NewConfigurableClient("snakedonut", "realm-o", args) + testConfigurableClient := NewConfigurableClient("snakedonut", "us0", args) log.Printf("Client instantiated: %s", testServer.URL) if testConfigurableClient.GetHTTPClient() == nil { t.Errorf("http client is nil") } if testConfigurableClient.apiKey != "snakedonut" { - t.Errorf("apiKey was not correctly passed") + t.Errorf("returned \n\n%#v want \n\n%#v", testConfigurableClient.apiKey, "snakedonut") } - if testConfigurableClient.apiKey != "realm-o" { - t.Errorf("apiKey was not correctly passed") + if testConfigurableClient.realm != "us0" { + t.Errorf("returned \n\n%#v want \n\n%#v", testConfigurableClient.realm, "us0") } } @@ -78,7 +84,7 @@ func TestConfigurableClientTimeout(t *testing.T) { testMux = http.NewServeMux() testServer = httptest.NewServer(testMux) - testMux.HandleFunc("/v2/checks/12", func(w http.ResponseWriter, r *http.Request) { + testMux.HandleFunc("/tests/browser/12", func(w http.ResponseWriter, r *http.Request) { time.Sleep(2 * time.Second) }) diff --git a/syntheticsclientv2/update_apicheckv2_test.go b/syntheticsclientv2/update_apicheckv2_test.go index a6f197b..f683257 100644 --- a/syntheticsclientv2/update_apicheckv2_test.go +++ b/syntheticsclientv2/update_apicheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +26,7 @@ import ( ) var ( - updateApiCheckV2Body = `{"test":{"active":true,"device_id":4,"frequency":5,"location_ids":["aws-us-east-1","aws-ap-northeast-1"],"name":"boop-test","scheduling_strategy":"round_robin","requests":[{"configuration":{"name":"Get-Test","requestMethod":"GET","url":"https://api.us1.signalfx.com/v2/synthetics/tests/api/489","headers":{"beep":"boop","X-SF-TOKEN":"jinglebellsbatmanshells"},"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"}]}]}}` + updateApiCheckV2Body = `{"test":{"active":true,"device_id":4,"frequency":5,"location_ids":["aws-us-east-1","aws-ap-northeast-1"],"name":"boop-test","scheduling_strategy":"round_robin","requests":[{"configuration":{"name":"Get-Test","requestMethod":"GET","url":"https://api.us1.signalfx.com/v2/synthetics/tests/api/489","headers":{"beep":"boop","X-SF-TOKEN":"jinglebellsbatmanshells"},"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"}]}]}}` inputApiCheckV2Update = ApiCheckV2Input{} ) @@ -33,10 +36,16 @@ func TestUpdateApiCheckV2(t *testing.T) { testMux.HandleFunc("/tests/api/10", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - w.Write([]byte(updateApiCheckV2Body)) + _, err := w.Write([]byte(updateApiCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(updateApiCheckV2Body), &inputApiCheckV2Update) + err := json.Unmarshal([]byte(updateApiCheckV2Body), &inputApiCheckV2Update) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.UpdateApiCheckV2(10, &inputApiCheckV2Update) if err != nil { @@ -70,4 +79,3 @@ func TestUpdateApiCheckV2(t *testing.T) { } } - diff --git a/syntheticsclientv2/update_browsercheckv2_test.go b/syntheticsclientv2/update_browsercheckv2_test.go index bf3ac84..9d316dc 100644 --- a/syntheticsclientv2/update_browsercheckv2_test.go +++ b/syntheticsclientv2/update_browsercheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +26,7 @@ import ( ) var ( - updateBrowserCheckV2Body = `{"test":{"name":"browser-beep-test","business_transactions":[{"name":"Synthetic transaction 1","steps":[{"name":"Go to URL","type":"go_to_url","url":"https://www.splunk.com","action":"go_to_url","options":{"url":"https://www.splunk.com"},"wait_for_nav":true},{"name":"Nexter step","type":"click_element","selector_type":"id","selector":"free-splunk-click-desktop","wait_for_nav":false}]}],"urlProtocol":"https://","startUrl":"www.splunk.com","location_ids":["aws-us-east-1"],"device_id":2,"frequency":5,"scheduling_strategy":"round_robin","active":true,"advanced_settings":{"authentication":{"username":"boopuser","password":"{{env.beep-var}}"},"cookies":[{"key":"super","value":"duper","domain":"www.batmansagent.com","path":"/boom/goes/beep"}],"headers":[{"name":"batman","value":"Agentoz","domain":"www.batmansagent.com"}],"host_overrides":[],"user_agent":"Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X; Splunk Synthetics) AppleWebKit/604.1.25 (KHTML, like Gecko) Version/11.0 Mobile/15A5304j Safari/604.1","verify_certificates":true}}}` + updateBrowserCheckV2Body = `{"test":{"name":"browser-beep-test","business_transactions":[{"name":"Synthetic transaction 1","steps":[{"name":"Go to URL","type":"go_to_url","url":"https://www.splunk.com","action":"go_to_url","options":{"url":"https://www.splunk.com"},"wait_for_nav":true},{"name":"Nexter step","type":"click_element","selector_type":"id","selector":"free-splunk-click-desktop","wait_for_nav":false}]}],"urlProtocol":"https://","startUrl":"www.splunk.com","location_ids":["aws-us-east-1"],"device_id":2,"frequency":5,"scheduling_strategy":"round_robin","active":true,"advanced_settings":{"authentication":{"username":"boopuser","password":"{{env.beep-var}}"},"cookies":[{"key":"super","value":"duper","domain":"www.batmansagent.com","path":"/boom/goes/beep"}],"headers":[{"name":"batman","value":"Agentoz","domain":"www.batmansagent.com"}],"host_overrides":[],"user_agent":"Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X; Splunk Synthetics) AppleWebKit/604.1.25 (KHTML, like Gecko) Version/11.0 Mobile/15A5304j Safari/604.1","verify_certificates":true}}}` inputBrowserCheckV2Update = BrowserCheckV2Input{} ) @@ -33,10 +36,16 @@ func TestUpdateBrowserCheckV2(t *testing.T) { testMux.HandleFunc("/tests/browser/10", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - w.Write([]byte(updateBrowserCheckV2Body)) + _, err := w.Write([]byte(updateBrowserCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(updateBrowserCheckV2Body), &inputBrowserCheckV2Update) + err := json.Unmarshal([]byte(updateBrowserCheckV2Body), &inputBrowserCheckV2Update) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.UpdateBrowserCheckV2(10, &inputBrowserCheckV2Update) if err != nil { @@ -50,4 +59,3 @@ func TestUpdateBrowserCheckV2(t *testing.T) { } } - diff --git a/syntheticsclientv2/update_httpcheckv2_test.go b/syntheticsclientv2/update_httpcheckv2_test.go index 5532307..d8794ac 100644 --- a/syntheticsclientv2/update_httpcheckv2_test.go +++ b/syntheticsclientv2/update_httpcheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +26,7 @@ import ( ) var ( - updateHttpCheckV2Body = `{"test":{"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"}]}}` + updateHttpCheckV2Body = `{"test":{"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"}]}}` inputHttpCheckV2Update = HttpCheckV2Input{} ) @@ -33,10 +36,16 @@ func TestUpdateHttpCheckV2(t *testing.T) { testMux.HandleFunc("/tests/http/10", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - w.Write([]byte(updateHttpCheckV2Body)) + _, err := w.Write([]byte(updateHttpCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(updateHttpCheckV2Body), &inputHttpCheckV2Update) + err := json.Unmarshal([]byte(updateHttpCheckV2Body), &inputHttpCheckV2Update) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.UpdateHttpCheckV2(10, &inputHttpCheckV2Update) if err != nil { @@ -50,4 +59,3 @@ func TestUpdateHttpCheckV2(t *testing.T) { } } - diff --git a/syntheticsclientv2/update_portcheckv2_test.go b/syntheticsclientv2/update_portcheckv2_test.go index 58869ca..47186fd 100644 --- a/syntheticsclientv2/update_portcheckv2_test.go +++ b/syntheticsclientv2/update_portcheckv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -33,10 +36,16 @@ func TestUpdatePortCheckV2(t *testing.T) { testMux.HandleFunc("/tests/port/1650", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - w.Write([]byte(updatePortCheckV2Body)) + _, err := w.Write([]byte(updatePortCheckV2Body)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(updatePortCheckV2Body), &inputPortCheckV2Update) + err := json.Unmarshal([]byte(updatePortCheckV2Body), &inputPortCheckV2Update) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.UpdatePortCheckV2(1650, &inputPortCheckV2Update) if err != nil { diff --git a/syntheticsclientv2/update_variablesv2_test.go b/syntheticsclientv2/update_variablesv2_test.go index 95fbe3a..ee69d14 100644 --- a/syntheticsclientv2/update_variablesv2_test.go +++ b/syntheticsclientv2/update_variablesv2_test.go @@ -1,3 +1,6 @@ +//go:build unit_tests +// +build unit_tests + // Copyright 2021 Splunk, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +26,7 @@ import ( ) var ( - updateVariableV2Body = `{"variable":{"description":"My super awesome test variable","name":"foo2","secret":false,"value":"bar"}}` + updateVariableV2Body = `{"variable":{"description":"My super awesome test variable","name":"foo2","secret":false,"value":"bar"}}` inputVariableV2Update = VariableV2Input{} ) @@ -33,10 +36,16 @@ func TestUpdateVariableV2(t *testing.T) { testMux.HandleFunc("/variables/10", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - w.Write([]byte(updateVariableV2Body)) + _, err := w.Write([]byte(updateVariableV2Body)) + if err != nil { + t.Fatal(err) + } }) - json.Unmarshal([]byte(updateVariableV2Body), &inputVariableV2Update) + err := json.Unmarshal([]byte(updateVariableV2Body), &inputVariableV2Update) + if err != nil { + t.Fatal(err) + } resp, _, err := testClient.UpdateVariableV2(10, &inputVariableV2Update) if err != nil { @@ -62,4 +71,3 @@ func TestUpdateVariableV2(t *testing.T) { } } - From 9e9b3d4db26658af53a879a7496942fd31cb7078 Mon Sep 17 00:00:00 2001 From: jeremyh Date: Tue, 22 Nov 2022 11:06:53 -0500 Subject: [PATCH 10/15] fix integration tests --- syntheticsclientv2/integration_test.go | 616 +++++++++++-------------- 1 file changed, 271 insertions(+), 345 deletions(-) diff --git a/syntheticsclientv2/integration_test.go b/syntheticsclientv2/integration_test.go index 73bb9cc..09a4c1f 100644 --- a/syntheticsclientv2/integration_test.go +++ b/syntheticsclientv2/integration_test.go @@ -18,452 +18,378 @@ package syntheticsclientv2 import ( - "encoding/json" - "fmt" - "os" - "testing" + "encoding/json" + "fmt" + "os" + "testing" ) -func TestLiveGetVariableV2(t *testing.T) { - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") +var ( +token = os.Getenv("API_ACCESS_TOKEN") +realm = os.Getenv("REALM") +getChecksV2Body = `{"testType":"","page":1,"perPage":50,"search":"","orderBy":"id"}` +inputGetChecksV2 = GetChecksV2Options{} +createVariableV2Body = `{"variable":{"description":"My super awesome test variable","name":"food","secret":false,"value":"bar"}}` +inputVariableV2Data = VariableV2Input{} +createHttpCheckV2Body = `{"test":{"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{} +createBrowserCheckV2Body = `{"test":{"name":"browser-beep-test","business_transactions":[{"name":"Synthetic transaction 1","steps":[{"name":"Go to URL","type":"go_to_url","url":"https://www.splunk.com","action":"go_to_url","wait_for_nav":true},{"name":"Nexter step","type":"click_element","selector_type":"id","wait_for_nav":false,"selector":"free-splunk-click-desktop"}]}],"urlProtocol":"https://","startUrl":"www.splunk.com","location_ids":["aws-us-east-1"],"device_id":1,"frequency":5,"scheduling_strategy":"round_robin","active":true,"advanced_settings":{"verify_certificates":true,"authentication":{"username":"boopuser","password":"{{env.beep-var}}"},"headers":[{"name":"batman","value":"Agentoz","domain":"www.batmansagent.com"}],"cookies":[{"key":"super","value":"duper","domain":"www.batmansagent.com","path":"/boom/goes/beep"}]}}}` +inputBrowserCheckV2Data = BrowserCheckV2Input{} +createPortCheckV2Body = `{"test":{"name":"splunk - port 443","type":"port","url":"","port":443,"protocol":"tcp","host":"www.splunk.com","location_ids":["aws-us-east-1"],"frequency":10,"scheduling_strategy":"round_robin","active":true}}` +inputPortCheckV2Data = PortCheckV2Input{} +createApiV2Body = `{"test":{"active":true,"device_id":1,"frequency":5,"location_ids":["aws-us-east-1"],"name":"boop-test","scheduling_strategy":"round_robin","requests":[{"configuration":{"name":"Get-Test","requestMethod":"GET","url":"https://api.us1.signalfx.com/v2/synthetics/tests/api/489","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"}]}]}}` +inputApiCheckV2Data = ApiCheckV2Input{} +) - //Create your client with the token - c := NewClient(token, realm) +// You will need to fill in values for the get and delete tests +// as the check ids will vary from organization to organization - // Make the request with your check settings and print result - res, _, err := c.GetVariableV2(246) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } +func TestLiveGetChecksV2(t *testing.T) { - if err != nil { - t.Fatal(err) - } + //Create your client with the token + c := NewClient(token, realm) -} + err := json.Unmarshal([]byte(getChecksV2Body), &inputGetChecksV2) + if err != nil { + t.Fatal(err) + } -func TestLiveGetPortCheckV2(t *testing.T) { + // Make the request with your check settings and print result + res, _, err := c.GetChecksV2(&inputGetChecksV2) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") + if err != nil { + t.Fatal(err) + } - //Create your client with the token - c := NewClient(token, realm) +} - // Make the request with your check settings and print result - res, _, err := c.GetPortCheckV2(1647) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } +func TestLiveCreateVariableV2(t *testing.T) { - if err != nil { - t.Fatal(err) - } + err := json.Unmarshal([]byte(createVariableV2Body), &inputVariableV2Data) + if err != nil { + t.Fatal(err) + } + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(inputVariableV2Data) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateVariableV2(&inputVariableV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } } -func TestLiveGetHttpCheckV2(t *testing.T) { - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") +func TestLiveGetVariableV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, _, err := c.GetHttpCheckV2(1528) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetVariableV2(246) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveGetChecksV2(t *testing.T) { - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") +func TestLiveDeleteVariableV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, _, err := c.GetChecksV2(inputGetChecksV2) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, err := c.DeleteVariableV2(254) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveGetBrowserCheckV2(t *testing.T) { +func TestLiveCreateHttpCheckV2(t *testing.T) { - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") + err := json.Unmarshal([]byte(createHttpCheckV2Body), &inputHttpCheckV2Data) + if err != nil { + t.Fatal(err) + } - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, _, err := c.GetBrowserCheckV2(495) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + + fmt.Println(inputHttpCheckV2Data) - if err != nil { - t.Fatal(err) - } + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateHttpCheckV2(&inputHttpCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } -} + if err != nil { + t.Fatal(err) + } -func TestLiveGetApiCheckV2(t *testing.T) { +} - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") +func TestLiveGetHttpCheckV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, _, err := c.GetApiCheckV2(489) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetHttpCheckV2(1528) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveDeleteVariableV2(t *testing.T) { - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) +func TestLiveDeleteHttpCheckV2(t *testing.T) { - fmt.Println(c) - fmt.Println(inputData) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, err := c.DeleteVariableV2(254) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, err := c.DeleteHttpCheckV2(1528) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveDeletePortCheckV2(t *testing.T) { +func TestLiveCreateBrowserCheckV2(t *testing.T) { - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") + err := json.Unmarshal([]byte(createBrowserCheckV2Body), &inputBrowserCheckV2Data) + if err != nil { + t.Fatal(err) + } - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - fmt.Println(c) - fmt.Println(inputData) + + fmt.Println(inputBrowserCheckV2Data) - // Make the request with your check settings and print result - res, err := c.DeletePortCheckV2(1649) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateBrowserCheckV2(&inputBrowserCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveDeleteApiCheckV2(t *testing.T) { - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) +func TestLiveGetBrowserCheckV2(t *testing.T) { - fmt.Println(c) - fmt.Println(inputData) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, err := c.DeleteApiCheckV2(1093) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetBrowserCheckV2(495) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveCreateVariableV2(t *testing.T) { - - err := json.Unmarshal([]byte(createVariableV2Body), &inputVariableV2Data) - if err != nil { - t.Fatal(err) - } - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) +func TestLiveDeleteBrowserCheckV2(t *testing.T) { - fmt.Println(c) - fmt.Println(inputVariableV2Data) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateVariableV2(&inputVariableV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } + // Make the request with your check settings and print result + res, err := c.DeleteBrowserCheckV2(1115) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveCreateHttpCheckV2(t *testing.T) { - - err := json.Unmarshal([]byte(createHttpCheckV2Body), &inputHttpCheckV2Data) - if err != nil { - t.Fatal(err) - } - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") +func TestLiveCreateApiCheckV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + err := json.Unmarshal([]byte(createApiV2Body), &inputApiCheckV2Data) + if err != nil { + t.Fatal(err) + } - fmt.Println(c) - fmt.Println(inputHttpCheckV2Data) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateHttpCheckV2(&inputHttpCheckV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateApiCheckV2(&inputApiCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveCreateBrowserCheckV2(t *testing.T) { - - err := json.Unmarshal([]byte(createBrowserCheckV2Body), &inputBrowserCheckV2Data) - if err != nil { - t.Fatal(err) - } - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) +func TestLiveGetApiCheckV2(t *testing.T) { - fmt.Println(c) - fmt.Println(inputBrowserCheckV2Data) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateBrowserCheckV2(&inputBrowserCheckV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetApiCheckV2(489) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveCreateApiCheckV2(t *testing.T) { - - err := json.Unmarshal([]byte(createApiV2Body), &inputData) - if err != nil { - t.Fatal(err) - } - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) +func TestLiveDeleteApiCheckV2(t *testing.T) { - fmt.Println(c) - fmt.Println(inputData) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateApiCheckV2(&inputData) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } + // Make the request with your check settings and print result + res, err := c.DeleteApiCheckV2(1093) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveCreatePortCheckV2(t *testing.T) { - err := json.Unmarshal([]byte(createPortCheckV2Body), &inputPortCheckV2Data) - if err != nil { - t.Fatal(err) - } + err := json.Unmarshal([]byte(createPortCheckV2Body), &inputPortCheckV2Data) + if err != nil { + t.Fatal(err) + } - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") + //Create your client with the token + c := NewClient(token, realm) - //Create your client with the token - c := NewClient(token, realm) + fmt.Println(inputPortCheckV2Data) - fmt.Println(c) - fmt.Println(inputPortCheckV2Data) + // Make the request with your check settings and print result + res, reqDetail, err := c.CreatePortCheckV2(&inputPortCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } - // Make the request with your check settings and print result - res, reqDetail, err := c.CreatePortCheckV2(&inputPortCheckV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveDeleteBrowserCheckV2(t *testing.T) { - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) +func TestLiveGetPortCheckV2(t *testing.T) { - fmt.Println(c) - fmt.Println(inputData) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, err := c.DeleteBrowserCheckV2(1115) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetPortCheckV2(1647) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } -func TestLiveDeleteHttpCheckV2(t *testing.T) { - - //Expects a token is available from the API_ACCESS_TOKEN environment variable - //Expects a valid realm (E.G. us0, us1, eu0, etc) environment variable - token := os.Getenv("API_ACCESS_TOKEN") - realm := os.Getenv("REALM") - - //Create your client with the token - c := NewClient(token, realm) +func TestLiveDeletePortCheckV2(t *testing.T) { - fmt.Println(c) - fmt.Println(inputData) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, err := c.DeleteHttpCheckV2(1528) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, err := c.DeletePortCheckV2(1649) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } + From 0dc84d57c4de219fdf85b913eeb7801be0500320 Mon Sep 17 00:00:00 2001 From: jeremyh Date: Tue, 29 Nov 2022 09:54:51 -0500 Subject: [PATCH 11/15] snake_case fixes until the API has been adjusted to camelCase --- syntheticsclientv2/common_models.go | 7 ++++--- syntheticsclientv2/get_apicheckv2_test.go | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go index f582931..967d23b 100644 --- a/syntheticsclientv2/common_models.go +++ b/syntheticsclientv2/common_models.go @@ -255,9 +255,10 @@ type BusinessTransactionStepsV2 []struct { type Device struct { ID int `json:"id,omitempty"` Label string `json:"label,omitempty"` - Networkconnection `json:"networkConnection,omitempty"` - Viewportheight int `json:"viewportHeight,omitempty"` - Viewportwidth int `json:"viewportWidth,omitempty"` + UserAgent string `json:"user_agent,omitempty"` + Networkconnection `json:"network_connection,omitempty"` + Viewportheight int `json:"viewport_height"` + Viewportwidth int `json:"viewport_width"` } type Requests []struct { diff --git a/syntheticsclientv2/get_apicheckv2_test.go b/syntheticsclientv2/get_apicheckv2_test.go index b1d9339..645a750 100644 --- a/syntheticsclientv2/get_apicheckv2_test.go +++ b/syntheticsclientv2/get_apicheckv2_test.go @@ -25,7 +25,7 @@ import ( ) var ( - getApiCheckV2Body = `{"test":{"active":true,"createdAt":"0001-01-01T00:00:00Z","device":{"id":1,"label":"Desktop","networkConnection":{}},"frequency":5,"id":489,"name":"Appinspect login API","requests":[{"configuration":{"body":"","headers":{},"name":"Login","requestMethod":"GET","url":"https://api.splunk.com/2.0/rest/login/splunk"}}],"type":"api","updatedAt":"0001-01-01T00:00:00Z"}}` + getApiCheckV2Body = `{"test":{"id":489,"name":"Appinspect login API","active":true,"frequency":5,"scheduling_strategy":"round_robin","created_at":"2022-08-16T15:47:43.730Z","updated_at":"2022-08-16T15:47:43.741Z","location_ids":["aws-us-east-1"],"type":"api","device":{"id":1,"label":"Desktop","user_agent":"Mozilla/5.0 (X11; Linux x86_64; Splunk Synthetics) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36","viewport_width":1366,"viewport_height":768,"network_connection":{"description":"Standard Cable","upload_bandwidth":5000,"download_bandwidth":20000,"latency":28,"packet_loss":null}},"requests":[{"configuration":{"name":"Login","url":"https://api.splunk.com/2.0/rest/login/splunk","requestMethod":"GET","headers":{},"body":null},"setup":[],"validations":[]}]}}` inputGetApiCheckV2 = verifyApiCheckV2Input(string(getApiCheckV2Body)) ) @@ -78,6 +78,10 @@ func TestGetApiCheckV2(t *testing.T) { t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Device, inputGetApiCheckV2.Test.Device) } + if !reflect.DeepEqual(resp.Test.Device.Viewportheight, inputGetApiCheckV2.Test.Device.Viewportheight) { + t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Device.Viewportheight, inputGetApiCheckV2.Test.Device.Viewportheight) + } + if !reflect.DeepEqual(resp.Test.Requests, inputGetApiCheckV2.Test.Requests) { t.Errorf("returned \n\n%#v want \n\n%#v", resp.Test.Requests, inputGetApiCheckV2.Test.Requests) } From 5e132ce0152b1d13690903e3df047dc48946054f Mon Sep 17 00:00:00 2001 From: jeremyh Date: Tue, 6 Dec 2022 10:51:27 -0500 Subject: [PATCH 12/15] update models and move integration tests --- syntheticsclientv2/common_models.go | 96 +++-- syntheticsclientv2/integration_test.go | 499 ++++++++++++------------- 2 files changed, 304 insertions(+), 291 deletions(-) diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go index 967d23b..fb0e1a4 100644 --- a/syntheticsclientv2/common_models.go +++ b/syntheticsclientv2/common_models.go @@ -193,16 +193,18 @@ type DNSOverrides struct { type Networkconnection struct { Description string `json:"description,omitempty"` - Downloadbandwidth int `json:"downloadBandwidth,omitempty"` + Downloadbandwidth int `json:"download_bandwidth,omitempty"` Latency int `json:"latency,omitempty"` - Packetloss int `json:"packetLoss,omitempty"` - Uploadbandwidth int `json:"uploadBandwidth,omitempty"` + Packetloss int `json:"packet_loss,omitempty"` + Uploadbandwidth int `json:"upload_bandwidth,omitempty"` } type Advancedsettings struct { Authentication `json:"authentication"` - Cookiesv2 `json:"cookies"` - BrowserHeaders `json:"headers,omitempty"` + Cookiesv2 []Cookiesv2 `json:"cookies"` + BrowserHeaders []BrowserHeaders `json:"headers,omitempty"` + HostOverrides []HostOverrides `json:"host_overrides,omitempty"` + UserAgent string `json:"user_agent,omitempty"` Verifycertificates bool `json:"verifyCertificates,omitempty"` } @@ -211,38 +213,47 @@ type Authentication struct { Username string `json:"username"` } -type Cookiesv2 []struct { +type Cookiesv2 struct { Key string `json:"key"` Value string `json:"value"` Domain string `json:"domain"` Path string `json:"path"` } -type BrowserHeaders []struct { +type BrowserHeaders struct { Name string `json:"name"` Value string `json:"value"` Domain string `json:"domain"` } -type Transactions []struct { +type HostOverrides struct { + Source string `json:"source"` + Target string `json:"target"` + KeepHostHeader bool `json:"keep_host_header"` +} + +type Transactions struct { Name string `json:"name"` - Stepsv2 `json:"steps"` + StepsV2 []StepsV2 `json:"steps"` } -type BusinessTransactions []struct { +type BusinessTransactions struct { Name string `json:"name"` - BusinessTransactionStepsV2 `json:"steps"` + StepsV2 []StepsV2 `json:"steps"` } -type Stepsv2 []struct { +type StepsV2 struct { Name string `json:"name"` + Type string `json:"type"` + URL string `json:"url,omitempty"` + Action string `json:"action,omitempty"` + WaitForNav bool `json:"wait_for_nav"` + SelectorType string `json:"selector_type,omitempty"` Selector string `json:"selector,omitempty"` - Selectortype string `json:"selectorType,omitempty"` - Type string `json:"type,omitempty"` - Waitfornav bool `json:"waitForNav,omitempty"` + Options `json:"options,omitempty"` } -type BusinessTransactionStepsV2 []struct { +type BusinessTransactionStepsV2 struct { Name string `json:"name"` Type string `json:"type"` URL string `json:"url,omitempty"` @@ -250,6 +261,11 @@ type BusinessTransactionStepsV2 []struct { WaitForNav bool `json:"wait_for_nav"` SelectorType string `json:"selector_type,omitempty"` Selector string `json:"selector,omitempty"` + Options `json:"options,omitempty"` +} + +type Options struct { + URL string `json:"url,omitempty"` } type Device struct { @@ -261,10 +277,10 @@ type Device struct { Viewportwidth int `json:"viewport_width"` } -type Requests []struct { +type Requests struct { Configuration `json:"configuration,omitempty"` - Setup `json:"setup,omitempty"` - Validations `json:"validations,omitempty"` + Setup []Setup `json:"setup,omitempty"` + Validations []Validations `json:"validations,omitempty"` } type Configuration struct { @@ -277,7 +293,7 @@ type Configuration struct { type Headers map[string]interface{} -type Setup []struct { +type Setup struct { Extractor string `json:"extractor,omitempty"` Name string `json:"name,omitempty"` Source string `json:"source,omitempty"` @@ -285,7 +301,7 @@ type Setup []struct { Variable string `json:"variable,omitempty"` } -type Validations []struct { +type Validations struct { Actual string `json:"actual,omitempty"` Comparator string `json:"comparator,omitempty"` Expected string `json:"expected,omitempty"` @@ -309,14 +325,14 @@ type Checks []struct { type Tests []struct { Active bool `json:"active"` - Createdat time.Time `json:"createdAt"` + Createdat time.Time `json:"created_at"` Frequency int `json:"frequency"` ID int `json:"id"` Locationids []string `json:"locationIds"` Name string `json:"name"` - Schedulingstrategy string `json:"schedulingStrategy"` + Schedulingstrategy string `json:"scheduling_strategy"` Type string `json:"type"` - Updatedat time.Time `json:"updatedAt"` + Updatedat time.Time `json:"updated_at"` } type GetChecksV2Options struct { @@ -332,18 +348,18 @@ type Errors []struct { Description string `json:"description,omitempty"` } -type HttpHeaders []struct { +type HttpHeaders struct { Name string `json:"name,omitempty"` Value string `json:"value,omitempty"` } type Variable struct { - Createdat time.Time `json:"createdAt,omitempty"` + Createdat time.Time `json:"created_at,omitempty"` Description string `json:"description,omitempty"` ID int `json:"id,omitempty"` Name string `json:"name"` Secret bool `json:"secret"` - Updatedat time.Time `json:"updatedAt,omitempty"` + Updatedat time.Time `json:"updated_at,omitempty"` Value string `json:"value"` } @@ -414,7 +430,7 @@ type HttpCheckV2Response struct { URL string `json:"url"` RequestMethod string `json:"request_method"` Body string `json:"body,omitempty"` - HttpHeaders `json:"headers,omitempty"` + HttpHeaders []HttpHeaders `json:"headers,omitempty"` } `json:"test"` } @@ -429,7 +445,7 @@ type HttpCheckV2Input struct { Active bool `json:"active"` RequestMethod string `json:"request_method"` Body string `json:"body,omitempty"` - HttpHeaders `json:"headers,omitempty"` + HttpHeaders []HttpHeaders `json:"headers,omitempty"` } `json:"test"` } @@ -440,31 +456,31 @@ type ApiCheckV2Input struct { Frequency int `json:"frequency"` Locationids []string `json:"location_ids"` Name string `json:"name"` - Requests `json:"requests"` - Schedulingstrategy string `json:"schedulingStrategy"` + Requests []Requests `json:"requests"` + Schedulingstrategy string `json:"scheduling_strategy"` } `json:"test"` } type ApiCheckV2Response struct { Test struct { Active bool `json:"active,omitempty"` - Createdat time.Time `json:"createdAt,omitempty"` + Createdat time.Time `json:"created_at"` Device `json:"device,omitempty"` Frequency int `json:"frequency,omitempty"` ID int `json:"id,omitempty"` Locationids []string `json:"location_ids,omitempty"` Name string `json:"name,omitempty"` - Requests `json:"requests,omitempty"` - Schedulingstrategy string `json:"schedulingStrategy,omitempty"` + Requests []Requests `json:"requests,omitempty"` + Schedulingstrategy string `json:"scheduling_strategy,omitempty"` Type string `json:"type,omitempty"` - Updatedat time.Time `json:"updatedAt,omitempty"` + Updatedat time.Time `json:"updated_at,omitempty"` } } type BrowserCheckV2Input struct { Test struct { Name string `json:"name"` - BusinessTransactions `json:"business_transactions"` + BusinessTransactions []BusinessTransactions `json:"business_transactions"` Urlprotocol string `json:"urlProtocol"` Starturl string `json:"startUrl"` LocationIds []string `json:"location_ids"` @@ -480,17 +496,17 @@ type BrowserCheckV2Response struct { Test struct { Active bool `json:"active"` Advancedsettings `json:"advanced_settings"` - BusinessTransactions `json:"business_transactions"` - Createdat time.Time `json:"createdAt"` + BusinessTransactions []BusinessTransactions `json:"business_transactions"` + Createdat time.Time `json:"created_at"` Device `json:"device"` Frequency int `json:"frequency"` ID int `json:"id"` Locationids []string `json:"location_ids"` Name string `json:"name"` Schedulingstrategy string `json:"scheduling_strategy"` - Transactions `json:"transactions"` + Transactions []Transactions `json:"transactions"` Type string `json:"type"` - Updatedat time.Time `json:"updatedAt"` + Updatedat time.Time `json:"updated_at"` } `json:"test"` } diff --git a/syntheticsclientv2/integration_test.go b/syntheticsclientv2/integration_test.go index 09a4c1f..bb9dda1 100644 --- a/syntheticsclientv2/integration_test.go +++ b/syntheticsclientv2/integration_test.go @@ -18,378 +18,375 @@ package syntheticsclientv2 import ( - "encoding/json" - "fmt" - "os" - "testing" + "encoding/json" + "fmt" + "os" + "testing" ) var ( -token = os.Getenv("API_ACCESS_TOKEN") -realm = os.Getenv("REALM") -getChecksV2Body = `{"testType":"","page":1,"perPage":50,"search":"","orderBy":"id"}` -inputGetChecksV2 = GetChecksV2Options{} -createVariableV2Body = `{"variable":{"description":"My super awesome test variable","name":"food","secret":false,"value":"bar"}}` -inputVariableV2Data = VariableV2Input{} -createHttpCheckV2Body = `{"test":{"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{} -createBrowserCheckV2Body = `{"test":{"name":"browser-beep-test","business_transactions":[{"name":"Synthetic transaction 1","steps":[{"name":"Go to URL","type":"go_to_url","url":"https://www.splunk.com","action":"go_to_url","wait_for_nav":true},{"name":"Nexter step","type":"click_element","selector_type":"id","wait_for_nav":false,"selector":"free-splunk-click-desktop"}]}],"urlProtocol":"https://","startUrl":"www.splunk.com","location_ids":["aws-us-east-1"],"device_id":1,"frequency":5,"scheduling_strategy":"round_robin","active":true,"advanced_settings":{"verify_certificates":true,"authentication":{"username":"boopuser","password":"{{env.beep-var}}"},"headers":[{"name":"batman","value":"Agentoz","domain":"www.batmansagent.com"}],"cookies":[{"key":"super","value":"duper","domain":"www.batmansagent.com","path":"/boom/goes/beep"}]}}}` -inputBrowserCheckV2Data = BrowserCheckV2Input{} -createPortCheckV2Body = `{"test":{"name":"splunk - port 443","type":"port","url":"","port":443,"protocol":"tcp","host":"www.splunk.com","location_ids":["aws-us-east-1"],"frequency":10,"scheduling_strategy":"round_robin","active":true}}` -inputPortCheckV2Data = PortCheckV2Input{} -createApiV2Body = `{"test":{"active":true,"device_id":1,"frequency":5,"location_ids":["aws-us-east-1"],"name":"boop-test","scheduling_strategy":"round_robin","requests":[{"configuration":{"name":"Get-Test","requestMethod":"GET","url":"https://api.us1.signalfx.com/v2/synthetics/tests/api/489","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"}]}]}}` -inputApiCheckV2Data = ApiCheckV2Input{} + token = os.Getenv("API_ACCESS_TOKEN") + realm = os.Getenv("REALM") + getChecksV2Body = `{"testType":"","page":1,"perPage":50,"search":"","orderBy":"id"}` + inputGetChecksV2 = GetChecksV2Options{} + createVariableV2Body = `{"variable":{"description":"My super awesome test variable","name":"food","secret":false,"value":"bar"}}` + inputVariableV2Data = VariableV2Input{} + createHttpCheckV2Body = `{"test":{"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{} + createBrowserCheckV2Body = `{"test":{"name":"browser-beep-test","business_transactions":[{"name":"Synthetic transaction 1","steps":[{"name":"Go to URL","type":"go_to_url","url":"https://www.splunk.com","action":"go_to_url","wait_for_nav":true},{"name":"Nexter step","type":"click_element","selector_type":"id","wait_for_nav":false,"selector":"free-splunk-click-desktop"}]}],"urlProtocol":"https://","startUrl":"www.splunk.com","location_ids":["aws-us-east-1"],"device_id":1,"frequency":5,"scheduling_strategy":"round_robin","active":true,"advanced_settings":{"verify_certificates":true,"authentication":{"username":"boopuser","password":"{{env.beep-var}}"},"headers":[{"name":"batman","value":"Agentoz","domain":"www.batmansagent.com"}],"cookies":[{"key":"super","value":"duper","domain":"www.batmansagent.com","path":"/boom/goes/beep"}]}}}` + inputBrowserCheckV2Data = BrowserCheckV2Input{} + createPortCheckV2Body = `{"test":{"name":"splunk - port 443","type":"port","url":"","port":443,"protocol":"tcp","host":"www.splunk.com","location_ids":["aws-us-east-1"],"frequency":10,"scheduling_strategy":"round_robin","active":true}}` + inputPortCheckV2Data = PortCheckV2Input{} + createApiV2Body = `{"test":{"active":true,"device_id":1,"frequency":5,"location_ids":["aws-us-east-1"],"name":"boop-test","scheduling_strategy":"round_robin","requests":[{"configuration":{"name":"Get-Test","requestMethod":"GET","url":"https://api.us1.signalfx.com/v2/synthetics/tests/api/489","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"}]}]}}` + inputApiCheckV2Data = ApiCheckV2Input{} ) -// You will need to fill in values for the get and delete tests +// You will need to fill in values for the get and delete tests // as the check ids will vary from organization to organization func TestLiveGetChecksV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - err := json.Unmarshal([]byte(getChecksV2Body), &inputGetChecksV2) - if err != nil { - t.Fatal(err) - } + err := json.Unmarshal([]byte(getChecksV2Body), &inputGetChecksV2) + if err != nil { + t.Fatal(err) + } - // Make the request with your check settings and print result - res, _, err := c.GetChecksV2(&inputGetChecksV2) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetChecksV2(&inputGetChecksV2) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveCreateVariableV2(t *testing.T) { - err := json.Unmarshal([]byte(createVariableV2Body), &inputVariableV2Data) - if err != nil { - t.Fatal(err) - } - - //Create your client with the token - c := NewClient(token, realm) - - fmt.Println(inputVariableV2Data) - - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateVariableV2(&inputVariableV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } - - if err != nil { - t.Fatal(err) - } + err := json.Unmarshal([]byte(createVariableV2Body), &inputVariableV2Data) + if err != nil { + t.Fatal(err) + } + + //Create your client with the token + c := NewClient(token, realm) + + fmt.Println(inputVariableV2Data) + + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateVariableV2(&inputVariableV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } + + if err != nil { + t.Fatal(err) + } } func TestLiveGetVariableV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, _, err := c.GetVariableV2(246) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetVariableV2(246) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveDeleteVariableV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, err := c.DeleteVariableV2(254) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, err := c.DeleteVariableV2(254) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveCreateHttpCheckV2(t *testing.T) { - err := json.Unmarshal([]byte(createHttpCheckV2Body), &inputHttpCheckV2Data) - if err != nil { - t.Fatal(err) - } + err := json.Unmarshal([]byte(createHttpCheckV2Body), &inputHttpCheckV2Data) + if err != nil { + t.Fatal(err) + } - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - - fmt.Println(inputHttpCheckV2Data) + fmt.Println(inputHttpCheckV2Data) - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateHttpCheckV2(&inputHttpCheckV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateHttpCheckV2(&inputHttpCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveGetHttpCheckV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, _, err := c.GetHttpCheckV2(1528) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetHttpCheckV2(1528) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveDeleteHttpCheckV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, err := c.DeleteHttpCheckV2(1528) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, err := c.DeleteHttpCheckV2(1528) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveCreateBrowserCheckV2(t *testing.T) { - err := json.Unmarshal([]byte(createBrowserCheckV2Body), &inputBrowserCheckV2Data) - if err != nil { - t.Fatal(err) - } + err := json.Unmarshal([]byte(createBrowserCheckV2Body), &inputBrowserCheckV2Data) + if err != nil { + t.Fatal(err) + } - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - - fmt.Println(inputBrowserCheckV2Data) + fmt.Println(inputBrowserCheckV2Data) - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateBrowserCheckV2(&inputBrowserCheckV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateBrowserCheckV2(&inputBrowserCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveGetBrowserCheckV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, _, err := c.GetBrowserCheckV2(495) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetBrowserCheckV2(495) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveDeleteBrowserCheckV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, err := c.DeleteBrowserCheckV2(1115) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, err := c.DeleteBrowserCheckV2(1115) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveCreateApiCheckV2(t *testing.T) { - err := json.Unmarshal([]byte(createApiV2Body), &inputApiCheckV2Data) - if err != nil { - t.Fatal(err) - } + err := json.Unmarshal([]byte(createApiV2Body), &inputApiCheckV2Data) + if err != nil { + t.Fatal(err) + } - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, reqDetail, err := c.CreateApiCheckV2(&inputApiCheckV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } + // Make the request with your check settings and print result + res, reqDetail, err := c.CreateApiCheckV2(&inputApiCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveGetApiCheckV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, _, err := c.GetApiCheckV2(489) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetApiCheckV2(489) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveDeleteApiCheckV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, err := c.DeleteApiCheckV2(1093) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, err := c.DeleteApiCheckV2(1093) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveCreatePortCheckV2(t *testing.T) { - err := json.Unmarshal([]byte(createPortCheckV2Body), &inputPortCheckV2Data) - if err != nil { - t.Fatal(err) - } + err := json.Unmarshal([]byte(createPortCheckV2Body), &inputPortCheckV2Data) + if err != nil { + t.Fatal(err) + } - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - fmt.Println(inputPortCheckV2Data) + fmt.Println(inputPortCheckV2Data) - // Make the request with your check settings and print result - res, reqDetail, err := c.CreatePortCheckV2(&inputPortCheckV2Data) - if err != nil { - fmt.Println(err) - } else { - fmt.Println(reqDetail) - JsonPrint(res) - } + // Make the request with your check settings and print result + res, reqDetail, err := c.CreatePortCheckV2(&inputPortCheckV2Data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(reqDetail) + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveGetPortCheckV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, _, err := c.GetPortCheckV2(1647) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, _, err := c.GetPortCheckV2(1647) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } func TestLiveDeletePortCheckV2(t *testing.T) { - //Create your client with the token - c := NewClient(token, realm) + //Create your client with the token + c := NewClient(token, realm) - // Make the request with your check settings and print result - res, err := c.DeletePortCheckV2(1649) - if err != nil { - fmt.Println(err) - } else { - JsonPrint(res) - } + // Make the request with your check settings and print result + res, err := c.DeletePortCheckV2(1649) + if err != nil { + fmt.Println(err) + } else { + JsonPrint(res) + } - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } } - From a8f7adc9018a345c42b562d8c41e520afc3d858b Mon Sep 17 00:00:00 2001 From: jeremyh Date: Wed, 7 Dec 2022 10:43:28 -0500 Subject: [PATCH 13/15] small field fix --- syntheticsclientv2/common_models.go | 130 ++++++++++++++-------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go index fb0e1a4..be252b9 100644 --- a/syntheticsclientv2/common_models.go +++ b/syntheticsclientv2/common_models.go @@ -201,11 +201,11 @@ type Networkconnection struct { type Advancedsettings struct { Authentication `json:"authentication"` - Cookiesv2 []Cookiesv2 `json:"cookies"` + Cookiesv2 []Cookiesv2 `json:"cookies"` BrowserHeaders []BrowserHeaders `json:"headers,omitempty"` - HostOverrides []HostOverrides `json:"host_overrides,omitempty"` - UserAgent string `json:"user_agent,omitempty"` - Verifycertificates bool `json:"verifyCertificates,omitempty"` + HostOverrides []HostOverrides `json:"host_overrides,omitempty"` + UserAgent string `json:"user_agent,omitempty"` + Verifycertificates bool `json:"verifyCertificates,omitempty"` } type Authentication struct { @@ -227,18 +227,18 @@ type BrowserHeaders struct { } type HostOverrides struct { - Source string `json:"source"` - Target string `json:"target"` - KeepHostHeader bool `json:"keep_host_header"` + Source string `json:"source"` + Target string `json:"target"` + KeepHostHeader bool `json:"keep_host_header"` } type Transactions struct { - Name string `json:"name"` + Name string `json:"name"` StepsV2 []StepsV2 `json:"steps"` } type BusinessTransactions struct { - Name string `json:"name"` + Name string `json:"name"` StepsV2 []StepsV2 `json:"steps"` } @@ -250,7 +250,7 @@ type StepsV2 struct { WaitForNav bool `json:"wait_for_nav"` SelectorType string `json:"selector_type,omitempty"` Selector string `json:"selector,omitempty"` - Options `json:"options,omitempty"` + Options `json:"options,omitempty"` } type BusinessTransactionStepsV2 struct { @@ -261,11 +261,11 @@ type BusinessTransactionStepsV2 struct { WaitForNav bool `json:"wait_for_nav"` SelectorType string `json:"selector_type,omitempty"` Selector string `json:"selector,omitempty"` - Options `json:"options,omitempty"` + Options `json:"options,omitempty"` } type Options struct { - URL string `json:"url,omitempty"` + URL string `json:"url,omitempty"` } type Device struct { @@ -279,7 +279,7 @@ type Device struct { type Requests struct { Configuration `json:"configuration,omitempty"` - Setup []Setup `json:"setup,omitempty"` + Setup []Setup `json:"setup,omitempty"` Validations []Validations `json:"validations,omitempty"` } @@ -418,46 +418,46 @@ type PortCheckV2Input struct { type HttpCheckV2Response struct { Test struct { - ID int `json:"id"` - Name string `json:"name"` - Active bool `json:"active"` - Frequency int `json:"frequency"` - SchedulingStrategy string `json:"scheduling_strategy"` - CreatedAt time.Time `json:"created_at,omitempty"` - UpdatedAt time.Time `json:"updated_at,omitempty"` - LocationIds []string `json:"location_ids"` - Type string `json:"type"` - URL string `json:"url"` - RequestMethod string `json:"request_method"` - Body string `json:"body,omitempty"` + ID int `json:"id"` + Name string `json:"name"` + Active bool `json:"active"` + Frequency int `json:"frequency"` + SchedulingStrategy string `json:"scheduling_strategy"` + CreatedAt time.Time `json:"created_at,omitempty"` + UpdatedAt time.Time `json:"updated_at,omitempty"` + LocationIds []string `json:"location_ids"` + Type string `json:"type"` + URL string `json:"url"` + RequestMethod string `json:"request_method"` + Body string `json:"body,omitempty"` HttpHeaders []HttpHeaders `json:"headers,omitempty"` } `json:"test"` } type HttpCheckV2Input struct { Test struct { - Name string `json:"name"` - Type string `json:"type"` - URL string `json:"url"` - LocationIds []string `json:"location_ids"` - Frequency int `json:"frequency"` - SchedulingStrategy string `json:"scheduling_strategy"` - Active bool `json:"active"` - RequestMethod string `json:"request_method"` - Body string `json:"body,omitempty"` + Name string `json:"name"` + Type string `json:"type"` + URL string `json:"url"` + LocationIds []string `json:"location_ids"` + Frequency int `json:"frequency"` + SchedulingStrategy string `json:"scheduling_strategy"` + Active bool `json:"active"` + RequestMethod string `json:"request_method"` + Body string `json:"body,omitempty"` HttpHeaders []HttpHeaders `json:"headers,omitempty"` } `json:"test"` } type ApiCheckV2Input struct { Test struct { - Active bool `json:"active"` - Deviceid int `json:"device_id"` - Frequency int `json:"frequency"` - Locationids []string `json:"location_ids"` - Name string `json:"name"` + Active bool `json:"active"` + Deviceid int `json:"device_id"` + Frequency int `json:"frequency"` + Locationids []string `json:"location_ids"` + Name string `json:"name"` Requests []Requests `json:"requests"` - Schedulingstrategy string `json:"scheduling_strategy"` + Schedulingstrategy string `json:"scheduling_strategy"` } `json:"test"` } @@ -466,28 +466,28 @@ type ApiCheckV2Response struct { Active bool `json:"active,omitempty"` Createdat time.Time `json:"created_at"` Device `json:"device,omitempty"` - Frequency int `json:"frequency,omitempty"` - ID int `json:"id,omitempty"` - Locationids []string `json:"location_ids,omitempty"` - Name string `json:"name,omitempty"` - Requests []Requests `json:"requests,omitempty"` - Schedulingstrategy string `json:"scheduling_strategy,omitempty"` - Type string `json:"type,omitempty"` - Updatedat time.Time `json:"updated_at,omitempty"` + Frequency int `json:"frequency,omitempty"` + ID int `json:"id,omitempty"` + Locationids []string `json:"location_ids,omitempty"` + Name string `json:"name,omitempty"` + Requests []Requests `json:"requests,omitempty"` + Schedulingstrategy string `json:"scheduling_strategy,omitempty"` + Type string `json:"type,omitempty"` + Updatedat time.Time `json:"updated_at,omitempty"` } } type BrowserCheckV2Input struct { Test struct { - Name string `json:"name"` + Name string `json:"name"` BusinessTransactions []BusinessTransactions `json:"business_transactions"` - Urlprotocol string `json:"urlProtocol"` - Starturl string `json:"startUrl"` - LocationIds []string `json:"location_ids"` - DeviceID int `json:"device_id"` - Frequency int `json:"frequency"` - Schedulingstrategy string `json:"scheduling_strategy"` - Active bool `json:"active"` + Urlprotocol string `json:"urlProtocol"` + Starturl string `json:"startUrl"` + LocationIds []string `json:"location_ids"` + DeviceID int `json:"device_id"` + Frequency int `json:"frequency"` + Schedulingstrategy string `json:"scheduling_strategy"` + Active bool `json:"active"` Advancedsettings `json:"advanced_settings"` } `json:"test"` } @@ -496,17 +496,17 @@ type BrowserCheckV2Response struct { Test struct { Active bool `json:"active"` Advancedsettings `json:"advanced_settings"` - BusinessTransactions []BusinessTransactions `json:"business_transactions"` - Createdat time.Time `json:"created_at"` + BusinessTransactions []BusinessTransactions `json:"business_transactions"` + Createdat time.Time `json:"created_at"` Device `json:"device"` - Frequency int `json:"frequency"` - ID int `json:"id"` - Locationids []string `json:"location_ids"` - Name string `json:"name"` - Schedulingstrategy string `json:"scheduling_strategy"` + Frequency int `json:"frequency"` + ID int `json:"id"` + Locationids []string `json:"location_ids"` + Name string `json:"name"` + Schedulingstrategy string `json:"scheduling_strategy"` Transactions []Transactions `json:"transactions"` - Type string `json:"type"` - Updatedat time.Time `json:"updated_at"` + Type string `json:"type"` + Updatedat time.Time `json:"updated_at"` } `json:"test"` } From 0dbe641a281e75fc7a05ae76172d2c738283658a Mon Sep 17 00:00:00 2001 From: jeremyh Date: Wed, 7 Dec 2022 11:11:10 -0500 Subject: [PATCH 14/15] prune unused structs from v2 --- syntheticsclientv2/common_models.go | 316 ---------------------------- 1 file changed, 316 deletions(-) diff --git a/syntheticsclientv2/common_models.go b/syntheticsclientv2/common_models.go index be252b9..3dbc6d0 100644 --- a/syntheticsclientv2/common_models.go +++ b/syntheticsclientv2/common_models.go @@ -19,178 +19,6 @@ import ( ) // Common and shared struct models used for more complex requests -type Links struct { - Self string `json:"self,omitempty"` - SelfHTML string `json:"self_html,omitempty"` - Metrics string `json:"metrics,omitempty"` - LastRun string `json:"last_run,omitempty"` -} - -type Tags []struct { - ID int `json:"id,omitempty"` - Name string `json:"name,omitempty"` -} - -type Status struct { - LastCode int `json:"last_code,omitempty"` - LastMessage string `json:"last_message,omitempty"` - LastResponseTime int `json:"last_response_time,omitempty"` - LastRunAt string `json:"last_run_at,omitempty"` - LastFailureAt string `json:"last_failure_at,omitempty"` - LastAlertAt string `json:"last_alert_at,omitempty"` - HasFailure bool `json:"has_failure,omitempty"` - HasLocationFailure bool `json:"has_location_failure,omitempty"` -} - -type NotifyWho struct { - Sms bool `json:"sms,omitempty"` - Call bool `json:"call,omitempty"` - Email bool `json:"email,omitempty"` - CustomUserEmail string `json:"custom_email"` - Type string `json:"type,omitempty"` - Links Links `json:"links,omitempty"` - ID int `json:"id,omitempty"` -} - -type NotificationWindows []struct { - StartTime string `json:"start_time,omitempty"` - EndTime string `json:"end_time,omitempty"` - DurationInMinutes int `json:"duration_in_minutes,omitempty"` - TimeZone string `json:"time_zone,omitempty"` -} - -type NotificationWindow struct { - StartTime string `json:"start_time,omitempty"` - EndTime string `json:"end_time,omitempty"` - DurationInMinutes int `json:"duration_in_minutes,omitempty"` - TimeZone string `json:"time_zone,omitempty"` -} - -type Escalations struct { - Sms bool `json:"sms,omitempty"` - Email bool `json:"email,omitempty"` - Call bool `json:"call,omitempty"` - AfterMinutes int `json:"after_minutes,omitempty"` - NotifyWho []NotifyWho `json:"notify_who,omitempty"` - IsRepeat bool `json:"is_repeat,omitempty"` - NotificationWindow NotificationWindow `json:"notification_window,omitempty"` -} - -type Notifications struct { - Sms bool `json:"sms,omitempty"` - Email bool `json:"email,omitempty"` - Call bool `json:"call,omitempty"` - NotifyWho []NotifyWho `json:"notify_who,omitempty"` - NotifyAfterFailureCount int `json:"notify_after_failure_count,omitempty"` - NotifyOnLocationFailure bool `json:"notify_on_location_failure,omitempty"` - NotificationWindows NotificationWindows `json:"notification_windows,omitempty"` - Escalations []Escalations `json:"escalations,omitempty"` - Muted bool `json:"muted,omitempty"` -} - -type SuccessCriteria struct { - ActionType string `json:"action_type,omitempty"` - ComparisonString string `json:"comparison_string,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` -} - -type Connection struct { - UploadBandwidth int `json:"upload_bandwidth,omitempty"` - DownloadBandwidth int `json:"download_bandwidth,omitempty"` - Latency int `json:"latency,omitempty"` - PacketLoss float64 `json:"packet_loss,omitempty"` -} - -type Locations []struct { - ID int `json:"id,omitempty"` - Name string `json:"name,omitempty"` - WorldRegion string `json:"world_region,omitempty"` - RegionCode string `json:"region_code,omitempty"` -} - -type Integrations []struct { - ID int `json:"id,omitempty"` - Name string `json:"name,omitempty"` -} - -type HTTPRequestHeaders struct { - UserAgent string `json:"User-Agent,omitempty"` -} - -type Browser struct { - Label string `json:"label,omitempty"` - Code string `json:"code,omitempty"` -} - -type Steps struct { - ItemMethod string `json:"item_method,omitempty"` - Value string `json:"value,omitempty"` - How string `json:"how,omitempty"` - What string `json:"what,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - VariableName string `json:"variable_name,omitempty"` - Name string `json:"name,omitempty"` - Position int `json:"position,omitempty"` -} - -type Cookies struct { - Key string `json:"key,omitempty"` - Value string `json:"value,omitempty"` - Domain string `json:"domain,omitempty"` - Path string `json:"path,omitempty"` -} - -type JavascriptFiles struct { - ID int `json:"id,omitempty"` - Name string `json:"name,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` - Links Links `json:"links,omitempty"` -} - -type ExcludedFiles struct { - ExclusionType string `json:"exclusion_type,omitempty"` - PresetName string `json:"preset_name,omitempty"` - URL string `json:"url,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` -} - -type BlackoutPeriods []struct { - StartDate string `json:"start_date,omitempty"` - EndDate string `json:"end_date,omitempty"` - Timezone string `json:"timezone,omitempty"` - StartTime string `json:"start_time,omitempty"` - EndTime string `json:"end_time,omitempty"` - RepeatType string `json:"repeat_type,omitempty"` - DurationInMinutes int `json:"duration_in_minutes,omitempty"` - IsRepeat bool `json:"is_repeat,omitempty"` - MonthlyRepeatType string `json:"monthly_repeat_type,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` -} - -type Viewport struct { - Height int `json:"height,omitempty"` - Width int `json:"width,omitempty"` -} - -type ThresholdMonitors struct { - Matcher string `json:"matcher,omitempty"` - MetricName string `json:"metric_name,omitempty"` - ComparisonType string `json:"comparison_type,omitempty"` - Value int `json:"value,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` -} - -type DNSOverrides struct { - OriginalDomainCom string `json:"original.domain.com,omitempty"` - OriginalHostCom string `json:"original.host.com,omitempty"` -} - type Networkconnection struct { Description string `json:"description,omitempty"` Downloadbandwidth int `json:"download_bandwidth,omitempty"` @@ -309,20 +137,6 @@ type Validations struct { Type string `json:"type,omitempty"` } -type Checks []struct { - ID int `json:"id"` - Name string `json:"name"` - Type string `json:"type"` - Frequency int `json:"frequency"` - Paused bool `json:"paused"` - Muted bool `json:"muted"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` - Links Links `json:"links"` - Status Status `json:"status"` - Tags Tags `json:"tags"` -} - type Tests []struct { Active bool `json:"active"` Createdat time.Time `json:"created_at"` @@ -509,133 +323,3 @@ type BrowserCheckV2Response struct { Updatedat time.Time `json:"updated_at"` } `json:"test"` } - -type HttpCheckInput struct { - ID int `json:"id,omitempty"` - Name string `json:"name,omitempty"` - Type string `json:"type,omitempty"` - Frequency int `json:"frequency,omitempty"` - Paused bool `json:"paused,omitempty"` - Muted bool `json:"muted,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` - Links Links `json:"links,omitempty"` - Tags []string `json:"tags"` - Status Status `json:"status,omitempty"` - RoundRobin bool `json:"round_robin,omitempty"` - AutoRetry bool `json:"auto_retry,omitempty"` - Enabled bool `json:"enabled,omitempty"` - BlackoutPeriods BlackoutPeriods `json:"blackout_periods,omitempty"` - Locations []int `json:"locations,omitempty"` - Integrations []int `json:"integrations,omitempty"` - HTTPRequestHeaders HTTPRequestHeaders `json:"http_request_headers,omitempty"` - HTTPRequestBody string `json:"http_request_body,omitempty"` - Notifications Notifications `json:"notifications,omitempty"` - URL string `json:"url,omitempty"` - HTTPMethod string `json:"http_method,omitempty"` - SuccessCriteria []SuccessCriteria `json:"success_criteria,omitempty"` - Connection Connection `json:"connection,omitempty"` -} - -type HttpCheckResponse struct { - ID int `json:"id,omitempty"` - Name string `json:"name,omitempty"` - Type string `json:"type,omitempty"` - Frequency int `json:"frequency,omitempty"` - Paused bool `json:"paused,omitempty"` - Muted bool `json:"muted,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` - Links Links `json:"links,omitempty"` - Tags Tags `json:"tags,omitempty"` - Status Status `json:"status,omitempty"` - RoundRobin bool `json:"round_robin,omitempty"` - AutoRetry bool `json:"auto_retry,omitempty"` - Enabled bool `json:"enabled,omitempty"` - BlackoutPeriods BlackoutPeriods `json:"blackout_periods,omitempty"` - Locations Locations `json:"locations,omitempty"` - Integrations Integrations `json:"integrations,omitempty"` - HTTPRequestHeaders HTTPRequestHeaders `json:"http_request_headers,omitempty"` - HTTPRequestBody string `json:"http_request_body,omitempty"` - Notifications Notifications `json:"notifications,omitempty"` - URL string `json:"url,omitempty"` - HTTPMethod string `json:"http_method,omitempty"` - SuccessCriteria []SuccessCriteria `json:"success_criteria,omitempty"` - Connection Connection `json:"connection,omitempty"` -} - -type BrowserCheckInput struct { - ID int `json:"id,omitempty"` - Name string `json:"name,omitempty"` - Type string `json:"type,omitempty"` - Frequency int `json:"frequency,omitempty"` - Paused bool `json:"paused,omitempty"` - Muted bool `json:"muted,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` - Links Links `json:"links,omitempty"` - Tags []string `json:"tags"` - Status Status `json:"status,omitempty"` - RoundRobin bool `json:"round_robin,omitempty"` - AutoRetry bool `json:"auto_retry,omitempty"` - Enabled bool `json:"enabled,omitempty"` - BlackoutPeriods BlackoutPeriods `json:"blackout_periods,omitempty"` - Locations []int `json:"locations,omitempty"` - Integrations []int `json:"integrations,omitempty"` - HTTPRequestHeaders HTTPRequestHeaders `json:"http_request_headers,omitempty"` - HTTPRequestBody string `json:"http_request_body,omitempty"` - HTTPMethod string `json:"http_method,omitempty"` - Notifications Notifications `json:"notifications,omitempty"` - URL string `json:"url,omitempty"` - UserAgent string `json:"user_agent,omitempty"` - AutoUpdateUserAgent bool `json:"auto_update_user_agent,omitempty"` - Browser Browser `json:"browser,omitempty"` - Steps []Steps `json:"steps,omitempty"` - Cookies []Cookies `json:"cookies,omitempty"` - JavascriptFiles []JavascriptFiles `json:"javascript_files,omitempty"` - ExcludedFiles []ExcludedFiles `json:"excluded_files,omitempty"` - Viewport Viewport `json:"viewport,omitempty"` - EnforceSslValidation bool `json:"enforce_ssl_validation,omitempty"` - ThresholdMonitors []ThresholdMonitors `json:"threshold_monitors,omitempty"` - DNSOverrides DNSOverrides `json:"dns_overrides,omitempty"` - Connection Connection `json:"connection,omitempty"` - WaitForFullMetrics bool `json:"wait_for_full_metrics,omitempty"` -} - -type BrowserCheckResponse struct { - ID int `json:"id,omitempty"` - Name string `json:"name,omitempty"` - Type string `json:"type,omitempty"` - Frequency int `json:"frequency,omitempty"` - Paused bool `json:"paused,omitempty"` - Muted bool `json:"muted,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` - Links Links `json:"links,omitempty"` - Tags Tags `json:"tags,omitempty"` - Status Status `json:"status,omitempty"` - RoundRobin bool `json:"round_robin,omitempty"` - AutoRetry bool `json:"auto_retry,omitempty"` - Enabled bool `json:"enabled,omitempty"` - BlackoutPeriods BlackoutPeriods `json:"blackout_periods,omitempty"` - Locations Locations `json:"locations,omitempty"` - Integrations Integrations `json:"integrations,omitempty"` - HTTPRequestHeaders HTTPRequestHeaders `json:"http_request_headers,omitempty"` - HTTPRequestBody string `json:"http_request_body,omitempty"` - HTTPMethod string `json:"http_method,omitempty"` - Notifications Notifications `json:"notifications,omitempty"` - URL string `json:"url,omitempty"` - UserAgent string `json:"user_agent,omitempty"` - AutoUpdateUserAgent bool `json:"auto_update_user_agent,omitempty"` - Browser Browser `json:"browser,omitempty"` - Steps []Steps `json:"steps,omitempty"` - Cookies []Cookies `json:"cookies,omitempty"` - JavascriptFiles []JavascriptFiles `json:"javascript_files,omitempty"` - ExcludedFiles []ExcludedFiles `json:"excluded_files,omitempty"` - Viewport Viewport `json:"viewport,omitempty"` - EnforceSslValidation bool `json:"enforce_ssl_validation,omitempty"` - ThresholdMonitors []ThresholdMonitors `json:"threshold_monitors,omitempty"` - DNSOverrides DNSOverrides `json:"dns_overrides,omitempty"` - Connection Connection `json:"connection,omitempty"` - WaitForFullMetrics bool `json:"wait_for_full_metrics,omitempty"` -} From e98d49a76f8a470ccb3d9703e696bb886e487773 Mon Sep 17 00:00:00 2001 From: jeremyh Date: Wed, 7 Dec 2022 12:20:15 -0500 Subject: [PATCH 15/15] fix the double / in urls --- syntheticsclientv2/synthetics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/syntheticsclientv2/synthetics.go b/syntheticsclientv2/synthetics.go index 08d5bf7..dcac0dd 100644 --- a/syntheticsclientv2/synthetics.go +++ b/syntheticsclientv2/synthetics.go @@ -131,7 +131,7 @@ func NewConfigurableClient(apiKey string, realm string, args ClientArgs) *Client httpClient: http.Client{Timeout: time.Duration(args.timeoutSeconds) * time.Second}, } if args.publicBaseUrl == "" { - client.publicBaseURL = "https://api." + realm + ".signalfx.com/v2/synthetics/" + client.publicBaseURL = "https://api." + realm + ".signalfx.com/v2/synthetics" } else { client.publicBaseURL = args.publicBaseUrl }