-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): New Relic Insights alert channel (#323)
Signed-off-by: Darren Murray <[email protected]>
- Loading branch information
1 parent
0348800
commit d7242b8
Showing
6 changed files
with
476 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/lacework/go-sdk/api" | ||
) | ||
|
||
func main() { | ||
lacework, err := api.NewClient("account", api.WithApiKeys("KEY", "SECRET")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
myNewRelicChannel := api.NewNewRelicAlertChannel("new-relic-alert-from-golang", | ||
api.NewRelicChannelData{ | ||
AccountID: 2338053, | ||
InsertKey: "x-xx-xxxxxxxxxxxxxxxxxx", | ||
}, | ||
) | ||
|
||
response, err := lacework.Integrations.CreateNewRelicAlertChannel(myNewRelicChannel) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Output: New Relic alert channel created: THE-INTEGRATION-GUID | ||
fmt.Printf("New Relic alert channel created: %s", response.Data[0].IntgGuid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// Author:: Darren Murray (<[email protected]>) | ||
// Copyright:: Copyright 2020, Lacework Inc. | ||
// License:: Apache License, Version 2.0 | ||
// | ||
// 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 api | ||
|
||
// NewNewRelicAlertChannel returns an instance of NewRelicAlertChannel | ||
// with the provided name and data. | ||
// | ||
// Basic usage: Initialize a new NewRelicAlertChannel struct, then | ||
// use the new instance to do CRUD operations | ||
// | ||
// client, err := api.NewClient("account") | ||
// if err != nil { | ||
// return err | ||
// } | ||
// | ||
// newRelicChannel := api.NewNewRelicAlertChannel("foo", | ||
// api.NewRelicChannelData{ | ||
// AccountID: 2338053, | ||
// InsertKey: "x-xx-xxxxxxxxxxxxxxxxxx", | ||
// }, | ||
// ) | ||
// | ||
// client.Integrations.CreateNewRelicAlertChannel(newRelicChannel) | ||
// | ||
func NewNewRelicAlertChannel(name string, data NewRelicChannelData) NewRelicAlertChannel { | ||
return NewRelicAlertChannel{ | ||
commonIntegrationData: commonIntegrationData{ | ||
Name: name, | ||
Type: NewRelicChannelIntegration.String(), | ||
Enabled: 1, | ||
}, | ||
Data: data, | ||
} | ||
} | ||
|
||
// CreateNewRelicAlertChannel creates an NEW_RELIC_INSIGHTS alert channel integration on the Lacework Server | ||
func (svc *IntegrationsService) CreateNewRelicAlertChannel(integration NewRelicAlertChannel) ( | ||
response NewRelicAlertChannelResponse, | ||
err error, | ||
) { | ||
err = svc.create(integration, &response) | ||
return | ||
} | ||
|
||
// GetNewRelicAlertChannel gets an NEW_RELIC_INSIGHTS alert channel integration that matches with | ||
// the provided integration guid on the Lacework Server | ||
func (svc *IntegrationsService) GetNewRelicAlertChannel(guid string) ( | ||
response NewRelicAlertChannelResponse, | ||
err error, | ||
) { | ||
err = svc.get(guid, &response) | ||
return | ||
} | ||
|
||
// UpdateNewRelicAlertChannel updates a single NEW_RELIC_INSIGHTS alert channel integration | ||
func (svc *IntegrationsService) UpdateNewRelicAlertChannel(data NewRelicAlertChannel) ( | ||
response NewRelicAlertChannelResponse, | ||
err error, | ||
) { | ||
err = svc.update(data.IntgGuid, data, &response) | ||
return | ||
} | ||
|
||
// ListNewRelicAlertChannel lists the NEW_RELIC_INSIGHTS external integrations available on the Lacework Server | ||
func (svc *IntegrationsService) ListNewRelicAlertChannel() (response NewRelicAlertChannelResponse, err error) { | ||
err = svc.listByType(NewRelicChannelIntegration, &response) | ||
return | ||
} | ||
|
||
type NewRelicAlertChannelResponse struct { | ||
Data []NewRelicAlertChannel `json:"data"` | ||
Ok bool `json:"ok"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type NewRelicAlertChannel struct { | ||
commonIntegrationData | ||
Data NewRelicChannelData `json:"DATA"` | ||
} | ||
|
||
type NewRelicChannelData struct { | ||
AccountID int `json:"ACCOUNT_ID" mapstructure:"ACCOUNT_ID"` | ||
InsertKey string `json:"INSERT_KEY" mapstructure:"INSERT_KEY"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
// | ||
// Author:: Darren Murray (<[email protected]>) | ||
// Copyright:: Copyright 2020, Lacework Inc. | ||
// License:: Apache License, Version 2.0 | ||
// | ||
// 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 api_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/lacework/go-sdk/api" | ||
"github.com/lacework/go-sdk/internal/intgguid" | ||
"github.com/lacework/go-sdk/internal/lacework" | ||
) | ||
|
||
func TestIntegrationsNewNewRelicAlertChannel(t *testing.T) { | ||
subject := api.NewNewRelicAlertChannel("integration_name", | ||
api.NewRelicChannelData{ | ||
AccountID: 2338053, | ||
InsertKey: "x-xx-xxxxxxxxxxxxxxxxxx", | ||
}, | ||
) | ||
assert.Equal(t, api.NewRelicChannelIntegration.String(), subject.Type) | ||
} | ||
|
||
func TestIntegrationsCreateNewRelicAlertChannel(t *testing.T) { | ||
var ( | ||
intgGUID = intgguid.New() | ||
fakeServer = lacework.MockServer() | ||
) | ||
fakeServer.MockAPI("external/integrations", func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "POST", r.Method, "CreateNewRelicAlertChannel should be a POST method") | ||
|
||
if assert.NotNil(t, r.Body) { | ||
body := httpBodySniffer(r) | ||
assert.Contains(t, body, "integration_name", "integration name is missing") | ||
assert.Contains(t, body, "NEW_RELIC_INSIGHTS", "wrong integration type") | ||
assert.Contains(t, body, "2338053", "wrong account id") | ||
assert.Contains(t, body, "x-xx-xxxxxxxxxxxxxxxxxx", "wrong insert key") | ||
assert.Contains(t, body, "ENABLED\":1", "integration is not enabled") | ||
} | ||
|
||
fmt.Fprintf(w, newRelicChannelIntegrationJsonResponse(intgGUID)) | ||
}) | ||
defer fakeServer.Close() | ||
|
||
c, err := api.NewClient("test", | ||
api.WithToken("TOKEN"), | ||
api.WithURL(fakeServer.URL()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
data := api.NewNewRelicAlertChannel("integration_name", | ||
api.NewRelicChannelData{ | ||
AccountID: 2338053, | ||
InsertKey: "x-xx-xxxxxxxxxxxxxxxxxx", | ||
}, | ||
) | ||
assert.Equal(t, "integration_name", data.Name, "NewRelicChannel integration name mismatch") | ||
assert.Equal(t, "NEW_RELIC_INSIGHTS", data.Type, "a new NewRelicChannel integration should match its type") | ||
assert.Equal(t, 1, data.Enabled, "a new NewRelicChannel integration should be enabled") | ||
|
||
response, err := c.Integrations.CreateNewRelicAlertChannel(data) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.True(t, response.Ok) | ||
if assert.Equal(t, 1, len(response.Data)) { | ||
resData := response.Data[0] | ||
assert.Equal(t, intgGUID, resData.IntgGuid) | ||
assert.Equal(t, "integration_name", resData.Name) | ||
assert.True(t, resData.State.Ok) | ||
assert.Equal(t, 2338053, resData.Data.AccountID) | ||
assert.Equal(t, "x-xx-xxxxxxxxxxxxxxxxxx", resData.Data.InsertKey) | ||
} | ||
} | ||
|
||
func TestIntegrationsGetNewRelicAlertChannel(t *testing.T) { | ||
var ( | ||
intgGUID = intgguid.New() | ||
apiPath = fmt.Sprintf("external/integrations/%s", intgGUID) | ||
fakeServer = lacework.MockServer() | ||
) | ||
fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "GET", r.Method, "GetNewRelicAlertChannel should be a GET method") | ||
fmt.Fprintf(w, newRelicChannelIntegrationJsonResponse(intgGUID)) | ||
}) | ||
defer fakeServer.Close() | ||
|
||
c, err := api.NewClient("test", | ||
api.WithToken("TOKEN"), | ||
api.WithURL(fakeServer.URL()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
response, err := c.Integrations.GetNewRelicAlertChannel(intgGUID) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.True(t, response.Ok) | ||
if assert.Equal(t, 1, len(response.Data)) { | ||
resData := response.Data[0] | ||
assert.Equal(t, intgGUID, resData.IntgGuid) | ||
assert.Equal(t, "integration_name", resData.Name) | ||
assert.True(t, resData.State.Ok) | ||
assert.Equal(t, 2338053, resData.Data.AccountID) | ||
assert.Equal(t, "x-xx-xxxxxxxxxxxxxxxxxx", resData.Data.InsertKey) | ||
} | ||
} | ||
|
||
func TestIntegrationsUpdateNewRelicAlertChannel(t *testing.T) { | ||
var ( | ||
intgGUID = intgguid.New() | ||
apiPath = fmt.Sprintf("external/integrations/%s", intgGUID) | ||
fakeServer = lacework.MockServer() | ||
) | ||
fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "PATCH", r.Method, "UpdateNewRelicAlertChannel should be a PATCH method") | ||
|
||
if assert.NotNil(t, r.Body) { | ||
body := httpBodySniffer(r) | ||
assert.Contains(t, body, intgGUID, "INTG_GUID missing") | ||
assert.Contains(t, body, "integration_name", "integration name is missing") | ||
assert.Contains(t, body, "NEW_RELIC_INSIGHTS", "wrong integration type") | ||
assert.Contains(t, body, "2338053", "wrong account id") | ||
assert.Contains(t, body, "x-xx-xxxxxxxxxxxxxxxxxx", "wrong insert key") | ||
assert.Contains(t, body, "ENABLED\":1", "integration is not enabled") | ||
} | ||
|
||
fmt.Fprintf(w, newRelicChannelIntegrationJsonResponse(intgGUID)) | ||
}) | ||
defer fakeServer.Close() | ||
|
||
c, err := api.NewClient("test", | ||
api.WithToken("TOKEN"), | ||
api.WithURL(fakeServer.URL()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
data := api.NewNewRelicAlertChannel("integration_name", | ||
api.NewRelicChannelData{ | ||
AccountID: 2338053, | ||
InsertKey: "x-xx-xxxxxxxxxxxxxxxxxx", | ||
}, | ||
) | ||
assert.Equal(t, "integration_name", data.Name, "NewRelicChannel integration name mismatch") | ||
assert.Equal(t, "NEW_RELIC_INSIGHTS", data.Type, "a new NewRelicChannel integration should match its type") | ||
assert.Equal(t, 1, data.Enabled, "a new NewRelicChannel integration should be enabled") | ||
data.IntgGuid = intgGUID | ||
|
||
response, err := c.Integrations.UpdateNewRelicAlertChannel(data) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.Equal(t, "SUCCESS", response.Message) | ||
assert.Equal(t, 1, len(response.Data)) | ||
assert.Equal(t, intgGUID, response.Data[0].IntgGuid) | ||
} | ||
|
||
func TestIntegrationsListNewRelicAlertChannel(t *testing.T) { | ||
var ( | ||
intgGUIDs = []string{intgguid.New(), intgguid.New(), intgguid.New()} | ||
fakeServer = lacework.MockServer() | ||
) | ||
fakeServer.MockAPI("external/integrations/type/NEW_RELIC_INSIGHTS", | ||
func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "GET", r.Method, "ListNewRelicAlertChannel should be a GET method") | ||
fmt.Fprintf(w, newRelicChanMultiIntegrationJsonResponse(intgGUIDs)) | ||
}, | ||
) | ||
defer fakeServer.Close() | ||
|
||
c, err := api.NewClient("test", | ||
api.WithToken("TOKEN"), | ||
api.WithURL(fakeServer.URL()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
response, err := c.Integrations.ListNewRelicAlertChannel() | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.True(t, response.Ok) | ||
assert.Equal(t, len(intgGUIDs), len(response.Data)) | ||
for _, d := range response.Data { | ||
assert.Contains(t, intgGUIDs, d.IntgGuid) | ||
} | ||
} | ||
|
||
func newRelicChannelIntegrationJsonResponse(intgGUID string) string { | ||
return ` | ||
{ | ||
"data": [` + singleNewRelicChanIntegration(intgGUID) + `], | ||
"ok": true, | ||
"message": "SUCCESS" | ||
} | ||
` | ||
} | ||
|
||
func newRelicChanMultiIntegrationJsonResponse(guids []string) string { | ||
integrations := []string{} | ||
for _, guid := range guids { | ||
integrations = append(integrations, singleNewRelicChanIntegration(guid)) | ||
} | ||
return ` | ||
{ | ||
"data": [` + strings.Join(integrations, ", ") + `], | ||
"ok": true, | ||
"message": "SUCCESS" | ||
} | ||
` | ||
} | ||
|
||
func singleNewRelicChanIntegration(id string) string { | ||
return ` | ||
{ | ||
"INTG_GUID": "` + id + `", | ||
"CREATED_OR_UPDATED_BY": "[email protected]", | ||
"CREATED_OR_UPDATED_TIME": "2020-Jul-16 19:59:22 UTC", | ||
"DATA": { | ||
"ACCOUNT_ID": 2338053, | ||
"INSERT_KEY": "x-xx-xxxxxxxxxxxxxxxxxx" | ||
}, | ||
"ENABLED": 1, | ||
"IS_ORG": 0, | ||
"NAME": "integration_name", | ||
"STATE": { | ||
"lastSuccessfulTime": "2020-Jul-16 18:26:54 UTC", | ||
"lastUpdatedTime": "2020-Jul-16 18:26:54 UTC", | ||
"ok": true | ||
}, | ||
"TYPE": "NEW_RELIC_INSIGHTS", | ||
"TYPE_NAME": "NEW_RELIC_INSIGHTS" | ||
} | ||
` | ||
} |
Oops, something went wrong.