-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds argocd_repository resource (#17)
* support for pre-1.6.0 repository service api client
- Loading branch information
Showing
8 changed files
with
407 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package argocd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/argoproj/argo-cd/pkg/apiclient/repository" | ||
application "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"strings" | ||
) | ||
|
||
func resourceArgoCDRepository() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArgoCDRepositoryCreate, | ||
Read: resourceArgoCDRepositoryRead, | ||
Update: resourceArgoCDRepositoryUpdate, | ||
Delete: resourceArgoCDRepositoryDelete, | ||
// TODO: add importer acceptance tests | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: repositorySchema(), | ||
} | ||
} | ||
|
||
func resourceArgoCDRepositoryCreate(d *schema.ResourceData, meta interface{}) error { | ||
server := meta.(ServerInterface) | ||
c := server.RepositoryClient | ||
repo := expandRepository(d) | ||
r, err := c.CreateRepository( | ||
context.Background(), | ||
&repository.RepoCreateRequest{ | ||
Repo: repo, | ||
Upsert: false, | ||
CredsOnly: false, | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if r.ConnectionState.Status == application.ConnectionStatusFailed { | ||
return fmt.Errorf( | ||
"could not connect to repository %s: %s", | ||
repo.Repo, | ||
r.ConnectionState.Message, | ||
) | ||
} | ||
d.SetId(r.Repo) | ||
return resourceArgoCDRepositoryRead(d, meta) | ||
} | ||
|
||
func resourceArgoCDRepositoryRead(d *schema.ResourceData, meta interface{}) error { | ||
server := meta.(ServerInterface) | ||
c := server.RepositoryClient | ||
r := &application.Repository{} | ||
|
||
featureRepositoryGetSupported, err := server.isFeatureSupported(featureRepositoryGet) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
switch featureRepositoryGetSupported { | ||
case true: | ||
r, err = c.Get(context.Background(), &repository.RepoQuery{ | ||
Repo: d.Id(), | ||
ForceRefresh: false, | ||
}) | ||
if err != nil { | ||
switch strings.Contains(err.Error(), "NotFound") { | ||
// Repository has already been deleted in an out-of-band fashion | ||
case true: | ||
d.SetId("") | ||
return nil | ||
default: | ||
return err | ||
} | ||
} | ||
case false: | ||
rl, err := c.ListRepositories(context.Background(), &repository.RepoQuery{ | ||
Repo: d.Id(), | ||
ForceRefresh: false, | ||
}) | ||
if err != nil { | ||
// TODO: check for NotFound condition? | ||
return err | ||
} | ||
if rl == nil { | ||
// Repository has already been deleted in an out-of-band fashion | ||
d.SetId("") | ||
return nil | ||
} | ||
for i, _r := range rl.Items { | ||
if _r.Repo == d.Id() { | ||
r = _r | ||
break | ||
} | ||
// Repository has already been deleted in an out-of-band fashion | ||
if i == len(rl.Items)-1 { | ||
d.SetId("") | ||
return nil | ||
} | ||
} | ||
|
||
} | ||
return flattenRepository(r, d) | ||
} | ||
|
||
func resourceArgoCDRepositoryUpdate(d *schema.ResourceData, meta interface{}) error { | ||
server := meta.(ServerInterface) | ||
c := server.RepositoryClient | ||
repo := expandRepository(d) | ||
r, err := c.UpdateRepository( | ||
context.Background(), | ||
&repository.RepoUpdateRequest{Repo: repo}, | ||
) | ||
if err != nil { | ||
switch strings.Contains(err.Error(), "NotFound") { | ||
// Repository has already been deleted in an out-of-band fashion | ||
case true: | ||
d.SetId("") | ||
return nil | ||
default: | ||
return err | ||
} | ||
} | ||
if r.ConnectionState.Status == application.ConnectionStatusFailed { | ||
return fmt.Errorf( | ||
"could not connect to repository %s: %s", | ||
repo.Repo, | ||
r.ConnectionState.Message, | ||
) | ||
} | ||
d.SetId(r.Repo) | ||
return resourceArgoCDRepositoryRead(d, meta) | ||
} | ||
|
||
func resourceArgoCDRepositoryDelete(d *schema.ResourceData, meta interface{}) error { | ||
server := meta.(ServerInterface) | ||
c := server.RepositoryClient | ||
_, err := c.DeleteRepository( | ||
context.Background(), | ||
&repository.RepoQuery{Repo: d.Id()}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId("") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package argocd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"testing" | ||
) | ||
|
||
func TestAccArgoCDRepository(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccArgoCDRepositorySimple(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"argocd_repository.simple", | ||
"connection_state_status", | ||
"Successful", | ||
), | ||
), | ||
}, | ||
{ | ||
Config: testAccArgoCDRepositoryHelm(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"argocd_repository.helm", | ||
"connection_state_status", | ||
"Successful", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccArgoCDRepositorySimple() string { | ||
return fmt.Sprintf(` | ||
resource "argocd_repository" "simple" { | ||
repo = "https://github.com/kubernetes-sigs/kustomize" | ||
} | ||
`) | ||
} | ||
|
||
func testAccArgoCDRepositoryHelm() string { | ||
return fmt.Sprintf(` | ||
resource "argocd_repository" "helm" { | ||
repo = "https://helm.nginx.com/stable" | ||
name = "nginx-stable" | ||
type = "helm" | ||
} | ||
`) | ||
} |
Oops, something went wrong.