Skip to content

Commit

Permalink
chore: garm login to interface
Browse files Browse the repository at this point in the history
for possible future test cases, the login request towards garm is now
also made in the corresponding interface. This will keep the code more
straigt and also makes possible test-cases easier to implement.

Signed-off-by: Mario Constanti <mario.constanti@mercedes-benz.com>
  • Loading branch information
bavarianbidi committed Sep 18, 2023
1 parent cc7363c commit a8cc6ae
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 27 deletions.
7 changes: 4 additions & 3 deletions internal/controller/enterprise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func (r *EnterpriseReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

scope, err := garmClient.NewEnterpriseClient(garmClient.GarmScopeParams{
enterpriseClient := garmClient.NewEnterpriseClient()
err = enterpriseClient.Login(garmClient.GarmScopeParams{
BaseURL: r.BaseURL,
Username: r.Username,
Password: r.Password,
Expand All @@ -74,10 +75,10 @@ func (r *EnterpriseReconciler) Reconcile(ctx context.Context, req ctrl.Request)

// Handle deleted enterprises
if !enterprise.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, scope, enterprise)
return r.reconcileDelete(ctx, enterpriseClient, enterprise)
}

return r.reconcileNormal(ctx, scope, enterprise)
return r.reconcileNormal(ctx, enterpriseClient, enterprise)
}

func (r *EnterpriseReconciler) reconcileNormal(ctx context.Context, client garmClient.EnterpriseClient, enterprise *garmoperatorv1alpha1.Enterprise) (ctrl.Result, error) {
Expand Down
7 changes: 4 additions & 3 deletions internal/controller/organization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func (r *OrganizationReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, nil
}

scope, err := garmClient.NewOrganizationClient(garmClient.GarmScopeParams{
organizationClient := garmClient.NewOrganizationClient()
err = organizationClient.Login(garmClient.GarmScopeParams{
BaseURL: r.BaseURL,
Username: r.Username,
Password: r.Password,
Expand All @@ -73,10 +74,10 @@ func (r *OrganizationReconciler) Reconcile(ctx context.Context, req ctrl.Request

// Handle deleted organizations
if !organization.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, scope, organization)
return r.reconcileDelete(ctx, organizationClient, organization)
}

return r.reconcileNormal(ctx, scope, organization)
return r.reconcileNormal(ctx, organizationClient, organization)
}

func (r *OrganizationReconciler) reconcileNormal(ctx context.Context, client garmClient.OrganizationClient, organization *garmoperatorv1alpha1.Organization) (ctrl.Result, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/pool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (r *PoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
return ctrl.Result{}, nil
}

var poolClient garmClient.PoolClient
poolClient := garmClient.NewPoolClient()
err := poolClient.Login(garmClient.GarmScopeParams{
BaseURL: r.BaseURL,
Username: r.Username,
Expand Down
7 changes: 4 additions & 3 deletions internal/controller/repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

scope, err := garmClient.NewRepositoryClient(garmClient.GarmScopeParams{
repositoryClient := garmClient.NewRepositoryClient()
err = repositoryClient.Login(garmClient.GarmScopeParams{
BaseURL: r.BaseURL,
Username: r.Username,
Password: r.Password,
Expand All @@ -73,10 +74,10 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request)

// Handle deleted repositories
if !repository.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, scope, repository)
return r.reconcileDelete(ctx, repositoryClient, repository)
}

return r.reconcileNormal(ctx, scope, repository)
return r.reconcileNormal(ctx, repositoryClient, repository)
}

func (r *RepositoryReconciler) reconcileNormal(ctx context.Context, client garmClient.RepositoryClient, repository *garmoperatorv1alpha1.Repository) (ctrl.Result, error) {
Expand Down
13 changes: 10 additions & 3 deletions pkg/client/enterprise.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

type EnterpriseClient interface {
Login(garmParams GarmScopeParams) error
ListEnterprises(param *enterprises.ListEnterprisesParams) (*enterprises.ListEnterprisesOK, error)
CreateEnterprise(param *enterprises.CreateEnterpriseParams) (*enterprises.CreateEnterpriseOK, error)
GetEnterprise(param *enterprises.GetEnterpriseParams) (*enterprises.GetEnterpriseOK, error)
Expand All @@ -23,13 +24,19 @@ type enterpriseClient struct {
token runtime.ClientAuthInfoWriter
}

func NewEnterpriseClient(garmParams GarmScopeParams) (EnterpriseClient, error) {
func NewEnterpriseClient() EnterpriseClient {
return &enterpriseClient{}
}

func (s *enterpriseClient) Login(garmParams GarmScopeParams) error {
garmClient, token, err := newGarmClient(garmParams)
if err != nil {
return nil, err
return err
}
s.client = garmClient
s.token = token

return &enterpriseClient{garmClient, token}, nil
return nil
}

func (s *enterpriseClient) ListEnterprises(param *enterprises.ListEnterprisesParams) (*enterprises.ListEnterprisesOK, error) {
Expand Down
15 changes: 15 additions & 0 deletions pkg/client/mock/enterprise.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/client/mock/organization.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/client/mock/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions pkg/client/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

type OrganizationClient interface {
Login(garmParams GarmScopeParams) error
ListOrganizations(param *organizations.ListOrgsParams) (*organizations.ListOrgsOK, error)
CreateOrganization(param *organizations.CreateOrgParams) (*organizations.CreateOrgOK, error)
GetOrganization(param *organizations.GetOrgParams) (*organizations.GetOrgOK, error)
Expand All @@ -23,13 +24,19 @@ type organizationClient struct {
token runtime.ClientAuthInfoWriter
}

func NewOrganizationClient(garmParams GarmScopeParams) (OrganizationClient, error) {
func NewOrganizationClient() OrganizationClient {
return &organizationClient{}
}

func (s *organizationClient) Login(garmParams GarmScopeParams) error {
garmClient, token, err := newGarmClient(garmParams)
if err != nil {
return nil, err
return err
}
s.client = garmClient
s.token = token

return &organizationClient{garmClient, token}, nil
return nil
}

func (s *organizationClient) ListOrganizations(param *organizations.ListOrgsParams) (*organizations.ListOrgsOK, error) {
Expand Down
11 changes: 3 additions & 8 deletions pkg/client/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,9 @@ type poolClient struct {
token runtime.ClientAuthInfoWriter
}

// func NewPoolClient(garmParams GarmScopeParams) (PoolClient, error) {
// garmClient, token, err := newGarmClient(garmParams)
// if err != nil {
// return nil, err
// }

// return &poolClient{garmClient, token}, nil
// }
func NewPoolClient() PoolClient {
return &poolClient{}
}

func (p *poolClient) Login(garmParams GarmScopeParams) error {
garmClient, token, err := newGarmClient(garmParams)
Expand Down
13 changes: 10 additions & 3 deletions pkg/client/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

type RepositoryClient interface {
Login(garmParams GarmScopeParams) error
ListRepositories(param *repositories.ListReposParams) (*repositories.ListReposOK, error)
CreateRepository(param *repositories.CreateRepoParams) (*repositories.CreateRepoOK, error)
GetRepository(param *repositories.GetRepoParams) (*repositories.GetRepoOK, error)
Expand All @@ -23,13 +24,19 @@ type repositoryClient struct {
token runtime.ClientAuthInfoWriter
}

func NewRepositoryClient(garmParams GarmScopeParams) (RepositoryClient, error) {
func NewRepositoryClient() RepositoryClient {
return &repositoryClient{}
}

func (s *repositoryClient) Login(garmParams GarmScopeParams) error {
garmClient, token, err := newGarmClient(garmParams)
if err != nil {
return nil, err
return err
}
s.client = garmClient
s.token = token

return &repositoryClient{garmClient, token}, nil
return nil
}

func (s *repositoryClient) ListRepositories(param *repositories.ListReposParams) (*repositories.ListReposOK, error) {
Expand Down

0 comments on commit a8cc6ae

Please sign in to comment.