Skip to content

Commit

Permalink
fix: Migrate ServiceNow alert channel(v2) (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmurray-lacework authored Oct 19, 2021
1 parent 7cc2d5e commit 09a413e
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 116 deletions.
64 changes: 58 additions & 6 deletions examples/resource_lacework_alert_channel_service_now/main.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,66 @@
provider "lacework" {}
terraform {
required_providers {
lacework = {
source = "lacework/lacework"
}
}
}

resource "lacework_alert_channel_service_now" "example" {
name = "Service Now Channel Alert Example"
instance_url = "snow-lacework.com"
username = "snow-user"
password = "snow-pass"
custom_template_file = <<TEMPLATE
name = var.channel_name
instance_url = var.instance_url
username = var.username
password = var.password
custom_template_file = var.custom_template_file
// test_integration input is used in this example only for testing
// purposes, it help us avoid sending a "test" request to the
// system we are integrating to. In production, this should remain
// turned on ("true") which is the default setting
test_integration = false
}

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

variable "instance_url" {
type = string
default = "https://dev123.service-now.com"
}

variable "username" {
type = string
default = "snow-user"
}

variable "password" {
type = string
default = "snow-pass"
}

variable "custom_template_file" {
type = string
default = <<TEMPLATE
{
"description" : "Generated by Lacework:",
"approval" : "Approved"
}
TEMPLATE
}

output "channel_name" {
value = lacework_alert_channel_service_now.example.name
}

output "instance_url" {
value = lacework_alert_channel_service_now.example.instance_url
}

output "username" {
value = lacework_alert_channel_service_now.example.username
}

output "custom_template_file" {
value = lacework_alert_channel_service_now.example.custom_template_file
}
78 changes: 78 additions & 0 deletions integration/resource_lacework_integration_service_now_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package integration

import (
"testing"

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

// TestServiceNowRestAlertChannelCreate applies integration terraform '../examples/resource_lacework_alert_channel_service_now'
// Uses the go-sdk to verify the created integration
// Applies an update with new channel name and Terraform destroy
func TestServiceNowRestAlertChannelCreate(t *testing.T) {
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: "../examples/resource_lacework_alert_channel_service_now",
Vars: map[string]interface{}{
"channel_name": "Service Now Alert Channel Example",
"instance_url": "https://dev123.service-now.com",
"username": "snow-user",
"password": "snow-pass",
"custom_template_file": customTemplate,
},
})
defer terraform.Destroy(t, terraformOptions)

// Create new ServiceNowRest Alert Channel
create := terraform.InitAndApplyAndIdempotent(t, terraformOptions)
created := GetAlertChannelProps(create)
if data, ok := created.Data.Data.(map[string]interface{}); ok {
assert.True(t, ok)

actualName := terraform.Output(t, terraformOptions, "channel_name")
actualUrl := terraform.Output(t, terraformOptions, "instance_url")
actualUsername := terraform.Output(t, terraformOptions, "username")
actualTemplate := terraform.Output(t, terraformOptions, "custom_template_file")

assert.Equal(t, "Service Now Alert Channel Example", created.Data.Name)
assert.Equal(t, "https://dev123.service-now.com", data["instanceUrl"])
assert.Equal(t, templateEncoded,
data["customTemplateFile"])
assert.Equal(t, "snow-user", data["userName"])

assert.Equal(t, "Service Now Alert Channel Example", actualName)
assert.Equal(t, "https://dev123.service-now.com", actualUrl)
assert.Equal(t, "snow-user", actualUsername)
assert.Equal(t, customTemplate, actualTemplate)
}
// Update ServiceNowRest Alert Channel
terraformOptions.Vars = map[string]interface{}{
"channel_name": "Service Now Alert Channel Updated",
"instance_url": "https://dev321.service-now.com",
"username": "snow-user-updated",
}

update := terraform.Apply(t, terraformOptions)
updated := GetAlertChannelProps(update)
if data, ok := updated.Data.Data.(map[string]interface{}); ok {
assert.True(t, ok)

actualName := terraform.Output(t, terraformOptions, "channel_name")
actualUrl := terraform.Output(t, terraformOptions, "instance_url")
actualUsername := terraform.Output(t, terraformOptions, "username")
actualTemplate := terraform.Output(t, terraformOptions, "custom_template_file")

assert.Equal(t, "Service Now Alert Channel Updated", updated.Data.Name)
assert.Equal(t, "https://dev321.service-now.com", data["instanceUrl"])
assert.Equal(t, templateEncoded, data["customTemplateFile"])
assert.Equal(t, "snow-user-updated", data["userName"])

assert.Equal(t, "Service Now Alert Channel Updated", actualName)
assert.Equal(t, "https://dev321.service-now.com", actualUrl)
assert.Equal(t, "snow-user-updated", actualUsername)
assert.Equal(t, customTemplate, actualTemplate)
}
}

var customTemplate = " {\n \"description\" : \"Generated by Lacework:\",\n \"approval\" : \"Approved\"\n }\n"
var templateEncoded = "data:application/json;name=i.json;base64,ICB7CiAgICAiZGVzY3JpcHRpb24iIDogIkdlbmVyYXRlZCBieSBMYWNld29yazoiLAogICAgImFwcHJvdmFsIiA6ICJBcHByb3ZlZCIKICB9Cg=="
Loading

0 comments on commit 09a413e

Please sign in to comment.