@@ -3,13 +3,17 @@ package provider
33import (
44 "context"
55 "fmt"
6+ "strings"
67
78 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1011 "github.com/mitchellh/mapstructure"
12+ rbcron "github.com/robfig/cron/v3"
1113)
1214
15+ var PrebuildsCRONParser = rbcron .NewParser (rbcron .Minute | rbcron .Hour | rbcron .Dom | rbcron .Month | rbcron .Dow )
16+
1317type WorkspacePreset struct {
1418 Name string `mapstructure:"name"`
1519 Parameters map [string ]string `mapstructure:"parameters"`
@@ -149,6 +153,21 @@ func workspacePresetDataSource() *schema.Resource {
149153 "cron" : {
150154 Type : schema .TypeString ,
151155 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+ },
152171 },
153172 "instances" : {
154173 Type : schema .TypeInt ,
@@ -166,3 +185,16 @@ func workspacePresetDataSource() *schema.Resource {
166185 },
167186 }
168187}
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