Skip to content

Commit

Permalink
refactor(cli): use APIv2 to create Datadog integrations + update api/…
Browse files Browse the repository at this point in the history
…examples (#571)
  • Loading branch information
vatasha authored Oct 7, 2021
1 parent 3135b95 commit 9d6ea0e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
19 changes: 11 additions & 8 deletions api/_examples/datadog-alert-channel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@ package main
import (
"fmt"
"log"
"os"

"github.com/lacework/go-sdk/api"
)

func main() {
lacework, err := api.NewClient("account", api.WithApiKeys("KEY", "SECRET"))
lacework, err := api.NewClient(os.Getenv("LW_ACCOUNT"), api.WithApiV2(),
api.WithApiKeys(os.Getenv("LW_API_KEY"), os.Getenv("LW_API_SECRET")))
if err != nil {
log.Fatal(err)
}

myDatadogChannel := api.NewDatadogAlertChannel("datadog-alert-from-golang",
api.DatadogChannelData{
DatadogSite: api.DatadogSiteEu,
DatadogService: api.DatadogServiceEventsSummary,
ApiKey: "datadog-key",
myDatadogChannel := api.NewAlertChannel("datadog-alert-from-golang",
api.DatadogAlertChannelType,
api.DatadogDataV2{
DatadogSite: api.DatadogSiteEu,
DatadogType: api.DatadogServiceEventsSummary,
ApiKey: "datadog-key",
},
)

response, err := lacework.Integrations.CreateDatadogAlertChannel(myDatadogChannel)
response, err := lacework.V2.AlertChannels.Create(myDatadogChannel)
if err != nil {
log.Fatal(err)
}

// Output: Datadog alert channel created: THE-INTEGRATION-GUID
fmt.Printf("Datadog alert channel created: %s", response.Data[0].IntgGuid)
fmt.Printf("Datadog alert channel created: %s", response.Data.IntgGuid)
}
6 changes: 3 additions & 3 deletions api/alert_channel_datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (svc *AlertChannelsService) UpdateDatadog(data AlertChannel) (response Data
}

type DatadogDataV2 struct {
APIKey string `json:"apiKey"`
DatadogSite string `json:"datadogSite,omitempty"`
DatadogType string `json:"datadogType,omitempty"`
ApiKey string `json:"apiKey"`
DatadogSite datadogSite `json:"datadogSite,omitempty"`
DatadogType datadogService `json:"datadogType,omitempty"`
}

type DatadogAlertChannelV2 struct {
Expand Down
20 changes: 10 additions & 10 deletions api/alert_channel_datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func TestAlertChannelsService_GetDatadog(t *testing.T) {
intgGUID = intgguid.New()
apiPath = fmt.Sprintf("AlertChannels/%s", intgGUID)
fakeServer = lacework.MockServer()
APIKey = "Im-a-fake-api-key"
DatadogSite = "com"
DatadogType = "Logs Detail"
ApiKey = "Im-a-fake-api-key"
DatadogSite = api.DatadogSiteCom
DatadogType = api.DatadogServiceLogsDetails
)
fakeServer.UseApiV2()
fakeServer.MockToken("TOKEN")
Expand All @@ -61,7 +61,7 @@ func TestAlertChannelsService_GetDatadog(t *testing.T) {
assert.Equal(t, intgGUID, response.Data.IntgGuid)
assert.Equal(t, "integration_name", response.Data.Name)
assert.True(t, response.Data.State.Ok)
assert.Equal(t, APIKey, response.Data.Data.APIKey)
assert.Equal(t, ApiKey, response.Data.Data.ApiKey)
assert.Equal(t, DatadogSite, response.Data.Data.DatadogSite)
assert.Equal(t, DatadogType, response.Data.Data.DatadogType)
}
Expand All @@ -72,9 +72,9 @@ func TestAlertChannelsService_UpdateDatadog(t *testing.T) {
intgGUID = intgguid.New()
apiPath = fmt.Sprintf("AlertChannels/%s", intgGUID)
fakeServer = lacework.MockServer()
APIKey = "Im-a-fake-api-key"
DatadogSite = "com"
DatadogType = "Logs Detail"
ApiKey = "Im-a-fake-api-key"
DatadogSite = api.DatadogSiteCom
DatadogType = api.DatadogServiceLogsDetails
)
fakeServer.UseApiV2()
fakeServer.MockToken("TOKEN")
Expand All @@ -88,7 +88,7 @@ func TestAlertChannelsService_UpdateDatadog(t *testing.T) {
assert.Contains(t, body, intgGUID, "INTG_GUID missing")
assert.Contains(t, body, "integration_name", "cloud account name is missing")
assert.Contains(t, body, "Datadog", "wrong cloud account type")
assert.Contains(t, body, APIKey, "missing API key")
assert.Contains(t, body, ApiKey, "missing API key")
assert.Contains(t, body, DatadogSite, "missing datadog site")
assert.Contains(t, body, DatadogType, "missing datadog type")
assert.Contains(t, body, "enabled\":1", "cloud account is not enabled")
Expand All @@ -107,7 +107,7 @@ func TestAlertChannelsService_UpdateDatadog(t *testing.T) {
alertChannel := api.NewAlertChannel("integration_name",
api.DatadogAlertChannelType,
api.DatadogDataV2{
APIKey: APIKey,
ApiKey: ApiKey,
DatadogSite: DatadogSite,
DatadogType: DatadogType,
},
Expand All @@ -122,7 +122,7 @@ func TestAlertChannelsService_UpdateDatadog(t *testing.T) {
assert.NotNil(t, response)
assert.Equal(t, intgGUID, response.Data.IntgGuid)
assert.True(t, response.Data.State.Ok)
assert.Equal(t, APIKey, response.Data.Data.APIKey)
assert.Equal(t, ApiKey, response.Data.Data.ApiKey)
assert.Equal(t, DatadogSite, response.Data.Data.DatadogSite)
assert.Equal(t, DatadogType, response.Data.Data.DatadogType)
}
Expand Down
13 changes: 7 additions & 6 deletions cli/cmd/integration_datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,17 @@ func createDatadogIntegration() error {
return err
}

datadog := api.NewDatadogAlertChannel(answers.Name,
api.DatadogChannelData{
DatadogSite: site,
DatadogService: service,
ApiKey: answers.ApiKey,
datadog := api.NewAlertChannel(answers.Name,
api.DatadogAlertChannelType,
api.DatadogDataV2{
DatadogSite: site,
DatadogType: service,
ApiKey: answers.ApiKey,
},
)

cli.StartProgress(" Creating integration...")
_, err = cli.LwApi.Integrations.CreateDatadogAlertChannel(datadog)
_, err = cli.LwApi.V2.AlertChannels.Create(datadog)
cli.StopProgress()
return err
}

0 comments on commit 9d6ea0e

Please sign in to comment.