Skip to content

Commit f819c37

Browse files
authored
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
1 parent 44f3e2e commit f819c37

File tree

2 files changed

+87
-9
lines changed

2 files changed

+87
-9
lines changed

argocd/resource_argocd_project_test.go

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ func TestAccArgoCDProject(t *testing.T) {
6464
// TODO: check all possible attributes
6565
),
6666
},
67+
{
68+
Config: testAccArgoCDProjectSimpleWithoutOrphaned(name),
69+
Check: resource.ComposeTestCheckFunc(
70+
resource.TestCheckResourceAttrSet(
71+
"argocd_project.simple",
72+
"metadata.0.uid",
73+
// TODO: check all possible attributes
74+
),
75+
),
76+
},
77+
{
78+
Config: testAccArgoCDProjectSimpleWithEmptyOrphaned(name),
79+
Check: resource.ComposeTestCheckFunc(
80+
resource.TestCheckResourceAttrSet(
81+
"argocd_project.simple",
82+
"metadata.0.uid",
83+
// TODO: check all possible attributes
84+
),
85+
),
86+
},
6787
},
6888
})
6989
}
@@ -74,7 +94,6 @@ func TestAccArgoCDProject_tokensCoexistence(t *testing.T) {
7494
ProviderFactories: testAccProviders,
7595
Steps: []resource.TestStep{
7696
{
77-
ExpectNonEmptyPlan: true,
7897
Config: testAccArgoCDProjectCoexistenceWithTokenResource(
7998
"test-acc-"+acctest.RandString(10),
8099
4,
@@ -229,6 +248,61 @@ resource "argocd_project" "simple" {
229248
`, name)
230249
}
231250

251+
func testAccArgoCDProjectSimpleWithoutOrphaned(name string) string {
252+
return fmt.Sprintf(`
253+
resource "argocd_project" "simple" {
254+
metadata {
255+
name = "%s"
256+
namespace = "argocd"
257+
labels = {
258+
acceptance = "true"
259+
}
260+
annotations = {
261+
"this.is.a.really.long.nested.key" = "yes, really!"
262+
}
263+
}
264+
265+
spec {
266+
description = "simple project"
267+
source_repos = ["*"]
268+
269+
destination {
270+
name = "anothercluster"
271+
namespace = "bar"
272+
}
273+
}
274+
}
275+
`, name)
276+
}
277+
278+
func testAccArgoCDProjectSimpleWithEmptyOrphaned(name string) string {
279+
return fmt.Sprintf(`
280+
resource "argocd_project" "simple" {
281+
metadata {
282+
name = "%s"
283+
namespace = "argocd"
284+
labels = {
285+
acceptance = "true"
286+
}
287+
annotations = {
288+
"this.is.a.really.long.nested.key" = "yes, really!"
289+
}
290+
}
291+
292+
spec {
293+
description = "simple project"
294+
source_repos = ["*"]
295+
296+
destination {
297+
name = "anothercluster"
298+
namespace = "bar"
299+
}
300+
orphaned_resources { }
301+
}
302+
}
303+
`, name)
304+
}
305+
232306
func testAccArgoCDProjectCoexistenceWithTokenResource(name string, count int) string {
233307
return fmt.Sprintf(`
234308
resource "argocd_project" "coexistence" {

argocd/structure_project.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argocd
33
import (
44
"encoding/json"
55
"fmt"
6+
67
application "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -63,16 +64,19 @@ func expandProjectSpec(d *schema.ResourceData) (
6364
}
6465
}
6566
if v, ok := s["orphaned_resources"]; ok {
66-
spec.OrphanedResources = &application.OrphanedResourcesMonitorSettings{}
6767
orphanedResources := v.([]interface{})
6868
if len(orphanedResources) > 0 {
69-
if _warn, _ok := orphanedResources[0].(map[string]interface{})["warn"]; _ok {
70-
warn := _warn.(bool)
71-
spec.OrphanedResources.Warn = &warn
72-
}
73-
if _ignore, _ok := orphanedResources[0].(map[string]interface{})["ignore"]; _ok {
74-
ignore := expandOrphanedResourcesIgnore(_ignore.(*schema.Set))
75-
spec.OrphanedResources.Ignore = ignore
69+
spec.OrphanedResources = &application.OrphanedResourcesMonitorSettings{}
70+
71+
if orphanedResources[0] != nil {
72+
if _warn, _ok := orphanedResources[0].(map[string]interface{})["warn"]; _ok {
73+
warn := _warn.(bool)
74+
spec.OrphanedResources.Warn = &warn
75+
}
76+
if _ignore, _ok := orphanedResources[0].(map[string]interface{})["ignore"]; _ok {
77+
ignore := expandOrphanedResourcesIgnore(_ignore.(*schema.Set))
78+
spec.OrphanedResources.Ignore = ignore
79+
}
7680
}
7781
}
7882
}

0 commit comments

Comments
 (0)