forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_project.go
293 lines (240 loc) · 7.31 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package rundeck
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/apparentlymart/go-rundeck-api/rundeck"
)
var projectConfigAttributes = map[string]string{
"project.name": "name",
"project.description": "description",
"service.FileCopier.default.provider": "default_node_file_copier_plugin",
"service.NodeExecutor.default.provider": "default_node_executor_plugin",
"project.ssh-authentication": "ssh_authentication_type",
"project.ssh-key-storage-path": "ssh_key_storage_path",
"project.ssh-keypath": "ssh_key_file_path",
}
func resourceRundeckProject() *schema.Resource {
return &schema.Resource{
Create: CreateProject,
Update: UpdateProject,
Delete: DeleteProject,
Exists: ProjectExists,
Read: ReadProject,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Unique name for the project",
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Description of the project to be shown in the Rundeck UI",
Default: "Managed by Terraform",
},
"ui_url": &schema.Schema{
Type: schema.TypeString,
Required: false,
Computed: true,
},
"resource_model_source": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Name of the resource model plugin to use",
},
"config": &schema.Schema{
Type: schema.TypeMap,
Required: true,
Description: "Configuration parameters for the selected plugin",
},
},
},
},
"default_node_file_copier_plugin": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "jsch-scp",
},
"default_node_executor_plugin": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "jsch-ssh",
},
"ssh_authentication_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "privateKey",
},
"ssh_key_storage_path": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"ssh_key_file_path": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"extra_config": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Description: "Additional raw configuration parameters to include in the project configuration, with dots replaced with slashes in the key names due to limitations in Terraform's config language.",
},
},
}
}
func CreateProject(d *schema.ResourceData, meta interface{}) error {
client := meta.(*rundeck.Client)
// Rundeck's model is a little inconsistent in that we can create
// a project via a high-level structure but yet we must update
// the project via its raw config properties.
// For simplicity's sake we create a bare minimum project here
// and then delegate to UpdateProject to fill in the rest of the
// configuration via the raw config properties.
project, err := client.CreateProject(&rundeck.Project{
Name: d.Get("name").(string),
})
if err != nil {
return err
}
d.SetId(project.Name)
d.Set("id", project.Name)
return UpdateProject(d, meta)
}
func UpdateProject(d *schema.ResourceData, meta interface{}) error {
client := meta.(*rundeck.Client)
// In Rundeck, updates are always in terms of the low-level config
// properties map, so we need to transform our data structure
// into the equivalent raw properties.
projectName := d.Id()
updateMap := map[string]string{}
slashReplacer := strings.NewReplacer("/", ".")
if extraConfig := d.Get("extra_config"); extraConfig != nil {
for k, v := range extraConfig.(map[string]interface{}) {
updateMap[slashReplacer.Replace(k)] = v.(string)
}
}
for configKey, attrKey := range projectConfigAttributes {
v := d.Get(attrKey).(string)
if v != "" {
updateMap[configKey] = v
}
}
for i, rmsi := range d.Get("resource_model_source").([]interface{}) {
rms := rmsi.(map[string]interface{})
pluginType := rms["type"].(string)
ci := rms["config"].(map[string]interface{})
attrKeyPrefix := fmt.Sprintf("resources.source.%v.", i+1)
typeKey := attrKeyPrefix + "type"
configKeyPrefix := fmt.Sprintf("%vconfig.", attrKeyPrefix)
updateMap[typeKey] = pluginType
for k, v := range ci {
updateMap[configKeyPrefix+k] = v.(string)
}
}
err := client.SetProjectConfig(projectName, updateMap)
if err != nil {
return err
}
return ReadProject(d, meta)
}
func ReadProject(d *schema.ResourceData, meta interface{}) error {
client := meta.(*rundeck.Client)
name := d.Id()
project, err := client.GetProject(name)
if err != nil {
return err
}
for configKey, attrKey := range projectConfigAttributes {
d.Set(projectConfigAttributes[configKey], nil)
if v, ok := project.Config[configKey]; ok {
d.Set(attrKey, v)
// Remove this key so it won't get included in extra_config
// later.
delete(project.Config, configKey)
}
}
resourceSourceMap := map[int]interface{}{}
configMaps := map[int]interface{}{}
for configKey, v := range project.Config {
if strings.HasPrefix(configKey, "resources.source.") {
nameParts := strings.Split(configKey, ".")
if len(nameParts) < 4 {
continue
}
index, err := strconv.Atoi(nameParts[2])
if err != nil {
continue
}
if _, ok := resourceSourceMap[index]; !ok {
configMap := map[string]interface{}{}
configMaps[index] = configMap
resourceSourceMap[index] = map[string]interface{}{
"config": configMap,
}
}
switch nameParts[3] {
case "type":
if len(nameParts) != 4 {
continue
}
m := resourceSourceMap[index].(map[string]interface{})
m["type"] = v
case "config":
if len(nameParts) != 5 {
continue
}
m := configMaps[index].(map[string]interface{})
m[nameParts[4]] = v
default:
continue
}
// Remove this key so it won't get included in extra_config
// later.
delete(project.Config, configKey)
}
}
resourceSources := []map[string]interface{}{}
resourceSourceIndices := []int{}
for k := range resourceSourceMap {
resourceSourceIndices = append(resourceSourceIndices, k)
}
sort.Ints(resourceSourceIndices)
for _, index := range resourceSourceIndices {
resourceSources = append(resourceSources, resourceSourceMap[index].(map[string]interface{}))
}
d.Set("resource_model_source", resourceSources)
extraConfig := map[string]string{}
dotReplacer := strings.NewReplacer(".", "/")
for k, v := range project.Config {
extraConfig[dotReplacer.Replace(k)] = v
}
d.Set("extra_config", extraConfig)
d.Set("name", project.Name)
d.Set("ui_url", project.URL)
return nil
}
func ProjectExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*rundeck.Client)
name := d.Id()
_, err := client.GetProject(name)
if _, ok := err.(rundeck.NotFoundError); ok {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func DeleteProject(d *schema.ResourceData, meta interface{}) error {
client := meta.(*rundeck.Client)
name := d.Id()
return client.DeleteProject(name)
}