Skip to content

Commit abe120c

Browse files
jnaheloudenouche
authored andcommitted
feat(resource_job_template_launch): add more configuration options (limit, inventory, completion wait)
1 parent 2abfb63 commit abe120c

3 files changed

Lines changed: 1452 additions & 4 deletions

File tree

awx/resource_job_template_launch.go

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ package awx
2929

3030
import (
3131
"context"
32+
"encoding/json"
3233
"fmt"
3334
"log"
3435
"strconv"
36+
"time"
3537

3638
awx "github.com/denouche/goawx/client"
3739
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
40+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
3841
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
3942
)
4043

@@ -51,33 +54,107 @@ func resourceJobTemplateLaunch() *schema.Resource {
5154
Description: "Job template ID",
5255
ForceNew: true,
5356
},
57+
"limit": {
58+
Type: schema.TypeString,
59+
Required: false,
60+
Optional: true,
61+
ForceNew: true,
62+
Description: "List of comma delimited hosts to limit job execution. Required ask_limit_on_launch set on job_template.",
63+
},
64+
"inventory": {
65+
Type: schema.TypeInt,
66+
Required: false,
67+
Optional: true,
68+
Default: "",
69+
Description: "Override Inventory ID. Required ask_inventory_on_launch set on job_template.",
70+
ForceNew: true,
71+
},
72+
"wait_for_completion": {
73+
Type: schema.TypeBool,
74+
Required: false,
75+
Optional: true,
76+
Default: false,
77+
Description: "Resource creation will wait for job completion.",
78+
ForceNew: true,
79+
},
5480
},
5581
}
5682
}
5783

84+
func statusInstanceState(ctx context.Context, svc *awx.JobService, id int) retry.StateRefreshFunc {
85+
return func() (interface{}, string, error) {
86+
output, err := svc.GetJob(id, map[string]string{})
87+
return output, output.Status, err
88+
}
89+
}
90+
91+
func jobTemplateLaunchWait(ctx context.Context, svc *awx.JobService, job *awx.JobLaunch, timeout time.Duration) error {
92+
93+
stateConf := &retry.StateChangeConf{
94+
Pending: []string{"new", "pending", "waiting", "running"},
95+
Target: []string{"successful"},
96+
Refresh: statusInstanceState(ctx, svc, job.ID),
97+
Timeout: timeout,
98+
Delay: 10 * time.Second,
99+
MinTimeout: 3 * time.Second,
100+
}
101+
102+
_, err := stateConf.WaitForStateContext(ctx)
103+
104+
return err
105+
}
106+
107+
// JobTemplateLaunchData provides payload data used by the JobTemplateLaunch method
108+
type JobTemplateLaunchData struct {
109+
Limit string `json:"limit,omitempty"`
110+
Inventory int `json:"inventory,omitempty"`
111+
}
112+
58113
func resourceJobTemplateLaunchCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
59114
var diags diag.Diagnostics
60115
client := m.(*awx.AWX)
61116
awxService := client.JobTemplateService
117+
awxJobService := client.JobService
118+
62119
jobTemplateID := d.Get("job_template_id").(int)
63120
_, err := awxService.GetJobTemplateByID(jobTemplateID, make(map[string]string))
64121
if err != nil {
65122
return buildDiagNotFoundFail("job template", jobTemplateID, err)
66123
}
67124

68-
res, err := awxService.Launch(jobTemplateID, map[string]interface{}{}, map[string]string{})
125+
data := JobTemplateLaunchData{
126+
Limit: d.Get("limit").(string),
127+
Inventory: d.Get("inventory").(int),
128+
}
129+
130+
var iData map[string]interface{}
131+
idata, _ := json.Marshal(data)
132+
json.Unmarshal(idata, &iData)
133+
134+
res, err := awxService.Launch(jobTemplateID, iData, map[string]string{})
69135
if err != nil {
70136
log.Printf("Failed to create Template Launch %v", err)
71137
diags = append(diags, diag.Diagnostic{
72138
Severity: diag.Error,
73139
Summary: "Unable to create JobTemplate",
74-
Detail: fmt.Sprintf("JobTemplate with name %s in the project id %d, failed to create %s", d.Get("name").(string), d.Get("project_id").(int), err.Error()),
140+
Detail: fmt.Sprintf("JobTemplateLaunch with template ID %d, failed to create %s", d.Get("job_template_id").(int), err.Error()),
75141
})
76142
return diags
77143
}
78144

79145
// return resourceJobRead(ctx, d, m)
80146
d.SetId(strconv.Itoa(res.ID))
147+
148+
if d.Get("wait_for_completion").(bool) {
149+
err = jobTemplateLaunchWait(ctx, awxJobService, res, d.Timeout(schema.TimeoutCreate))
150+
if err != nil {
151+
diags = append(diags, diag.Diagnostic{
152+
Severity: diag.Error,
153+
Summary: "JobTemplate execution failure",
154+
Detail: fmt.Sprintf("JobTemplateLaunch with template ID %d, failed to complete %s", d.Get("job_template_id").(int), err.Error()),
155+
})
156+
}
157+
}
81158
return diags
82159
}
83160

go.mod

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@ module github.com/denouche/terraform-provider-awx
33
go 1.14
44

55
require (
6+
github.com/Masterminds/semver v1.5.0 // indirect
7+
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
8+
github.com/andybalholm/crlf v0.0.0-20171020200849-670099aa064f // indirect
9+
github.com/apparentlymart/go-cidr v1.0.1 // indirect
10+
github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0 // indirect
611
github.com/denouche/goawx v0.18.1
12+
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
713
github.com/gruntwork-io/terratest v0.31.2
8-
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
9-
github.com/stretchr/testify v1.7.0
14+
github.com/hashicorp/go-getter v1.5.3 // indirect
15+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0
16+
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce // indirect
17+
github.com/stretchr/testify v1.8.3
18+
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
1019
gopkg.in/yaml.v2 v2.4.0
1120
)

0 commit comments

Comments
 (0)