@@ -68,37 +68,6 @@ func resourceArgoCDProjectCreate(ctx context.Context, d *schema.ResourceData, me
6868
6969 projectName := objectMeta .Name
7070
71- if _ , ok := tokenMutexProjectMap [projectName ]; ! ok {
72- tokenMutexProjectMap [projectName ] = & sync.RWMutex {}
73- }
74-
75- tokenMutexProjectMap [projectName ].RLock ()
76- p , err := si .ProjectClient .Get (ctx , & projectClient.ProjectQuery {
77- Name : projectName ,
78- })
79- tokenMutexProjectMap [projectName ].RUnlock ()
80-
81- if err != nil {
82- if ! strings .Contains (err .Error (), "NotFound" ) {
83- return []diag.Diagnostic {
84- {
85- Severity : diag .Error ,
86- Summary : fmt .Sprintf ("Project %s could not be created" , projectName ),
87- Detail : err .Error (),
88- },
89- }
90- }
91- }
92-
93- if p != nil {
94- switch p .DeletionTimestamp {
95- case nil :
96- default :
97- // Pre-existing project is still in Kubernetes soft deletion queue
98- time .Sleep (time .Duration (* p .DeletionGracePeriodSeconds ))
99- }
100- }
101-
10271 featureProjectSourceNamespacesSupported , err := si .isFeatureSupported (featureProjectSourceNamespaces )
10372 if err != nil {
10473 return []diag.Diagnostic {
@@ -122,7 +91,34 @@ func resourceArgoCDProjectCreate(ctx context.Context, d *schema.ResourceData, me
12291 }
12392 }
12493
94+ if _ , ok := tokenMutexProjectMap [projectName ]; ! ok {
95+ tokenMutexProjectMap [projectName ] = & sync.RWMutex {}
96+ }
97+
12598 tokenMutexProjectMap [projectName ].Lock ()
99+
100+ p , err := si .ProjectClient .Get (ctx , & projectClient.ProjectQuery {
101+ Name : projectName ,
102+ })
103+ if err != nil && ! strings .Contains (err .Error (), "NotFound" ) {
104+ tokenMutexProjectMap [projectName ].Unlock ()
105+
106+ return []diag.Diagnostic {
107+ {
108+ Severity : diag .Error ,
109+ Summary : fmt .Sprintf ("failed to get project %s" , projectName ),
110+ Detail : err .Error (),
111+ },
112+ }
113+ } else if p != nil {
114+ switch p .DeletionTimestamp {
115+ case nil :
116+ default :
117+ // Pre-existing project is still in Kubernetes soft deletion queue
118+ time .Sleep (time .Duration (* p .DeletionGracePeriodSeconds ))
119+ }
120+ }
121+
126122 p , err = si .ProjectClient .Create (ctx , & projectClient.ProjectCreateRequest {
127123 Project : & application.AppProject {
128124 ObjectMeta : objectMeta ,
@@ -132,6 +128,7 @@ func resourceArgoCDProjectCreate(ctx context.Context, d *schema.ResourceData, me
132128 // TODO: make that a resource flag with proper acceptance tests
133129 Upsert : false ,
134130 })
131+
135132 tokenMutexProjectMap [projectName ].Unlock ()
136133
137134 if err != nil {
@@ -238,6 +235,29 @@ func resourceArgoCDProjectUpdate(ctx context.Context, d *schema.ResourceData, me
238235
239236 projectName := objectMeta .Name
240237
238+ featureProjectSourceNamespacesSupported , err := si .isFeatureSupported (featureProjectSourceNamespaces )
239+ if err != nil {
240+ return []diag.Diagnostic {
241+ {
242+ Severity : diag .Error ,
243+ Summary : "feature not supported" ,
244+ Detail : err .Error (),
245+ },
246+ }
247+ } else if ! featureProjectSourceNamespacesSupported {
248+ _ , sourceNamespacesOk := d .GetOk ("spec.0.source_namespaces" )
249+ if sourceNamespacesOk {
250+ return []diag.Diagnostic {
251+ {
252+ Severity : diag .Error ,
253+ Summary : fmt .Sprintf (
254+ "project source_namespaces is only supported from ArgoCD %s onwards" ,
255+ featureVersionConstraintsMap [featureProjectSourceNamespaces ].String ()),
256+ },
257+ }
258+ }
259+ }
260+
241261 if _ , ok := tokenMutexProjectMap [projectName ]; ! ok {
242262 tokenMutexProjectMap [projectName ] = & sync.RWMutex {}
243263 }
@@ -249,23 +269,22 @@ func resourceArgoCDProjectUpdate(ctx context.Context, d *schema.ResourceData, me
249269 },
250270 }
251271
252- tokenMutexProjectMap [projectName ].RLock ()
272+ tokenMutexProjectMap [projectName ].Lock ()
273+
253274 p , err := si .ProjectClient .Get (ctx , & projectClient.ProjectQuery {
254275 Name : d .Id (),
255276 })
256- tokenMutexProjectMap [projectName ].RUnlock ()
257-
258277 if err != nil {
278+ tokenMutexProjectMap [projectName ].Unlock ()
279+
259280 return []diag.Diagnostic {
260281 {
261282 Severity : diag .Error ,
262- Summary : "failed to get project" ,
283+ Summary : fmt . Sprintf ( "failed to get existing project %s" , projectName ) ,
263284 Detail : err .Error (),
264285 },
265286 }
266- }
267-
268- if p != nil {
287+ } else if p != nil {
269288 // Kubernetes API requires providing the up-to-date correct ResourceVersion for updates
270289 projectRequest .Project .ResourceVersion = p .ResourceVersion
271290
@@ -282,6 +301,8 @@ func resourceArgoCDProjectUpdate(ctx context.Context, d *schema.ResourceData, me
282301 // i == -1 means the role does not exist
283302 // and was recently added within Terraform tf files
284303 if i != - 1 {
304+ tokenMutexProjectMap [projectName ].Unlock ()
305+
285306 return []diag.Diagnostic {
286307 {
287308 Severity : diag .Error ,
@@ -296,8 +317,8 @@ func resourceArgoCDProjectUpdate(ctx context.Context, d *schema.ResourceData, me
296317 }
297318 }
298319
299- tokenMutexProjectMap [projectName ].Lock ()
300320 _ , err = si .ProjectClient .Update (ctx , projectRequest )
321+
301322 tokenMutexProjectMap [projectName ].Unlock ()
302323
303324 if err != nil {
0 commit comments