From f819c37257cbe7d91f6a84d58129f230b3048caf Mon Sep 17 00:00:00 2001 From: MrLuje Date: Thu, 31 Mar 2022 11:52:07 +0200 Subject: [PATCH] fix: constant orphaned changes if not specified & crash (#153) * fix: constant orphaned changes if not specified BREAKING CHANGE: To be on par with the API surface of Argo CD, "orphaned resources monitoring" will be only enabled if it is explicitly declared. Monitoring enabled because of the previous default behavior will be detected as a removal on next apply. * fix: prevent crash on empty orphaned * fix(tests): ExpectNonEmptyPlan not needed anymore since orphaned bug is gone --- argocd/resource_argocd_project_test.go | 76 +++++++++++++++++++++++++- argocd/structure_project.go | 20 ++++--- 2 files changed, 87 insertions(+), 9 deletions(-) diff --git a/argocd/resource_argocd_project_test.go b/argocd/resource_argocd_project_test.go index 1b9be29d..6cfb0a66 100644 --- a/argocd/resource_argocd_project_test.go +++ b/argocd/resource_argocd_project_test.go @@ -64,6 +64,26 @@ func TestAccArgoCDProject(t *testing.T) { // TODO: check all possible attributes ), }, + { + Config: testAccArgoCDProjectSimpleWithoutOrphaned(name), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet( + "argocd_project.simple", + "metadata.0.uid", + // TODO: check all possible attributes + ), + ), + }, + { + Config: testAccArgoCDProjectSimpleWithEmptyOrphaned(name), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet( + "argocd_project.simple", + "metadata.0.uid", + // TODO: check all possible attributes + ), + ), + }, }, }) } @@ -74,7 +94,6 @@ func TestAccArgoCDProject_tokensCoexistence(t *testing.T) { ProviderFactories: testAccProviders, Steps: []resource.TestStep{ { - ExpectNonEmptyPlan: true, Config: testAccArgoCDProjectCoexistenceWithTokenResource( "test-acc-"+acctest.RandString(10), 4, @@ -229,6 +248,61 @@ resource "argocd_project" "simple" { `, name) } +func testAccArgoCDProjectSimpleWithoutOrphaned(name string) string { + return fmt.Sprintf(` + resource "argocd_project" "simple" { + metadata { + name = "%s" + namespace = "argocd" + labels = { + acceptance = "true" + } + annotations = { + "this.is.a.really.long.nested.key" = "yes, really!" + } + } + + spec { + description = "simple project" + source_repos = ["*"] + + destination { + name = "anothercluster" + namespace = "bar" + } + } + } + `, name) +} + +func testAccArgoCDProjectSimpleWithEmptyOrphaned(name string) string { + return fmt.Sprintf(` + resource "argocd_project" "simple" { + metadata { + name = "%s" + namespace = "argocd" + labels = { + acceptance = "true" + } + annotations = { + "this.is.a.really.long.nested.key" = "yes, really!" + } + } + + spec { + description = "simple project" + source_repos = ["*"] + + destination { + name = "anothercluster" + namespace = "bar" + } + orphaned_resources { } + } + } + `, name) +} + func testAccArgoCDProjectCoexistenceWithTokenResource(name string, count int) string { return fmt.Sprintf(` resource "argocd_project" "coexistence" { diff --git a/argocd/structure_project.go b/argocd/structure_project.go index 6328e801..3e8efa4b 100644 --- a/argocd/structure_project.go +++ b/argocd/structure_project.go @@ -3,6 +3,7 @@ package argocd import ( "encoding/json" "fmt" + application "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" meta "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -63,16 +64,19 @@ func expandProjectSpec(d *schema.ResourceData) ( } } if v, ok := s["orphaned_resources"]; ok { - spec.OrphanedResources = &application.OrphanedResourcesMonitorSettings{} orphanedResources := v.([]interface{}) if len(orphanedResources) > 0 { - if _warn, _ok := orphanedResources[0].(map[string]interface{})["warn"]; _ok { - warn := _warn.(bool) - spec.OrphanedResources.Warn = &warn - } - if _ignore, _ok := orphanedResources[0].(map[string]interface{})["ignore"]; _ok { - ignore := expandOrphanedResourcesIgnore(_ignore.(*schema.Set)) - spec.OrphanedResources.Ignore = ignore + spec.OrphanedResources = &application.OrphanedResourcesMonitorSettings{} + + if orphanedResources[0] != nil { + if _warn, _ok := orphanedResources[0].(map[string]interface{})["warn"]; _ok { + warn := _warn.(bool) + spec.OrphanedResources.Warn = &warn + } + if _ignore, _ok := orphanedResources[0].(map[string]interface{})["ignore"]; _ok { + ignore := expandOrphanedResourcesIgnore(_ignore.(*schema.Set)) + spec.OrphanedResources.Ignore = ignore + } } } }