Skip to content

Commit 1d60ab8

Browse files
Alexandre Bortoluzzidenouche
authored andcommitted
feat: schedule extra data
1 parent 176e085 commit 1d60ab8

5 files changed

Lines changed: 39 additions & 9 deletions

File tree

awx/helpers.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,20 @@ func normalizeYamlOk(s interface{}) (string, bool) {
168168
b, _ := yaml.Marshal(j)
169169
return string(b[:]), true
170170
}
171+
172+
func unmarshalYaml(str string) map[string]interface{} {
173+
asMap := map[string]interface{}{}
174+
err := yaml.Unmarshal([]byte(str), &asMap)
175+
if err != nil {
176+
asMap = nil
177+
}
178+
return asMap
179+
}
180+
181+
func marshalYaml(v interface{}) string {
182+
extraDataBytes, err := yaml.Marshal(v)
183+
if err != nil {
184+
return string(extraDataBytes)
185+
}
186+
return ""
187+
}

awx/resource_schedule.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ resource "awx_schedule" "default" {
88
name = "schedule-test"
99
rrule = "DTSTART;TZID=Europe/Paris:20211214T120000 RRULE:INTERVAL=1;FREQ=DAILY"
1010
unified_job_template_id = awx_job_template.baseconfig.id
11+
extra_data = <<EOL
12+
organization_name: testorg
13+
EOL
1114
}
1215
```
1316
@@ -58,6 +61,12 @@ func resourceSchedule() *schema.Resource {
5861
Type: schema.TypeInt,
5962
Optional: true,
6063
},
64+
"extra_data": {
65+
Type: schema.TypeString,
66+
Optional: true,
67+
Default: "",
68+
Description: "Extra data to be pass for the schedule (YAML format)",
69+
},
6170
},
6271
}
6372
}
@@ -74,6 +83,7 @@ func resourceScheduleCreate(ctx context.Context, d *schema.ResourceData, m inter
7483
"description": d.Get("description").(string),
7584
"enabled": d.Get("enabled").(bool),
7685
"inventory": d.Get("inventory").(int),
86+
"extra_data": unmarshalYaml(d.Get("extra_data").(string)),
7787
}, map[string]string{})
7888
if err != nil {
7989
log.Printf("Fail to Create Schedule %v", err)
@@ -110,7 +120,8 @@ func resourceScheduleUpdate(ctx context.Context, d *schema.ResourceData, m inter
110120
"unified_job_template": d.Get("unified_job_template_id").(int),
111121
"description": d.Get("description").(string),
112122
"enabled": d.Get("enabled").(bool),
113-
"inventory": d.Get("inventory").(int),
123+
"inventory": AtoipOr(d.Get("inventory").(string), nil),
124+
"extra_data": unmarshalYaml(d.Get("extra_data").(string)),
114125
}, map[string]string{})
115126
if err != nil {
116127
diags = append(diags, diag.Diagnostic{
@@ -167,6 +178,7 @@ func setScheduleResourceData(d *schema.ResourceData, r *awx.Schedule) *schema.Re
167178
d.Set("description", r.Description)
168179
d.Set("enabled", r.Enabled)
169180
d.Set("inventory", r.Inventory)
181+
d.Set("extra_data", marshalYaml(r.ExtraData))
170182
d.SetId(strconv.Itoa(r.ID))
171183
return d
172184
}

awx/resource_workflow_job_template_schedule.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ resource "awx_workflow_job_template_schedule" "default" {
99
1010
name = "schedule-test"
1111
rrule = "DTSTART;TZID=Europe/Paris:20211214T120000 RRULE:INTERVAL=1;FREQ=DAILY"
12+
extra_data = <<EOL
13+
organization_name: testorg
14+
EOL
1215
}
1316
```
1417
@@ -39,7 +42,6 @@ func resourceWorkflowJobTemplateSchedule() *schema.Resource {
3942
Required: true,
4043
Description: "The workflow_job_template id for this schedule",
4144
},
42-
4345
"name": {
4446
Type: schema.TypeString,
4547
Required: true,
@@ -66,12 +68,11 @@ func resourceWorkflowJobTemplateSchedule() *schema.Resource {
6668
Optional: true,
6769
Description: "Inventory applied as a prompt, assuming job template prompts for inventory (id, default=``)",
6870
},
69-
"extra_data": {
71+
"extra_data": {
7072
Type: schema.TypeString,
7173
Optional: true,
7274
Default: "",
73-
Description: "Extra data to be pass for the scheudle (same like extra_vars)",
74-
StateFunc: normalizeJsonYaml,
75+
Description: "Extra data to be pass for the schedule (YAML format)",
7576
},
7677
},
7778
}
@@ -90,7 +91,7 @@ func resourceWorkflowJobTemplateScheduleCreate(ctx context.Context, d *schema.Re
9091
"description": d.Get("description").(string),
9192
"enabled": d.Get("enabled").(bool),
9293
"inventory": AtoipOr(d.Get("inventory").(string), nil),
93-
"extra_data": d.Get("extra_data").(string),
94+
"extra_data": unmarshalYaml(d.Get("extra_data").(string)),
9495
}, map[string]string{})
9596
if err != nil {
9697
log.Printf("Fail to Create Schedule for WorkflowJobTemplate %d: %v", workflowJobTemplateID, err)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/denouche/terraform-provider-awx
33
go 1.14
44

55
require (
6-
github.com/denouche/goawx v0.17.0
6+
github.com/denouche/goawx v0.18.1
77
github.com/gruntwork-io/terratest v0.31.2
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
99
github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2
137137
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
138138
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
139139
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
140-
github.com/denouche/goawx v0.17.0 h1:mtCfsYdKBSTO/3BGFPU7q13shAneLSlaJYEmqgLVwC0=
141-
github.com/denouche/goawx v0.17.0/go.mod h1:MppzSteoj2xgfiqiRWW/Bf1a8z2FrRyvah1z0J2vJTY=
140+
github.com/denouche/goawx v0.18.1 h1:fYwrvnmOrLc8OKOjhSAZnO/engSJsIjBKp6ytQABfsk=
141+
github.com/denouche/goawx v0.18.1/go.mod h1:MppzSteoj2xgfiqiRWW/Bf1a8z2FrRyvah1z0J2vJTY=
142142
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
143143
github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8=
144144
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=

0 commit comments

Comments
 (0)