Skip to content

Commit

Permalink
Use schema.TypeSet for application sync_policy (#228)
Browse files Browse the repository at this point in the history
* chore: don't clean `.vscode` folder

* build(deps): bump `hashicorp/terraform-plugin-sdk` to `v2.24.1`

* tests: remove duplicate `v` from log message

* tests: add `allow_empty` to ArgoCD simple wait test

Removes the "expected diff" which results from the state containing all 3 map keys whilst the resource definition only defines 2.

* feat!: use set instead of map in  `argocd_application.spec.sync_policy`

`schema.TypeSet` fits these resources better since, as per the [provider SDK docs](https://developer.hashicorp.com/terraform/plugin/sdkv2/schemas/schema-types#typemap):

> Using the Elem block to define specific keys for the map is currently not possible.
  • Loading branch information
onematchfox authored Jan 20, 2023
1 parent 94394f5 commit 7ce3476
Show file tree
Hide file tree
Showing 12 changed files with 1,071 additions and 243 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ release: clean linux windows darwin freebsd
for f in $(shell ls bin/); do zip bin/$${f}.zip bin/$${f}; done

clean:
git clean -fXd -e \!vendor -e \!vendor/**/*
git clean -fXd -e \!vendor -e \!vendor/**/* -e \!.vscode

test: fmtcheck
go test $(TEST) -timeout=30s -parallel=4
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ resource "argocd_application" "kustomize" {
}
sync_policy {
automated = {
automated {
prune = true
self_heal = true
}
Expand Down
2 changes: 1 addition & 1 deletion argocd/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ func testAccPreCheckFeatureNotSupported(t *testing.T, feature int) {
t.Fatal("feature constraint is not handled by the provider")
}
if i := versionConstraint.Compare(serverVersion); i != 1 {
t.Skipf("not running test if feature is already supported (v%s)", v)
t.Skipf("not running test if feature is already supported (%s)", v)
}
}
9 changes: 7 additions & 2 deletions argocd/resource_argocd_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func resourceArgoCDApplication() *schema.Resource {
},
Schema: map[string]*schema.Schema{
"metadata": metadataSchema("applications.argoproj.io"),
"spec": applicationSpecSchemaV3(),
"spec": applicationSpecSchemaV4(),
"wait": {
Type: schema.TypeBool,
Description: "Upon application creation or update, wait for application health/sync status to be healthy/Synced, upon application deletion, wait for application to be removed, when set to true.",
Expand All @@ -41,7 +41,7 @@ func resourceArgoCDApplication() *schema.Resource {
Default: true,
},
},
SchemaVersion: 3,
SchemaVersion: 4,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceArgoCDApplicationV0().CoreConfigSchema().ImpliedType(),
Expand All @@ -58,6 +58,11 @@ func resourceArgoCDApplication() *schema.Resource {
Upgrade: resourceArgoCDApplicationStateUpgradeV2,
Version: 2,
},
{
Type: resourceArgoCDApplicationV3().CoreConfigSchema().ImpliedType(),
Upgrade: resourceArgoCDApplicationStateUpgradeV3,
Version: 3,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Expand Down
60 changes: 32 additions & 28 deletions argocd/resource_argocd_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ ingress:
),
},
{
Config: testAccArgoCDApplicationSimpleWait(commonName),
ExpectNonEmptyPlan: true,
Config: testAccArgoCDApplicationSimpleWait(commonName),
Check: resource.TestCheckResourceAttr(
"argocd_application.simple",
"wait",
Expand Down Expand Up @@ -206,32 +205,32 @@ ingress:
),
resource.TestCheckResourceAttr(
"argocd_application.sync_policy",
"spec.0.sync_policy.0.automated.prune",
"spec.0.sync_policy.0.automated.0.prune",
"true",
),
resource.TestCheckResourceAttr(
"argocd_application.sync_policy",
"spec.0.sync_policy.0.automated.self_heal",
"spec.0.sync_policy.0.automated.0.self_heal",
"true",
),
resource.TestCheckResourceAttr(
"argocd_application.sync_policy",
"spec.0.sync_policy.0.automated.allow_empty",
"spec.0.sync_policy.0.automated.0.allow_empty",
"true",
),
resource.TestCheckResourceAttr(
"argocd_application.sync_policy",
"spec.0.sync_policy.0.retry.0.backoff.duration",
"spec.0.sync_policy.0.retry.0.backoff.0.duration",
"30s",
),
resource.TestCheckResourceAttr(
"argocd_application.sync_policy",
"spec.0.sync_policy.0.retry.0.backoff.max_duration",
"spec.0.sync_policy.0.retry.0.backoff.0.max_duration",
"2m",
),
resource.TestCheckResourceAttr(
"argocd_application.sync_policy",
"spec.0.sync_policy.0.retry.0.backoff.factor",
"spec.0.sync_policy.0.retry.0.backoff.0.factor",
"2",
),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -332,11 +331,11 @@ func TestAccArgoCDApplication_NoSyncPolicyBlock(t *testing.T) {
),
resource.TestCheckNoResourceAttr(
"argocd_application.simple",
"spec.0.sync_policy.0.retry.0.backoff.duration",
"spec.0.sync_policy.0.retry.0.backoff.0.duration",
),
resource.TestCheckNoResourceAttr(
"argocd_application.simple",
"spec.0.sync_policy.0.automated.prune",
"spec.0.sync_policy.0.automated.0.prune",
),
),
},
Expand All @@ -361,7 +360,7 @@ func TestAccArgoCDApplication_Recurse(t *testing.T) {
),
resource.TestCheckNoResourceAttr(
"argocd_application.directory",
"spec.0.source.0.directory.0.jsonnet",
"spec.0.source.0.directory.0.jsonnet.0",
),
),
},
Expand Down Expand Up @@ -389,7 +388,7 @@ func TestAccArgoCDApplication_Recurse(t *testing.T) {
),
resource.TestCheckNoResourceAttr(
"argocd_application.directory",
"spec.0.source.0.directory.0.jsonnet",
"spec.0.source.0.directory.0.jsonnet.0",
),
),
},
Expand All @@ -403,7 +402,7 @@ func TestAccArgoCDApplication_Recurse(t *testing.T) {
),
resource.TestCheckNoResourceAttr(
"argocd_application.directory",
"spec.0.source.0.directory.0.jsonnet",
"spec.0.source.0.directory.0.jsonnet.0",
),
),
},
Expand All @@ -416,7 +415,7 @@ func TestAccArgoCDApplication_Recurse(t *testing.T) {
),
resource.TestCheckNoResourceAttr(
"argocd_application.directory",
"spec.0.source.0.directory.0.jsonnet",
"spec.0.source.0.directory.0.jsonnet.0",
),
),
},
Expand All @@ -438,11 +437,11 @@ func TestAccArgoCDApplication_EmptySyncPolicyBlock(t *testing.T) {
),
resource.TestCheckNoResourceAttr(
"argocd_application.simple",
"spec.0.sync_policy.0.retry.0.backoff.duration",
"spec.0.sync_policy.0.retry.0.backoff.0.duration",
),
resource.TestCheckNoResourceAttr(
"argocd_application.simple",
"spec.0.sync_policy.0.automated.prune",
"spec.0.sync_policy.0.automated.0.prune",
),
),
},
Expand All @@ -464,11 +463,11 @@ func TestAccArgoCDApplication_NoAutomatedBlock(t *testing.T) {
),
resource.TestCheckResourceAttrSet(
"argocd_application.simple",
"spec.0.sync_policy.0.retry.0.backoff.duration",
"spec.0.sync_policy.0.retry.0.backoff.0.duration",
),
resource.TestCheckNoResourceAttr(
"argocd_application.simple",
"spec.0.sync_policy.0.automated.prune",
"spec.0.sync_policy.0.automated.0.prune",
),
),
},
Expand All @@ -488,9 +487,13 @@ func TestAccArgoCDApplication_EmptyAutomatedBlock(t *testing.T) {
"argocd_application.simple",
"metadata.0.uid",
),
resource.TestCheckResourceAttrSet(
"argocd_application.simple",
"spec.0.sync_policy.0.automated.#",
),
resource.TestCheckNoResourceAttr(
"argocd_application.simple",
"spec.0.sync_policy.0.automated.prune",
"spec.0.sync_policy.0.automated.0.prune",
),
),
},
Expand Down Expand Up @@ -645,7 +648,7 @@ func TestAccArgoCDApplication_Info(t *testing.T) {
),
resource.TestCheckNoResourceAttr(
"argocd_application.info",
"spec.0.info",
"spec.0.info.0",
),
),
},
Expand All @@ -659,7 +662,7 @@ func TestAccArgoCDApplication_Info(t *testing.T) {
),
resource.TestCheckNoResourceAttr(
"argocd_application.info",
"spec.0.info",
"spec.0.info.0",
),
),
},
Expand Down Expand Up @@ -908,9 +911,10 @@ resource "argocd_application" "simple" {
}
}
sync_policy {
automated = {
prune = true
self_heal = true
automated {
prune = true
self_heal = true
allow_empty = false
}
}
destination {
Expand Down Expand Up @@ -1210,14 +1214,14 @@ resource "argocd_application" "sync_policy" {
}
sync_policy {
automated = {
automated {
prune = true
self_heal = true
allow_empty = true
}
retry {
limit = "5"
backoff = {
backoff {
duration = "30s"
max_duration = "2m"
factor = "2"
Expand Down Expand Up @@ -1386,7 +1390,7 @@ resource "argocd_application" "simple" {
sync_policy {
retry {
limit = "5"
backoff = {
backoff {
duration = "30s"
max_duration = "2m"
factor = "2"
Expand Down Expand Up @@ -1419,7 +1423,7 @@ resource "argocd_application" "simple" {
}
}
sync_policy {
automated = {}
automated {}
}
destination {
server = "https://kubernetes.default.svc"
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 @@ -197,7 +197,7 @@ func TestAccArgoCDCluster_metadata(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr(
"argocd_cluster.cluster_metadata",
"metadata",
"metadata.0",
),
),
},
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestAccArgoCDCluster_metadata(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr(
"argocd_cluster.cluster_metadata",
"metadata.0.labels",
"metadata.0.labels.test",
),
resource.TestCheckResourceAttr(
"argocd_cluster.cluster_metadata",
Expand Down
Loading

0 comments on commit 7ce3476

Please sign in to comment.