terraform/builtin/providers/github/resource_github_membership.go

90 lines
2.3 KiB
Go

package github
import (
"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": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"role": &schema.Schema{
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(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(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(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(n, meta.(*Organization).name)
return err
}