forked from denouche/terraform-provider-awx
/
data_source_notification_template.go
87 lines (76 loc) · 1.96 KB
/
data_source_notification_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
/*
*TBD*
Example Usage
```hcl
data "awx_notification_template" "default" {
name = "private_services"
}
```
*/
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 dataSourceNotificationTemplate() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNotificationTemplatesRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func dataSourceNotificationTemplatesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
params := make(map[string]string)
if groupName, okName := d.GetOk("name"); okName {
params["name"] = groupName.(string)
}
if groupID, okID := d.GetOk("id"); okID {
params["id"] = strconv.Itoa(groupID.(int))
}
if len(params) == 0 {
return buildDiagnosticsMessage(
"Get: Missing Parameters",
"Please use one of the selectors (name or id)",
)
}
notificationTemplates, _, err := client.NotificationTemplatesService.List(params)
if err != nil {
return buildDiagnosticsMessage(
"Get: Fail to fetch NotificationTemplate",
"Fail to find the group got: %s",
err.Error(),
)
}
if len(notificationTemplates) > 1 {
return buildDiagnosticsMessage(
"Get: find more than one Element",
"The Query Returns more than one Group, %d",
len(notificationTemplates),
)
}
if len(notificationTemplates) == 0 {
return buildDiagnosticsMessage(
"Get: Notification Template does not exist",
"The Query Returns no Notification Template matching filter %v",
params,
)
}
notificationTemplate := notificationTemplates[0]
d = setNotificationTemplateResourceData(d, notificationTemplate)
return diags
}