Skip to content

Commit

Permalink
add delete resource for slack channels
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDurward committed May 27, 2018
1 parent fe824ca commit 06fadbb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 72 deletions.
12 changes: 5 additions & 7 deletions example/main.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Custom Terraform Provider: Slack
# Required: API_TOKEN

# Terraform Slack Provider
# Example
provider "slack" {
api_token = "SLACK_API_TOKEN"
}

# Create Slack Channel named OG Channel
resource "slack_channel" "this" {
channel_name = "OG Channel"
}
resource "slack_channel" "jenkins_ci" {
channel_name = "jenkins"
}
5 changes: 0 additions & 5 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ func Provider() terraform.ResourceProvider {
}

func configureProvider(d *schema.ResourceData) (interface{}, error) {
/*
Config: APIToken
A string value used to authenticate new slack clients
i.e api := slack.New(m.(*Config).APIKey)
*/
config := &Config{
APIToken: d.Get("api_token").(string),
}
Expand Down
1 change: 1 addition & 0 deletions provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var testAccProvider *schema.Provider

//TODO: Need to add actual tests...
func init() {
testAccProvider = Provider().(*schema.Provider)
}
Expand Down
82 changes: 22 additions & 60 deletions resource_channel.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package main

import (
"github.com/nlopes/slack"

"github.com/hashicorp/terraform/helper/schema"
"github.com/timdurward/slack"
)

func resourceChannel() *schema.Resource {
Expand All @@ -21,11 +20,6 @@ func resourceChannel() *schema.Resource {
Required: true,
},

"is_private": &schema.Schema{
Type: schema.TypeBool,
Description: "Boolean value to check if Slack channel will be private or not",
Optional: true,
},
"channel_topic": &schema.Schema{
Type: schema.TypeString,
Description: "Sets the topic for a channel",
Expand All @@ -40,28 +34,30 @@ func resourceChannelExists(d *schema.ResourceData, meta interface{}) (b bool, e
}

func resourceChannelCreate(d *schema.ResourceData, meta interface{}) error {
isPrivate := d.Get("is_private").(bool)

if isPrivate {
err := createPrivateChannel(d, meta)
if err != nil {
return err
}
} else {
err := createPublicChannel(d, meta)
if err != nil {
return err
}
api := slack.New(meta.(*Config).APIToken)

// Create Slack Channel
//TODO: Add 21 Character Limit check
channel, err := api.CreateChannel(d.Get("channel_name").(string))
if err != nil {
return err
}
d.SetId(channel.ID)

// Create Slack Channel Topic
if _, err := api.SetChannelTopic(channel.ID, d.Get("channel_topic").(string)); err != nil {
return err
}

return nil
}

func resourceChannelRead(d *schema.ResourceData, meta interface{}) error {
api := slack.New(meta.(*Config).APIToken)

_, channelResponse := api.GetChannelInfo(d.Id())

if channelResponse != nil {
// Checks if Slack Channel exists, if not remove resource from state
_, err := api.GetChannelInfo(d.Id())
if err != nil {
d.SetId("")
return nil
}
Expand All @@ -74,47 +70,13 @@ func resourceChannelUpdate(d *schema.ResourceData, meta interface{}) error {
}

func resourceChannelDelete(d *schema.ResourceData, meta interface{}) error {
return nil
}

func createPrivateChannel(d *schema.ResourceData, meta interface{}) error {
channelName := d.Get("channel_name").(string)
channelTopic := d.Get("channel_topic").(string)
api := slack.New(meta.(*Config).APIToken)

privateChannel, privateChannelerror := api.CreateGroup(channelName)

if privateChannelerror != nil {
return privateChannelerror
}

_, topicError := api.SetGroupTopic(privateChannel.ID, channelTopic)

if topicError != nil {
return topicError
}

d.SetId(privateChannel.ID)
return privateChannelerror
}

func createPublicChannel(d *schema.ResourceData, meta interface{}) error {
channelName := d.Get("channel_name").(string)
channelTopic := d.Get("channel_topic").(string)
api := slack.New(meta.(*Config).APIToken)

publicChannel, publicChannelError := api.CreateGroup(channelName)

if publicChannelError != nil {
return publicChannelError
}

_, topicError := api.SetGroupTopic(publicChannel.ID, channelTopic)

if topicError != nil {
return topicError
// Deletes Slack Channel and clears state
_, err := api.DeleteChannel(d.Id())
if err != nil {
return err
}

d.SetId(publicChannel.ID)
return nil
}

0 comments on commit 06fadbb

Please sign in to comment.