Skip to content

Commit e626a9c

Browse files
authored
Merge pull request #165 from Cyb3r-Jak3/add-roles-to-server
Add readonly roles attribute to server
2 parents 6eb3dc8 + c6bfcc2 commit e626a9c

9 files changed

+200
-19
lines changed

discord/data_source_discord_server.go

+64
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,55 @@ func dataSourceDiscordServer() *schema.Resource {
7575
Computed: true,
7676
Description: "The ID of the owner.",
7777
},
78+
"roles": {
79+
Type: schema.TypeList,
80+
Computed: true,
81+
Description: "List of roles in the server.",
82+
Elem: &schema.Resource{
83+
Schema: map[string]*schema.Schema{
84+
"name": {
85+
Type: schema.TypeString,
86+
Computed: true,
87+
Description: "The name of the role.",
88+
},
89+
"permissions": {
90+
Type: schema.TypeInt,
91+
Computed: true,
92+
Description: "The permission bits of the role.",
93+
},
94+
"color": {
95+
Type: schema.TypeInt,
96+
Computed: true,
97+
Description: "Integer representation of the role color with decimal color code.",
98+
},
99+
"hoist": {
100+
Type: schema.TypeBool,
101+
Computed: true,
102+
Description: "If the role is hoisted.",
103+
},
104+
"mentionable": {
105+
Type: schema.TypeBool,
106+
Computed: true,
107+
Description: "If the role is mentionable.",
108+
},
109+
"position": {
110+
Type: schema.TypeInt,
111+
Computed: true,
112+
Description: "Position of the role. This is reverse indexed, with `@everyone` being `0`.",
113+
},
114+
"managed": {
115+
Type: schema.TypeBool,
116+
Computed: true,
117+
Description: "If role is managed by another service.",
118+
},
119+
"id": {
120+
Type: schema.TypeString,
121+
Computed: true,
122+
Description: "The ID of the role.",
123+
},
124+
},
125+
},
126+
},
78127
},
79128
}
80129
}
@@ -130,5 +179,20 @@ func dataSourceDiscordServerRead(ctx context.Context, d *schema.ResourceData, m
130179
d.Set("owner_id", server.OwnerID)
131180
}
132181

182+
var roleMap []map[string]interface{}
183+
for _, role := range server.Roles {
184+
roleMap = append(roleMap, map[string]interface{}{
185+
"name": role.Name,
186+
"permissions": role.Permissions,
187+
"color": role.Color,
188+
"hoist": role.Hoist,
189+
"mentionable": role.Mentionable,
190+
"position": role.Position,
191+
"managed": role.Managed,
192+
"id": role.ID,
193+
})
194+
}
195+
d.Set("roles", roleMap)
196+
133197
return diags
134198
}

discord/data_source_discord_server_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func TestAccDatasourceDiscordServer(t *testing.T) {
2929
resource.TestCheckResourceAttr(name, "explicit_content_filter", "2"),
3030
resource.TestCheckResourceAttr(name, "afk_timeout", "300"),
3131
resource.TestCheckResourceAttrSet(name, "owner_id"),
32+
resource.TestCheckResourceAttrSet(name, "roles"),
3233
),
3334
},
3435
},

discord/resource_discord_channel.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ func resourceChannelUpdate(ctx context.Context, d *schema.ResourceData, m interf
295295
)
296296

297297
name = map[bool]string{true: d.Get("name").(string), false: channel.Name}[d.HasChange("name")]
298-
position = map[bool]int{true: int(d.Get("position").(int)), false: int(channel.Position)}[d.HasChange("position")]
298+
position = map[bool]int{true: d.Get("position").(int), false: channel.Position}[d.HasChange("position")]
299299

300300
switch channelType {
301301
case "text", "news":
@@ -305,8 +305,8 @@ func resourceChannelUpdate(ctx context.Context, d *schema.ResourceData, m interf
305305
}
306306
case "voice":
307307
{
308-
bitRate = map[bool]int{true: int(d.Get("bitrate").(int)), false: channel.Bitrate}[d.HasChange("bitrate")]
309-
userLimit = map[bool]int{true: int(d.Get("user_limit").(int)), false: channel.UserLimit}[d.HasChange("user_limit")]
308+
bitRate = map[bool]int{true: d.Get("bitrate").(int), false: channel.Bitrate}[d.HasChange("bitrate")]
309+
userLimit = map[bool]int{true: d.Get("user_limit").(int), false: channel.UserLimit}[d.HasChange("user_limit")]
310310
}
311311
}
312312

discord/resource_discord_role.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package discord
22

33
import (
4+
"context"
45
"github.com/bwmarrin/discordgo"
56
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
8-
"golang.org/x/net/context"
99
)
1010

1111
func resourceDiscordRole() *schema.Resource {

discord/resource_discord_server.go

+84-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package discord
22

33
import (
44
"fmt"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
56

67
"github.com/bwmarrin/discordgo"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -66,20 +67,11 @@ func baseServerSchema() map[string]*schema.Schema {
6667
Description: "ID of the channel AFK users will be moved to.",
6768
},
6869
"afk_timeout": {
69-
Type: schema.TypeInt,
70-
Optional: true,
71-
Default: 300,
72-
Description: "How many seconds before moving an AFK user.",
73-
ValidateFunc: func(val interface{}, key string) (warns []string, errors []error) {
74-
v := val.(int)
75-
// See: https://discord.com/developers/docs/resources/guild#guild-object-guild-structure
76-
expected := []int{60, 300, 900, 1800, 3600}
77-
if !contains(expected, v) {
78-
errors = append(errors, fmt.Errorf("afk_timeout must be set to one of the following values: %d, but got: %d", expected, v))
79-
}
80-
81-
return
82-
},
70+
Type: schema.TypeInt,
71+
Optional: true,
72+
Default: 300,
73+
Description: "How many seconds before moving an AFK user.",
74+
ValidateFunc: validation.IntInSlice([]int{60, 300, 900, 1800, 3600}),
8375
},
8476
"icon_url": {
8577
Type: schema.TypeString,
@@ -122,6 +114,55 @@ func baseServerSchema() map[string]*schema.Schema {
122114
Computed: true,
123115
Description: "The ID of the server.",
124116
},
117+
"roles": {
118+
Type: schema.TypeList,
119+
Computed: true,
120+
Description: "List of roles in the server.",
121+
Elem: &schema.Resource{
122+
Schema: map[string]*schema.Schema{
123+
"name": {
124+
Type: schema.TypeString,
125+
Computed: true,
126+
Description: "The name of the role.",
127+
},
128+
"permissions": {
129+
Type: schema.TypeInt,
130+
Computed: true,
131+
Description: "The permission bits of the role.",
132+
},
133+
"color": {
134+
Type: schema.TypeInt,
135+
Computed: true,
136+
Description: "Integer representation of the role color with decimal color code.",
137+
},
138+
"hoist": {
139+
Type: schema.TypeBool,
140+
Computed: true,
141+
Description: "If the role is hoisted.",
142+
},
143+
"mentionable": {
144+
Type: schema.TypeBool,
145+
Computed: true,
146+
Description: "If the role is mentionable.",
147+
},
148+
"position": {
149+
Type: schema.TypeInt,
150+
Computed: true,
151+
Description: "Position of the role. This is reverse indexed, with `@everyone` being `0`.",
152+
},
153+
"managed": {
154+
Type: schema.TypeBool,
155+
Computed: true,
156+
Description: "If role is managed by another service.",
157+
},
158+
"id": {
159+
Type: schema.TypeString,
160+
Computed: true,
161+
Description: "The ID of the role.",
162+
},
163+
},
164+
},
165+
},
125166
}
126167
}
127168

@@ -270,6 +311,21 @@ func resourceServerCreate(ctx context.Context, d *schema.ResourceData, m interfa
270311
d.Set("icon_hash", server.Icon)
271312
d.Set("splash_hash", server.Splash)
272313

314+
var roleMap []map[string]interface{}
315+
for _, role := range server.Roles {
316+
roleMap = append(roleMap, map[string]interface{}{
317+
"name": role.Name,
318+
"permissions": role.Permissions,
319+
"color": role.Color,
320+
"hoist": role.Hoist,
321+
"mentionable": role.Mentionable,
322+
"position": role.Position,
323+
"managed": role.Managed,
324+
"id": role.ID,
325+
})
326+
}
327+
d.Set("roles", roleMap)
328+
273329
return diags
274330
}
275331

@@ -316,7 +372,20 @@ func resourceServerRead(ctx context.Context, d *schema.ResourceData, m interface
316372
if d.Get("owner_id").(string) != "" && server.OwnerID != "" {
317373
d.Set("owner_id", server.OwnerID)
318374
}
319-
375+
roleMap := make([]map[string]interface{}, len(server.Roles))
376+
for _, role := range server.Roles {
377+
roleMap = append(roleMap, map[string]interface{}{
378+
"name": role.Name,
379+
"permissions": role.Permissions,
380+
"color": role.Color,
381+
"hoist": role.Hoist,
382+
"mentionable": role.Mentionable,
383+
"position": role.Position,
384+
"managed": role.Managed,
385+
"id": role.ID,
386+
})
387+
}
388+
d.Set("roles", roleMap)
320389
return diags
321390
}
322391

discord/resource_discord_server_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func TestAccResourceDiscordServer(t *testing.T) {
2222
resource.TestCheckResourceAttr(name, "explicit_content_filter", "0"),
2323
resource.TestCheckResourceAttr(name, "afk_timeout", "300"),
2424
resource.TestCheckResourceAttrSet(name, "owner_id"),
25+
resource.TestCheckResourceAttr(name, "roles.#", "1"),
26+
resource.TestCheckResourceAttrSet(name, "roles.0.id"),
2527
),
2628
},
2729
},

docs/data-sources/server.md

+15
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,20 @@ output "discord_api_region" {
4040
- `id` (String) The ID of the server.
4141
- `owner_id` (String) The ID of the owner.
4242
- `region` (String) The region of the server.
43+
- `roles` (List of Object) List of roles in the server. (see [below for nested schema](#nestedatt--roles))
4344
- `splash_hash` (String) The hash of the server splash.
4445
- `verification_level` (Number) The required verification level of the server.
46+
47+
<a id="nestedatt--roles"></a>
48+
### Nested Schema for `roles`
49+
50+
Read-Only:
51+
52+
- `color` (Number)
53+
- `hoist` (Boolean)
54+
- `id` (String)
55+
- `managed` (Boolean)
56+
- `mentionable` (Boolean)
57+
- `name` (String)
58+
- `permissions` (Number)
59+
- `position` (Number)

docs/resources/managed_server.md

+15
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,23 @@ resource "discord_managed_server" "my_server" {
4444

4545
- `icon_hash` (String) Hash of the icon.
4646
- `id` (String) The ID of the server.
47+
- `roles` (List of Object) List of roles in the server. (see [below for nested schema](#nestedatt--roles))
4748
- `splash_hash` (String) Hash of the splash.
4849

50+
<a id="nestedatt--roles"></a>
51+
### Nested Schema for `roles`
52+
53+
Read-Only:
54+
55+
- `color` (Number)
56+
- `hoist` (Boolean)
57+
- `id` (String)
58+
- `managed` (Boolean)
59+
- `mentionable` (Boolean)
60+
- `name` (String)
61+
- `permissions` (Number)
62+
- `position` (Number)
63+
4964
## Import
5065

5166
Import is supported using the following syntax:

docs/resources/server.md

+15
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,24 @@ resource "discord_server" "my_server" {
4444

4545
- `icon_hash` (String) Hash of the icon.
4646
- `id` (String) The ID of the server.
47+
- `roles` (List of Object) List of roles in the server. (see [below for nested schema](#nestedatt--roles))
4748
- `server_id` (String) The ID of the server to manage.
4849
- `splash_hash` (String) Hash of the splash.
4950

51+
<a id="nestedatt--roles"></a>
52+
### Nested Schema for `roles`
53+
54+
Read-Only:
55+
56+
- `color` (Number)
57+
- `hoist` (Boolean)
58+
- `id` (String)
59+
- `managed` (Boolean)
60+
- `mentionable` (Boolean)
61+
- `name` (String)
62+
- `permissions` (Number)
63+
- `position` (Number)
64+
5065
## Import
5166

5267
Import is supported using the following syntax:

0 commit comments

Comments
 (0)