forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_github_issue_label.go
104 lines (87 loc) · 2.44 KB
/
resource_github_issue_label.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
package github
import (
"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGithubIssueLabel() *schema.Resource {
return &schema.Resource{
Create: resourceGithubIssueLabelCreate,
Read: resourceGithubIssueLabelRead,
Update: resourceGithubIssueLabelUpdate,
Delete: resourceGithubIssueLabelDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"repository": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"color": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceGithubIssueLabelCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
r := d.Get("repository").(string)
n := d.Get("name").(string)
c := d.Get("color").(string)
_, _, err := client.Issues.CreateLabel(meta.(*Organization).name, r, &github.Label{
Name: &n,
Color: &c,
})
if err != nil {
return err
}
d.SetId(buildTwoPartID(&r, &n))
return resourceGithubIssueLabelRead(d, meta)
}
func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
r, n := parseTwoPartID(d.Id())
githubLabel, _, err := client.Issues.GetLabel(meta.(*Organization).name, r, n)
if err != nil {
d.SetId("")
return nil
}
d.Set("repository", r)
d.Set("name", n)
d.Set("color", githubLabel.Color)
d.Set("url", githubLabel.URL)
return nil
}
func resourceGithubIssueLabelUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
r := d.Get("repository").(string)
n := d.Get("name").(string)
c := d.Get("color").(string)
_, originalName := parseTwoPartID(d.Id())
_, _, err := client.Issues.EditLabel(meta.(*Organization).name, r, originalName, &github.Label{
Name: &n,
Color: &c,
})
if err != nil {
return err
}
d.SetId(buildTwoPartID(&r, &n))
return resourceGithubIssueLabelRead(d, meta)
}
func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
r := d.Get("repository").(string)
n := d.Get("name").(string)
_, err := client.Issues.DeleteLabel(meta.(*Organization).name, r, n)
return err
}