From a8cc6ae3763ad3a326abb4cf6c354966cc661b1a Mon Sep 17 00:00:00 2001 From: Mario Constanti Date: Mon, 18 Sep 2023 11:56:51 +0200 Subject: [PATCH] chore: garm login to interface 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 --- internal/controller/enterprise_controller.go | 7 ++++--- internal/controller/organization_controller.go | 7 ++++--- internal/controller/pool_controller.go | 2 +- internal/controller/repository_controller.go | 7 ++++--- pkg/client/enterprise.go | 13 ++++++++++--- pkg/client/mock/enterprise.go | 15 +++++++++++++++ pkg/client/mock/organization.go | 15 +++++++++++++++ pkg/client/mock/repository.go | 15 +++++++++++++++ pkg/client/organization.go | 13 ++++++++++--- pkg/client/pool.go | 11 +++-------- pkg/client/repository.go | 13 ++++++++++--- 11 files changed, 91 insertions(+), 27 deletions(-) diff --git a/internal/controller/enterprise_controller.go b/internal/controller/enterprise_controller.go index 616ecc4c..a4a16f59 100644 --- a/internal/controller/enterprise_controller.go +++ b/internal/controller/enterprise_controller.go @@ -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, @@ -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) { diff --git a/internal/controller/organization_controller.go b/internal/controller/organization_controller.go index a7490478..2c28eeee 100644 --- a/internal/controller/organization_controller.go +++ b/internal/controller/organization_controller.go @@ -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, @@ -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) { diff --git a/internal/controller/pool_controller.go b/internal/controller/pool_controller.go index cccee122..06258957 100644 --- a/internal/controller/pool_controller.go +++ b/internal/controller/pool_controller.go @@ -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, diff --git a/internal/controller/repository_controller.go b/internal/controller/repository_controller.go index d61bfa70..9cfb1fe5 100644 --- a/internal/controller/repository_controller.go +++ b/internal/controller/repository_controller.go @@ -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, @@ -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) { diff --git a/pkg/client/enterprise.go b/pkg/client/enterprise.go index fce2d58a..bbbd183b 100644 --- a/pkg/client/enterprise.go +++ b/pkg/client/enterprise.go @@ -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) @@ -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) { diff --git a/pkg/client/mock/enterprise.go b/pkg/client/mock/enterprise.go index c0cebbcb..c9a1c477 100644 --- a/pkg/client/mock/enterprise.go +++ b/pkg/client/mock/enterprise.go @@ -9,6 +9,7 @@ import ( reflect "reflect" enterprises "github.com/cloudbase/garm/client/enterprises" + client "github.com/mercedes-benz/garm-operator/pkg/client" gomock "go.uber.org/mock/gomock" ) @@ -94,6 +95,20 @@ func (mr *MockEnterpriseClientMockRecorder) ListEnterprises(param interface{}) * return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListEnterprises", reflect.TypeOf((*MockEnterpriseClient)(nil).ListEnterprises), param) } +// Login mocks base method. +func (m *MockEnterpriseClient) Login(garmParams client.GarmScopeParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Login", garmParams) + ret0, _ := ret[0].(error) + return ret0 +} + +// Login indicates an expected call of Login. +func (mr *MockEnterpriseClientMockRecorder) Login(garmParams interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Login", reflect.TypeOf((*MockEnterpriseClient)(nil).Login), garmParams) +} + // UpdateEnterprise mocks base method. func (m *MockEnterpriseClient) UpdateEnterprise(param *enterprises.UpdateEnterpriseParams) (*enterprises.UpdateEnterpriseOK, error) { m.ctrl.T.Helper() diff --git a/pkg/client/mock/organization.go b/pkg/client/mock/organization.go index 1a2c1be7..8997d4e8 100644 --- a/pkg/client/mock/organization.go +++ b/pkg/client/mock/organization.go @@ -9,6 +9,7 @@ import ( reflect "reflect" organizations "github.com/cloudbase/garm/client/organizations" + client "github.com/mercedes-benz/garm-operator/pkg/client" gomock "go.uber.org/mock/gomock" ) @@ -94,6 +95,20 @@ func (mr *MockOrganizationClientMockRecorder) ListOrganizations(param interface{ return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListOrganizations", reflect.TypeOf((*MockOrganizationClient)(nil).ListOrganizations), param) } +// Login mocks base method. +func (m *MockOrganizationClient) Login(garmParams client.GarmScopeParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Login", garmParams) + ret0, _ := ret[0].(error) + return ret0 +} + +// Login indicates an expected call of Login. +func (mr *MockOrganizationClientMockRecorder) Login(garmParams interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Login", reflect.TypeOf((*MockOrganizationClient)(nil).Login), garmParams) +} + // UpdateOrganization mocks base method. func (m *MockOrganizationClient) UpdateOrganization(param *organizations.UpdateOrgParams) (*organizations.UpdateOrgOK, error) { m.ctrl.T.Helper() diff --git a/pkg/client/mock/repository.go b/pkg/client/mock/repository.go index e5a435df..dc5225ef 100644 --- a/pkg/client/mock/repository.go +++ b/pkg/client/mock/repository.go @@ -9,6 +9,7 @@ import ( reflect "reflect" repositories "github.com/cloudbase/garm/client/repositories" + client "github.com/mercedes-benz/garm-operator/pkg/client" gomock "go.uber.org/mock/gomock" ) @@ -94,6 +95,20 @@ func (mr *MockRepositoryClientMockRecorder) ListRepositories(param interface{}) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRepositories", reflect.TypeOf((*MockRepositoryClient)(nil).ListRepositories), param) } +// Login mocks base method. +func (m *MockRepositoryClient) Login(garmParams client.GarmScopeParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Login", garmParams) + ret0, _ := ret[0].(error) + return ret0 +} + +// Login indicates an expected call of Login. +func (mr *MockRepositoryClientMockRecorder) Login(garmParams interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Login", reflect.TypeOf((*MockRepositoryClient)(nil).Login), garmParams) +} + // UpdateRepository mocks base method. func (m *MockRepositoryClient) UpdateRepository(param *repositories.UpdateRepoParams) (*repositories.UpdateRepoOK, error) { m.ctrl.T.Helper() diff --git a/pkg/client/organization.go b/pkg/client/organization.go index 485c2df3..07656104 100644 --- a/pkg/client/organization.go +++ b/pkg/client/organization.go @@ -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) @@ -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) { diff --git a/pkg/client/pool.go b/pkg/client/pool.go index 6e31fc97..fd9d18f7 100644 --- a/pkg/client/pool.go +++ b/pkg/client/pool.go @@ -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) diff --git a/pkg/client/repository.go b/pkg/client/repository.go index a3badbca..deada08f 100644 --- a/pkg/client/repository.go +++ b/pkg/client/repository.go @@ -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) @@ -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) {