Skip to content

Commit

Permalink
fix: constant orphaned changes if not specified & crash (#153)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MrLuje authored Mar 31, 2022
1 parent 44f3e2e commit f819c37
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
76 changes: 75 additions & 1 deletion argocd/resource_argocd_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
),
},
},
})
}
Expand All @@ -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,
Expand Down Expand Up @@ -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" {
Expand Down
20 changes: 12 additions & 8 deletions argocd/structure_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
}
}
Expand Down

0 comments on commit f819c37

Please sign in to comment.