Skip to content

Commit

Permalink
SCALRCORE-29700 Slack integration - separate events for dry and apply… (
Browse files Browse the repository at this point in the history
#300)

* SCALRCORE-29700 Slack integration - separate events for dry and apply runs

* SCALRCORE-29700 upd changelog
  • Loading branch information
DayS1eeper committed Feb 5, 2024
1 parent 0281af5 commit 455c597
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `scalr_slack_integration`: new attribute `run_mode` ([#300](https://github.com/Scalr/terraform-provider-scalr/pull/300))

## [1.8.0] - 2024-01-19

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/slack_integration.md
Expand Up @@ -19,6 +19,7 @@ resource "scalr_slack_integration" "test" {
name = "my-channel"
account_id = "acc-xxxxxxxxxx"
events = ["run_approval_required", "run_success", "run_errored"]
run_mode = "apply"
channel_id = "xxxxxxxxxx" # Can be found in slack UI (channel settings/info popup)
environments = ["env-xxxxxxxxxx"]
workspaces = ["ws-xxxxxxxxxx", "ws-yyyyyyyyyy"]
Expand All @@ -38,6 +39,7 @@ resource "scalr_slack_integration" "test" {
### Optional

- `account_id` (String) ID of the account.
- `run_mode` (String) What type of runs should be reported, available options: `all`, `apply`, `dry`.
- `workspaces` (Set of String) List of workspaces where events should be triggered. Workspaces should be in provided environments. If no workspace is given for a specified environment, events will trigger in all of its workspaces.

### Read-Only
Expand Down
1 change: 1 addition & 0 deletions examples/resources/scalr_slack_integration/resource.tf
Expand Up @@ -2,6 +2,7 @@ resource "scalr_slack_integration" "test" {
name = "my-channel"
account_id = "acc-xxxxxxxxxx"
events = ["run_approval_required", "run_success", "run_errored"]
run_mode = "apply"
channel_id = "xxxxxxxxxx" # Can be found in slack UI (channel settings/info popup)
environments = ["env-xxxxxxxxxx"]
workspaces = ["ws-xxxxxxxxxx", "ws-yyyyyyyyyy"]
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -6,7 +6,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
github.com/scalr/go-scalr v0.0.0-20231221160155-699b7314507d
github.com/scalr/go-scalr v0.0.0-20240130151041-f97b6471385b
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -260,8 +260,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
github.com/scalr/go-scalr v0.0.0-20231221160155-699b7314507d h1:QTAeSn25kGhnuWRZ/AFcDlDEGoTRK/jewYRVYVo2vew=
github.com/scalr/go-scalr v0.0.0-20231221160155-699b7314507d/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/scalr/go-scalr v0.0.0-20240130151041-f97b6471385b h1:xrIz7QUZhOQ5fDiPiR03ynmR7ZNi9PV/4AGdSFMZHT8=
github.com/scalr/go-scalr v0.0.0-20240130151041-f97b6471385b/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
Expand Down
21 changes: 20 additions & 1 deletion scalr/resource_scalr_slack_integration.go
Expand Up @@ -3,11 +3,12 @@ package scalr
import (
"context"
"errors"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scalr/go-scalr"
"log"
)

func resourceScalrSlackIntegration() *schema.Resource {
Expand Down Expand Up @@ -48,6 +49,18 @@ func resourceScalrSlackIntegration() *schema.Resource {
Required: true,
MinItems: 1,
},
"run_mode": {
Description: "What type of runs should be reported, available options: `all`, `apply`, `dry`.",
Type: schema.TypeString,
Optional: true,
Default: "all",
ValidateDiagFunc: validation.ToDiagFunc(
validation.StringInSlice(
[]string{"all", "apply", "dry"},
false,
),
),
},
"channel_id": {
Description: "Slack channel ID the event will be sent to.",
Type: schema.TypeString,
Expand Down Expand Up @@ -132,6 +145,7 @@ func resourceScalrSlackIntegrationCreate(ctx context.Context, d *schema.Resource
Name: &name,
ChannelId: scalr.String(d.Get("channel_id").(string)),
Events: parseEvents(d),
RunMode: scalr.String(d.Get("run_mode").(string)),
Account: &scalr.Account{ID: accountID},
Environments: parseEnvironments(d),
}
Expand Down Expand Up @@ -179,6 +193,7 @@ func resourceScalrSlackIntegrationRead(ctx context.Context, d *schema.ResourceDa
_ = d.Set("name", slackIntegration.Name)
_ = d.Set("channel_id", slackIntegration.ChannelId)
_ = d.Set("events", slackIntegration.Events)
_ = d.Set("run_mode", slackIntegration.RunMode)
_ = d.Set("account_id", slackIntegration.Account.ID)

environmentIDs := make([]string, 0)
Expand Down Expand Up @@ -214,6 +229,10 @@ func resourceScalrSlackIntegrationUpdate(ctx context.Context, d *schema.Resource
options.Events = events
}

if d.HasChange("run_mode") {
options.RunMode = scalr.String(d.Get("run_mode").(string))
}

if d.HasChange("environments") {
envs := parseEnvironments(d)
options.Environments = envs
Expand Down
12 changes: 12 additions & 0 deletions scalr/resource_scalr_slack_integration_test.go
Expand Up @@ -44,6 +44,11 @@ func TestAccSlackIntegration_basic(t *testing.T) {
"account_id",
defaultAccount,
),
resource.TestCheckResourceAttr(
"scalr_slack_integration.test",
"run_mode",
"dry",
),
resource.TestCheckTypeSetElemAttr(
"scalr_slack_integration.test",
"events.*",
Expand Down Expand Up @@ -75,6 +80,11 @@ func TestAccSlackIntegration_basic(t *testing.T) {
"account_id",
defaultAccount,
),
resource.TestCheckResourceAttr(
"scalr_slack_integration.test",
"run_mode",
"apply",
),
resource.TestCheckTypeSetElemAttr(
"scalr_slack_integration.test",
"events.*",
Expand All @@ -100,6 +110,7 @@ resource scalr_environment test {
resource "scalr_slack_integration" "test" {
name = "test-create"
account_id = scalr_environment.test.account_id
run_mode = "dry"
events = ["run_approval_required", "run_errored"]
channel_id = "C123"
environments = [scalr_environment.test.id]
Expand All @@ -114,6 +125,7 @@ resource scalr_environment test {
resource "scalr_slack_integration" "test" {
name = "test-create2"
account_id = scalr_environment.test.account_id
run_mode = "apply"
events = ["run_success", "run_errored"]
channel_id = "C123"
environments = [scalr_environment.test.id]
Expand Down

0 comments on commit 455c597

Please sign in to comment.