Skip to content

Commit 00db915

Browse files
authored
feat: workflow job template notifications (#3)
1 parent f9d5dce commit 00db915

5 files changed

Lines changed: 245 additions & 30 deletions

awx/provider.go

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,39 @@ func Provider() *schema.Provider {
3737
},
3838
},
3939
ResourcesMap: map[string]*schema.Resource{
40-
"awx_credential_azure_key_vault": resourceCredentialAzureKeyVault(),
41-
"awx_credential_google_compute_engine": resourceCredentialGoogleComputeEngine(),
42-
"awx_credential_input_source": resourceCredentialInputSource(),
43-
"awx_credential": resourceCredential(),
44-
"awx_credential_type": resourceCredentialType(),
45-
"awx_credential_machine": resourceCredentialMachine(),
46-
"awx_credential_scm": resourceCredentialSCM(),
47-
"awx_execution_environment": resourceExecutionEnvironment(),
48-
"awx_host": resourceHost(),
49-
"awx_inventory_group": resourceInventoryGroup(),
50-
"awx_inventory_source": resourceInventorySource(),
51-
"awx_inventory": resourceInventory(),
52-
"awx_job_template_credential": resourceJobTemplateCredentials(),
53-
"awx_job_template": resourceJobTemplate(),
54-
"awx_job_template_launch": resourceJobTemplateLaunch(),
55-
"awx_job_template_notification_template_error": resourceJobTemplateNotificationTemplateError(),
56-
"awx_job_template_notification_template_started": resourceJobTemplateNotificationTemplateStarted(),
57-
"awx_job_template_notification_template_success": resourceJobTemplateNotificationTemplateSuccess(),
58-
"awx_notification_template": resourceNotificationTemplate(),
59-
"awx_organization": resourceOrganization(),
60-
"awx_project": resourceProject(),
61-
"awx_schedule": resourceSchedule(),
62-
"awx_settings_ldap_team_map": resourceSettingsLDAPTeamMap(),
63-
"awx_team": resourceTeam(),
64-
"awx_workflow_job_template_node_always": resourceWorkflowJobTemplateNodeAlways(),
65-
"awx_workflow_job_template_node_failure": resourceWorkflowJobTemplateNodeFailure(),
66-
"awx_workflow_job_template_node_success": resourceWorkflowJobTemplateNodeSuccess(),
67-
"awx_workflow_job_template_node": resourceWorkflowJobTemplateNode(),
68-
"awx_workflow_job_template": resourceWorkflowJobTemplate(),
69-
"awx_workflow_job_template_schedule": resourceWorkflowJobTemplateSchedule(),
40+
"awx_credential_azure_key_vault": resourceCredentialAzureKeyVault(),
41+
"awx_credential_google_compute_engine": resourceCredentialGoogleComputeEngine(),
42+
"awx_credential_input_source": resourceCredentialInputSource(),
43+
"awx_credential": resourceCredential(),
44+
"awx_credential_type": resourceCredentialType(),
45+
"awx_credential_machine": resourceCredentialMachine(),
46+
"awx_credential_scm": resourceCredentialSCM(),
47+
"awx_execution_environment": resourceExecutionEnvironment(),
48+
"awx_host": resourceHost(),
49+
"awx_inventory_group": resourceInventoryGroup(),
50+
"awx_inventory_source": resourceInventorySource(),
51+
"awx_inventory": resourceInventory(),
52+
"awx_job_template_credential": resourceJobTemplateCredentials(),
53+
"awx_job_template": resourceJobTemplate(),
54+
"awx_job_template_launch": resourceJobTemplateLaunch(),
55+
"awx_job_template_notification_template_error": resourceJobTemplateNotificationTemplateError(),
56+
"awx_job_template_notification_template_started": resourceJobTemplateNotificationTemplateStarted(),
57+
"awx_job_template_notification_template_success": resourceJobTemplateNotificationTemplateSuccess(),
58+
"awx_notification_template": resourceNotificationTemplate(),
59+
"awx_organization": resourceOrganization(),
60+
"awx_project": resourceProject(),
61+
"awx_schedule": resourceSchedule(),
62+
"awx_settings_ldap_team_map": resourceSettingsLDAPTeamMap(),
63+
"awx_team": resourceTeam(),
64+
"awx_workflow_job_template_node_always": resourceWorkflowJobTemplateNodeAlways(),
65+
"awx_workflow_job_template_node_failure": resourceWorkflowJobTemplateNodeFailure(),
66+
"awx_workflow_job_template_node_success": resourceWorkflowJobTemplateNodeSuccess(),
67+
"awx_workflow_job_template_node": resourceWorkflowJobTemplateNode(),
68+
"awx_workflow_job_template": resourceWorkflowJobTemplate(),
69+
"awx_workflow_job_template_schedule": resourceWorkflowJobTemplateSchedule(),
70+
"awx_workflow_job_template_notification_template_error": resourceWorkflowJobTemplateNotificationTemplateError(),
71+
"awx_workflow_job_template_notification_template_started": resourceWorkflowJobTemplateNotificationTemplateStarted(),
72+
"awx_workflow_job_template_notification_template_success": resourceWorkflowJobTemplateNotificationTemplateSuccess(),
7073
},
7174
DataSourcesMap: map[string]*schema.Resource{
7275
"awx_credential_azure_key_vault": dataSourceCredentialAzure(),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
*TBD*
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "awx_workflow_job_template_notification_template_error" "baseconfig" {
8+
workflow_job_template_id = awx_workflow_job_template.baseconfig.id
9+
notification_template_id = awx_notification_template.default.id
10+
}
11+
```
12+
13+
*/
14+
package awx
15+
16+
import (
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
18+
)
19+
20+
func resourceWorkflowJobTemplateNotificationTemplateError() *schema.Resource {
21+
return &schema.Resource{
22+
CreateContext: resourceWorkflowJobTemplateNotificationTemplateCreateForType("error"),
23+
DeleteContext: resourceWorkflowJobTemplateNotificationTemplateDeleteForType("error"),
24+
ReadContext: resourceWorkflowJobTemplateNotificationTemplateRead,
25+
26+
Schema: map[string]*schema.Schema{
27+
"workflow_job_template_id": {
28+
Type: schema.TypeInt,
29+
Required: true,
30+
ForceNew: true,
31+
},
32+
"notification_template_id": {
33+
Type: schema.TypeInt,
34+
Required: true,
35+
ForceNew: true,
36+
},
37+
},
38+
}
39+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package awx
2+
3+
import (
4+
"context"
5+
"strconv"
6+
7+
awx "github.com/denouche/goawx/client"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func getResourceWorkflowJobTemplateNotificationTemplateAssociateFuncForType(client *awx.WorkflowJobTemplateNotificationTemplatesService, typ string) func(workflowJobTemplateID int, notificationTemplateID int) (*awx.NotificationTemplate, error) {
13+
switch typ {
14+
case "error":
15+
return client.AssociateWorkflowJobTemplateNotificationTemplatesError
16+
case "success":
17+
return client.AssociateWorkflowJobTemplateNotificationTemplatesSuccess
18+
case "started":
19+
return client.AssociateWorkflowJobTemplateNotificationTemplatesStarted
20+
}
21+
return nil
22+
}
23+
24+
func getResourceWorkflowJobTemplateNotificationTemplateDisassociateFuncForType(client *awx.WorkflowJobTemplateNotificationTemplatesService, typ string) func(workflowJobTemplateID int, notificationTemplateID int) (*awx.NotificationTemplate, error) {
25+
switch typ {
26+
case "error":
27+
return client.DisassociateWorkflowJobTemplateNotificationTemplatesError
28+
case "success":
29+
return client.DisassociateWorkflowJobTemplateNotificationTemplatesSuccess
30+
case "started":
31+
return client.DisassociateWorkflowJobTemplateNotificationTemplatesStarted
32+
}
33+
return nil
34+
}
35+
36+
func resourceWorkflowJobTemplateNotificationTemplateCreateForType(typ string) func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
37+
return func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
38+
var diags diag.Diagnostics
39+
client := m.(*awx.AWX)
40+
awxWorkflowJobTemplateService := client.WorkflowJobTemplateService
41+
workflowJobTemplateID := d.Get("workflow_job_template_id").(int)
42+
_, err := awxWorkflowJobTemplateService.GetWorkflowJobTemplateByID(workflowJobTemplateID, make(map[string]string))
43+
if err != nil {
44+
return buildDiagNotFoundFail("workflow job template", workflowJobTemplateID, err)
45+
}
46+
47+
awxWorkflowJobTemplateNotifService := client.WorkflowJobTemplateNotificationTemplatesService
48+
notificationTemplateID := d.Get("notification_template_id").(int)
49+
associationFunc := getResourceWorkflowJobTemplateNotificationTemplateAssociateFuncForType(awxWorkflowJobTemplateNotifService, typ)
50+
if associationFunc == nil {
51+
return buildDiagnosticsMessage("Create: WorkflowJobTemplate not AssociateWorkflowJobTemplateNotificationTemplates", "Fail to find association function for notification_template type %s", typ)
52+
}
53+
54+
result, err := associationFunc(workflowJobTemplateID, notificationTemplateID)
55+
if err != nil {
56+
return buildDiagnosticsMessage("Create: WorkflowJobTemplate not AssociateWorkflowJobTemplateNotificationTemplates", "Fail to associate notification_template credentials with ID %v, for workflow_job_template ID %v, got error: %s", notificationTemplateID, workflowJobTemplateID, err.Error())
57+
}
58+
59+
d.SetId(strconv.Itoa(result.ID))
60+
return diags
61+
}
62+
}
63+
64+
func resourceWorkflowJobTemplateNotificationTemplateRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
65+
var diags diag.Diagnostics
66+
return diags
67+
}
68+
69+
func resourceWorkflowJobTemplateNotificationTemplateDeleteForType(typ string) func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
70+
return func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
71+
var diags diag.Diagnostics
72+
client := m.(*awx.AWX)
73+
awxWorkflowJobTemplateService := client.WorkflowJobTemplateService
74+
workflowJobTemplateID := d.Get("workflow_job_template_id").(int)
75+
_, err := awxWorkflowJobTemplateService.GetWorkflowJobTemplateByID(workflowJobTemplateID, make(map[string]string))
76+
if err != nil {
77+
return buildDiagNotFoundFail("workflow job template", workflowJobTemplateID, err)
78+
}
79+
80+
awxWorkflowJobTemplateNotifService := client.WorkflowJobTemplateNotificationTemplatesService
81+
notificationTemplateID := d.Get("notification_template_id").(int)
82+
disassociationFunc := getResourceWorkflowJobTemplateNotificationTemplateDisassociateFuncForType(awxWorkflowJobTemplateNotifService, typ)
83+
if disassociationFunc == nil {
84+
return buildDiagnosticsMessage("Create: WorkflowJobTemplate not DisassociateWorkflowJobTemplateNotificationTemplates", "Fail to find disassociation function for notification_template type %s", typ)
85+
}
86+
87+
_, err = disassociationFunc(workflowJobTemplateID, notificationTemplateID)
88+
if err != nil {
89+
return buildDiagnosticsMessage("Create: WorkflowJobTemplate not DisassociateWorkflowJobTemplateNotificationTemplates", "Fail to associate notification_template credentials with ID %v, for job_template ID %v, got error: %s", notificationTemplateID, workflowJobTemplateID, err.Error())
90+
}
91+
92+
d.SetId("")
93+
return diags
94+
}
95+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
*TBD*
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "awx_workflow_job_template_notification_template_started" "baseconfig" {
8+
workflow_job_template_id = awx_workflow_job_template.baseconfig.id
9+
notification_template_id = awx_notification_template.default.id
10+
}
11+
```
12+
13+
*/
14+
package awx
15+
16+
import (
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
18+
)
19+
20+
func resourceWorkflowJobTemplateNotificationTemplateStarted() *schema.Resource {
21+
return &schema.Resource{
22+
CreateContext: resourceWorkflowJobTemplateNotificationTemplateCreateForType("started"),
23+
DeleteContext: resourceWorkflowJobTemplateNotificationTemplateDeleteForType("started"),
24+
ReadContext: resourceWorkflowJobTemplateNotificationTemplateRead,
25+
26+
Schema: map[string]*schema.Schema{
27+
"workflow_job_template_id": {
28+
Type: schema.TypeInt,
29+
Required: true,
30+
ForceNew: true,
31+
},
32+
"notification_template_id": {
33+
Type: schema.TypeInt,
34+
Required: true,
35+
ForceNew: true,
36+
},
37+
},
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
*TBD*
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "awx_workflow_job_template_notification_template_success" "baseconfig" {
8+
workflow_job_template_id = awx_workflow_job_template.baseconfig.id
9+
notification_template_id = awx_notification_template.default.id
10+
}
11+
```
12+
13+
*/
14+
package awx
15+
16+
import (
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
18+
)
19+
20+
func resourceWorkflowJobTemplateNotificationTemplateSuccess() *schema.Resource {
21+
return &schema.Resource{
22+
CreateContext: resourceWorkflowJobTemplateNotificationTemplateCreateForType("success"),
23+
DeleteContext: resourceWorkflowJobTemplateNotificationTemplateDeleteForType("success"),
24+
ReadContext: resourceWorkflowJobTemplateNotificationTemplateRead,
25+
26+
Schema: map[string]*schema.Schema{
27+
"workflow_job_template_id": {
28+
Type: schema.TypeInt,
29+
Required: true,
30+
ForceNew: true,
31+
},
32+
"notification_template_id": {
33+
Type: schema.TypeInt,
34+
Required: true,
35+
ForceNew: true,
36+
},
37+
},
38+
}
39+
}

0 commit comments

Comments
 (0)