github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/github/resource_github_repository_collaborator.go (about) 1 package github 2 3 import ( 4 "github.com/google/go-github/github" 5 "github.com/hashicorp/terraform/helper/schema" 6 ) 7 8 func resourceGithubRepositoryCollaborator() *schema.Resource { 9 return &schema.Resource{ 10 Create: resourceGithubRepositoryCollaboratorCreate, 11 Read: resourceGithubRepositoryCollaboratorRead, 12 // editing repository collaborators are not supported by github api so forcing new on any changes 13 Delete: resourceGithubRepositoryCollaboratorDelete, 14 15 Schema: map[string]*schema.Schema{ 16 "username": &schema.Schema{ 17 Type: schema.TypeString, 18 Required: true, 19 ForceNew: true, 20 }, 21 "repository": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 "permission": &schema.Schema{ 27 Type: schema.TypeString, 28 Optional: true, 29 ForceNew: true, 30 Default: "push", 31 ValidateFunc: validateValueFunc([]string{"pull", "push", "admin"}), 32 }, 33 }, 34 } 35 } 36 37 func resourceGithubRepositoryCollaboratorCreate(d *schema.ResourceData, meta interface{}) error { 38 client := meta.(*Organization).client 39 u := d.Get("username").(string) 40 r := d.Get("repository").(string) 41 p := d.Get("permission").(string) 42 43 _, err := client.Repositories.AddCollaborator(meta.(*Organization).name, r, u, 44 &github.RepositoryAddCollaboratorOptions{Permission: p}) 45 46 if err != nil { 47 return err 48 } 49 50 d.SetId(buildTwoPartID(&r, &u)) 51 52 return resourceGithubRepositoryCollaboratorRead(d, meta) 53 } 54 55 func resourceGithubRepositoryCollaboratorRead(d *schema.ResourceData, meta interface{}) error { 56 client := meta.(*Organization).client 57 u := d.Get("username").(string) 58 r := d.Get("repository").(string) 59 60 isCollaborator, _, err := client.Repositories.IsCollaborator(meta.(*Organization).name, r, u) 61 62 if !isCollaborator || err != nil { 63 d.SetId("") 64 return nil 65 } 66 67 collaborators, _, err := client.Repositories.ListCollaborators(meta.(*Organization).name, r, 68 &github.ListOptions{}) 69 70 if err != nil { 71 return err 72 } 73 74 for _, c := range collaborators { 75 if *c.Login == u { 76 permName, err := getRepoPermission(c.Permissions) 77 78 if err != nil { 79 return err 80 } 81 82 d.Set("permission", permName) 83 84 return nil 85 } 86 } 87 88 return nil 89 } 90 91 func resourceGithubRepositoryCollaboratorDelete(d *schema.ResourceData, meta interface{}) error { 92 client := meta.(*Organization).client 93 u := d.Get("username").(string) 94 r := d.Get("repository").(string) 95 96 _, err := client.Repositories.RemoveCollaborator(meta.(*Organization).name, r, u) 97 98 return err 99 }