Skip to content

Commit 94394f5

Browse files
authored
Merge pull request #226 from MrLuje/feat/project-scoped-repository
feat: add support for add project scoped repositories
2 parents ea23a8a + f9a0c10 commit 94394f5

File tree

8 files changed

+145
-5
lines changed

8 files changed

+145
-5
lines changed

argocd/features.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
featureRepositoryGet
2626
featureTokenIDs
2727
featureProjectScopedClusters
28+
featureProjectScopedRepositories
2829
featureClusterMetadata
2930
featureRepositoryCertificates
3031
featureApplicationHelmSkipCrds
@@ -38,6 +39,7 @@ var featureVersionConstraintsMap = map[int]*semver.Version{
3839
featureRepositoryGet: semver.MustParse("1.6.0"),
3940
featureTokenIDs: semver.MustParse("1.5.3"),
4041
featureProjectScopedClusters: semver.MustParse("2.2.0"),
42+
featureProjectScopedRepositories: semver.MustParse("2.2.0"),
4143
featureClusterMetadata: semver.MustParse("2.2.0"),
4244
featureRepositoryCertificates: semver.MustParse("1.2.0"),
4345
featureApplicationHelmSkipCrds: semver.MustParse("2.3.0"),

argocd/provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ func testAccPreCheckFeatureNotSupported(t *testing.T, feature int) {
7777
t.Fatal("feature constraint is not handled by the provider")
7878
}
7979
if i := versionConstraint.Compare(serverVersion); i != 1 {
80-
t.Skipf("version %s does not support feature", v)
80+
t.Skipf("not running test if feature is already supported (v%s)", v)
8181
}
8282
}

argocd/resource_argocd_repository.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,29 @@ func resourceArgoCDRepositoryCreate(ctx context.Context, d *schema.ResourceData,
4141
c := *server.RepositoryClient
4242
repo := expandRepository(d)
4343

44-
err := resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
44+
featureProjectScopedRepositoriesSupported, err := server.isFeatureSupported(featureProjectScopedRepositories)
45+
if err != nil {
46+
return []diag.Diagnostic{
47+
{
48+
Severity: diag.Error,
49+
Summary: "feature not supported",
50+
Detail: err.Error(),
51+
},
52+
}
53+
}
54+
if !featureProjectScopedRepositoriesSupported && repo.Project != "" {
55+
return []diag.Diagnostic{
56+
{
57+
Severity: diag.Error,
58+
Summary: fmt.Sprintf(
59+
"repository project is only supported from ArgoCD %s onwards",
60+
featureVersionConstraintsMap[featureProjectScopedRepositories].String()),
61+
Detail: "See https://argo-cd.readthedocs.io/en/stable/user-guide/projects/#project-scoped-repositories-and-clusters",
62+
},
63+
}
64+
}
65+
66+
err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
4567
tokenMutexConfiguration.Lock()
4668
r, err := c.CreateRepository(
4769
ctx,
@@ -191,6 +213,28 @@ func resourceArgoCDRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
191213
c := *server.RepositoryClient
192214
repo := expandRepository(d)
193215

216+
featureProjectScopedRepositoriesSupported, err := server.isFeatureSupported(featureProjectScopedRepositories)
217+
if err != nil {
218+
return []diag.Diagnostic{
219+
{
220+
Severity: diag.Error,
221+
Summary: "feature not supported",
222+
Detail: err.Error(),
223+
},
224+
}
225+
}
226+
if !featureProjectScopedRepositoriesSupported && repo.Project != "" {
227+
return []diag.Diagnostic{
228+
{
229+
Severity: diag.Error,
230+
Summary: fmt.Sprintf(
231+
"repository project is only supported from ArgoCD %s onwards",
232+
featureVersionConstraintsMap[featureProjectScopedRepositories].String()),
233+
Detail: "See https://argo-cd.readthedocs.io/en/stable/user-guide/projects/#project-scoped-repositories-and-clusters",
234+
},
235+
}
236+
}
237+
194238
tokenMutexConfiguration.Lock()
195239
r, err := c.UpdateRepository(
196240
ctx,

argocd/resource_argocd_repository_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package argocd
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -81,6 +82,60 @@ func TestAccArgoCDRepository(t *testing.T) {
8182
})
8283
}
8384

85+
func TestAccArgoCDRepositoryScoped(t *testing.T) {
86+
projectName := acctest.RandString(10)
87+
resource.Test(t, resource.TestCase{
88+
PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureSupported(t, featureProjectScopedRepositories) },
89+
ProviderFactories: testAccProviders,
90+
Steps: []resource.TestStep{
91+
{
92+
Config: testAccArgoCDRepositoryHelmProjectScoped(projectName),
93+
Check: resource.ComposeTestCheckFunc(
94+
resource.TestCheckResourceAttr(
95+
"argocd_repository.helm",
96+
"connection_state_status",
97+
"Successful",
98+
),
99+
resource.TestCheckResourceAttr(
100+
"argocd_repository.helm",
101+
"project",
102+
projectName,
103+
),
104+
),
105+
},
106+
},
107+
})
108+
}
109+
110+
func TestAccArgoCDRepositoryScoped_NotSupported_On_OlderVersions(t *testing.T) {
111+
name := acctest.RandomWithPrefix("test-acc-scoped-repo")
112+
113+
resource.ParallelTest(t, resource.TestCase{
114+
PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureNotSupported(t, featureProjectScopedRepositories) },
115+
ProviderFactories: testAccProviders,
116+
Steps: []resource.TestStep{
117+
// Create tests
118+
{
119+
Config: testAccArgoCDRepositoryHelmProjectScoped(name),
120+
ExpectError: regexp.MustCompile("repository project is only supported from ArgoCD"),
121+
},
122+
// Update tests (create repo without project, update it with project)
123+
{
124+
Config: testAccArgoCDRepositoryHelm(),
125+
Check: resource.TestCheckResourceAttr(
126+
"argocd_repository.helm",
127+
"connection_state_status",
128+
"Successful",
129+
),
130+
},
131+
{
132+
Config: testAccArgoCDRepositoryHelmProjectScoped(name),
133+
ExpectError: regexp.MustCompile("repository project is only supported from ArgoCD"),
134+
},
135+
},
136+
})
137+
}
138+
84139
func testAccArgoCDRepositorySimple() string {
85140
return fmt.Sprintf(`
86141
resource "argocd_repository" "simple" {
@@ -99,6 +154,34 @@ resource "argocd_repository" "helm" {
99154
`)
100155
}
101156

157+
func testAccArgoCDRepositoryHelmProjectScoped(project string) string {
158+
return fmt.Sprintf(`
159+
resource "argocd_project" "simple" {
160+
metadata {
161+
name = "%[1]s"
162+
namespace = "argocd"
163+
}
164+
165+
spec {
166+
description = "simple project"
167+
source_repos = ["*"]
168+
169+
destination {
170+
name = "anothercluster"
171+
namespace = "bar"
172+
}
173+
}
174+
}
175+
176+
resource "argocd_repository" "helm" {
177+
repo = "https://helm.nginx.com/stable"
178+
name = "nginx-stable-scoped"
179+
type = "helm"
180+
project = "%[1]s"
181+
}
182+
`, project)
183+
}
184+
102185
func testAccArgoCDRepositoryPublicUsageInApplication(name string) string {
103186
return testAccArgoCDRepositorySimple() + fmt.Sprintf(`
104187
resource "argocd_application" "public" {

argocd/schema_repository.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package argocd
22

33
import (
44
"fmt"
5+
56
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
67
)
78

@@ -33,6 +34,11 @@ func repositorySchema() map[string]*schema.Schema {
3334
Description: "only for Helm repos",
3435
Optional: true,
3536
},
37+
"project": {
38+
Type: schema.TypeString,
39+
Description: "The project name, in case the repository is project scoped",
40+
Optional: true,
41+
},
3642
"username": {
3743
Type: schema.TypeString,
3844
Description: "Username for authenticating at the repo server",
@@ -67,7 +73,7 @@ func repositorySchema() map[string]*schema.Schema {
6773
"enable_oci": {
6874
Type: schema.TypeBool,
6975
Description: "Specify whether the repo server should be viewed as OCI compliant",
70-
Optional: true,
76+
Optional: true,
7177
},
7278
"type": {
7379
Type: schema.TypeString,

argocd/schema_repository_credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func repositoryCredentialsSchema() map[string]*schema.Schema {
4545
"enable_oci": {
4646
Type: schema.TypeBool,
4747
Description: "Specify whether the repo server should be viewed as OCI compliant",
48-
Optional: true,
48+
Optional: true,
4949
},
5050
}
5151
}

argocd/structure_repository.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func expandRepository(d *schema.ResourceData) *application.Repository {
2525
if v, ok := d.GetOk("name"); ok {
2626
repository.Name = v.(string)
2727
}
28+
if v, ok := d.GetOk("project"); ok {
29+
repository.Project = v.(string)
30+
}
2831
if v, ok := d.GetOk("username"); ok {
2932
repository.Username = v.(string)
3033
}
@@ -59,6 +62,7 @@ func flattenRepository(repository *application.Repository, d *schema.ResourceDat
5962
"inherited_creds": repository.InheritedCreds,
6063
"insecure": repository.Insecure,
6164
"name": repository.Name,
65+
"project": repository.Project,
6266
// TODO: in case of repositoryCredentials existence, will perma-diff
6367
//"username": repository.Username,
6468
// TODO: ArgoCD API does not return sensitive data!

docs/resources/repository.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ resource "argocd_repository" "private" {
3535
* `enable_lfs` - (Optional), boolean, whether git-lfs support should be enabled for this repository.
3636
* `username` - (Optional), string, username to authenticate against the repository server.
3737
* `password` - (Optional), string, password to authenticate against the repository server.
38+
* `project` - (Optional), string, if the repository will be project-scoped, the name of that project. Refer to this [doc](https://argo-cd.readthedocs.io/en/stable/user-guide/projects/#project-scoped-repositories-and-clusters). Requires ArgoCD 2.2.0 onwards.
3839
* `ssh_private_key` - (Optional), string, SSH private key data to authenticate against the repository server. **Only for Git repositories**.
3940
* `tls_client_cert_data` - (Optional), TLS client cert data to authenticate against the repository server.
4041
* `tls_client_cert_key` - (Optional), TLS client cert key to authenticate against the repository server.
@@ -52,4 +53,4 @@ ArgoCD repositories can be imported using an id consisting of `{repo}`, e.g.
5253
$ terraform import argocd_repository.myrepo [email protected]:somerepo.git
5354
```
5455

55-
**NOTE**: as ArgoCD API does not return any sensitive information, a subsequent _terraform apply_ should be executed to make the password, ssh_private_key and tls_client_cert_key attributes converge to their expected values defined within the plan.
56+
**NOTE**: as ArgoCD API does not return any sensitive information, a subsequent _terraform apply_ should be executed to make the password, ssh_private_key and tls_client_cert_key attributes converge to their expected values defined within the plan.

0 commit comments

Comments
 (0)