Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new Microsoft Teams alert channel resource #68

Merged
merged 6 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
provider "lacework" {}

resource "lacework_alert_channel_microsoft_teams" "example" {
name = "Microsoft Teams Channel Alert Example"
teams_url = "https://outlook.office.com/webhook/api-token"
}
45 changes: 23 additions & 22 deletions lacework/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,29 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"lacework_agent_access_token": resourceLaceworkAgentAccessToken(),
"lacework_alert_channel_aws_cloudwatch": resourceLaceworkAlertChannelAwsCloudWatch(),
"lacework_alert_channel_aws_s3": resourceLaceworkAlertChannelAwsS3(),
"lacework_alert_channel_datadog": resourceLaceworkAlertChannelDatadog(),
"lacework_alert_channel_gcp_pub_sub": resourceLaceworkAlertChannelGcpPubSub(),
"lacework_alert_channel_jira_cloud": resourceLaceworkAlertChannelJiraCloud(),
"lacework_alert_channel_jira_server": resourceLaceworkAlertChannelJiraServer(),
"lacework_alert_channel_pagerduty": resourceLaceworkAlertChannelPagerDuty(),
"lacework_alert_channel_slack": resourceLaceworkAlertChannelSlack(),
"lacework_alert_channel_splunk": resourceLaceworkAlertChannelSplunk(),
"lacework_alert_channel_service_now": resourceLaceworkAlertChannelServiceNow(),
"lacework_alert_channel_webhook": resourceLaceworkAlertChannelWebhook(),
"lacework_integration_aws_cfg": resourceLaceworkIntegrationAwsCfg(),
"lacework_integration_aws_ct": resourceLaceworkIntegrationAwsCloudTrail(),
"lacework_integration_azure_cfg": resourceLaceworkIntegrationAzureCfg(),
"lacework_integration_azure_al": resourceLaceworkIntegrationAzureActivityLog(),
"lacework_integration_docker_hub": resourceLaceworkIntegrationDockerHub(),
"lacework_integration_docker_v2": resourceLaceworkIntegrationDockerV2(),
"lacework_integration_ecr": resourceLaceworkIntegrationEcr(),
"lacework_integration_gcp_cfg": resourceLaceworkIntegrationGcpCfg(),
"lacework_integration_gcp_at": resourceLaceworkIntegrationGcpAt(),
"lacework_integration_gcr": resourceLaceworkIntegrationGcr(),
"lacework_agent_access_token": resourceLaceworkAgentAccessToken(),
"lacework_alert_channel_aws_cloudwatch": resourceLaceworkAlertChannelAwsCloudWatch(),
"lacework_alert_channel_aws_s3": resourceLaceworkAlertChannelAwsS3(),
"lacework_alert_channel_datadog": resourceLaceworkAlertChannelDatadog(),
"lacework_alert_channel_gcp_pub_sub": resourceLaceworkAlertChannelGcpPubSub(),
"lacework_alert_channel_jira_cloud": resourceLaceworkAlertChannelJiraCloud(),
"lacework_alert_channel_jira_server": resourceLaceworkAlertChannelJiraServer(),
"lacework_alert_channel_pagerduty": resourceLaceworkAlertChannelPagerDuty(),
"lacework_alert_channel_microsoft_teams": resourceLaceworkAlertChannelMicrosoftTeams(),
"lacework_alert_channel_slack": resourceLaceworkAlertChannelSlack(),
"lacework_alert_channel_splunk": resourceLaceworkAlertChannelSplunk(),
"lacework_alert_channel_service_now": resourceLaceworkAlertChannelServiceNow(),
"lacework_alert_channel_webhook": resourceLaceworkAlertChannelWebhook(),
"lacework_integration_aws_cfg": resourceLaceworkIntegrationAwsCfg(),
"lacework_integration_aws_ct": resourceLaceworkIntegrationAwsCloudTrail(),
"lacework_integration_azure_cfg": resourceLaceworkIntegrationAzureCfg(),
"lacework_integration_azure_al": resourceLaceworkIntegrationAzureActivityLog(),
"lacework_integration_docker_hub": resourceLaceworkIntegrationDockerHub(),
"lacework_integration_docker_v2": resourceLaceworkIntegrationDockerV2(),
"lacework_integration_ecr": resourceLaceworkIntegrationEcr(),
"lacework_integration_gcp_cfg": resourceLaceworkIntegrationGcpCfg(),
"lacework_integration_gcp_at": resourceLaceworkIntegrationGcpAt(),
"lacework_integration_gcr": resourceLaceworkIntegrationGcr(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down
208 changes: 208 additions & 0 deletions lacework/resource_lacework_alert_channel_microsoft_teams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package lacework

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/lacework/go-sdk/api"
)

func resourceLaceworkAlertChannelMicrosoftTeams() *schema.Resource {
return &schema.Resource{
Create: resourceLaceworkAlertChannelMicrosoftTeamsCreate,
Read: resourceLaceworkAlertChannelMicrosoftTeamsRead,
Update: resourceLaceworkAlertChannelMicrosoftTeamsUpdate,
Delete: resourceLaceworkAlertChannelMicrosoftTeamsDelete,

Importer: &schema.ResourceImporter{
State: importLaceworkIntegration,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"intg_guid": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"teams_url": {
Type: schema.TypeString,
Required: true,
},
"created_or_updated_time": {
Type: schema.TypeString,
Computed: true,
},
"created_or_updated_by": {
Type: schema.TypeString,
Computed: true,
},
"type_name": {
Type: schema.TypeString,
Computed: true,
},
"org_level": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func resourceLaceworkAlertChannelMicrosoftTeamsCreate(d *schema.ResourceData, meta interface{}) error {
var (
lacework = meta.(*api.Client)
microsoftTeams = api.NewMicrosoftTeamsAlertChannel(d.Get("name").(string),
api.MicrosoftTeamsChannelData{
TeamsURL: d.Get("teams_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)
if err != nil {
return err
}

integration := response.Data[0]
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("org_level", integration.IsOrg == 1)

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

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

log.Printf("[INFO] Reading %s integration with guid: %v\n", api.MicrosoftTeamsChannelIntegration, d.Id())
response, err := lacework.Integrations.GetMicrosoftTeamsAlertChannel(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("teams_url", integration.Data.TeamsURL)

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

d.SetId("")
return nil
}

func resourceLaceworkAlertChannelMicrosoftTeamsUpdate(d *schema.ResourceData, meta interface{}) error {
var (
lacework = meta.(*api.Client)
microsoftTeams = api.NewMicrosoftTeamsAlertChannel(d.Get("name").(string),
api.MicrosoftTeamsChannelData{
TeamsURL: d.Get("teams_url").(string),
},
)
)

if !d.Get("enabled").(bool) {
microsoftTeams.Enabled = 0
}

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)
if err != nil {
return err
}

integration := response.Data[0]
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)

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

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

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

log.Printf("[INFO] Deleted %s integration with guid: %v\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)
}

return nil
}
Loading