8000 Support for /v1/synthetics by btoueg · Pull Request #228 · zorkian/go-datadog-api · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support for /v1/synthetics #228

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,302 changes: 1,302 additions & 0 deletions datadog-accessors.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion events.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (client *Client) GetEvent(id int) (*Event, error) {
return out.Event, nil
}

// QueryEvents returns a slice of events from the query stream.
// GetEvents returns a slice of events from the query stream.
func (client *Client) GetEvents(start, end int,
priority, sources, tags string) ([]Event, error) {
// Since this is a GET request, we need to build a query string.
Expand Down
6 changes: 3 additions & 3 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GetBool(v *bool) (bool, bool) {
// to store v and returns a pointer to it.
func Int(v int) *int { return &v }

// GetInt is a helper routine that returns a boolean representing
// GetIntOk is a helper routine that returns a boolean representing
// if a value was set, and if so, dereferences the pointer to it.
func GetIntOk(v *int) (int, bool) {
if v != nil {
Expand All @@ -42,7 +42,7 @@ func GetIntOk(v *int) (int, bool) {
// to store v and returns a pointer to it.
func String(v string) *string { return &v }

// GetString is a helper routine that returns a boolean representing
// GetStringOk is a helper routine that returns a boolean representing
// if a value was set, and if so, dereferences the pointer to it.
func GetStringOk(v *string) (string, bool) {
if v != nil {
Expand All @@ -56,7 +56,7 @@ func GetStringOk(v *string) (string, bool) {
// to store v and returns a pointer to it.
func JsonNumber(v json.Number) *json.Number { return &v }

// GetJsonNumber is a helper routine that returns a boolean representing
// GetJsonNumberOk is a helper routine that returns a boolean representing
// if a value was set, and if so, dereferences the pointer to it.
func GetJsonNumberOk(v *json.Number) (json.Number, bool) {
if v != nil {
Expand Down
215 changes: 215 additions & 0 deletions integration/synthetics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package integration

import (
"testing"

"github.com/stretchr/testify/assert"
datadog "github.com/zorkian/go-datadog-api"
)

func TestSyntheticsCreateAndDelete(t *testing.T) {
expected := getTestSynthetics()
// create the monitor and compare it
actual := createTestSynthetics(t)
defer cleanUpSynthetics(t, actual.GetPublicId())

// Set ID of our original struct to zero so we can easily compare the results
expected.SetPublicId(actual.GetPublicId())
// Set Creator to the original struct as we can't predict details of the creator
expected.SetCreatedAt(actual.GetCreatedAt())
expected.SetModifiedAt(actual.GetModifiedAt())

assert.Equal(t, expected, actual)

actual, err := client.GetSyntheticsTest(*actual.PublicId)
if err != nil {
t.Fatalf("Retrieving a synthetics failed when it shouldn't: (%s)", err)
}
expected.SetStatus(actual.GetStatus())
expected.SetCreatedBy(actual.GetCreatedBy())
expected.SetModifiedBy(actual.GetModifiedBy())
assert.Equal(t, expected, actual)
}

func TestSyntheticsUpdate(t *testing.T) {

syntheticsTest := createTestSynthetics(t)
defer cleanUpSynthetics(t, syntheticsTest.GetPublicId())

syntheticsTest.SetName("___New-Test-Synthetics___")
publicId := syntheticsTest.GetPublicId()
createdAt := syntheticsTest.GetCreatedAt()
syntheticsTest.PublicId = nil
syntheticsTest.CreatedAt = nil
syntheticsTest.ModifiedAt = nil
actual, err := client.UpdateSyntheticsTest(publicId, syntheticsTest)
if err != nil {
t.Fatalf("Updating a synthetics test failed when it shouldn't: %s", err)
}

syntheticsTest.SetPublicId(publicId)
syntheticsTest.SetCreatedAt(createdAt)
syntheticsTest.SetModifiedAt(actual.GetModifiedAt())
assert.Equal(t, syntheticsTest, actual)

}

func TestSyntheticsUpdateRemovingTags(t *testing.T) {

syntheticsTest := createTestSynthetics(t)
defer cleanUpSynthetics(t, syntheticsTest.GetPublicId())

publicId := syntheticsTest.GetPublicId()
createdAt := syntheticsTest.GetCreatedAt()
syntheticsTest.PublicId = nil
syntheticsTest.CreatedAt = nil
syntheticsTest.ModifiedAt = nil
syntheticsTest.Tags = []string{}
actual, err := client.UpdateSyntheticsTest(publicId, syntheticsTest)
if err != nil {
t.Fatalf("Updating a synthetics test failed when it shouldn't: %s", err)
}

syntheticsTest.SetPublicId(publicId)
syntheticsTest.SetCreatedAt(createdAt)
syntheticsTest.SetModifiedAt(actual.GetModifiedAt())
assert.Equal(t, syntheticsTest, actual)

}

func TestSyntheticsGetAllTests(t *testing.T) {
syntheticsTests, err := client.GetSyntheticsTestsByType("api")
if err != nil {
t.Fatalf("Retrieving synthetics tests failed when it shouldn't: %s", err)
}
num := len(syntheticsTests)

syntheticsTest := createTestSynthetics(t)
defer cleanUpSynthetics(t, syntheticsTest.GetPublicId())

syntheticsTests, err = client.GetSyntheticsTestsByType("api")
if err != nil {
t.Fatalf("Retrieving synthetics tests failed when it shouldn't: %s", err)
}

if num+1 != len(syntheticsTests) {
t.Fatalf("Number of synthetics tests didn't match expected: %d != %d", len(syntheticsTests), num+1)
}
}

func TestMonitorPauseResume(t *testing.T) {
syntheticsTest := createTestSynthetics(t)
defer cleanUpSynthetics(t, syntheticsTest.GetPublicId())

publicId := syntheticsTest.GetPublicId()

// Pause SyntheticsTest
_, err := client.PauseSyntheticsTest(publicId)
if err != nil {
t.Fatalf("Failed to pause test")
}

syntheticsTest, err = client.GetSyntheticsTest(publicId)
if err != nil {
t.Fatalf("Retrieving synthetics test failed when it shouldn't: %s", err)
}

assert.Equal(t, "paused", *syntheticsTest.Status)

// Resume SyntheticsTest
_, err = client.ResumeSyntheticsTest(publicId)
if err != nil {
t.Fatalf("Failed to resume synthetics test")
}

syntheticsTest, err = client.GetSyntheticsTest(publicId)
if err != nil {
t.Fatalf("Retrieving synthetics test failed when it shouldn't: %s", err)
}

assert.Equal(t, "live", *syntheticsTest.Status)
}

func TestSyntheticsGetAllLocations(t *testing.T) {
syntheticsLocations, err := client.GetSyntheticsLocations()
if err != nil {
t.Fatalf("Retrieving synthetics locations failed when it shouldn't: %s", err)
}
num := len(syntheticsLocations)

if num == 0 {
t.Fatalf("Number of synthetics locations should be more than 0")
}
}

func TestSyntheticsGetAllDevices(t *testing.T) {
syntheticsDevices, err := client.GetSyntheticsBrowserDevices()
if err != nil {
t.Fatalf("Retrieving synthetics browser devices failed when it shouldn't: %s", err)
}
num := len(syntheticsDevices)

if num == 0 {
t.Fatalf("Number of synthetics devices should be more than 0")
}
}

/*
Testing of global mute and unmuting has not been added for following reasons:
* Disabling and enabling of global monitoring does an @all mention which is noisy
* It exposes risk to users that run integration tests in their main account
* There is no endpoint to verify success
*/

func getTestSynthetics() *datadog.SyntheticsTest {
c := &datadog.SyntheticsConfig{
Request: &datadog.SyntheticsRequest{
Method: datadog.String("GET"),
Url: datadog.String("https://example.org"),
Timeout: datadog.Int(30),
},
Assertions: []datadog.SyntheticsAssertion{{
Type: datadog.String("statusCode"),
Operator: datadog.String("is"),
Target: float64(200),
}},
}
o := &datadog.SyntheticsOptions{
TickEvery: datadog.Int(60),
}

return &datadog.SyntheticsTest{
Message: datadog.String("Test message"),
Name: datadog.String("Test synthetics"),
Config: c,
Options: o,
Locations: []string{"aws:eu-central-1"},
Type: datadog.String("api"),
Tags: []string{"tag1:value1", "tag2:value2"},
}
}

func createTestSynthetics(t *testing.T) *datadog.SyntheticsTest {
synthetics := getTestSynthetics()
synthetics, err := client.CreateSyntheticsTest(synthetics)
if err != nil {
t.Fatalf("Creating a synthetics failed when it shouldn't: %s", err)
}

return synthetics
}

func cleanUpSynthetics(t *testing.T, publicId string) {
if err := client.DeleteSyntheticsTests([]string{publicId}); err != nil {
t.Fatalf("Deleting a synthetics failed when it shouldn't. Manual cleanup needed. (%s)", err)
}

deletedSynthetics, err := client.GetSyntheticsTest(publicId)
if deletedSynthetics != nil {
t.Fatal("Synthetics hasn't been deleted when it should have been. Manual cleanup needed.")
}

if err == nil {
t.Fatal("Fetching deleted synthetics didn't lead to an error.")
}
}
4 changes: 2 additions & 2 deletions monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (client *Client) GetMonitor(id int) (*Monitor, error) {
return &out, nil
}

// GetMonitor retrieves monitors by name
// GetMonitorsByName retrieves monitors by name
func (self *Client) GetMonitorsByName(name string) ([]Monitor, error) {
var out reqMonitors
query, err := url.ParseQuery(fmt.Sprintf("name=%v", name))
Expand All @@ -155,7 +155,7 @@ func (self *Client) GetMonitorsByName(name string) ([]Monitor, error) {
return out.Monitors, nil
}

// GetMonitor retrieves monitors by a slice of tags
// GetMonitorsByTags retrieves monitors by a slice of tags
func (self *Client) GetMonitorsByTags(tags []string) ([]Monitor, error) {
var out reqMonitors
query, err := url.ParseQuery(fmt.Sprintf("monitor_tags=%v", strings.Join(tags, ",")))
Expand Down
Loading
0