Skip to content

Commit

Permalink
Add support for timezone in sync window (#314)
Browse files Browse the repository at this point in the history
Co-authored-by: OneMatchFox <878612+onematchfox@users.noreply.github.com>
  • Loading branch information
jsmcnair and onematchfox committed Jul 4, 2023
1 parent aaf4a18 commit e6eff84
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 8 deletions.
3 changes: 1 addition & 2 deletions argocd/resource_argocd_account_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ func TestAccArgoCDAccountToken_RenewBefore(t *testing.T) {

func TestAccArgoCDAccountToken_RenewAfter(t *testing.T) {
resourceName := "argocd_account_token.renew_after"

renewAfterSeconds := 2
renewAfterSeconds := 30

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
4 changes: 2 additions & 2 deletions argocd/resource_argocd_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestAccArgoCDCluster(t *testing.T) {
ResourceName: "argocd_cluster.simple",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"config.0.bearer_token"},
ImportStateVerifyIgnore: []string{"config.0.bearer_token", "info"},
},
{
Config: testAccArgoCDClusterTLSCertificate(t, acctest.RandString(10)),
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestAccArgoCDCluster_projectScope(t *testing.T) {
ResourceName: "argocd_cluster.project_scope",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"config.0.bearer_token"},
ImportStateVerifyIgnore: []string{"config.0.bearer_token", "info"},
},
},
})
Expand Down
43 changes: 43 additions & 0 deletions argocd/resource_argocd_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func TestAccArgoCDProject(t *testing.T) {
),
ExpectError: regexp.MustCompile("cannot parse schedule"),
},
{
Config: testAccArgoCDProjectSyncWindowTimezoneError(
"test-acc-" + acctest.RandString(10),
),
ExpectError: regexp.MustCompile("cannot parse timezone"),
},
{
Config: testAccArgoCDProjectSimple(name),
Check: resource.TestCheckResourceAttrSet(
Expand Down Expand Up @@ -332,6 +338,7 @@ resource "argocd_project" "simple" {
duration = "12h"
schedule = "22 1 5 * *"
manual_sync = false
timezone = "Europe/London"
}
signature_keys = [
"4AEE18F83AFDEB23",
Expand Down Expand Up @@ -791,3 +798,39 @@ resource "argocd_project" "simple" {
}
`, name)
}

func testAccArgoCDProjectSyncWindowTimezoneError(name string) string {
return fmt.Sprintf(`
resource "argocd_project" "failure" {
metadata {
name = "%s"
namespace = "argocd"
}
spec {
description = "expected timezone failure"
destination {
server = "https://kubernetes.default.svc"
namespace = "*"
}
source_repos = ["*"]
role {
name = "incorrect-syncwindow"
policies = [
"p, proj:%s:testrole, applications, override, %s/foo, allow",
]
}
sync_window {
kind = "allow"
applications = ["api-*"]
clusters = ["*"]
namespaces = ["*"]
duration = "1h"
schedule = "10 1 * * *"
manual_sync = true
timezone = "invalid"
}
}
}
`, name, name, name)
}
3 changes: 1 addition & 2 deletions argocd/resource_argocd_project_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ func TestAccArgoCDProjectToken_RenewBefore(t *testing.T) {

func TestAccArgoCDProjectToken_RenewAfter(t *testing.T) {
resourceName := "argocd_project_token.renew_after"

renewAfterSeconds := 2
renewAfterSeconds := 30

// Note: not running in parallel as this is a time sensitive test case
resource.Test(t, resource.TestCase{
Expand Down
7 changes: 7 additions & 0 deletions argocd/schema_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,13 @@ func projectSpecSchemaV2() *schema.Schema {
ValidateFunc: validateSyncWindowSchedule,
Optional: true,
},
"timezone": {
Type: schema.TypeString,
Description: "Timezone that the schedule will be evaluated in.",
ValidateFunc: validateSyncWindowTimezone,
Optional: true,
Default: "UTC",
},
},
},
},
Expand Down
1 change: 1 addition & 0 deletions argocd/structure_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ func expandSyncWindows(sws []interface{}) (result []*application.SyncWindow) {
ManualSync: sw["manual_sync"].(bool),
Namespaces: expandStringList(sw["namespaces"].([]interface{})),
Schedule: sw["schedule"].(string),
TimeZone: sw["timezone"].(string),
})
}

Expand Down
1 change: 1 addition & 0 deletions argocd/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func flattenSyncWindows(sws application.SyncWindows) (result []map[string]interf
"manual_sync": sw.ManualSync,
"namespaces": sw.Namespaces,
"schedule": sw.Schedule,
"timezone": sw.TimeZone,
})
}

Expand Down
15 changes: 13 additions & 2 deletions argocd/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"regexp"
"strings"
"time"
_ "time/tzdata"

"github.com/argoproj/pkg/time"
argocdtime "github.com/argoproj/pkg/time"
"github.com/robfig/cron"
"golang.org/x/crypto/ssh"
apiValidation "k8s.io/apimachinery/pkg/api/validation"
Expand Down Expand Up @@ -105,13 +107,22 @@ func validateSyncWindowSchedule(value interface{}, key string) (ws []string, es
func validateSyncWindowDuration(value interface{}, key string) (ws []string, es []error) {
v := value.(string)

if _, err := time.ParseDuration(v); err != nil {
if _, err := argocdtime.ParseDuration(v); err != nil {
es = append(es, fmt.Errorf("%s: cannot parse duration '%s': %s", key, v, err))
}

return
}

func validateSyncWindowTimezone(value interface{}, key string) (ws []string, es []error) {
v := value.(string)
if _, err := time.LoadLocation(v); err != nil {
es = append(es, fmt.Errorf("%s: cannot parse timezone '%s': %s", key, v, err))
}

return
}

func validateDuration(value interface{}, key string) (ws []string, es []error) {
v := value.(string)

Expand Down
2 changes: 2 additions & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ resource "argocd_project" "myproject" {
duration = "12h"
schedule = "22 1 5 * *"
manual_sync = false
timezone = "Europe/London"
}
signature_keys = [
Expand Down Expand Up @@ -274,6 +275,7 @@ Optional:
- `manual_sync` (Boolean) Enables manual syncs when they would otherwise be blocked.
- `namespaces` (List of String) List of namespaces that the window will apply to.
- `schedule` (String) Time the window will begin, specified in cron format.
- `timezone` (String) Timezone that the schedule will be evaluated in.

## Import

Expand Down
1 change: 1 addition & 0 deletions examples/resources/argocd_project/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ resource "argocd_project" "myproject" {
duration = "12h"
schedule = "22 1 5 * *"
manual_sync = false
timezone = "Europe/London"
}

signature_keys = [
Expand Down
1 change: 1 addition & 0 deletions manifests/local-dev/project.tf
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ resource "argocd_project" "foo" {
duration = "12h"
schedule = "22 1 5 * *"
manual_sync = false
timezone = "Europe/London"
}
}
}

0 comments on commit e6eff84

Please sign in to comment.