forked from hashicorp/terraform-provider-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_project.go
191 lines (155 loc) · 3.97 KB
/
resource_project.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package bitbucket
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"github.com/hashicorp/terraform/helper/schema"
"strings"
)
// Project is the project data we need to send to create a project on the bitbucket api
type Project struct {
Key string `json:"key,omitempty"`
IsPrivate bool `json:"is_private,omitempty"`
Owner string `json:"owner.username,omitempty"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
UUID string `json:"uuid,omitempty"`
}
func resourceProject() *schema.Resource {
return &schema.Resource{
Create: resourceProjectCreate,
Update: resourceProjectUpdate,
Read: resourceProjectRead,
Delete: resourceProjectDelete,
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},
"is_private": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"owner": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func newProjectFromResource(d *schema.ResourceData) *Project {
project := &Project{
Name: d.Get("name").(string),
IsPrivate: d.Get("is_private").(bool),
Description: d.Get("description").(string),
Key: d.Get("key").(string),
}
return project
}
func resourceProjectUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
project := newProjectFromResource(d)
var jsonbuffer []byte
jsonpayload := bytes.NewBuffer(jsonbuffer)
enc := json.NewEncoder(jsonpayload)
enc.Encode(project)
var projectKey string
projectKey = d.Get("key").(string)
if projectKey == "" {
projectKey = d.Get("key").(string)
}
_, err := client.Put(fmt.Sprintf("2.0/teams/%s/projects/%s",
d.Get("owner").(string),
projectKey,
), jsonpayload)
if err != nil {
return err
}
return resourceProjectRead(d, m)
}
func resourceProjectCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
project := newProjectFromResource(d)
bytedata, err := json.Marshal(project)
if err != nil {
return err
}
var projectKey string
projectKey = d.Get("key").(string)
if projectKey == "" {
projectKey = d.Get("key").(string)
}
owner := d.Get("owner").(string)
if owner == "" {
return fmt.Errorf("owner must not be a empty string")
}
_, err = client.Post(fmt.Sprintf("2.0/teams/%s/projects/",
d.Get("owner").(string),
), bytes.NewBuffer(bytedata))
if err != nil {
return err
}
d.SetId(string(fmt.Sprintf("%s/%s", d.Get("owner").(string), projectKey)))
return resourceProjectRead(d, m)
}
func resourceProjectRead(d *schema.ResourceData, m interface{}) error {
id := d.Id()
if id != "" {
idparts := strings.Split(id, "/")
if len(idparts) == 2 {
d.Set("owner", idparts[0])
d.Set("key", idparts[1])
} else {
return fmt.Errorf("Incorrect ID format, should match `owner/key`")
}
}
var projectKey string
projectKey = d.Get("key").(string)
if projectKey == "" {
projectKey = d.Get("key").(string)
}
client := m.(*Client)
projectReq, _ := client.Get(fmt.Sprintf("2.0/teams/%s/projects/%s",
d.Get("owner").(string),
projectKey,
))
if projectReq.StatusCode == 200 {
var project Project
body, readerr := ioutil.ReadAll(projectReq.Body)
if readerr != nil {
return readerr
}
decodeerr := json.Unmarshal(body, &project)
if decodeerr != nil {
return decodeerr
}
d.Set("key", project.Key)
d.Set("is_private", project.IsPrivate)
d.Set("name", project.Name)
d.Set("description", project.Description)
}
return nil
}
func resourceProjectDelete(d *schema.ResourceData, m interface{}) error {
var projectKey string
projectKey = d.Get("key").(string)
if projectKey == "" {
projectKey = d.Get("key").(string)
}
client := m.(*Client)
_, err := client.Delete(fmt.Sprintf("2.0/teams/%s/projects/%s",
d.Get("owner").(string),
projectKey,
))
return err
}