Skip to content

Commit

Permalink
fix: migrate Microsoft Teams Alert Channels to API v2
Browse files Browse the repository at this point in the history
Signed-off-by: Salim Afiune Maya <afiune@lacework.net>
  • Loading branch information
afiune committed Oct 12, 2021
1 parent 5d277f9 commit 36a78e3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 367 deletions.
37 changes: 35 additions & 2 deletions examples/resource_lacework_alert_channel_microsoft_teams/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
terraform {
required_providers {
lacework = {
source = "lacework/lacework"
}
}
}

provider "lacework" {}

resource "lacework_alert_channel_microsoft_teams" "example" {
name = "Microsoft Teams Channel Alert Example"
webhook_url = "https://outlook.office.com/webhook/api-token"
name = var.channel_name
webhook_url = var.webhook_url

// 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
}

// Variables and Outputs used in our integration tests (integration/)
variable "channel_name" {
type = string
default = "Microsoft Teams Alert Channel Example"
}

variable "webhook_url" {
type = string
default = "https://outlook.office.com/webhook/api-token"
}

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

output "webhook_url" {
value = lacework_alert_channel_microsoft_teams.example.webhook_url
}
147 changes: 53 additions & 94 deletions lacework/resource_lacework_alert_channel_microsoft_teams.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lacework

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -21,28 +20,32 @@ func resourceLaceworkAlertChannelMicrosoftTeams() *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 name of the alert channel",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "The state of the external integration",
},
"webhook_url": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
Description: "The webhook url for the integration",
},
"test_integration": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether to test the integration of an alert channel upon creation and modification",
},
"intg_guid": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier of the integration",
},
"created_or_updated_time": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -66,86 +69,75 @@ func resourceLaceworkAlertChannelMicrosoftTeams() *schema.Resource {
func resourceLaceworkAlertChannelMicrosoftTeamsCreate(d *schema.ResourceData, meta interface{}) error {
var (
lacework = meta.(*api.Client)
microsoftTeams = api.NewMicrosoftTeamsAlertChannel(d.Get("name").(string),
api.MicrosoftTeamsChannelData{
WebhookURL: d.Get("webhook_url").(string),
microsoftTeams = api.NewAlertChannel(d.Get("name").(string),
api.MicrosoftTeamsAlertChannelType,
api.MicrosoftTeamsData{
TeamsURL: d.Get("webhook_url").(string),
},
)
)
if !d.Get("enabled").(bool) {
microsoftTeams.Enabled = 0
}

log.Printf("[INFO] Creating %s integration with data:\n%+v\n", api.MicrosoftTeamsChannelIntegration, microsoftTeams)
response, err := lacework.Integrations.CreateMicrosoftTeamsAlertChannel(microsoftTeams)
if err != nil {
return err
}

log.Println("[INFO] Verifying server response data")
err = validateMicrosoftTeamsAlertChannelResponse(&response)
log.Printf("[INFO] Creating %s integration with data:\n%+v\n", api.MicrosoftTeamsAlertChannelType, microsoftTeams)
response, err := lacework.V2.AlertChannels.Create(microsoftTeams)
if err != nil {
return err
}

integration := response.Data[0]
integration := response.Data
d.SetId(integration.IntgGuid)
d.Set("name", integration.Name)
d.Set("intg_guid", integration.IntgGuid)
d.Set("enabled", integration.Enabled == 1)
d.Set("created_or_updated_time", integration.CreatedOrUpdatedTime)
d.Set("created_or_updated_by", integration.CreatedOrUpdatedBy)
d.Set("type_name", integration.TypeName)
d.Set("type_name", integration.Type)
d.Set("org_level", integration.IsOrg == 1)

if d.Get("test_integration").(bool) {
log.Printf("[INFO] Testing %s integration for guid %s\n", api.MicrosoftTeamsChannelIntegration, d.Id())
log.Printf("[INFO] Testing %s integration for guid %s\n", api.MicrosoftTeamsAlertChannelType, d.Id())
if err := VerifyAlertChannelAndRollback(d.Id(), lacework); err != nil {
return err
}
log.Printf("[INFO] Tested %s integration with guid %s successfully\n", api.MicrosoftTeamsChannelIntegration, d.Id())
log.Printf("[INFO] Tested %s integration with guid %s successfully\n", api.MicrosoftTeamsAlertChannelType, d.Id())
}

log.Printf("[INFO] Created %s integration with guid %s\n", api.MicrosoftTeamsChannelIntegration, integration.IntgGuid)
log.Printf("[INFO] Created %s integration with guid %s\n", api.MicrosoftTeamsAlertChannelType, integration.IntgGuid)
return nil
}

func resourceLaceworkAlertChannelMicrosoftTeamsRead(d *schema.ResourceData, meta interface{}) error {
lacework := meta.(*api.Client)

log.Printf("[INFO] Reading %s integration with guid %s\n", api.MicrosoftTeamsChannelIntegration, d.Id())
response, err := lacework.Integrations.GetMicrosoftTeamsAlertChannel(d.Id())
log.Printf("[INFO] Reading %s integration with guid %s\n", api.MicrosoftTeamsAlertChannelType, d.Id())
response, err := lacework.V2.AlertChannels.GetMicrosoftTeams(d.Id())
if err != nil {
return err
}

for _, integration := range response.Data {
if integration.IntgGuid == d.Id() {
d.Set("name", integration.Name)
d.Set("intg_guid", integration.IntgGuid)
d.Set("enabled", integration.Enabled == 1)
d.Set("created_or_updated_time", integration.CreatedOrUpdatedTime)
d.Set("created_or_updated_by", integration.CreatedOrUpdatedBy)
d.Set("type_name", integration.TypeName)
d.Set("org_level", integration.IsOrg == 1)
d.Set("webhook_url", integration.Data.WebhookURL)

log.Printf("[INFO] Read %s integration with guid %s\n",
api.MicrosoftTeamsChannelIntegration, integration.IntgGuid)
return nil
}
}

d.SetId("")
d.Set("name", response.Data.Name)
d.Set("intg_guid", response.Data.IntgGuid)
d.Set("enabled", response.Data.Enabled == 1)
d.Set("created_or_updated_time", response.Data.CreatedOrUpdatedTime)
d.Set("created_or_updated_by", response.Data.CreatedOrUpdatedBy)
d.Set("type_name", response.Data.Type)
d.Set("org_level", response.Data.IsOrg == 1)
d.Set("webhook_url", response.Data.Data.TeamsURL)

log.Printf("[INFO] Read %s integration with guid %s\n",
api.MicrosoftTeamsAlertChannelType, response.Data.IntgGuid)
return nil
}

func resourceLaceworkAlertChannelMicrosoftTeamsUpdate(d *schema.ResourceData, meta interface{}) error {
var (
lacework = meta.(*api.Client)
microsoftTeams = api.NewMicrosoftTeamsAlertChannel(d.Get("name").(string),
api.MicrosoftTeamsChannelData{
WebhookURL: d.Get("webhook_url").(string),
microsoftTeams = api.NewAlertChannel(d.Get("name").(string),
api.MicrosoftTeamsAlertChannelType,
api.MicrosoftTeamsData{
TeamsURL: d.Get("webhook_url").(string),
},
)
)
Expand All @@ -156,75 +148,42 @@ func resourceLaceworkAlertChannelMicrosoftTeamsUpdate(d *schema.ResourceData, me

microsoftTeams.IntgGuid = d.Id()

log.Printf("[INFO] Updating %s integration with data:\n%+v\n", api.MicrosoftTeamsChannelIntegration, microsoftTeams)
response, err := lacework.Integrations.UpdateMicrosoftTeamsAlertChannel(microsoftTeams)
if err != nil {
return err
}

log.Println("[INFO] Verifying server response data")
err = validateMicrosoftTeamsAlertChannelResponse(&response)
log.Printf("[INFO] Updating %s integration with data:\n%+v\n", api.MicrosoftTeamsAlertChannelType, microsoftTeams)
response, err := lacework.V2.AlertChannels.UpdateMicrosoftTeams(microsoftTeams)
if err != nil {
return err
}

integration := response.Data[0]
integration := response.Data
d.Set("name", integration.Name)
d.Set("intg_guid", integration.IntgGuid)
d.Set("enabled", integration.Enabled == 1)
d.Set("created_or_updated_time", integration.CreatedOrUpdatedTime)
d.Set("created_or_updated_by", integration.CreatedOrUpdatedBy)
d.Set("type_name", integration.TypeName)
d.Set("type_name", integration.Type)
d.Set("org_level", integration.IsOrg == 1)

if d.Get("test_integration").(bool) {
log.Printf("[INFO] Testing %s integration for guid %s\n", api.MicrosoftTeamsChannelIntegration, d.Id())
log.Printf("[INFO] Testing %s integration for guid %s\n", api.MicrosoftTeamsAlertChannelType, d.Id())
if err := lacework.V2.AlertChannels.Test(d.Id()); err != nil {
return err
}
log.Printf("[INFO] Tested %s integration with guid %s successfully\n", api.MicrosoftTeamsChannelIntegration, d.Id())
log.Printf("[INFO] Tested %s integration with guid %s successfully\n", api.MicrosoftTeamsAlertChannelType, d.Id())
}

log.Printf("[INFO] Updated %s integration with guid %s\n", api.MicrosoftTeamsChannelIntegration, d.Id())
log.Printf("[INFO] Updated %s integration with guid %s\n", api.MicrosoftTeamsAlertChannelType, d.Id())
return nil
}

func resourceLaceworkAlertChannelMicrosoftTeamsDelete(d *schema.ResourceData, meta interface{}) error {
lacework := meta.(*api.Client)

log.Printf("[INFO] Deleting %s integration with guid %s\n", api.MicrosoftTeamsChannelIntegration, d.Id())
_, err := lacework.Integrations.Delete(d.Id())
log.Printf("[INFO] Deleting %s integration with guid %s\n", api.MicrosoftTeamsAlertChannelType, d.Id())
err := lacework.V2.AlertChannels.Delete(d.Id())
if err != nil {
return err
}

log.Printf("[INFO] Deleted %s integration with guid %s\n", api.MicrosoftTeamsChannelIntegration, d.Id())
return nil
}

func validateMicrosoftTeamsAlertChannelResponse(response *api.MicrosoftTeamsAlertChannelResponse) error {
if len(response.Data) == 0 {
msg := `
Unable to read sever response data. (empty 'data' field)
This was an unexpected behavior, verify that your integration has been
created successfully and report this issue to support@lacework.net
`
return fmt.Errorf(msg)
}

if len(response.Data) > 1 {
msg := `
There is more that one integration inside the server response data.
List of integrations:
`
for _, integration := range response.Data {
msg = msg + fmt.Sprintf("\t%s: %s\n", integration.IntgGuid, integration.Name)
}
msg = msg + unexpectedBehaviorMsg()
return fmt.Errorf(msg)
}

log.Printf("[INFO] Deleted %s integration with guid %s\n", api.MicrosoftTeamsAlertChannelType, d.Id())
return nil
}
Loading

0 comments on commit 36a78e3

Please sign in to comment.