@@ -29,12 +29,15 @@ package awx
2929
3030import (
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+
58113func 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
0 commit comments