generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
resource_template.go
508 lines (456 loc) · 17.6 KB
/
resource_template.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package template
import (
"context"
"net/http"
"github.com/antihax/optional"
"github.com/harness/harness-openapi-go-client/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceTemplate() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Template.",
ReadContext: resourceTemplateRead,
UpdateContext: resourceTemplateCreateOrUpdate,
DeleteContext: resourceTemplateDelete,
CreateContext: resourceTemplateCreateOrUpdate,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"template_yaml": {
Description: "Yaml for creating new Template." + helpers.Descriptions.YamlText.String(),
Type: schema.TypeString,
Required: true,
},
"version": {
Description: "Version Label for Template.",
Type: schema.TypeString,
Optional: true,
},
"is_stable": {
Description: "True if given version for template to be set as stable.",
Type: schema.TypeBool,
Optional: true,
},
"comments": {
Description: "Specify comment with respect to changes.",
Type: schema.TypeString,
Optional: true,
},
"git_details": {
Description: "Contains parameters related to creating an Entity for Git Experience.",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"branch_name": {
Description: "Name of the branch.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"file_path": {
Description: "File path of the Entity in the repository.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"commit_message": {
Description: "Commit message used for the merge commit.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"base_branch": {
Description: "Name of the default branch (this checks out a new branch titled by branch_name).",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"connector_ref": {
Description: "Identifier of the Harness Connector used for CRUD operations on the Entity." + helpers.Descriptions.ConnectorRefText.String(),
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"store_type": {
Description: "Specifies whether the Entity is to be stored in Git or not. Possible values: INLINE, REMOTE.",
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"INLINE", "REMOTE"}, false),
},
"repo_name": {
Description: "Name of the repository.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"last_object_id": {
Description: "Last object identifier (for Github). To be provided only when updating Pipeline.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"last_commit_id": {
Description: "Last commit identifier (for Git Repositories other than Github). To be provided only when updating Pipeline.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourceTemplateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetClientWithContext(ctx)
template_id := d.Id()
org_id := d.Get("org_id").(string)
project_id := d.Get("project_id").(string)
var comments = helpers.BuildField(d, "comments")
var store_type = helpers.BuildField(d, "git_details.0.store_type")
var base_branch = helpers.BuildField(d, "git_details.0.base_branch")
var commit_message = helpers.BuildField(d, "git_details.0.commit_message")
var connector_ref = helpers.BuildField(d, "git_details.0.connector_ref")
var branch_name optional.String
branch_name = helpers.BuildField(d, "git_details.0.branch_name")
version := d.Get("version").(string)
var err error
var resp nextgen.TemplateWithInputsResponse
var httpResp *http.Response
if project_id != "" {
if version == "" {
resp, httpResp, err = c.ProjectTemplateApi.GetTemplateStableProject(ctx, org_id, project_id, template_id, &nextgen.ProjectTemplateApiGetTemplateStableProjectOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: branch_name})
} else {
resp, httpResp, err = c.ProjectTemplateApi.GetTemplateProject(ctx, project_id, template_id, org_id, version, &nextgen.ProjectTemplateApiGetTemplateProjectOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: branch_name})
}
} else if org_id != "" && project_id == "" {
if version == "" {
resp, httpResp, err = c.OrgTemplateApi.GetTemplateStableOrg(ctx, org_id, template_id, &nextgen.OrgTemplateApiGetTemplateStableOrgOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: branch_name,
})
} else {
resp, httpResp, err = c.OrgTemplateApi.GetTemplateOrg(ctx, template_id, org_id, version, &nextgen.OrgTemplateApiGetTemplateOrgOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: branch_name,
})
}
} else {
if version == "" {
resp, httpResp, err = c.AccountTemplateApi.GetTemplateStableAcc(ctx, template_id, &nextgen.AccountTemplateApiGetTemplateStableAccOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: branch_name,
})
} else {
resp, httpResp, err = c.AccountTemplateApi.GetTemplateAcc(ctx, template_id, version, &nextgen.AccountTemplateApiGetTemplateAccOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: branch_name,
})
}
}
if httpResp.StatusCode == 404 {
d.SetId("")
d.MarkNewResource()
return nil
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readTemplate(d, resp, comments.Value(), store_type, base_branch, commit_message, connector_ref)
return nil
}
func resourceTemplateCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetClientWithContext(ctx)
var err error
var template_id string
var branch_name string
var store_type optional.String
var base_branch optional.String
var commit_message optional.String
var connector_ref optional.String
var resp nextgen.TemplateResponse
var httpResp *http.Response
id := d.Id()
org_id := d.Get("org_id").(string)
project_id := d.Get("project_id").(string)
comments := d.Get("comments").(string)
version := d.Get("version").(string)
if id == "" {
template := buildCreateTemplate(d)
if template.GitDetails != nil {
base_branch = optional.NewString(template.GitDetails.BaseBranch)
store_type = optional.NewString(template.GitDetails.StoreType)
commit_message = optional.NewString(template.GitDetails.CommitMessage)
connector_ref = optional.NewString(template.GitDetails.ConnectorRef)
branch_name = template.GitDetails.BranchName
}
if project_id != "" {
resp, httpResp, err = c.ProjectTemplateApi.CreateTemplatesProject(ctx, org_id, project_id, &nextgen.ProjectTemplateApiCreateTemplatesProjectOpts{
Body: optional.NewInterface(template),
HarnessAccount: optional.NewString(c.AccountId),
})
if resp.Identifier != "" {
template_id = resp.Identifier
} else {
template_id = resp.Slug
}
} else if org_id != "" && project_id == "" {
resp, httpResp, err = c.OrgTemplateApi.CreateTemplatesOrg(ctx, org_id, &nextgen.OrgTemplateApiCreateTemplatesOrgOpts{
HarnessAccount: optional.NewString(c.AccountId),
Body: optional.NewInterface(template),
})
if resp.Identifier != "" {
template_id = resp.Identifier
} else {
template_id = resp.Slug
}
} else {
resp, httpResp, err = c.AccountTemplateApi.CreateTemplatesAcc(ctx, &nextgen.AccountTemplateApiCreateTemplatesAccOpts{
HarnessAccount: optional.NewString(c.AccountId),
Body: optional.NewInterface(template),
})
if resp.Identifier != "" {
template_id = resp.Identifier
} else {
template_id = resp.Slug
}
}
} else {
template := buildUpdateTemplate(d)
if template.GitDetails != nil {
base_branch = optional.NewString(template.GitDetails.BaseBranch)
branch_name = template.GitDetails.BranchName
store_type = optional.NewString(template.GitDetails.StoreType)
commit_message = optional.NewString(template.GitDetails.CommitMessage)
connector_ref = optional.NewString(template.GitDetails.ConnectorRef)
}
if project_id != "" {
resp, httpResp, err = c.ProjectTemplateApi.UpdateTemplateProject(ctx, project_id, id, org_id, version, &nextgen.ProjectTemplateApiUpdateTemplateProjectOpts{
Body: optional.NewInterface(template),
HarnessAccount: optional.NewString(c.AccountId),
})
if resp.Identifier != "" {
template_id = resp.Identifier
} else {
template_id = resp.Slug
}
} else if org_id != "" && project_id == "" {
resp, httpResp, err = c.OrgTemplateApi.UpdateTemplateOrg(ctx, id, org_id, version, &nextgen.OrgTemplateApiUpdateTemplateOrgOpts{
HarnessAccount: optional.NewString(c.AccountId),
Body: optional.NewInterface(template),
})
if resp.Identifier != "" {
template_id = resp.Identifier
} else {
template_id = resp.Slug
}
} else {
resp, httpResp, err = c.AccountTemplateApi.UpdateTemplateAcc(ctx, id, version, &nextgen.AccountTemplateApiUpdateTemplateAccOpts{
HarnessAccount: optional.NewString(c.AccountId),
Body: optional.NewInterface(template),
})
if resp.Identifier != "" {
template_id = resp.Identifier
} else {
template_id = resp.Slug
}
}
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
var respGet nextgen.TemplateWithInputsResponse
if project_id != "" {
if version == "" {
respGet, httpResp, err = c.ProjectTemplateApi.GetTemplateStableProject(ctx, org_id, project_id, template_id, &nextgen.ProjectTemplateApiGetTemplateStableProjectOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: optional.NewString(branch_name)})
} else {
respGet, httpResp, err = c.ProjectTemplateApi.GetTemplateProject(ctx, project_id, template_id, org_id, version, &nextgen.ProjectTemplateApiGetTemplateProjectOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: optional.NewString(branch_name)})
}
} else if org_id != "" && project_id == "" {
if version == "" {
respGet, httpResp, err = c.OrgTemplateApi.GetTemplateStableOrg(ctx, org_id, template_id, &nextgen.OrgTemplateApiGetTemplateStableOrgOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: optional.NewString(branch_name),
})
} else {
respGet, httpResp, err = c.OrgTemplateApi.GetTemplateOrg(ctx, template_id, org_id, version, &nextgen.OrgTemplateApiGetTemplateOrgOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: optional.NewString(branch_name),
})
}
} else {
if version == "" {
respGet, httpResp, err = c.AccountTemplateApi.GetTemplateStableAcc(ctx, template_id, &nextgen.AccountTemplateApiGetTemplateStableAccOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: optional.NewString(branch_name),
})
} else {
respGet, httpResp, err = c.AccountTemplateApi.GetTemplateAcc(ctx, template_id, version, &nextgen.AccountTemplateApiGetTemplateAccOpts{
HarnessAccount: optional.NewString(c.AccountId),
BranchName: optional.NewString(branch_name),
})
}
}
readTemplate(d, respGet, comments, store_type, base_branch, commit_message, connector_ref)
return nil
}
func resourceTemplateDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetClientWithContext(ctx)
id := d.Get("identifier").(string)
org_id := d.Get("org_id").(string)
project_id := d.Get("project_id").(string)
version := d.Get("version").(string)
var httpResp *http.Response
var err error
if project_id != "" {
httpResp, err = c.ProjectTemplateApi.DeleteTemplateProject(ctx, project_id, id, org_id, version, &nextgen.ProjectTemplateApiDeleteTemplateProjectOpts{
HarnessAccount: optional.NewString(c.AccountId),
Comments: helpers.BuildField(d, "comments")})
} else if org_id != "" && project_id == "" {
httpResp, err = c.OrgTemplateApi.DeleteTemplateOrg(ctx, id, org_id, version, &nextgen.OrgTemplateApiDeleteTemplateOrgOpts{
HarnessAccount: optional.NewString(c.AccountId),
Comments: helpers.BuildField(d, "comments")})
} else {
httpResp, err = c.AccountTemplateApi.DeleteTemplateAcc(ctx, id, version, &nextgen.AccountTemplateApiDeleteTemplateAccOpts{
HarnessAccount: optional.NewString(c.AccountId),
Comments: helpers.BuildField(d, "comments")})
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func buildUpdateTemplate(d *schema.ResourceData) nextgen.TemplateUpdateRequestBody {
template := nextgen.TemplateUpdateRequestBody{
TemplateYaml: d.Get("template_yaml").(string),
Comments: d.Get("comments").(string),
}
if attr, ok := d.GetOk("git_details"); ok {
config := attr.([]interface{})[0].(map[string]interface{})
if attr, ok := config["store_type"]; ok {
if attr != "" {
template.GitDetails = &nextgen.GitUpdateDetails1{}
if attr, ok := config["branch_name"]; ok {
template.GitDetails.BranchName = attr.(string)
}
if attr, ok := config["file_path"]; ok {
template.GitDetails.FilePath = attr.(string)
}
if attr, ok := config["last_object_id"]; ok {
template.GitDetails.LastObjectId = attr.(string)
}
if attr, ok := config["commit_message"]; ok {
template.GitDetails.CommitMessage = attr.(string)
}
if attr, ok := config["base_branch"]; ok {
template.GitDetails.BaseBranch = attr.(string)
}
if attr, ok := config["connector_ref"]; ok {
template.GitDetails.ConnectorRef = attr.(string)
}
if attr, ok := config["store_type"]; ok {
template.GitDetails.StoreType = attr.(string)
}
if attr, ok := config["repo_name"]; ok {
template.GitDetails.RepoName = attr.(string)
}
}
}
}
return template
}
func buildCreateTemplate(d *schema.ResourceData) nextgen.TemplateCreateRequestBody {
template := nextgen.TemplateCreateRequestBody{
TemplateYaml: d.Get("template_yaml").(string),
IsStable: d.Get("is_stable").(bool),
Comments: d.Get("comments").(string),
}
if attr, ok := d.GetOk("git_details"); ok {
config := attr.([]interface{})[0].(map[string]interface{})
template.GitDetails = &nextgen.GitCreateDetails1{}
if attr, ok := config["branch_name"]; ok {
template.GitDetails.BranchName = attr.(string)
}
if attr, ok := config["file_path"]; ok {
template.GitDetails.FilePath = attr.(string)
}
if attr, ok := config["commit_message"]; ok {
template.GitDetails.CommitMessage = attr.(string)
}
if attr, ok := config["base_branch"]; ok {
template.GitDetails.BaseBranch = attr.(string)
}
if attr, ok := config["connector_ref"]; ok {
template.GitDetails.ConnectorRef = attr.(string)
}
if attr, ok := config["store_type"]; ok {
template.GitDetails.StoreType = attr.(string)
}
if attr, ok := config["repo_name"]; ok {
template.GitDetails.RepoName = attr.(string)
}
}
return template
}
func readTemplate(d *schema.ResourceData, template nextgen.TemplateWithInputsResponse, comments string, store_type optional.String, base_branch optional.String, commit_message optional.String, connector_ref optional.String) {
if template.Template.Identifier != "" {
d.SetId(template.Template.Identifier)
} else {
d.SetId(template.Template.Slug)
}
if template.Template.Identifier != "" {
d.Set("identifier", template.Template.Identifier)
} else {
d.Set("identifier", template.Template.Slug)
}
d.Set("name", template.Template.Name)
d.Set("org_id", template.Template.Org)
d.Set("project_id", template.Template.Project)
d.Set("template_yaml", template.Template.Yaml)
d.Set("is_stable", template.Template.StableTemplate)
d.Set("description", template.Template.Description)
d.Set("version", template.Template.VersionLabel)
d.Set("comments", comments)
if template.Template.GitDetails != nil {
d.Set("git_details", []interface{}{readGitDetails(template, store_type, base_branch, commit_message, connector_ref)})
}
}
func readGitDetails(template nextgen.TemplateWithInputsResponse, store_type optional.String, base_branch optional.String, commit_message optional.String, connector_ref optional.String) map[string]interface{} {
git_details := map[string]interface{}{
"branch_name": template.Template.GitDetails.BranchName,
"file_path": template.Template.GitDetails.FilePath,
"repo_name": template.Template.GitDetails.RepoName,
"last_commit_id": template.Template.GitDetails.CommitId,
"last_object_id": template.Template.GitDetails.ObjectId,
}
if store_type.IsSet() {
git_details["store_type"] = store_type.Value()
}
if base_branch.IsSet() {
git_details["base_branch"] = base_branch.Value()
}
if commit_message.IsSet() {
git_details["commit_message"] = commit_message.Value()
}
if connector_ref.IsSet() {
git_details["connector_ref"] = connector_ref.Value()
}
return git_details
}