forked from buildkite/terraform-provider-buildkite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_pipeline.go
174 lines (148 loc) · 4.29 KB
/
resource_pipeline.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package buildkite
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/shurcooL/graphql"
)
type PipelineNode struct {
DefaultBranch graphql.String
Description graphql.String
Id graphql.String
Name graphql.String
Repository struct {
Url graphql.String
}
Slug graphql.String
Steps struct {
Yaml graphql.String
}
Uuid graphql.String
WebhookURL graphql.String `graphql:"webhookURL"`
}
func resourcePipeline() *schema.Resource {
return &schema.Resource{
Create: CreatePipeline,
Read: ReadPipeline,
Update: UpdatePipeline,
Delete: DeletePipeline,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Required: true,
Type: schema.TypeString,
},
"description": &schema.Schema{
Optional: true,
Type: schema.TypeString,
},
"repository": &schema.Schema{
Required: true,
Type: schema.TypeString,
},
"webhook_url": &schema.Schema{
Computed: true,
Type: schema.TypeString,
},
"slug": &schema.Schema{
Computed: true,
Type: schema.TypeString,
},
"steps": &schema.Schema{
Required: true,
Type: schema.TypeString,
},
},
}
}
func CreatePipeline(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
id, err := GetOrganizationID(client.organization, client.graphql)
if err != nil {
return err
}
var mutation struct {
PipelineCreate struct {
Pipeline PipelineNode
} `graphql:"pipelineCreate(input: {organizationId: $org, name: $name, description: $desc, repository: {url: $repository_url}, steps: {yaml: $steps}})"`
}
vars := map[string]interface{}{
"desc": graphql.String(d.Get("description").(string)),
"name": graphql.String(d.Get("name").(string)),
"org": id,
"repository_url": graphql.String(d.Get("repository").(string)),
"steps": graphql.String(d.Get("steps").(string)),
}
err = client.graphql.Mutate(context.Background(), &mutation, vars)
if err != nil {
return err
}
updatePipeline(d, &mutation.PipelineCreate.Pipeline)
return nil
}
func ReadPipeline(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
var query struct {
Node struct {
Pipeline PipelineNode `graphql:"... on Pipeline"`
} `graphql:"node(id: $id)"`
}
vars := map[string]interface{}{
"id": graphql.ID(d.Id()),
}
err := client.graphql.Query(context.Background(), &query, vars)
if err != nil {
return err
}
updatePipeline(d, &query.Node.Pipeline)
return nil
}
func UpdatePipeline(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
var mutation struct {
PipelineUpdate struct {
Pipeline PipelineNode
} `graphql:"pipelineUpdate(input: {id: $id, name: $name, description: $desc, repository: {url: $repository_url}, steps: {yaml: $steps}})"`
}
vars := map[string]interface{}{
"desc": graphql.String(d.Get("description").(string)),
"id": graphql.ID(d.Id()),
"name": graphql.String(d.Get("name").(string)),
"repository_url": graphql.String(d.Get("repository").(string)),
"steps": graphql.String("steps:\n - command: \"buildkite-agent pipeline upload\"\n label: \":pipeline:\""),
}
err := client.graphql.Mutate(context.Background(), &mutation, vars)
if err != nil {
return err
}
updatePipeline(d, &mutation.PipelineUpdate.Pipeline)
return nil
}
func DeletePipeline(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
// there is no delete mutation in graphql yet so we must use rest api
req, err := http.NewRequest("DELETE", fmt.Sprintf("https://api.buildkite.com/v2/organizations/%s/pipelines/%s",
client.organization, d.Get("slug").(string)), strings.NewReader(""))
if err != nil {
return err
}
resp, err := client.http.Do(req)
if err != nil && resp.StatusCode != 204 {
return err
}
return nil
}
func updatePipeline(d *schema.ResourceData, t *PipelineNode) {
d.SetId(string(t.Id))
d.Set("description", string(t.Description))
d.Set("name", string(t.Name))
d.Set("repository", string(t.Repository.Url))
d.Set("slug", string(t.Slug))
d.Set("steps", string(t.Steps.Yaml))
d.Set("uuid", string(t.Uuid))
d.Set("webhook_url", string(t.WebhookURL))
}