Skip to content

Commit 9c5b488

Browse files
committed
feat: add notification_template resource
1 parent d2436c7 commit 9c5b488

10 files changed

Lines changed: 337 additions & 7 deletions
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
*TBD*
3+
4+
Example Usage
5+
6+
```hcl
7+
data "awx_notification_template" "default" {
8+
name = "private_services"
9+
}
10+
```
11+
12+
*/
13+
package awx
14+
15+
import (
16+
"context"
17+
"strconv"
18+
19+
awx "github.com/denouche/goawx/client"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
22+
)
23+
24+
func dataSourceNotificationTemplate() *schema.Resource {
25+
return &schema.Resource{
26+
ReadContext: dataSourceNotificationTemplatesRead,
27+
Schema: map[string]*schema.Schema{
28+
"id": {
29+
Type: schema.TypeInt,
30+
Optional: true,
31+
Computed: true,
32+
},
33+
"name": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
Computed: true,
37+
},
38+
},
39+
}
40+
}
41+
42+
func dataSourceNotificationTemplatesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
43+
var diags diag.Diagnostics
44+
client := m.(*awx.AWX)
45+
params := make(map[string]string)
46+
if groupName, okName := d.GetOk("name"); okName {
47+
params["name"] = groupName.(string)
48+
}
49+
50+
if groupID, okID := d.GetOk("id"); okID {
51+
params["id"] = strconv.Itoa(groupID.(int))
52+
}
53+
54+
if len(params) == 0 {
55+
return buildDiagnosticsMessage(
56+
"Get: Missing Parameters",
57+
"Please use one of the selectors (name or id)",
58+
)
59+
}
60+
61+
notificationTemplates, _, err := client.NotificationTemplatesService.List(params)
62+
if err != nil {
63+
return buildDiagnosticsMessage(
64+
"Get: Fail to fetch NotificationTemplate",
65+
"Fail to find the group got: %s",
66+
err.Error(),
67+
)
68+
}
69+
if len(notificationTemplates) > 1 {
70+
return buildDiagnosticsMessage(
71+
"Get: find more than one Element",
72+
"The Query Returns more than one Group, %d",
73+
len(notificationTemplates),
74+
)
75+
}
76+
77+
notificationTemplate := notificationTemplates[0]
78+
d = setNotificationTemplateResourceData(d, notificationTemplate)
79+
return diags
80+
}

awx/data_source_schedule.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ func dataSourceSchedule() *schema.Resource {
3535
Optional: true,
3636
Computed: true,
3737
},
38-
"organization_id": {
39-
Type: schema.TypeInt,
40-
Optional: true,
41-
Computed: true,
42-
},
4338
},
4439
}
4540
}

awx/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func Provider() *schema.Provider {
5151
"awx_job_template_credential": resourceJobTemplateCredentials(),
5252
"awx_job_template": resourceJobTemplate(),
5353
"awx_job_template_launch": resourceJobTemplateLaunch(),
54+
"awx_notification_template": resourceNotificationTemplate(),
5455
"awx_organization": resourceOrganization(),
5556
"awx_project": resourceProject(),
5657
"awx_schedule": resourceSchedule(),
@@ -72,6 +73,7 @@ func Provider() *schema.Provider {
7273
"awx_inventory": dataSourceInventory(),
7374
"awx_inventory_role": dataSourceInventoryRole(),
7475
"awx_job_template": dataSourceJobTemplate(),
76+
"awx_notification_template": dataSourceNotificationTemplate(),
7577
"awx_organization": dataSourceOrganization(),
7678
"awx_project": dataSourceProject(),
7779
"awx_project_role": dataSourceProjectRole(),
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
*TBD*
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "awx_notification_template" "default" {
8+
name = "notification_template-test"
9+
}
10+
```
11+
12+
*/
13+
package awx
14+
15+
import (
16+
"context"
17+
"fmt"
18+
"log"
19+
"strconv"
20+
21+
awx "github.com/denouche/goawx/client"
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
23+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
24+
)
25+
26+
func resourceNotificationTemplate() *schema.Resource {
27+
return &schema.Resource{
28+
CreateContext: resourceNotificationTemplateCreate,
29+
ReadContext: resourceNotificationTemplateRead,
30+
UpdateContext: resourceNotificationTemplateUpdate,
31+
DeleteContext: resourceNotificationTemplateDelete,
32+
33+
Schema: map[string]*schema.Schema{
34+
"name": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
},
38+
"description": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
},
42+
"organization_id": {
43+
Type: schema.TypeString,
44+
Required: true,
45+
},
46+
"notification_type": {
47+
Type: schema.TypeString,
48+
Optional: true,
49+
ExactlyOneOf: []string{
50+
"email", "grafana", "irc", "mattermost", "pagerduty", "rocketchat", "slack", "twilio", "webhook",
51+
},
52+
},
53+
"notification_configuration": {
54+
Type: schema.TypeMap,
55+
Optional: true,
56+
},
57+
},
58+
}
59+
}
60+
61+
func resourceNotificationTemplateCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
62+
var diags diag.Diagnostics
63+
client := m.(*awx.AWX)
64+
awxService := client.NotificationTemplatesService
65+
66+
result, err := awxService.Create(map[string]interface{}{
67+
"name": d.Get("name").(string),
68+
"description": d.Get("description").(string),
69+
"organization": d.Get("organization_id").(string),
70+
"notification_type": d.Get("notification_type").(string),
71+
"notification_configuration": d.Get("notification_configuration").(map[string]interface{}),
72+
}, map[string]string{})
73+
if err != nil {
74+
log.Printf("Fail to Create notification_template %v", err)
75+
diags = append(diags, diag.Diagnostic{
76+
Severity: diag.Error,
77+
Summary: "Unable to create NotificationTemplate",
78+
Detail: fmt.Sprintf("NotificationTemplate failed to create %s", err.Error()),
79+
})
80+
return diags
81+
}
82+
83+
d.SetId(strconv.Itoa(result.ID))
84+
return resourceNotificationTemplateRead(ctx, d, m)
85+
}
86+
87+
func resourceNotificationTemplateUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
88+
var diags diag.Diagnostics
89+
client := m.(*awx.AWX)
90+
awxService := client.NotificationTemplatesService
91+
id, diags := convertStateIDToNummeric("Update NotificationTemplate", d)
92+
if diags.HasError() {
93+
return diags
94+
}
95+
96+
params := make(map[string]string)
97+
_, err := awxService.GetById(id, params)
98+
if err != nil {
99+
return buildDiagNotFoundFail("notification_template", id, err)
100+
}
101+
102+
_, err = awxService.Update(id, map[string]interface{}{
103+
"name": d.Get("name").(string),
104+
"description": d.Get("description").(string),
105+
"organization": d.Get("organization_id").(string),
106+
"notification_type": d.Get("notification_type").(string),
107+
"notification_configuration": d.Get("notification_configuration").(map[string]interface{}),
108+
}, map[string]string{})
109+
if err != nil {
110+
diags = append(diags, diag.Diagnostic{
111+
Severity: diag.Error,
112+
Summary: "Unable to update NotificationTemplate",
113+
Detail: fmt.Sprintf("notification_template with name %s failed to update %s", d.Get("name").(string), err.Error()),
114+
})
115+
return diags
116+
}
117+
118+
return resourceNotificationTemplateRead(ctx, d, m)
119+
}
120+
121+
func resourceNotificationTemplateRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
122+
var diags diag.Diagnostics
123+
client := m.(*awx.AWX)
124+
awxService := client.NotificationTemplatesService
125+
id, diags := convertStateIDToNummeric("Read notification_template", d)
126+
if diags.HasError() {
127+
return diags
128+
}
129+
130+
res, err := awxService.GetById(id, make(map[string]string))
131+
if err != nil {
132+
return buildDiagNotFoundFail("notification_template", id, err)
133+
134+
}
135+
d = setNotificationTemplateResourceData(d, res)
136+
return nil
137+
}
138+
139+
func resourceNotificationTemplateDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
140+
client := m.(*awx.AWX)
141+
awxService := client.NotificationTemplatesService
142+
id, diags := convertStateIDToNummeric(diagElementHostTitle, d)
143+
if diags.HasError() {
144+
return diags
145+
}
146+
147+
if _, err := awxService.Delete(id); err != nil {
148+
return buildDiagDeleteFail(
149+
diagElementHostTitle,
150+
fmt.Sprintf("id %v, got %s ",
151+
id, err.Error()))
152+
}
153+
d.SetId("")
154+
return nil
155+
}
156+
157+
func setNotificationTemplateResourceData(d *schema.ResourceData, r *awx.NotificationTemplate) *schema.ResourceData {
158+
d.Set("name", r.Name)
159+
d.Set("description", r.Description)
160+
d.Set("organization", r.Organization)
161+
d.Set("notification_type", r.NotificationType)
162+
d.Set("notification_configuration", r.NotificationConfiguration)
163+
d.SetId(strconv.Itoa(r.ID))
164+
return d
165+
}

awx/resource_workflow_job_template_schedule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func resourceWorkflowJobTemplateSchedule() *schema.Resource {
5050
},
5151
"unified_job_template_id": {
5252
Type: schema.TypeInt,
53-
Optional: true,
53+
Required: true,
5454
},
5555
"description": {
5656
Type: schema.TypeString,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
layout: "awx"
3+
page_title: "AWX: awx_notification_template"
4+
sidebar_current: "docs-awx-datasource-notification-template"
5+
description: |-
6+
*TBD*
7+
---
8+
9+
# awx_inventory
10+
11+
*TBD*
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "awx_notification_template" "default" {
17+
name = "private_services"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
* `id` - (Optional)
26+
* `name` - (Optional)
27+

docs/data-sources/schedule.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
layout: "awx"
3+
page_title: "AWX: awx_schedule"
4+
sidebar_current: "docs-awx-datasource-schedule"
5+
description: |-
6+
*TBD*
7+
---
8+
9+
# awx_inventory
10+
11+
*TBD*
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "awx_schedule" "default" {
17+
name = "private_services"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
* `id` - (Optional)
26+
* `name` - (Optional)
27+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: "awx"
3+
page_title: "AWX: awx_notification_template"
4+
sidebar_current: "docs-awx-resource-notification-template"
5+
description: |-
6+
*TBD*
7+
---
8+
9+
# awx_notification_template
10+
11+
*TBD*
12+
13+
## Example Usage
14+
15+
```hcl
16+
resource "awx_notification_template" "default" {
17+
name = "schedule-test"
18+
notification_type = "webhook"
19+
organization_id = data.awx_organization.default.id
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `name` - (Required)
28+
* `notification_type` - (Required)
29+
* `organization_id` - (Required)
30+
* `description` - (Optional)
31+
* `notification_type` - (Optional)
32+
* `notification_configuration` - (Optional)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/denouche/terraform-provider-awx
33
go 1.14
44

55
require (
6-
github.com/denouche/goawx v0.6.3
6+
github.com/denouche/goawx v0.6.4-0.20211224134320-e2dec0fa4bb4
77
github.com/gruntwork-io/terratest v0.31.2
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
99
github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
138138
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
139139
github.com/denouche/goawx v0.6.3 h1:ZAlySZXxO17QO1UmGTGtUar6UZeVN2hjBC+0sDQVYSw=
140140
github.com/denouche/goawx v0.6.3/go.mod h1:MppzSteoj2xgfiqiRWW/Bf1a8z2FrRyvah1z0J2vJTY=
141+
github.com/denouche/goawx v0.6.4-0.20211224134320-e2dec0fa4bb4 h1:3cCRm4ubes7RaTk5dClfv3THgHI7FRMFKLXXaO2iiH4=
142+
github.com/denouche/goawx v0.6.4-0.20211224134320-e2dec0fa4bb4/go.mod h1:MppzSteoj2xgfiqiRWW/Bf1a8z2FrRyvah1z0J2vJTY=
141143
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
142144
github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8=
143145
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=

0 commit comments

Comments
 (0)