forked from hashicorp/terraform
/
resource_github_team_repository.go
129 lines (102 loc) · 3.42 KB
/
resource_github_team_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package github
import (
"errors"
"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
)
const pullPermission string = "pull"
const pushPermission string = "push"
const adminPermission string = "admin"
func resourceGithubTeamRepository() *schema.Resource {
return &schema.Resource{
Create: resourceGithubTeamRepositoryCreate,
Read: resourceGithubTeamRepositoryRead,
Update: resourceGithubTeamRepositoryUpdate,
Delete: resourceGithubTeamRepositoryDelete,
Schema: map[string]*schema.Schema{
"team_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"repository": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"permission": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "pull",
ValidateFunc: validateValueFunc([]string{"pull", "push", "admin"}),
},
},
}
}
func resourceGithubTeamRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
t := d.Get("team_id").(string)
r := d.Get("repository").(string)
p := d.Get("permission").(string)
_, err := client.Organizations.AddTeamRepo(toGithubID(t), meta.(*Organization).name, r,
&github.OrganizationAddTeamRepoOptions{Permission: p})
if err != nil {
return err
}
d.SetId(buildTwoPartID(&t, &r))
return resourceGithubTeamRepositoryRead(d, meta)
}
func resourceGithubTeamRepositoryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
t := d.Get("team_id").(string)
r := d.Get("repository").(string)
repo, _, repoErr := client.Organizations.IsTeamRepo(toGithubID(t), meta.(*Organization).name, r)
if repoErr != nil {
d.SetId("")
return nil
}
repositoryName := repo.Name
d.Set("team_id", t)
d.Set("repository", repositoryName)
permName, permErr := getRepoPermission(repo.Permissions)
if permErr != nil {
return permErr
}
d.Set("permission", permName)
return nil
}
func resourceGithubTeamRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
t := d.Get("team_id").(string)
r := d.Get("repository").(string)
p := d.Get("permission").(string)
// the go-github library's AddTeamRepo method uses the add/update endpoint from Github API
_, err := client.Organizations.AddTeamRepo(toGithubID(t), meta.(*Organization).name, r,
&github.OrganizationAddTeamRepoOptions{Permission: p})
if err != nil {
return err
}
return resourceGithubTeamRepositoryRead(d, meta)
}
func resourceGithubTeamRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
t := d.Get("team_id").(string)
r := d.Get("repository").(string)
_, err := client.Organizations.RemoveTeamRepo(toGithubID(t), meta.(*Organization).name, r)
return err
}
func getRepoPermission(p *map[string]bool) (string, error) {
// Permissions are returned in this map format such that if you have a certain level
// of permission, all levels below are also true. For example, if a team has push
// permission, the map will be: {"pull": true, "push": true, "admin": false}
if (*p)[adminPermission] {
return adminPermission, nil
} else if (*p)[pushPermission] {
return pushPermission, nil
} else {
if (*p)[pullPermission] {
return pullPermission, nil
}
return "", errors.New("At least one permission expected from permissions map.")
}
}