Skip to content

Commit

Permalink
Merge pull request #49 from jmatsu/fix/channel_deletion
Browse files Browse the repository at this point in the history
Allow users to choose which change should happen to channels when destroying them
  • Loading branch information
jmatsu authored Sep 1, 2021
2 parents dfc65d9 + 6b8b58f commit 8a9e371
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

This is a [Terraform](https://www.terraform.io/) provider for [Slack](https://slack.com)

## Maintainers

@jmatsu, @billcchung

# Installation

ref: https://registry.terraform.io/providers/jmatsu/slack/latest
Expand All @@ -31,7 +27,7 @@ See https://www.terraform.io/docs/configuration/providers.html#third-party-plugi

## Limitations

**I do not have any Plus or Enterprise Grid workspace which I'm free to use unfortunately.**
**I do not have any Plus or Enterprise Grid workspace which I'm free to use, unfortunately.**

That's why several resources, e.g. a slack user, have not been supported yet.

Expand Down Expand Up @@ -61,6 +57,7 @@ resource "slack_conversation" "..." {
name = "<name>"
topic = "..."
purpose = "..."
action_on_destroy = "<archive|none>" # this is required since v0.8.0
is_archive = <true|false>
is_private = <true|false>
}
Expand Down Expand Up @@ -106,4 +103,8 @@ git push "v$version"

## LICENSE

Under [MIT](./LICENSE)
Under [MIT](./LICENSE)

## Maintainers

@jmatsu, @billcchung
23 changes: 20 additions & 3 deletions slack/resource_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slack

import (
"context"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/slack-go/slack"
"log"
Expand Down Expand Up @@ -38,6 +39,12 @@ func resourceSlackChannel() *schema.Resource {
Optional: true,
Default: false,
},
"action_on_destroy": {
Type: schema.TypeString,
Description: "Either of none or archive",
Required: true,
ValidateFunc: validateConversationActionOnDestroyValue,
},
"is_shared": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -174,10 +181,20 @@ func resourceSlackChannelDelete(d *schema.ResourceData, meta interface{}) error
ctx := context.WithValue(context.Background(), ctxId, d.Id())
id := d.Id()

log.Printf("[DEBUG] Deleting(archive) Channel: %s (%s)", id, d.Get("name"))
action := d.Get("action_on_destroy").(string)

if err := client.ArchiveChannelContext(ctx, id); err != nil {
return err
switch action {
case conversationActionOnDestroyNone:
log.Printf("[DEBUG] Do nothing on Channel: %s (%s)", id, d.Get("name"))
case conversationActionOnDestroyArchive:
log.Printf("[DEBUG] Deleting(archive) Channel: %s (%s)", id, d.Get("name"))
if err := client.ArchiveChannelContext(ctx, id); err != nil {
if err.Error() != "already_archived" {
return err
}
}
default:
return fmt.Errorf("unknown action was provided. (%s)", action)
}

d.SetId("")
Expand Down
34 changes: 31 additions & 3 deletions slack/resource_conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@ package slack

import (
"context"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/slack-go/slack"
"log"
)

const (
conversationActionOnDestroyNone = "none"
conversationActionOnDestroyArchive = "archive"
)

var validateConversationActionOnDestroyValue = validation.StringInSlice([]string{
conversationActionOnDestroyNone,
conversationActionOnDestroyArchive,
}, false)

func resourceSlackConversation() *schema.Resource {
return &schema.Resource{
Read: resourceSlackConversationRead,
Expand Down Expand Up @@ -40,6 +52,12 @@ func resourceSlackConversation() *schema.Resource {
Optional: true,
Default: false,
},
"action_on_destroy": {
Type: schema.TypeString,
Description: "Either of none or archive",
Required: true,
ValidateFunc: validateConversationActionOnDestroyValue,
},
"is_shared": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -173,10 +191,20 @@ func resourceSlackConversationDelete(d *schema.ResourceData, meta interface{}) e
ctx := context.WithValue(context.Background(), ctxId, d.Id())
id := d.Id()

log.Printf("[DEBUG] Deleting(archive) Conversation: %s (%s)", id, d.Get("name"))
action := d.Get("action_on_destroy").(string)

if err := client.ArchiveConversationContext(ctx, id); err != nil {
return err
switch action {
case conversationActionOnDestroyNone:
log.Printf("[DEBUG] Do nothing on Conversation: %s (%s)", id, d.Get("name"))
case conversationActionOnDestroyArchive:
log.Printf("[DEBUG] Deleting(archive) Conversation: %s (%s)", id, d.Get("name"))
if err := client.ArchiveConversationContext(ctx, id); err != nil {
if err.Error() != "already_archived" {
return err
}
}
default:
return fmt.Errorf("unknown action was provided. (%s)", action)
}

d.SetId("")
Expand Down
23 changes: 20 additions & 3 deletions slack/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slack

import (
"context"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/slack-go/slack"
"log"
Expand Down Expand Up @@ -44,6 +45,12 @@ func resourceSlackGroup() *schema.Resource {
Optional: true,
Default: false,
},
"action_on_destroy": {
Type: schema.TypeString,
Description: "Either of none or archive",
Required: true,
ValidateFunc: validateConversationActionOnDestroyValue,
},
"is_shared": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -175,10 +182,20 @@ func resourceSlackGroupDelete(d *schema.ResourceData, meta interface{}) error {
ctx := context.WithValue(context.Background(), ctxId, d.Id())
id := d.Id()

log.Printf("[DEBUG] Deleting(archive) Group: %s (%s)", id, d.Get("name"))
action := d.Get("action_on_destroy").(string)

if err := client.ArchiveGroupContext(ctx, id); err != nil {
return err
switch action {
case conversationActionOnDestroyNone:
log.Printf("[DEBUG] Do nothing on Group: %s (%s)", id, d.Get("name"))
case conversationActionOnDestroyArchive:
log.Printf("[DEBUG] Deleting(archive) Group: %s (%s)", id, d.Get("name"))
if err := client.ArchiveGroupContext(ctx, id); err != nil {
if err.Error() != "already_archived" {
return err
}
}
default:
return fmt.Errorf("unknown action was provided. (%s)", action)
}

d.SetId("")
Expand Down

0 comments on commit 8a9e371

Please sign in to comment.