Skip to content

Commit

Permalink
Merge pull request #124 from lacework/afiune/ALLY-102/email-alert-cha…
Browse files Browse the repository at this point in the history
…nnel

feat: new lacework_alert_channel_email resource
  • Loading branch information
afiune authored Jul 27, 2021
2 parents 8bc0bd9 + 99cedc7 commit 0455780
Show file tree
Hide file tree
Showing 26 changed files with 796 additions and 86 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ website/vendor

# Test exclusions
!command/test-fixtures/**/*.tfstate
!command/test-fixtures/**/.terraform/
!command/test-fixtures/**/.terraform/

# generated via "make integration-tests"
examples/**/.terraform.lock.hcl
11 changes: 5 additions & 6 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ GOFLAGS=-mod=vendor
CGO_ENABLED?=0
PACKAGENAME?=terraform-provider-lacework
VERSION=$(shell cat VERSION)
LOCAL_PROVIDERS="$$HOME/.terraform.d/plugins"
BINARY_PATH="registry.terraform.io/lacework/lacework/99.0.0/$$(go env GOOS)_$$(go env GOARCH)/terraform-provider-lacework_v99.0.0"
export GOFLAGS CGO_ENABLED

Expand Down Expand Up @@ -52,14 +51,14 @@ build-cross-platform:
-arch="amd64 386" \
github.com/lacework/$(PACKAGENAME)

install: fmtcheck
install: write-terraform-rc fmtcheck
mkdir -vp $(DIR)
go build -o $(DIR)/$(PACKAGENAME)
go build -o $(DIR)/$(BINARY_PATH)

uninstall:
@rm -vf $(DIR)/$(PACKAGENAME)

integration-test:
integration-test: install
go test ./integration -v

test: fmtcheck
Expand Down Expand Up @@ -129,5 +128,5 @@ endif
write-terraform-rc:
scripts/mirror-provider.sh

mirror-lacework-provider: write-terraform-rc
go build -o "${LOCAL_PROVIDERS}/${BINARY_PATH}"
clean-test:
find . \( -name ".terraform*" -o -name "terraform.tfstate*" \) -exec rm -rf {} \;
10 changes: 5 additions & 5 deletions examples/resource_lacework_alert_channel_datadog/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ terraform {
provider "lacework" {}

variable "channel_name" {
type = string
default = "Datadog Channel Alert Example"
type = string
default = "Datadog Alert Channel Example"
}

resource "lacework_alert_channel_datadog" "example" {
name = var.channel_name
datadog_site = "eu"
name = var.channel_name
datadog_site = "eu"
datadog_service = "Events Summary"
api_key = "datadog-key"
api_key = "datadog-key"
}
19 changes: 19 additions & 0 deletions examples/resource_lacework_alert_channel_email/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
terraform {
required_providers {
lacework = {
source = "lacework/lacework"
}
}
}

provider "lacework" {}

variable "channel_name" {
type = string
default = "Email Alert Channel Example"
}

resource "lacework_alert_channel_email" "example" {
name = var.channel_name
recipients = ["foo@example.com"]
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
github.com/lacework/go-sdk v0.10.1
github.com/lacework/go-sdk v0.10.2-0.20210724174255-10f6fdf9ad38
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
github.com/kyokomi/emoji/v2 v2.2.8/go.mod h1:JUcn42DTdsXJo1SWanHh4HKDEyPaR5CqkmoirZZP9qE=
github.com/lacework/go-sdk v0.10.1 h1:3RASm189SDo6jXRCP7oEtciWCHTVpL4XJnzCiHAzb44=
github.com/lacework/go-sdk v0.10.1/go.mod h1:YRkaH/2GLcUqkvqXr55nqxcJfC+/j/h4EUUUwwVga7Y=
github.com/lacework/go-sdk v0.10.2-0.20210724174255-10f6fdf9ad38 h1:FsT/7JJ/pTKXXgKhYkMTRT8hLGWTVvM14kZpy/nGK0Q=
github.com/lacework/go-sdk v0.10.2-0.20210724174255-10f6fdf9ad38/go.mod h1:YRkaH/2GLcUqkvqXr55nqxcJfC+/j/h4EUUUwwVga7Y=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down
12 changes: 8 additions & 4 deletions integration/integration.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package integration

import (
"github.com/lacework/go-sdk/api"
"log"
"os"
"strings"

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

var LwClient *api.Client
Expand All @@ -15,7 +16,10 @@ func init() {

func lwTestCLient() (lw *api.Client) {
lw, err := api.NewClient(os.Getenv("LW_ACCOUNT"),
api.WithApiKeys(os.Getenv("LW_API_KEY"), os.Getenv("LW_API_SECRET")))
api.WithApiKeys(os.Getenv("LW_API_KEY"), os.Getenv("LW_API_SECRET")),
api.WithSubaccount(os.Getenv("LW_SUBACCOUNT")),
api.WithApiV2(),
)

if err != nil {
log.Fatalf("Failed to create new go-sdk client, %v", err)
Expand All @@ -28,8 +32,8 @@ func GetIntegrationName(result string) string {
id := strings.Split(resultSplit[1], "]")[0]

res, err := LwClient.Integrations.Get(id)
if err != nil {
log.Fatalf("Unable to find integration id: %v", id)
if err != nil || len(res.Data) == 0 {
log.Fatalf("Unable to find integration id: %s\n Response: %v", id, res)
}

return res.Data[0].Name
Expand Down
9 changes: 5 additions & 4 deletions integration/resource_lacework_alert_channel_datadog_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package integration

import (
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
"testing"
)

// TestDatadogAlertChannelCreate applies integration terraform '../examples/resource_lacework_alert_channel_datadog'
Expand All @@ -17,12 +18,12 @@ func TestDatadogAlertChannelCreate(t *testing.T) {

// Create new Datadog Alert Channel
create := terraform.InitAndApply(t, terraformOptions)
assert.Equal(t, "Datadog Channel Alert Example", GetIntegrationName(create))
assert.Equal(t, "Datadog Alert Channel Example", GetIntegrationName(create))

// Update Datadog Alert Channel
terraformOptions.Vars = map[string]interface{}{
"channel_name": "Datadog Channel Alert Updated"}
"channel_name": "Datadog Alert Channel Updated"}

update := terraform.Apply(t, terraformOptions)
assert.Equal(t, "Datadog Channel Alert Updated", GetIntegrationName(update))
assert.Equal(t, "Datadog Alert Channel Updated", GetIntegrationName(update))
}
31 changes: 31 additions & 0 deletions integration/resource_lacework_alert_channel_email_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package integration

import (
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)

// TestAlertChannelEmailCreate applies integration terraform:
// => '../examples/resource_lacework_alert_channel_email'
//
// It uses the go-sdk to verify the created integration,
// applies an update with new alert channel name and destroys it
func TestAlertChannelEmailCreate(t *testing.T) {
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: "../examples/resource_lacework_alert_channel_email",
})
defer terraform.Destroy(t, terraformOptions)

// Create new Email Alert Channel
create := terraform.InitAndApply(t, terraformOptions)
assert.Equal(t, "Email Alert Channel Example", GetIntegrationName(create))

// Update Email Alert Channel
terraformOptions.Vars = map[string]interface{}{
"channel_name": "Email Alert Channel Updated"}

update := terraform.Apply(t, terraformOptions)
assert.Equal(t, "Email Alert Channel Updated", GetIntegrationName(update))
}
1 change: 1 addition & 0 deletions lacework/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func Provider() *schema.Provider {
"lacework_alert_channel_aws_s3": resourceLaceworkAlertChannelAwsS3(),
"lacework_alert_channel_cisco_webex": resourceLaceworkAlertChannelCiscoWebex(),
"lacework_alert_channel_datadog": resourceLaceworkAlertChannelDatadog(),
"lacework_alert_channel_email": resourceLaceworkAlertChannelEmail(),
"lacework_alert_channel_gcp_pub_sub": resourceLaceworkAlertChannelGcpPubSub(),
"lacework_alert_channel_jira_cloud": resourceLaceworkAlertChannelJiraCloud(),
"lacework_alert_channel_jira_server": resourceLaceworkAlertChannelJiraServer(),
Expand Down
39 changes: 22 additions & 17 deletions lacework/resource_lacework_alert_channel_datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@ func resourceLaceworkAlertChannelDatadog() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"intg_guid": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Required: true,
Description: "The integration name",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "The state of the external integration",
},
"datadog_site": {
Type: schema.TypeString,
Optional: true,
Default: string(api.DatadogSiteCom),
Type: schema.TypeString,
Optional: true,
Default: string(api.DatadogSiteCom),
Description: "Where to store your logs, either the US or Europe",
ValidateFunc: func(value interface{}, key string) ([]string, []error) {
switch value.(string) {
case string(api.DatadogSiteEu), string(api.DatadogSiteCom):
Expand All @@ -52,9 +51,10 @@ func resourceLaceworkAlertChannelDatadog() *schema.Resource {
},
},
"datadog_service": {
Type: schema.TypeString,
Optional: true,
Default: string(api.DatadogServiceLogsDetails),
Type: schema.TypeString,
Optional: true,
Default: string(api.DatadogServiceLogsDetails),
Description: "The level of detail of logs or event stream",
ValidateFunc: func(value interface{}, key string) ([]string, []error) {
switch value.(string) {
case string(api.DatadogServiceLogsDetails), string(api.DatadogServiceLogsSummary), string(api.DatadogServiceEventsSummary):
Expand All @@ -70,8 +70,14 @@ func resourceLaceworkAlertChannelDatadog() *schema.Resource {
},
},
"api_key": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
Description: "The Datadog api key required to submit metrics and events to Datadog",
},
"intg_guid": {
Type: schema.TypeString,
Required: true,
Computed: true,
},
"created_or_updated_time": {
Type: schema.TypeString,
Expand Down Expand Up @@ -157,7 +163,6 @@ func resourceLaceworkAlertChannelDatadogRead(d *schema.ResourceData, meta interf
d.Set("org_level", integration.IsOrg == 1)
d.Set("datadog_site", integration.Data.DatadogSite)
d.Set("datadog_service", integration.Data.DatadogService)
d.Set("api_key", integration.Data.ApiKey)

log.Printf("[INFO] Read %s integration with guid: %v\n",
api.DatadogChannelIntegration, integration.IntgGuid)
Expand Down
Loading

0 comments on commit 0455780

Please sign in to comment.