@@ -43,6 +43,9 @@ func resourceArgoCDClusterCreate(ctx context.Context, d *schema.ResourceData, me
43
43
44
44
// Cluster are unique by "server address" so we should check there is no existing cluster with this address before
45
45
existingClusters , err := si .ClusterClient .List (ctx , & clusterClient.ClusterQuery {
46
+ // Starting argo-cd server v2.8.0 filtering on list api endpoint is fixed, else it is ignored, see:
47
+ // - https://github.com/oboukili/terraform-provider-argocd/issues/266#issuecomment-1739122022
48
+ // - https://github.com/argoproj/argo-cd/pull/13363
46
49
Id : & clusterClient.ClusterID {
47
50
Type : "server" ,
48
51
Value : rtrimmedServer ,
@@ -53,6 +56,7 @@ func resourceArgoCDClusterCreate(ctx context.Context, d *schema.ResourceData, me
53
56
return errorToDiagnostics (fmt .Sprintf ("failed to list existing clusters when creating cluster %s" , cluster .Server ), err )
54
57
}
55
58
59
+ // Here we will filter ourselves on the list so that we are backward compatible for argo-cd server with version < v2.8.0 (see coment above)
56
60
if len (existingClusters .Items ) > 0 {
57
61
for _ , existingCluster := range existingClusters .Items {
58
62
if rtrimmedServer == strings .TrimRight (existingCluster .Server , "/" ) {
@@ -103,7 +107,57 @@ func resourceArgoCDClusterRead(ctx context.Context, d *schema.ResourceData, meta
103
107
return nil
104
108
}
105
109
106
- return argoCDAPIError ("read" , "cluster" , d .Id (), err )
110
+ // Fix for https://github.com/argoproj-labs/terraform-provider-argocd/issues/266
111
+ // This fix is added here as a workaround to ensure backward compatibility, as
112
+ // it is triggered only on the specific usecase where the issue happens.
113
+ // Additional remarks about this code:
114
+ // * it is a copy/paste of the code used by resourceArgoCDClusterCreate to check if
115
+ // the cluster already exists (with some obvious changes to return value and mutex type)
116
+ // * it should at term replace the `si.ClusterClient.Get` code for this method
117
+ if strings .Contains (err .Error (), "PermissionDenied" ) {
118
+ cluster , err := expandCluster (d )
119
+ if err != nil {
120
+ return errorToDiagnostics ("failed to expand cluster" , err )
121
+ }
122
+
123
+ tokenMutexClusters .RLock ()
124
+
125
+ rtrimmedServer := strings .TrimRight (cluster .Server , "/" )
126
+
127
+ // Cluster are unique by "server address" so we should check there is no existing cluster with this address before
128
+ existingClusters , err := si .ClusterClient .List (ctx , & clusterClient.ClusterQuery {
129
+ // Starting argo-cd server v2.8.0 filtering on list api endpoint is fixed, else it is ignored, see:
130
+ // - https://github.com/oboukili/terraform-provider-argocd/issues/266#issuecomment-1739122022
131
+ // - https://github.com/argoproj/argo-cd/pull/13363
132
+ Id : & clusterClient.ClusterID {
133
+ Type : "server" ,
134
+ Value : rtrimmedServer ,
135
+ },
136
+ })
137
+
138
+ tokenMutexClusters .RUnlock ()
139
+
140
+ if err != nil {
141
+ return errorToDiagnostics (fmt .Sprintf ("failed to list existing clusters when reading cluster %s" , cluster .Server ), err )
142
+ }
143
+
144
+ // Here we will filter ourselves on the list so that we are backward compatible for argo-cd server with version < v2.8.0 (see coment above)
145
+ if len (existingClusters .Items ) > 0 {
146
+ for _ , existingCluster := range existingClusters .Items {
147
+ if rtrimmedServer == strings .TrimRight (existingCluster .Server , "/" ) {
148
+ // Cluster was found, return
149
+ return nil
150
+ }
151
+ }
152
+ }
153
+
154
+ // Cluster was not found, return with empty Id
155
+ d .SetId ("" )
156
+
157
+ return nil
158
+ } else {
159
+ return argoCDAPIError ("read" , "cluster" , d .Id (), err )
160
+ }
107
161
}
108
162
109
163
if err = flattenCluster (c , d ); err != nil {
0 commit comments