Skip to content

Commit

Permalink
Shared api service clients across resources (#18)
Browse files Browse the repository at this point in the history
* shared api service clients across resources
* use ArgoCD 1.6.1 for local development acceptance testing
  • Loading branch information
oboukili authored Jul 8, 2020
1 parent 52e9c9e commit c9543e2
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 431 deletions.
8 changes: 4 additions & 4 deletions argocd/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ var (
)

type ServerInterface struct {
ApiClient apiclient.Client
ApplicationClient application.ApplicationServiceClient
ProjectClient project.ProjectServiceClient
RepositoryClient repository.RepositoryServiceClient
ApiClient *apiclient.Client
ApplicationClient *application.ApplicationServiceClient
ProjectClient *project.ProjectServiceClient
RepositoryClient *repository.RepositoryServiceClient
ServerVersion *semver.Version
ServerVersionMessage *version.VersionMessage
}
Expand Down
8 changes: 4 additions & 4 deletions argocd/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ func initServerInterface(
}

return ServerInterface{
apiClient,
applicationClient,
projectClient,
repositoryClient,
&apiClient,
&applicationClient,
&projectClient,
&repositoryClient,
serverVersion,
serverVersionMessage}, err
}
Expand Down
8 changes: 4 additions & 4 deletions argocd/resource_argocd_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func resourceArgoCDApplicationCreate(d *schema.ResourceData, meta interface{}) e
return err
}
server := meta.(ServerInterface)
c := server.ApplicationClient
c := *server.ApplicationClient
app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &objectMeta.Name,
})
Expand Down Expand Up @@ -83,7 +83,7 @@ func resourceArgoCDApplicationCreate(d *schema.ResourceData, meta interface{}) e

func resourceArgoCDApplicationRead(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := server.ApplicationClient
c := *server.ApplicationClient
appName := d.Id()
app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &appName,
Expand Down Expand Up @@ -111,7 +111,7 @@ func resourceArgoCDApplicationUpdate(d *schema.ResourceData, meta interface{}) e
return err
}
server := meta.(ServerInterface)
c := server.ApplicationClient
c := *server.ApplicationClient
appRequest := &applicationClient.ApplicationUpdateRequest{
Application: &application.Application{
ObjectMeta: objectMeta,
Expand Down Expand Up @@ -147,7 +147,7 @@ func resourceArgoCDApplicationUpdate(d *schema.ResourceData, meta interface{}) e

func resourceArgoCDApplicationDelete(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := server.ApplicationClient
c := *server.ApplicationClient
appName := d.Id()
_, err := c.Delete(context.Background(), &applicationClient.ApplicationDeleteRequest{Name: &appName})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions argocd/resource_argocd_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func resourceArgoCDProjectCreate(d *schema.ResourceData, meta interface{}) error
return err
}
server := meta.(ServerInterface)
c := server.ProjectClient
c := *server.ProjectClient
p, err := c.Get(context.Background(), &projectClient.ProjectQuery{
Name: objectMeta.Name,
})
Expand Down Expand Up @@ -73,7 +73,7 @@ func resourceArgoCDProjectCreate(d *schema.ResourceData, meta interface{}) error

func resourceArgoCDProjectRead(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := server.ProjectClient
c := *server.ProjectClient
p, err := c.Get(context.Background(), &projectClient.ProjectQuery{
Name: d.Id(),
})
Expand All @@ -97,7 +97,7 @@ func resourceArgoCDProjectUpdate(d *schema.ResourceData, meta interface{}) error
return err
}
server := meta.(ServerInterface)
c := server.ProjectClient
c := *server.ProjectClient
projectRequest := &projectClient.ProjectUpdateRequest{
Project: &application.AppProject{
ObjectMeta: objectMeta,
Expand Down Expand Up @@ -133,7 +133,7 @@ func resourceArgoCDProjectUpdate(d *schema.ResourceData, meta interface{}) error

func resourceArgoCDProjectDelete(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := server.ProjectClient
c := *server.ProjectClient
_, err := c.Delete(context.Background(), &projectClient.ProjectQuery{Name: d.Id()})
if err != nil {
return err
Expand Down
50 changes: 25 additions & 25 deletions argocd/resource_argocd_project_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
argoCDProject "github.com/argoproj/argo-cd/pkg/apiclient/project"
"github.com/argoproj/argo-cd/pkg/apiclient/project"
application "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/cristalhq/jwt/v3"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -72,19 +72,19 @@ func resourceArgoCDProjectToken() *schema.Resource {

func resourceArgoCDProjectTokenCreate(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := server.ProjectClient
c := *server.ProjectClient
var claims jwt.StandardClaims
var expiresIn int64

project := d.Get("project").(string)
p := d.Get("project").(string)
role := d.Get("role").(string)
opts := &argoCDProject.ProjectTokenCreateRequest{
Project: project,
opts := &project.ProjectTokenCreateRequest{
Project: p,
Role: role,
}

if _, ok := tokenMutexProjectMap[project]; !ok {
tokenMutexProjectMap[project] = &sync.RWMutex{}
if _, ok := tokenMutexProjectMap[p]; !ok {
tokenMutexProjectMap[p] = &sync.RWMutex{}
}
if d, ok := d.GetOk("description"); ok {
opts.Description = d.(string)
Expand Down Expand Up @@ -120,14 +120,14 @@ func resourceArgoCDProjectTokenCreate(d *schema.ResourceData, meta interface{})
return err
}

tokenMutexProjectMap[project].Lock()
tokenMutexProjectMap[p].Lock()
resp, err := c.CreateToken(context.Background(), opts)
// ensure issuedAt is unique upon multiple simultaneous resource creation invocations
// as this is the unique ID for old tokens
if !featureTokenIDSupported {
time.Sleep(1 * time.Second)
}
tokenMutexProjectMap[project].Unlock()
tokenMutexProjectMap[p].Unlock()
if err != nil {
return err
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func resourceArgoCDProjectTokenCreate(d *schema.ResourceData, meta interface{})
}
d.SetId(claims.ID)
} else {
d.SetId(fmt.Sprintf("%s-%s-%d", project, role, claims.IssuedAt.Unix()))
d.SetId(fmt.Sprintf("%s-%s-%d", p, role, claims.IssuedAt.Unix()))
}
return resourceArgoCDProjectTokenRead(d, meta)
}
Expand All @@ -177,10 +177,10 @@ func resourceArgoCDProjectTokenRead(d *schema.ResourceData, meta interface{}) er
var requestTokenIAT int64 = 0

server := meta.(ServerInterface)
c := server.ProjectClient
c := *server.ProjectClient

// Delete token from state if project has been deleted in an out-of-band fashion
project, err := c.Get(context.Background(), &argoCDProject.ProjectQuery{
p, err := c.Get(context.Background(), &project.ProjectQuery{
Name: d.Get("project").(string),
})
if err != nil {
Expand All @@ -192,8 +192,8 @@ func resourceArgoCDProjectTokenRead(d *schema.ResourceData, meta interface{}) er
return err
}
}
if _, ok := tokenMutexProjectMap[project.Name]; !ok {
tokenMutexProjectMap[project.Name] = &sync.RWMutex{}
if _, ok := tokenMutexProjectMap[p.Name]; !ok {
tokenMutexProjectMap[p.Name] = &sync.RWMutex{}
}
featureTokenIDSupported, err := server.isFeatureSupported(featureTokenIDs)
if err != nil {
Expand All @@ -214,13 +214,13 @@ func resourceArgoCDProjectTokenRead(d *schema.ResourceData, meta interface{}) er
}
}

tokenMutexProjectMap[project.Name].RLock()
token, _, err = project.GetJWTToken(
tokenMutexProjectMap[p.Name].RLock()
token, _, err = p.GetJWTToken(
d.Get("role").(string),
requestTokenIAT,
requestTokenID,
)
tokenMutexProjectMap[project.Name].RUnlock()
tokenMutexProjectMap[p.Name].RUnlock()
if err != nil {
// Token has been deleted in an out-of-band fashion
d.SetId("")
Expand Down Expand Up @@ -269,17 +269,17 @@ func resourceArgoCDProjectTokenUpdate(d *schema.ResourceData, meta interface{})

func resourceArgoCDProjectTokenDelete(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := server.ProjectClient
c := *server.ProjectClient

project := d.Get("project").(string)
p := d.Get("project").(string)
role := d.Get("role").(string)
opts := &argoCDProject.ProjectTokenDeleteRequest{
Project: project,
opts := &project.ProjectTokenDeleteRequest{
Project: p,
Role: role,
}

if _, ok := tokenMutexProjectMap[project]; !ok {
tokenMutexProjectMap[project] = &sync.RWMutex{}
if _, ok := tokenMutexProjectMap[p]; !ok {
tokenMutexProjectMap[p] = &sync.RWMutex{}
}

featureTokenIDSupported, err := server.isFeatureSupported(featureTokenIDs)
Expand All @@ -298,11 +298,11 @@ func resourceArgoCDProjectTokenDelete(d *schema.ResourceData, meta interface{})
}
}

tokenMutexProjectMap[project].Lock()
tokenMutexProjectMap[p].Lock()
if _, err := c.DeleteToken(context.Background(), opts); err != nil {
return err
}
tokenMutexProjectMap[project].Unlock()
tokenMutexProjectMap[p].Unlock()
d.SetId("")
return nil
}
8 changes: 4 additions & 4 deletions argocd/resource_argocd_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func resourceArgoCDRepository() *schema.Resource {

func resourceArgoCDRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := server.RepositoryClient
c := *server.RepositoryClient
repo := expandRepository(d)
r, err := c.CreateRepository(
context.Background(),
Expand All @@ -51,7 +51,7 @@ func resourceArgoCDRepositoryCreate(d *schema.ResourceData, meta interface{}) er

func resourceArgoCDRepositoryRead(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := server.RepositoryClient
c := *server.RepositoryClient
r := &application.Repository{}

featureRepositoryGetSupported, err := server.isFeatureSupported(featureRepositoryGet)
Expand Down Expand Up @@ -107,7 +107,7 @@ func resourceArgoCDRepositoryRead(d *schema.ResourceData, meta interface{}) erro

func resourceArgoCDRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := server.RepositoryClient
c := *server.RepositoryClient
repo := expandRepository(d)
r, err := c.UpdateRepository(
context.Background(),
Expand Down Expand Up @@ -136,7 +136,7 @@ func resourceArgoCDRepositoryUpdate(d *schema.ResourceData, meta interface{}) er

func resourceArgoCDRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := server.RepositoryClient
c := *server.RepositoryClient
_, err := c.DeleteRepository(
context.Background(),
&repository.RepoQuery{Repo: d.Id()},
Expand Down
4 changes: 2 additions & 2 deletions argocd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package argocd
import (
"fmt"
"github.com/argoproj/argo-cd/pkg/apiclient"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
application "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
util "github.com/argoproj/gitops-engine/pkg/utils/io"
"regexp"
"strconv"
Expand Down Expand Up @@ -108,7 +108,7 @@ func validatePolicy(project string, role string, policy string) error {
return nil
}

func isValidToken(token *v1alpha1.JWTToken, expiresIn int64) error {
func isValidToken(token *application.JWTToken, expiresIn int64) error {
// Check token expiry
if expiresIn > 0 && token.ExpiresAt < time.Now().Unix() {
return fmt.Errorf("token has expired")
Expand Down
Loading

0 comments on commit c9543e2

Please sign in to comment.