Skip to content

Commit

Permalink
feat(cli): manage EmailUser alert channels
Browse files Browse the repository at this point in the history
Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Jul 26, 2021
1 parent ddddbc6 commit 4624dfb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ const (
// Jira integration type
JiraIntegration

// Email integration type
EmailIntegration

// VictorOps channel integration type
VictorOpsChannelIntegration

Expand Down Expand Up @@ -132,6 +135,7 @@ var IntegrationTypes = map[integrationType]string{
AwsCloudWatchIntegration: "CLOUDWATCH_EB",
PagerDutyIntegration: "PAGER_DUTY_API",
JiraIntegration: "JIRA",
EmailIntegration: "EMAIL_USER",
VictorOpsChannelIntegration: "VICTOR_OPS",
WebhookIntegration: "WEBHOOK",
}
Expand Down
18 changes: 18 additions & 0 deletions cli/cmd/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package cmd

import (
"fmt"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -200,6 +201,7 @@ func promptCreateIntegration() error {
Message: "Choose an integration type to create: ",
Options: []string{
"Slack Alert Channel",
"Email Alert Channel",
"AWS S3 Alert Channel",
"Cisco Webex Alert Channel",
"Datadog Alert Channel",
Expand Down Expand Up @@ -239,6 +241,8 @@ func promptCreateIntegration() error {
switch integration {
case "Slack Alert Channel":
return createSlackAlertChannelIntegration()
case "Email Alert Channel":
return createEmailAlertChannelIntegration()
case "GCP PubSub Alert Channel":
return createGcpPubSubChannelIntegration()
case "Microsoft Teams Alert Channel":
Expand Down Expand Up @@ -820,6 +824,20 @@ func reflectIntegrationData(raw api.RawIntegration) [][]string {

return out

case api.EmailIntegration.String():
// Use v2 endpoint for Email Alert Channel
emailAlertChan, err := cli.LwApi.V2.AlertChannels.GetEmailUser(raw.IntgGuid)
if err != nil {
cli.Log.Debugw("unable to get EmailUser Alert Channel (v2)",
"error", err.Error(),
)
break
}
return [][]string{
{"RECIPIENTS",
strings.Join(emailAlertChan.Data.Data.ChannelProps.Recipients, "\n")},
}

case api.JiraIntegration.String():

var iData api.JiraAlertChannelData
Expand Down
68 changes: 68 additions & 0 deletions cli/cmd/integration_email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Author:: Salim Afiune Maya (<[email protected]>)
// Copyright:: Copyright 2021, Lacework Inc.
// License:: Apache License, Version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package cmd

import (
"strings"

"github.com/AlecAivazis/survey/v2"

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

func createEmailAlertChannelIntegration() error {
questions := []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{Message: "Name: "},
Validate: survey.Required,
},
{
Name: "recipients",
Prompt: &survey.Multiline{Message: "List of Recipients: "},
Validate: survey.Required,
},
}

answers := struct {
Name string
Recipients string
}{}

err := survey.Ask(questions, &answers,
survey.WithIcons(promptIconsFunc),
)
if err != nil {
return err
}

emailAlertChan := api.NewAlertChannel(answers.Name,
api.EmailUserAlertChannel,
api.EmailUserData{
ChannelProps: api.EmailUserChannelProps{
Recipients: strings.Split(answers.Recipients, "\n"),
},
},
)

cli.StartProgress(" Creating integration...")
_, err = cli.LwApi.V2.AlertChannels.Create(emailAlertChan)
cli.StopProgress()
return err
}

0 comments on commit 4624dfb

Please sign in to comment.