Skip to content

Commit 22c99cd

Browse files
authored
fix: crash when adding a role on an existing project without role (#154)
1 parent 9838250 commit 22c99cd

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

argocd/resource_argocd_project.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ func resourceArgoCDProjectUpdate(ctx context.Context, d *schema.ResourceData, me
248248
},
249249
}
250250
}
251+
} else { // Only preserve preexisting JWTs for managed roles if we found an existing matching project
252+
projectRequest.Project.Spec.Roles[i].JWTTokens = pr.JWTTokens
251253
}
252-
projectRequest.Project.Spec.Roles[i].JWTTokens = pr.JWTTokens
253254
}
254255
}
255256

argocd/resource_argocd_project_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,35 @@ func TestAccArgoCDProject_tokensCoexistence(t *testing.T) {
114114
})
115115
}
116116

117+
func TestAccArgoCDProjectUpdateAddRole(t *testing.T) {
118+
name := acctest.RandomWithPrefix("test-acc")
119+
120+
resource.Test(t, resource.TestCase{
121+
PreCheck: func() { testAccPreCheck(t) },
122+
ProviderFactories: testAccProviders,
123+
Steps: []resource.TestStep{
124+
{
125+
Config: testAccArgoCDProjectSimpleWithoutRole(name),
126+
Check: resource.ComposeTestCheckFunc(
127+
resource.TestCheckResourceAttrSet(
128+
"argocd_project.simple",
129+
"metadata.0.uid",
130+
),
131+
),
132+
},
133+
{
134+
Config: testAccArgoCDProjectSimpleWithRole(name),
135+
Check: resource.ComposeTestCheckFunc(
136+
resource.TestCheckResourceAttrSet(
137+
"argocd_project.simple",
138+
"metadata.0.uid",
139+
),
140+
),
141+
},
142+
},
143+
})
144+
}
145+
117146
func testAccArgoCDProjectSimple(name string) string {
118147
return fmt.Sprintf(`
119148
resource "argocd_project" "simple" {
@@ -393,3 +422,80 @@ resource "argocd_project" "failure" {
393422
}
394423
`, name, name, name)
395424
}
425+
426+
func testAccArgoCDProjectSimpleWithoutRole(name string) string {
427+
return fmt.Sprintf(`
428+
resource "argocd_project" "simple" {
429+
metadata {
430+
name = "%s"
431+
namespace = "argocd"
432+
labels = {
433+
acceptance = "true"
434+
}
435+
annotations = {
436+
"this.is.a.really.long.nested.key" = "yes, really!"
437+
}
438+
}
439+
440+
spec {
441+
description = "simple project"
442+
source_repos = ["*"]
443+
444+
destination {
445+
name = "anothercluster"
446+
namespace = "bar"
447+
}
448+
orphaned_resources {
449+
warn = true
450+
ignore {
451+
group = "apps/v1"
452+
kind = "Deployment"
453+
name = "ignored1"
454+
}
455+
}
456+
}
457+
}
458+
`, name)
459+
}
460+
461+
func testAccArgoCDProjectSimpleWithRole(name string) string {
462+
return fmt.Sprintf(`
463+
resource "argocd_project" "simple" {
464+
metadata {
465+
name = "%s"
466+
namespace = "argocd"
467+
labels = {
468+
acceptance = "true"
469+
}
470+
annotations = {
471+
"this.is.a.really.long.nested.key" = "yes, really!"
472+
}
473+
}
474+
475+
spec {
476+
description = "simple project"
477+
source_repos = ["*"]
478+
479+
destination {
480+
name = "anothercluster"
481+
namespace = "bar"
482+
}
483+
orphaned_resources {
484+
warn = true
485+
ignore {
486+
group = "apps/v1"
487+
kind = "Deployment"
488+
name = "ignored1"
489+
}
490+
}
491+
role {
492+
name = "anotherrole"
493+
policies = [
494+
"p, proj:%s:anotherrole, applications, get, %s/*, allow",
495+
"p, proj:%s:anotherrole, applications, sync, %s/*, deny",
496+
]
497+
}
498+
}
499+
}
500+
`, name, name, name, name, name)
501+
}

0 commit comments

Comments
 (0)