-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new Microsoft Teams alert channel resource (#68)
* feat: new Microsoft Teams alert channel resource Signed-off-by: Darren Murray <darren.murray@lacework.net>
- Loading branch information
1 parent
428fe6f
commit 1f467c6
Showing
12 changed files
with
542 additions
and
44 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
examples/resource_lacework_alert_channel_microsoft_teams/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
webhook_url = "https://outlook.office.com/webhook/api-token" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
lacework/resource_lacework_alert_channel_microsoft_teams.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
"webhook_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{ | ||
WebhookURL: 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) | ||
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("webhook_url", integration.Data.WebhookURL) | ||
|
||
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{ | ||
WebhookURL: d.Get("webhook_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 | ||
} |
Oops, something went wrong.