forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_github_membership.go
91 lines (73 loc) · 2.34 KB
/
resource_github_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
package github
import (
"context"
"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGithubMembership() *schema.Resource {
return &schema.Resource{
Create: resourceGithubMembershipCreate,
Read: resourceGithubMembershipRead,
Update: resourceGithubMembershipUpdate,
Delete: resourceGithubMembershipDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"role": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateValueFunc([]string{"member", "admin"}),
Default: "member",
},
},
}
}
func resourceGithubMembershipCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
n := d.Get("username").(string)
r := d.Get("role").(string)
membership, _, err := client.Organizations.EditOrgMembership(context.TODO(), n, meta.(*Organization).name,
&github.Membership{Role: &r})
if err != nil {
return err
}
d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login))
return resourceGithubMembershipRead(d, meta)
}
func resourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
_, n := parseTwoPartID(d.Id())
membership, _, err := client.Organizations.GetOrgMembership(context.TODO(), n, meta.(*Organization).name)
if err != nil {
d.SetId("")
return nil
}
d.Set("username", membership.User.Login)
d.Set("role", membership.Role)
return nil
}
func resourceGithubMembershipUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
n := d.Get("username").(string)
r := d.Get("role").(string)
membership, _, err := client.Organizations.EditOrgMembership(context.TODO(), n, meta.(*Organization).name, &github.Membership{
Role: &r,
})
if err != nil {
return err
}
d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login))
return nil
}
func resourceGithubMembershipDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
n := d.Get("username").(string)
_, err := client.Organizations.RemoveOrgMembership(context.TODO(), n, meta.(*Organization).name)
return err
}