Skip to content

Commit b956ae7

Browse files
committed
fix(repositories): use list api to search for repository)
Signed-off-by: Nathanael Liechti <[email protected]>
1 parent 5e4ae92 commit b956ae7

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

argocd/resource_argocd_repository.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,33 @@ func resourceArgoCDRepositoryRead(ctx context.Context, d *schema.ResourceData, m
8383
}
8484

8585
tokenMutexConfiguration.RLock()
86-
r, err := si.RepositoryClient.Get(ctx, &repository.RepoQuery{
87-
Repo: d.Id(),
88-
AppProject: d.State().Attributes["project"],
89-
ForceRefresh: true,
90-
})
86+
repos, err := si.RepositoryClient.List(ctx, &repository.RepoQuery{})
9187
tokenMutexConfiguration.RUnlock()
9288

93-
if err != nil {
94-
// Repository has already been deleted in an out-of-band fashion
95-
if strings.Contains(err.Error(), "NotFound") {
96-
d.SetId("")
97-
return nil
89+
var finalRepo *application.Repository
90+
91+
if repos != nil {
92+
if len(repos.Items) >= 1 {
93+
for _, repo := range repos.Items {
94+
if repo.Repo == d.Id() {
95+
finalRepo = repo
96+
}
97+
}
9898
}
99+
}
99100

101+
if finalRepo == nil {
102+
// if finalRepo is empty this means that either there no repositories at all or the desired one isn't in the list
103+
// that means we clear it from state and terraform will create it if desired
104+
d.SetId("")
105+
return nil
106+
}
107+
108+
if err != nil {
100109
return argoCDAPIError("read", "repository", d.Id(), err)
101110
}
102111

103-
if err = flattenRepository(r, d); err != nil {
112+
if err = flattenRepository(finalRepo, d); err != nil {
104113
return errorToDiagnostics(fmt.Sprintf("failed to flatten repository %s", d.Id()), err)
105114
}
106115

0 commit comments

Comments
 (0)