github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/github/resource_github_team_repository.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 resourceGithubTeamRepository() *schema.Resource { 9 return &schema.Resource{ 10 Create: resourceGithubTeamRepositoryCreate, 11 Read: resourceGithubTeamRepositoryRead, 12 Update: resourceGithubTeamRepositoryUpdate, 13 Delete: resourceGithubTeamRepositoryDelete, 14 Importer: &schema.ResourceImporter{ 15 State: schema.ImportStatePassthrough, 16 }, 17 18 Schema: map[string]*schema.Schema{ 19 "team_id": &schema.Schema{ 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 "repository": &schema.Schema{ 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 }, 29 "permission": &schema.Schema{ 30 Type: schema.TypeString, 31 Optional: true, 32 Default: "pull", 33 ValidateFunc: validateValueFunc([]string{"pull", "push", "admin"}), 34 }, 35 }, 36 } 37 } 38 39 func resourceGithubTeamRepositoryCreate(d *schema.ResourceData, meta interface{}) error { 40 client := meta.(*Organization).client 41 t := d.Get("team_id").(string) 42 r := d.Get("repository").(string) 43 p := d.Get("permission").(string) 44 45 _, err := client.Organizations.AddTeamRepo(toGithubID(t), meta.(*Organization).name, r, 46 &github.OrganizationAddTeamRepoOptions{Permission: p}) 47 48 if err != nil { 49 return err 50 } 51 52 d.SetId(buildTwoPartID(&t, &r)) 53 54 return resourceGithubTeamRepositoryRead(d, meta) 55 } 56 57 func resourceGithubTeamRepositoryRead(d *schema.ResourceData, meta interface{}) error { 58 client := meta.(*Organization).client 59 t, r := parseTwoPartID(d.Id()) 60 61 repo, _, repoErr := client.Organizations.IsTeamRepo(toGithubID(t), meta.(*Organization).name, r) 62 63 if repoErr != nil { 64 d.SetId("") 65 return nil 66 } 67 68 repositoryName := repo.Name 69 70 d.Set("team_id", t) 71 d.Set("repository", repositoryName) 72 73 permName, permErr := getRepoPermission(repo.Permissions) 74 75 if permErr != nil { 76 return permErr 77 } 78 79 d.Set("permission", permName) 80 81 return nil 82 } 83 84 func resourceGithubTeamRepositoryUpdate(d *schema.ResourceData, meta interface{}) error { 85 client := meta.(*Organization).client 86 t := d.Get("team_id").(string) 87 r := d.Get("repository").(string) 88 p := d.Get("permission").(string) 89 90 // the go-github library's AddTeamRepo method uses the add/update endpoint from Github API 91 _, err := client.Organizations.AddTeamRepo(toGithubID(t), meta.(*Organization).name, r, 92 &github.OrganizationAddTeamRepoOptions{Permission: p}) 93 94 if err != nil { 95 return err 96 } 97 d.SetId(buildTwoPartID(&t, &r)) 98 99 return resourceGithubTeamRepositoryRead(d, meta) 100 } 101 102 func resourceGithubTeamRepositoryDelete(d *schema.ResourceData, meta interface{}) error { 103 client := meta.(*Organization).client 104 t := d.Get("team_id").(string) 105 r := d.Get("repository").(string) 106 107 _, err := client.Organizations.RemoveTeamRepo(toGithubID(t), meta.(*Organization).name, r) 108 109 return err 110 }