-
Notifications
You must be signed in to change notification settings - Fork 20
/
resource_channel.go
125 lines (104 loc) · 3.22 KB
/
resource_channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/slack-go/slack"
)
func resourceChannel() *schema.Resource {
return &schema.Resource{
Create: resourceChannelCreate,
Read: resourceChannelRead,
Update: resourceChannelUpdate,
Delete: resourceChannelDelete,
Exists: resourceChannelExists,
Importer: &schema.ResourceImporter{
State: resourceChannelImportState,
},
Schema: map[string]*schema.Schema{
"channel_name": &schema.Schema{
Type: schema.TypeString,
Description: "The name of Slack Channel that will be created",
Required: true,
ValidateFunc: validation.StringLenBetween(1, 80),
},
"channel_purpose": &schema.Schema{
Type: schema.TypeString,
Description: "Sets the purpose for a channel",
Optional: true,
},
"channel_topic": &schema.Schema{
Type: schema.TypeString,
Description: "Sets the topic for a channel",
Optional: true,
},
},
}
}
func resourceChannelExists(d *schema.ResourceData, meta interface{}) (b bool, e error) {
return true, nil
}
func resourceChannelCreate(d *schema.ResourceData, meta interface{}) error {
api := slack.New(meta.(*Config).APIToken)
// Create Slack Channel
channel, err := api.CreateChannel(d.Get("channel_name").(string))
if err != nil {
return err
}
d.SetId(channel.ID)
// Update Slack Channel Purpose
if _, err := api.SetChannelPurpose(d.Id(), d.Get("channel_purpose").(string)); err != nil {
return err
}
// 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)
// Checks if Slack Channel exists, if not remove resource from state
_, err := api.GetChannelInfo(d.Id())
if err != nil {
d.SetId("")
return nil
}
return nil
}
func resourceChannelUpdate(d *schema.ResourceData, meta interface{}) error {
api := slack.New(meta.(*Config).APIToken)
name := d.Get("channel_name").(string)
if _, err := api.RenameChannel(d.Id(), name); err != nil {
return err
}
// Update Slack Channel Purpose
if _, err := api.SetChannelPurpose(d.Id(), d.Get("channel_purpose").(string)); err != nil {
return err
}
// Update Slack Channel Topic
if _, err := api.SetChannelTopic(d.Id(), d.Get("channel_topic").(string)); err != nil {
return err
}
return nil
}
func resourceChannelDelete(d *schema.ResourceData, meta interface{}) error {
api := slack.New(meta.(*Config).APIToken)
// Deletes Slack Channel and clears state
if _, err := api.DeleteChannel(d.Id()); err != nil {
return err
}
return nil
}
func resourceChannelImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
api := slack.New(meta.(*Config).APIToken)
// Checks if Slack Channel exists, if not remove resource from state
channel, err := api.GetConversationInfo(d.Id(), false)
if err != nil {
d.SetId("")
return nil, err
}
d.Set("channel_name", channel.Name)
d.Set("channel_purpose", channel.Purpose.Value)
d.Set("channel_topic", channel.Topic.Value)
return []*schema.ResourceData{d}, nil
}