forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_github_team.go
101 lines (88 loc) · 2.46 KB
/
resource_github_team.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
package github
import (
"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGithubTeam() *schema.Resource {
return &schema.Resource{
Create: resourceGithubTeamCreate,
Read: resourceGithubTeamRead,
Update: resourceGithubTeamUpdate,
Delete: resourceGithubTeamDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"privacy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "secret",
ValidateFunc: validateValueFunc([]string{"secret", "closed"}),
},
},
}
}
func resourceGithubTeamCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
n := d.Get("name").(string)
desc := d.Get("description").(string)
p := d.Get("privacy").(string)
githubTeam, _, err := client.Organizations.CreateTeam(meta.(*Organization).name, &github.Team{
Name: &n,
Description: &desc,
Privacy: &p,
})
if err != nil {
return err
}
d.SetId(fromGithubID(githubTeam.ID))
return resourceGithubTeamRead(d, meta)
}
func resourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
team, err := getGithubTeam(d, client)
if err != nil {
d.SetId("")
return nil
}
d.Set("description", team.Description)
d.Set("name", team.Name)
d.Set("privacy", team.Privacy)
return nil
}
func resourceGithubTeamUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
team, err := getGithubTeam(d, client)
if err != nil {
d.SetId("")
return nil
}
name := d.Get("name").(string)
description := d.Get("description").(string)
privacy := d.Get("privacy").(string)
team.Description = &description
team.Name = &name
team.Privacy = &privacy
team, _, err = client.Organizations.EditTeam(*team.ID, team)
if err != nil {
return err
}
d.SetId(fromGithubID(team.ID))
return resourceGithubTeamRead(d, meta)
}
func resourceGithubTeamDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
id := toGithubID(d.Id())
_, err := client.Organizations.DeleteTeam(id)
return err
}
func getGithubTeam(d *schema.ResourceData, github *github.Client) (*github.Team, error) {
id := toGithubID(d.Id())
team, _, err := github.Organizations.GetTeam(id)
return team, err
}