forked from denouche/terraform-provider-awx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_job_template_launch.go
More file actions
190 lines (164 loc) · 5.35 KB
/
resource_job_template_launch.go
File metadata and controls
190 lines (164 loc) · 5.35 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
*TBD*
Example Usage
```hcl
data "awx_inventory" "default" {
name = "private_services"
organization_id = data.awx_organization.default.id
}
resource "awx_job_template" "baseconfig" {
name = "baseconfig"
job_type = "run"
inventory_id = data.awx_inventory.default.id
project_id = awx_project.base_service_config.id
playbook = "master-configure-system.yml"
become_enabled = true
}
resource "awx_job_template_launch" "now" {
job_template_id = awx_job_template.baseconfig.id
}
```
*/
package awx
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"time"
awx "github.com/denouche/goawx/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceJobTemplateLaunch() *schema.Resource {
return &schema.Resource{
CreateContext: resourceJobTemplateLaunchCreate,
ReadContext: resourceJobRead,
DeleteContext: resourceJobDelete,
Schema: map[string]*schema.Schema{
"job_template_id": {
Type: schema.TypeInt,
Required: true,
Description: "Job template ID",
ForceNew: true,
},
"limit": {
Type: schema.TypeString,
Required: false,
Optional: true,
ForceNew: true,
Description: "List of comma delimited hosts to limit job execution. Required ask_limit_on_launch set on job_template.",
},
"inventory_id": {
Type: schema.TypeInt,
Required: false,
Optional: true,
Computed: true,
Description: "Override Inventory ID. Required ask_inventory_on_launch set on job_template.",
ForceNew: true,
},
"extra_vars": {
Type: schema.TypeString,
Optional: true,
Description: "Override job template variables. YAML or JSON values are supported.",
ForceNew: true,
StateFunc: normalizeJsonYaml,
},
"wait_for_completion": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: false,
Description: "Resource creation will wait for job completion.",
ForceNew: true,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
},
}
}
func statusInstanceState(ctx context.Context, svc *awx.JobService, id int) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := svc.GetJob(id, map[string]string{})
return output, output.Status, err
}
}
func jobTemplateLaunchWait(ctx context.Context, svc *awx.JobService, job *awx.JobLaunch, timeout time.Duration) error {
stateConf := &retry.StateChangeConf{
Pending: []string{"new", "pending", "waiting", "running"},
Target: []string{"successful"},
Refresh: statusInstanceState(ctx, svc, job.ID),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err := stateConf.WaitForStateContext(ctx)
return err
}
// JobTemplateLaunchData provides payload data used by the JobTemplateLaunch method
type JobTemplateLaunchData struct {
Limit string `json:"limit,omitempty"`
InventoryID int `json:"inventory,omitempty"`
ExtraVars string `json:"extra_vars,omitempty"`
}
func resourceJobTemplateLaunchCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
awxService := client.JobTemplateService
awxJobService := client.JobService
jobTemplateID := d.Get("job_template_id").(int)
_, err := awxService.GetJobTemplateByID(jobTemplateID, make(map[string]string))
if err != nil {
return buildDiagNotFoundFail("job template", jobTemplateID, err)
}
data := JobTemplateLaunchData{
Limit: d.Get("limit").(string),
InventoryID: d.Get("inventory_id").(int),
ExtraVars: d.Get("extra_vars").(string),
}
var iData map[string]interface{}
idata, _ := json.Marshal(data)
json.Unmarshal(idata, &iData)
res, err := awxService.Launch(jobTemplateID, iData, map[string]string{})
if err != nil {
log.Printf("Failed to create Template Launch %v", err)
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to create JobTemplate",
Detail: fmt.Sprintf("JobTemplateLaunch with template ID %d, failed to create %s", d.Get("job_template_id").(int), err.Error()),
})
return diags
}
// return resourceJobRead(ctx, d, m)
d.SetId(strconv.Itoa(res.ID))
if d.Get("wait_for_completion").(bool) {
err = jobTemplateLaunchWait(ctx, awxJobService, res, d.Timeout(schema.TimeoutCreate))
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "JobTemplate execution failure",
Detail: fmt.Sprintf("JobTemplateLaunch with ID %d and template ID %d, failed to complete %s", res.ID, d.Get("job_template_id").(int), err.Error()),
})
}
}
return diags
}
func resourceJobRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
return diags
}
func resourceJobDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
awxService := client.JobService
jobID, diags := convertStateIDToNummeric("Delete Job", d)
_, err := awxService.GetJob(jobID, map[string]string{})
if err != nil {
return buildDiagNotFoundFail("job", jobID, err)
}
d.SetId("")
return diags
}