forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_github_team_membership.go
100 lines (81 loc) · 2.38 KB
/
resource_github_team_membership.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
package github
import (
"strings"
"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGithubTeamMembership() *schema.Resource {
return &schema.Resource{
Create: resourceGithubTeamMembershipCreate,
Read: resourceGithubTeamMembershipRead,
// editing team memberships are not supported by github api so forcing new on any changes
Delete: resourceGithubTeamMembershipDelete,
Schema: map[string]*schema.Schema{
"team_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"username": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"role": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "member",
ValidateFunc: validateValueFunc([]string{"member", "maintainer"}),
},
},
}
}
func resourceGithubTeamMembershipCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
t := d.Get("team_id").(string)
n := d.Get("username").(string)
r := d.Get("role").(string)
_, _, err := client.Organizations.AddTeamMembership(toGithubID(t), n,
&github.OrganizationAddTeamMembershipOptions{Role: r})
if err != nil {
return err
}
d.SetId(buildTwoPartID(&t, &n))
return resourceGithubTeamMembershipRead(d, meta)
}
func resourceGithubTeamMembershipRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
t := d.Get("team_id").(string)
n := d.Get("username").(string)
membership, _, err := client.Organizations.GetTeamMembership(toGithubID(t), n)
if err != nil {
d.SetId("")
return nil
}
team, user := getTeamAndUserFromURL(membership.URL)
d.Set("username", user)
d.Set("role", membership.Role)
d.Set("team_id", team)
return nil
}
func resourceGithubTeamMembershipDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
t := d.Get("team_id").(string)
n := d.Get("username").(string)
_, err := client.Organizations.RemoveTeamMembership(toGithubID(t), n)
return err
}
func getTeamAndUserFromURL(url *string) (string, string) {
var team, user string
urlSlice := strings.Split(*url, "/")
for v := range urlSlice {
if urlSlice[v] == "teams" {
team = urlSlice[v+1]
}
if urlSlice[v] == "memberships" {
user = urlSlice[v+1]
}
}
return team, user
}