forked from denouche/terraform-provider-awx
/
resource_job_template_notification_template_shared.go
95 lines (82 loc) · 4.22 KB
/
resource_job_template_notification_template_shared.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
package awx
import (
"context"
"strconv"
awx "github.com/denouche/goawx/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func getResourceJobTemplateNotificationTemplateAssociateFuncForType(client *awx.JobTemplateNotificationTemplatesService, typ string) func(jobTemplateID int, notificationTemplateID int) (*awx.NotificationTemplate, error) {
switch typ {
case "error":
return client.AssociateJobTemplateNotificationTemplatesError
case "success":
return client.AssociateJobTemplateNotificationTemplatesSuccess
case "started":
return client.AssociateJobTemplateNotificationTemplatesStarted
}
return nil
}
func getResourceJobTemplateNotificationTemplateDisassociateFuncForType(client *awx.JobTemplateNotificationTemplatesService, typ string) func(jobTemplateID int, notificationTemplateID int) (*awx.NotificationTemplate, error) {
switch typ {
case "error":
return client.DisassociateJobTemplateNotificationTemplatesError
case "success":
return client.DisassociateJobTemplateNotificationTemplatesSuccess
case "started":
return client.DisassociateJobTemplateNotificationTemplatesStarted
}
return nil
}
func resourceJobTemplateNotificationTemplateCreateForType(typ string) func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
awxJobTemplateService := client.JobTemplateService
jobTemplateID := d.Get("job_template_id").(int)
_, err := awxJobTemplateService.GetJobTemplateByID(jobTemplateID, make(map[string]string))
if err != nil {
return buildDiagNotFoundFail("job template", jobTemplateID, err)
}
awxJobTemplateNotifService := client.JobTemplateNotificationTemplatesService
notificationTemplateID := d.Get("notification_template_id").(int)
associationFunc := getResourceJobTemplateNotificationTemplateAssociateFuncForType(awxJobTemplateNotifService, typ)
if associationFunc == nil {
return buildDiagnosticsMessage("Create: JobTemplate not AssociateJobTemplateNotificationTemplates", "Fail to find association function for notification_template type %s", typ)
}
result, err := associationFunc(jobTemplateID, notificationTemplateID)
if err != nil {
return buildDiagnosticsMessage("Create: JobTemplate not AssociateJobTemplateNotificationTemplates", "Fail to associate notification_template credentials with ID %v, for job_template ID %v, got error: %s", notificationTemplateID, jobTemplateID, err.Error())
}
d.SetId(strconv.Itoa(result.ID))
return diags
}
}
func resourceJobTemplateNotificationTemplateRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
return diags
}
func resourceJobTemplateNotificationTemplateDeleteForType(typ string) func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
awxJobTemplateService := client.JobTemplateService
jobTemplateID := d.Get("job_template_id").(int)
_, err := awxJobTemplateService.GetJobTemplateByID(jobTemplateID, make(map[string]string))
if err != nil {
return buildDiagNotFoundFail("job template", jobTemplateID, err)
}
awxJobTemplateNotifService := client.JobTemplateNotificationTemplatesService
notificationTemplateID := d.Get("notification_template_id").(int)
disassociationFunc := getResourceJobTemplateNotificationTemplateDisassociateFuncForType(awxJobTemplateNotifService, typ)
if disassociationFunc == nil {
return buildDiagnosticsMessage("Create: JobTemplate not DisassociateJobTemplateNotificationTemplates", "Fail to find disassociation function for notification_template type %s", typ)
}
_, err = disassociationFunc(jobTemplateID, notificationTemplateID)
if err != nil {
return buildDiagnosticsMessage("Create: JobTemplate not DisassociateJobTemplateNotificationTemplates", "Fail to associate notification_template credentials with ID %v, for job_template ID %v, got error: %s", notificationTemplateID, jobTemplateID, err.Error())
}
d.SetId("")
return diags
}
}