/
application_resource.go
254 lines (234 loc) · 6.63 KB
/
application_resource.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package provider
import (
"errors"
"log"
"regexp"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jgramoll/terraform-provider-spinnaker/client"
"github.com/mitchellh/mapstructure"
)
const (
// ApplicationKey key for application in map
ApplicationKey = "application"
)
var (
applicationNameRegex = regexp.MustCompile("^[a-zA-Z_0-9.-]*$")
emailRegex = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
)
func applicationResource() *schema.Resource {
return &schema.Resource{
Create: resourceApplicationCreate,
Read: resourceApplicationRead,
Update: resourceApplicationUpdate,
Delete: resourceApplicationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"accounts": &schema.Schema{
Type: schema.TypeList,
Description: "Accounts",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"cloud_providers": &schema.Schema{
Type: schema.TypeList,
Description: "Cloud Providers",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"email": &schema.Schema{
Type: schema.TypeString,
Description: "Email Address",
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
if !emailRegex.MatchString(v) {
errs = append(errs, errors.New("a valid email address is required"))
}
return
},
},
"enable_restart_running_executions": &schema.Schema{
Type: schema.TypeBool,
Description: "Enable restarting running pipelines",
Optional: true,
Default: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Description: "Application Name",
Required: true,
ForceNew: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
if len(v) > 249 {
errs = append(errs, errors.New("applicaiton name must be shorter than 250 characters"))
}
if !applicationNameRegex.MatchString(v) {
errs = append(errs, errors.New("application name can't have special characters or spaces"))
}
return
},
},
"instance_port": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"permissions": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"execute": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"read": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"write": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"platform_health_only": &schema.Schema{
Type: schema.TypeBool,
Description: "Consider only cloud provider health when executing tasks",
Optional: true,
Default: true,
},
"platform_health_only_show_override": &schema.Schema{
Type: schema.TypeBool,
Description: "Show health override option for each operation",
Optional: true,
Default: false,
},
"provider_settings": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"aws": &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"use_ami_block_device_mappings": &schema.Schema{
Description: "Prefer AMI Block Device Mappings",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
},
},
},
},
"repo_project_key": &schema.Schema{
Type: schema.TypeString,
Description: "Repository project key",
Optional: true,
},
"repo_slug": &schema.Schema{
Type: schema.TypeString,
Description: "Repository slug",
Optional: true,
},
"repo_type": &schema.Schema{
Type: schema.TypeString,
Description: "Repository type",
Optional: true,
},
},
}
}
func resourceApplicationCreate(d *schema.ResourceData, m interface{}) error {
var application application
configRaw := d.Get("").(map[string]interface{})
if err := mapstructure.Decode(configRaw, &application); err != nil {
return err
}
log.Println("[DEBUG] Creating application", application.Name)
applicationService := m.(*Services).ApplicationService
err := applicationService.CreateApplication(application.toClientApplication())
if err != nil {
return err
}
// Retry until app exists
_, err = applicationService.GetApplicationByNameWithRetries(application.Name)
if err != nil {
if serr, ok := err.(*client.SpinnakerError); ok {
if serr.Status == 404 {
d.SetId("")
return nil
}
}
return err
}
d.SetId(application.Name)
return resourceApplicationRead(d, m)
}
func resourceApplicationRead(d *schema.ResourceData, m interface{}) error {
applicationService := m.(*Services).ApplicationService
a, err := applicationService.GetApplicationByName(d.Id())
if err != nil {
if serr, ok := err.(*client.SpinnakerError); ok {
if serr.Status == 404 {
d.SetId("")
return nil
}
}
return err
}
log.Println("[DEBUG] Got application", a.Name)
return fromClientApplication(a).setResourceData(d)
}
func resourceApplicationUpdate(d *schema.ResourceData, m interface{}) error {
var application application
configRaw := d.Get("").(map[string]interface{})
if err := mapstructure.Decode(configRaw, &application); err != nil {
return err
}
applicationService := m.(*Services).ApplicationService
if err := applicationService.UpdateApplication(application.toClientApplication()); err != nil {
return err
}
log.Println("[DEBUG] Updated application", d.Id())
return resourceApplicationRead(d, m)
}
func resourceApplicationDelete(d *schema.ResourceData, m interface{}) error {
var a application
configRaw := d.Get("").(map[string]interface{})
if err := mapstructure.Decode(configRaw, &a); err != nil {
return err
}
log.Println("[DEBUG] Deleting application", d.Id())
applicationService := m.(*Services).ApplicationService
err := applicationService.DeleteApplication(a.toClientApplication())
if err != nil {
return err
}
d.SetId("")
return nil
}