/
data_source_workflow_job_template.go
92 lines (81 loc) · 2.51 KB
/
data_source_workflow_job_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
/*
Use this data source to list workflow job templates.
Example Usage
```hcl
data "awx_workflow_job_template" "default" {}
data "awx_workflow_job_template" "default" {
name = "Default"
}
data "awx_workflow_job_template" "default" {
id = 1
}
```
*/
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 dataSourceWorkflowJobTemplate() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceWorkflowJobTemplateRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func dataSourceWorkflowJobTemplateRead(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, okGroupID := d.GetOk("id"); okGroupID {
params["id"] = strconv.Itoa(groupID.(int))
}
workflowJobTemplate, _, err := client.WorkflowJobTemplateService.ListWorkflowJobTemplates(params)
if err != nil {
return buildDiagnosticsMessage(
"Get: Fail to fetch Inventory Group",
"Fail to find the group got: %s",
err.Error(),
)
}
if groupName, okName := d.GetOk("name"); okName {
for _, template := range workflowJobTemplate {
if template.Name == groupName {
d = setWorkflowJobTemplateResourceData(d, template)
return diags
}
}
}
if _, okGroupID := d.GetOk("id"); okGroupID {
if len(workflowJobTemplate) != 1 {
return buildDiagnosticsMessage(
"Get: find more than one Element",
"The Query Returns more than one Group, %d",
len(workflowJobTemplate),
)
}
d = setWorkflowJobTemplateResourceData(d, workflowJobTemplate[0])
return diags
}
return buildDiagnosticsMessage(
"Get: find more than one Element",
"The Query Returns more than one Group, %d",
len(workflowJobTemplate),
)
}