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