From 0906653f8de20fdf5a09f62d6eb74eaf8d32f8c5 Mon Sep 17 00:00:00 2001 From: Chris Trombley Date: Wed, 22 Jan 2020 11:02:26 -0800 Subject: [PATCH] feat(alerts): add GetSyntheticsCondition method (#105) --- pkg/alerts/synthetics_conditions.go | 19 +++++++++++++++++++ .../synthetics_conditions_integration_test.go | 10 ++++++++-- pkg/alerts/synthetics_conditions_test.go | 13 +++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/pkg/alerts/synthetics_conditions.go b/pkg/alerts/synthetics_conditions.go index 46fdbe44..5d78f7db 100644 --- a/pkg/alerts/synthetics_conditions.go +++ b/pkg/alerts/synthetics_conditions.go @@ -2,6 +2,8 @@ package alerts import ( "fmt" + + "github.com/newrelic/newrelic-client-go/pkg/errors" ) // ListSyntheticsConditions returns a list of Synthetics alert conditions for a given policy. @@ -29,6 +31,23 @@ func (alerts *Alerts) ListSyntheticsConditions(policyID int) ([]*SyntheticsCondi return conditions, nil } +// GetSyntheticsCondition retrieves a specific Synthetics alert condition. +func (alerts *Alerts) GetSyntheticsCondition(policyID int, conditionID int) (*SyntheticsCondition, error) { + conditions, err := alerts.ListSyntheticsConditions(policyID) + + if err != nil { + return nil, err + } + + for _, c := range conditions { + if c.ID == conditionID { + return c, nil + } + } + + return nil, errors.NewNotFoundf("no condition found for policy %d and condition ID %d", policyID, conditionID) +} + // CreateSyntheticsCondition creates a new Synthetics alert condition. func (alerts *Alerts) CreateSyntheticsCondition(policyID int, condition SyntheticsCondition) (*SyntheticsCondition, error) { resp := syntheticsConditionResponse{} diff --git a/pkg/alerts/synthetics_conditions_integration_test.go b/pkg/alerts/synthetics_conditions_integration_test.go index 29ceb22b..09650c4a 100644 --- a/pkg/alerts/synthetics_conditions_integration_test.go +++ b/pkg/alerts/synthetics_conditions_integration_test.go @@ -77,9 +77,15 @@ func TestIntegrationSyntheticsConditions(t *testing.T) { require.NoError(t, err) require.NotZero(t, conditions) + // Test: Get + condition, err := alerts.GetSyntheticsCondition(policy.ID, conditions[0].ID) + + require.NoError(t, err) + require.NotZero(t, condition) + // Test: Update - created.Name = fmt.Sprintf("test-synthetics-alert-condition-updated-%s", testRandStr) - updated, err := alerts.UpdateSyntheticsCondition(*created) + condition.Name = fmt.Sprintf("test-synthetics-alert-condition-updated-%s", testRandStr) + updated, err := alerts.UpdateSyntheticsCondition(*condition) require.NoError(t, err) require.NotZero(t, updated) diff --git a/pkg/alerts/synthetics_conditions_test.go b/pkg/alerts/synthetics_conditions_test.go index 47e4ea68..080b34e2 100644 --- a/pkg/alerts/synthetics_conditions_test.go +++ b/pkg/alerts/synthetics_conditions_test.go @@ -40,6 +40,19 @@ func TestListSyntheticsConditions(t *testing.T) { require.Equal(t, expected, actual) } +func TestGetSyntheticsCondition(t *testing.T) { + t.Parallel() + respJSON := fmt.Sprintf(`{ "synthetics_conditions": [%s] }`, testSyntheticsConditionJson) + alerts := newMockResponse(t, respJSON, http.StatusOK) + + expected := &testSyntheticsCondition + actual, err := alerts.GetSyntheticsCondition(testPolicyID, testSyntheticsCondition.ID) + + require.NoError(t, err) + require.NotNil(t, actual) + require.Equal(t, expected, actual) +} + func TestCreateSyntheticsCondition(t *testing.T) { t.Parallel() respJSON := fmt.Sprintf(`{ "synthetics_condition": %s }`, testSyntheticsConditionJson)