Skip to content

Commit 52e9c9e

Browse files
authored
Adds argocd_repository resource (#17)
* support for pre-1.6.0 repository service api client
1 parent 874105c commit 52e9c9e

File tree

8 files changed

+407
-19
lines changed

8 files changed

+407
-19
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ provider "argocd" {
5454
auth_token = "1234..." # env ARGOCD_AUTH_TOKEN
5555
# username = "admin" # env ARGOCD_AUTH_USERNAME
5656
# password = "foo" # env ARGOCD_AUTH_PASSWORD
57-
insecure = false # env ARGOCD_INSECURE
57+
insecure = false # env ARGOCD_INSECURE
58+
}
59+
60+
resource "argocd_repository" "nginx_helm" {
61+
repo = "https://helm.nginx.com/stable"
62+
name = "nginx-stable"
63+
type = "helm"
5864
}
5965
6066
resource "argocd_project" "myproject" {
@@ -157,15 +163,13 @@ resource "argocd_application" "kustomize" {
157163
path = "examples/helloWorld"
158164
target_revision = "master"
159165
kustomize {
160-
name_prefix = "foo-"
161-
name_suffix = "-bar"
162-
images = [
163-
"hashicorp/terraform:light",
164-
]
165-
common_labels = {
166-
"this.is.a.common" = "la-bel"
167-
"another.io/one" = "true"
168-
}
166+
name_prefix = "foo-"
167+
name_suffix = "-bar"
168+
images = ["hashicorp/terraform:light"]
169+
common_labels = {
170+
"this.is.a.common" = "la-bel"
171+
"another.io/one" = "true"
172+
}
169173
}
170174
}
171175

argocd/features.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ import (
66
"github.com/argoproj/argo-cd/pkg/apiclient"
77
"github.com/argoproj/argo-cd/pkg/apiclient/application"
88
"github.com/argoproj/argo-cd/pkg/apiclient/project"
9+
"github.com/argoproj/argo-cd/pkg/apiclient/repository"
910
"github.com/argoproj/argo-cd/pkg/apiclient/version"
1011
)
1112

1213
const (
1314
featureApplicationLevelSyncOptions = iota
15+
featureRepositoryGet
1416
featureTokenIDs
1517
)
1618

1719
var (
1820
featureVersionConstraintsMap = map[int]*semver.Version{
1921
featureApplicationLevelSyncOptions: semver.MustParse("1.5.0"),
22+
featureRepositoryGet: semver.MustParse("1.6.0"),
2023
featureTokenIDs: semver.MustParse("1.5.3"),
2124
}
2225
)
@@ -25,6 +28,7 @@ type ServerInterface struct {
2528
ApiClient apiclient.Client
2629
ApplicationClient application.ApplicationServiceClient
2730
ProjectClient project.ProjectServiceClient
31+
RepositoryClient repository.RepositoryServiceClient
2832
ServerVersion *semver.Version
2933
ServerVersionMessage *version.VersionMessage
3034
}

argocd/provider.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/argoproj/argo-cd/pkg/apiclient"
88
"github.com/argoproj/argo-cd/pkg/apiclient/application"
99
"github.com/argoproj/argo-cd/pkg/apiclient/project"
10+
"github.com/argoproj/argo-cd/pkg/apiclient/repository"
1011
"github.com/argoproj/argo-cd/pkg/apiclient/session"
1112
util "github.com/argoproj/gitops-engine/pkg/utils/io"
1213
"github.com/golang/protobuf/ptypes/empty"
@@ -103,6 +104,7 @@ func Provider(doneCh chan bool) terraform.ResourceProvider {
103104
"argocd_application": resourceArgoCDApplication(),
104105
"argocd_project": resourceArgoCDProject(),
105106
"argocd_project_token": resourceArgoCDProjectToken(),
107+
"argocd_repository": resourceArgoCDRepository(),
106108
},
107109
ConfigureFunc: func(d *schema.ResourceData) (interface{}, error) {
108110
apiClient, err := initApiClient(d)
@@ -118,13 +120,25 @@ func Provider(doneCh chan bool) terraform.ResourceProvider {
118120
if err != nil {
119121
return nil, err
120122
}
123+
124+
rcCloser, repositoryClient, err := apiClient.NewRepoClient()
125+
if err != nil {
126+
return nil, err
127+
}
128+
121129
// Clients connection pooling, close when the provider execution ends
122130
go func(done chan bool) {
123131
<-done
124132
util.Close(pcCloser)
125133
util.Close(acCloser)
134+
util.Close(rcCloser)
126135
}(doneCh)
127-
return initServerInterface(apiClient, projectClient, applicationClient)
136+
return initServerInterface(
137+
apiClient,
138+
projectClient,
139+
applicationClient,
140+
repositoryClient,
141+
)
128142
},
129143
}
130144
}
@@ -133,6 +147,7 @@ func initServerInterface(
133147
apiClient apiclient.Client,
134148
projectClient project.ProjectServiceClient,
135149
applicationClient application.ApplicationServiceClient,
150+
repositoryClient repository.RepositoryServiceClient,
136151
) (interface{}, error) {
137152
acCloser, versionClient, err := apiClient.NewVersionClient()
138153
if err != nil {
@@ -156,6 +171,7 @@ func initServerInterface(
156171
apiClient,
157172
applicationClient,
158173
projectClient,
174+
repositoryClient,
159175
serverVersion,
160176
serverVersionMessage}, err
161177
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package argocd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/argoproj/argo-cd/pkg/apiclient/repository"
7+
application "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
"strings"
10+
)
11+
12+
func resourceArgoCDRepository() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceArgoCDRepositoryCreate,
15+
Read: resourceArgoCDRepositoryRead,
16+
Update: resourceArgoCDRepositoryUpdate,
17+
Delete: resourceArgoCDRepositoryDelete,
18+
// TODO: add importer acceptance tests
19+
Importer: &schema.ResourceImporter{
20+
State: schema.ImportStatePassthrough,
21+
},
22+
Schema: repositorySchema(),
23+
}
24+
}
25+
26+
func resourceArgoCDRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
27+
server := meta.(ServerInterface)
28+
c := server.RepositoryClient
29+
repo := expandRepository(d)
30+
r, err := c.CreateRepository(
31+
context.Background(),
32+
&repository.RepoCreateRequest{
33+
Repo: repo,
34+
Upsert: false,
35+
CredsOnly: false,
36+
},
37+
)
38+
if err != nil {
39+
return err
40+
}
41+
if r.ConnectionState.Status == application.ConnectionStatusFailed {
42+
return fmt.Errorf(
43+
"could not connect to repository %s: %s",
44+
repo.Repo,
45+
r.ConnectionState.Message,
46+
)
47+
}
48+
d.SetId(r.Repo)
49+
return resourceArgoCDRepositoryRead(d, meta)
50+
}
51+
52+
func resourceArgoCDRepositoryRead(d *schema.ResourceData, meta interface{}) error {
53+
server := meta.(ServerInterface)
54+
c := server.RepositoryClient
55+
r := &application.Repository{}
56+
57+
featureRepositoryGetSupported, err := server.isFeatureSupported(featureRepositoryGet)
58+
if err != nil {
59+
panic(err)
60+
}
61+
62+
switch featureRepositoryGetSupported {
63+
case true:
64+
r, err = c.Get(context.Background(), &repository.RepoQuery{
65+
Repo: d.Id(),
66+
ForceRefresh: false,
67+
})
68+
if err != nil {
69+
switch strings.Contains(err.Error(), "NotFound") {
70+
// Repository has already been deleted in an out-of-band fashion
71+
case true:
72+
d.SetId("")
73+
return nil
74+
default:
75+
return err
76+
}
77+
}
78+
case false:
79+
rl, err := c.ListRepositories(context.Background(), &repository.RepoQuery{
80+
Repo: d.Id(),
81+
ForceRefresh: false,
82+
})
83+
if err != nil {
84+
// TODO: check for NotFound condition?
85+
return err
86+
}
87+
if rl == nil {
88+
// Repository has already been deleted in an out-of-band fashion
89+
d.SetId("")
90+
return nil
91+
}
92+
for i, _r := range rl.Items {
93+
if _r.Repo == d.Id() {
94+
r = _r
95+
break
96+
}
97+
// Repository has already been deleted in an out-of-band fashion
98+
if i == len(rl.Items)-1 {
99+
d.SetId("")
100+
return nil
101+
}
102+
}
103+
104+
}
105+
return flattenRepository(r, d)
106+
}
107+
108+
func resourceArgoCDRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
109+
server := meta.(ServerInterface)
110+
c := server.RepositoryClient
111+
repo := expandRepository(d)
112+
r, err := c.UpdateRepository(
113+
context.Background(),
114+
&repository.RepoUpdateRequest{Repo: repo},
115+
)
116+
if err != nil {
117+
switch strings.Contains(err.Error(), "NotFound") {
118+
// Repository has already been deleted in an out-of-band fashion
119+
case true:
120+
d.SetId("")
121+
return nil
122+
default:
123+
return err
124+
}
125+
}
126+
if r.ConnectionState.Status == application.ConnectionStatusFailed {
127+
return fmt.Errorf(
128+
"could not connect to repository %s: %s",
129+
repo.Repo,
130+
r.ConnectionState.Message,
131+
)
132+
}
133+
d.SetId(r.Repo)
134+
return resourceArgoCDRepositoryRead(d, meta)
135+
}
136+
137+
func resourceArgoCDRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
138+
server := meta.(ServerInterface)
139+
c := server.RepositoryClient
140+
_, err := c.DeleteRepository(
141+
context.Background(),
142+
&repository.RepoQuery{Repo: d.Id()},
143+
)
144+
if err != nil {
145+
return err
146+
}
147+
d.SetId("")
148+
return nil
149+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package argocd
2+
3+
import (
4+
"fmt"
5+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
6+
"testing"
7+
)
8+
9+
func TestAccArgoCDRepository(t *testing.T) {
10+
resource.ParallelTest(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
Providers: testAccProviders,
13+
Steps: []resource.TestStep{
14+
{
15+
Config: testAccArgoCDRepositorySimple(),
16+
Check: resource.ComposeTestCheckFunc(
17+
resource.TestCheckResourceAttr(
18+
"argocd_repository.simple",
19+
"connection_state_status",
20+
"Successful",
21+
),
22+
),
23+
},
24+
{
25+
Config: testAccArgoCDRepositoryHelm(),
26+
Check: resource.ComposeTestCheckFunc(
27+
resource.TestCheckResourceAttr(
28+
"argocd_repository.helm",
29+
"connection_state_status",
30+
"Successful",
31+
),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
func testAccArgoCDRepositorySimple() string {
39+
return fmt.Sprintf(`
40+
resource "argocd_repository" "simple" {
41+
repo = "https://github.com/kubernetes-sigs/kustomize"
42+
}
43+
`)
44+
}
45+
46+
func testAccArgoCDRepositoryHelm() string {
47+
return fmt.Sprintf(`
48+
resource "argocd_repository" "helm" {
49+
repo = "https://helm.nginx.com/stable"
50+
name = "nginx-stable"
51+
type = "helm"
52+
}
53+
`)
54+
}

0 commit comments

Comments
 (0)