/
resource_schedule.go
177 lines (154 loc) · 4.77 KB
/
resource_schedule.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
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
/*
Schedule a job template.
rrule field represents an iCal recurrence rule. More information can be found by reading this page: [iCal rrule](https://icalendar.org/iCalendar-RFC-5545/3-8-5-3-recurrence-rule.html)
Example Usage
```hcl
data
resource "awx_schedule" "default" {
name = "schedule-test"
unified_job_template_id = data.awx_job_template.baseconfig.id
rrule = "DTSTART;TZID=Europe/Paris:20211214T120000 RRULE:INTERVAL=1;FREQ=DAILY"
}
```
*/
package awx
import (
"context"
"fmt"
"log"
"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 resourceSchedule() *schema.Resource {
return &schema.Resource{
CreateContext: resourceScheduleCreate,
ReadContext: resourceScheduleRead,
UpdateContext: resourceScheduleUpdate,
DeleteContext: resourceScheduleDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"rrule": {
Type: schema.TypeString,
Required: true,
},
"unified_job_template_id": {
Type: schema.TypeInt,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"inventory": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}
func resourceScheduleCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
awxService := client.ScheduleService
result, err := awxService.Create(map[string]interface{}{
"name": d.Get("name").(string),
"rrule": d.Get("rrule").(string),
"unified_job_template": d.Get("unified_job_template_id").(int),
"description": d.Get("description").(string),
"enabled": d.Get("enabled").(bool),
"inventory": d.Get("inventory").(int),
}, map[string]string{})
if err != nil {
log.Printf("Fail to Create Schedule %v", err)
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to create Schedule",
Detail: fmt.Sprintf("Schedule failed to create %s", err.Error()),
})
return diags
}
d.SetId(strconv.Itoa(result.ID))
return resourceScheduleRead(ctx, d, m)
}
func resourceScheduleUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
awxService := client.ScheduleService
id, diags := convertStateIDToNummeric("Update Schedule", d)
if diags.HasError() {
return diags
}
params := make(map[string]string)
_, err := awxService.GetByID(id, params)
if err != nil {
return buildDiagNotFoundFail("schedule", id, err)
}
_, err = awxService.Update(id, map[string]interface{}{
"name": d.Get("name").(string),
"rrule": d.Get("rrule").(string),
"unified_job_template": d.Get("unified_job_template_id").(int),
"description": d.Get("description").(string),
"enabled": d.Get("enabled").(bool),
"inventory": d.Get("inventory").(int),
}, map[string]string{})
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to update Schedule",
Detail: fmt.Sprintf("Schedule with name %s failed to update %s", d.Get("name").(string), err.Error()),
})
return diags
}
return resourceScheduleRead(ctx, d, m)
}
func resourceScheduleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
awxService := client.ScheduleService
id, diags := convertStateIDToNummeric("Read schedule", d)
if diags.HasError() {
return diags
}
res, err := awxService.GetByID(id, make(map[string]string))
if err != nil {
return buildDiagNotFoundFail("schedule", id, err)
}
d = setScheduleResourceData(d, res)
return nil
}
func resourceScheduleDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*awx.AWX)
awxService := client.ScheduleService
id, diags := convertStateIDToNummeric(diagElementHostTitle, d)
if diags.HasError() {
return diags
}
if _, err := awxService.Delete(id); err != nil {
return buildDiagDeleteFail(
diagElementHostTitle,
fmt.Sprintf("id %v, got %s ",
id, err.Error()))
}
d.SetId("")
return nil
}
func setScheduleResourceData(d *schema.ResourceData, r *awx.Schedule) *schema.ResourceData {
d.Set("name", r.Name)
d.Set("rrule", r.Rrule)
d.Set("unified_job_template_id", r.UnifiedJobTemplate)
d.Set("description", r.Description)
d.Set("enabled", r.Enabled)
d.Set("inventory", r.Inventory)
d.SetId(strconv.Itoa(r.ID))
return d
}