github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/github/resource_github_membership.go (about) 1 package github 2 3 import ( 4 "context" 5 6 "github.com/google/go-github/github" 7 "github.com/hashicorp/terraform/helper/schema" 8 ) 9 10 func resourceGithubMembership() *schema.Resource { 11 12 return &schema.Resource{ 13 Create: resourceGithubMembershipCreate, 14 Read: resourceGithubMembershipRead, 15 Update: resourceGithubMembershipUpdate, 16 Delete: resourceGithubMembershipDelete, 17 Importer: &schema.ResourceImporter{ 18 State: schema.ImportStatePassthrough, 19 }, 20 21 Schema: map[string]*schema.Schema{ 22 "username": { 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 "role": { 28 Type: schema.TypeString, 29 Optional: true, 30 ValidateFunc: validateValueFunc([]string{"member", "admin"}), 31 Default: "member", 32 }, 33 }, 34 } 35 } 36 37 func resourceGithubMembershipCreate(d *schema.ResourceData, meta interface{}) error { 38 client := meta.(*Organization).client 39 n := d.Get("username").(string) 40 r := d.Get("role").(string) 41 42 membership, _, err := client.Organizations.EditOrgMembership(context.TODO(), n, meta.(*Organization).name, 43 &github.Membership{Role: &r}) 44 if err != nil { 45 return err 46 } 47 48 d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login)) 49 50 return resourceGithubMembershipRead(d, meta) 51 } 52 53 func resourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) error { 54 client := meta.(*Organization).client 55 _, n := parseTwoPartID(d.Id()) 56 57 membership, _, err := client.Organizations.GetOrgMembership(context.TODO(), n, meta.(*Organization).name) 58 if err != nil { 59 d.SetId("") 60 return nil 61 } 62 63 d.Set("username", membership.User.Login) 64 d.Set("role", membership.Role) 65 return nil 66 } 67 68 func resourceGithubMembershipUpdate(d *schema.ResourceData, meta interface{}) error { 69 client := meta.(*Organization).client 70 n := d.Get("username").(string) 71 r := d.Get("role").(string) 72 73 membership, _, err := client.Organizations.EditOrgMembership(context.TODO(), n, meta.(*Organization).name, &github.Membership{ 74 Role: &r, 75 }) 76 if err != nil { 77 return err 78 } 79 d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login)) 80 81 return nil 82 } 83 84 func resourceGithubMembershipDelete(d *schema.ResourceData, meta interface{}) error { 85 client := meta.(*Organization).client 86 n := d.Get("username").(string) 87 88 _, err := client.Organizations.RemoveOrgMembership(context.TODO(), n, meta.(*Organization).name) 89 90 return err 91 }