@@ -3,13 +3,17 @@ package provider
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
6
7
7
8
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8
9
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
10
11
"github.com/mitchellh/mapstructure"
12
+ rbcron "github.com/robfig/cron/v3"
11
13
)
12
14
15
+ var PrebuildsCRONParser = rbcron .NewParser (rbcron .Minute | rbcron .Hour | rbcron .Dom | rbcron .Month | rbcron .Dow )
16
+
13
17
type WorkspacePreset struct {
14
18
Name string `mapstructure:"name"`
15
19
Parameters map [string ]string `mapstructure:"parameters"`
@@ -149,6 +153,21 @@ func workspacePresetDataSource() *schema.Resource {
149
153
"cron" : {
150
154
Type : schema .TypeString ,
151
155
Required : true ,
156
+ ValidateFunc : func (val interface {}, key string ) ([]string , []error ) {
157
+ cronSpec := val .(string )
158
+
159
+ err := validatePrebuildsCronSpec (cronSpec )
160
+ if err != nil {
161
+ return nil , []error {fmt .Errorf ("cron spec failed validation: %w" , err )}
162
+ }
163
+
164
+ _ , err = PrebuildsCRONParser .Parse (cronSpec )
165
+ if err != nil {
166
+ return nil , []error {fmt .Errorf ("failed to parse cron spec: %w" , err )}
167
+ }
168
+
169
+ return nil , nil
170
+ },
152
171
},
153
172
"instances" : {
154
173
Type : schema .TypeInt ,
@@ -166,3 +185,16 @@ func workspacePresetDataSource() *schema.Resource {
166
185
},
167
186
}
168
187
}
188
+
189
+ // validatePrebuildsCronSpec ensures that the minute, day-of-month and month options of spec are all set to *
190
+ func validatePrebuildsCronSpec (spec string ) error {
191
+ parts := strings .Fields (spec )
192
+ if len (parts ) != 5 {
193
+ return fmt .Errorf ("cron specification should consist of 5 fields" )
194
+ }
195
+ if parts [0 ] != "*" || parts [2 ] != "*" || parts [3 ] != "*" {
196
+ return fmt .Errorf ("minute, day-of-month and month should be *" )
197
+ }
198
+
199
+ return nil
200
+ }
0 commit comments