From 5feb0f99f106ae026de4a64181802ede7c001442 Mon Sep 17 00:00:00 2001 From: dhaus67 Date: Tue, 17 Jan 2023 19:44:42 +0100 Subject: [PATCH] Revert "ROX-12344: Add organisation name to central request" (#736) Revert "ROX-12344: Add organisation name to central request (#683)" This reverts commit ab36d4f7662dee1ff9418c2fa6c798d6f58977ea. --- Makefile | 2 +- .../pkg/api/dbapi/central_request_types.go | 7 +- .../dinosaur/pkg/api/private/api/openapi.yaml | 2 - .../model_managed_central_all_of_spec_auth.go | 1 - .../dinosaur/pkg/handlers/cloud_accounts.go | 4 +- .../pkg/handlers/cloud_accounts_test.go | 14 +- .../202212150000_add_organisation_name.go | 128 ------------------ .../dinosaur/pkg/migrations/migrations.go | 34 ++--- .../dinosaur/pkg/presenters/managedcentral.go | 1 - internal/dinosaur/pkg/services/dinosaur.go | 18 +-- .../pkg/services/quota/ams_quota_service.go | 12 +- .../services/quota/ams_quota_service_test.go | 119 +++++++--------- openapi/fleet-manager-private.yaml | 2 - pkg/client/ocm/client.go | 116 +--------------- pkg/client/ocm/client_moq.go | 100 +++++++------- pkg/errors/errors.go | 11 -- templates/service-template.yml | 10 -- 17 files changed, 139 insertions(+), 442 deletions(-) delete mode 100644 internal/dinosaur/pkg/migrations/202212150000_add_organisation_name.go diff --git a/Makefile b/Makefile index eba0d3b533..92e97f3281 100644 --- a/Makefile +++ b/Makefile @@ -481,7 +481,7 @@ db/start: .PHONY: db/start db/migrate: - $(GO) run ./cmd/fleet-manager migrate + OCM_ENV=integration $(GO) run ./cmd/fleet-manager migrate .PHONY: db/migrate db/teardown: diff --git a/internal/dinosaur/pkg/api/dbapi/central_request_types.go b/internal/dinosaur/pkg/api/dbapi/central_request_types.go index 74292e2b63..f9943f5a85 100644 --- a/internal/dinosaur/pkg/api/dbapi/central_request_types.go +++ b/internal/dinosaur/pkg/api/dbapi/central_request_types.go @@ -33,10 +33,9 @@ type CentralRequest struct { OwnerUserID string `json:"owner_user_id"` // Instance-independent part of the Central's hostname. For example, this // can be `rhacs-dev.com`, `acs-stage.rhcloud.com`, etc. - Host string `json:"host"` - OrganisationID string `json:"organisation_id" gorm:"index"` - OrganisationName string `json:"organisation_name"` - FailedReason string `json:"failed_reason"` + Host string `json:"host"` + OrganisationID string `json:"organisation_id" gorm:"index"` + FailedReason string `json:"failed_reason"` // PlacementID field should be updated every time when a CentralRequest is assigned to an OSD cluster (even if it's the same one again) PlacementID string `json:"placement_id"` Central api.JSON `json:"central"` // Schema is defined by dbapi.CentralSpec diff --git a/internal/dinosaur/pkg/api/private/api/openapi.yaml b/internal/dinosaur/pkg/api/private/api/openapi.yaml index ed8164ae2e..5cf2060e39 100644 --- a/internal/dinosaur/pkg/api/private/api/openapi.yaml +++ b/internal/dinosaur/pkg/api/private/api/openapi.yaml @@ -437,8 +437,6 @@ components: type: string ownerOrgId: type: string - ownerOrgName: - type: string issuer: type: string ManagedCentral_allOf_spec_uiEndpoint_tls: diff --git a/internal/dinosaur/pkg/api/private/model_managed_central_all_of_spec_auth.go b/internal/dinosaur/pkg/api/private/model_managed_central_all_of_spec_auth.go index 4604762138..16a7f4aeea 100644 --- a/internal/dinosaur/pkg/api/private/model_managed_central_all_of_spec_auth.go +++ b/internal/dinosaur/pkg/api/private/model_managed_central_all_of_spec_auth.go @@ -17,6 +17,5 @@ type ManagedCentralAllOfSpecAuth struct { ClientOrigin string `json:"clientOrigin,omitempty"` OwnerUserId string `json:"ownerUserId,omitempty"` OwnerOrgId string `json:"ownerOrgId,omitempty"` - OwnerOrgName string `json:"ownerOrgName,omitempty"` Issuer string `json:"issuer,omitempty"` } diff --git a/internal/dinosaur/pkg/handlers/cloud_accounts.go b/internal/dinosaur/pkg/handlers/cloud_accounts.go index 05a33d4826..0424ddb726 100644 --- a/internal/dinosaur/pkg/handlers/cloud_accounts.go +++ b/internal/dinosaur/pkg/handlers/cloud_accounts.go @@ -42,12 +42,12 @@ func (h *cloudAccountsHandler) actionFunc(r *http.Request) func() (i interface{} if err != nil { return nil, errors.NewWithCause(errors.ErrorForbidden, err, "cannot make request without orgID claim") } - organization, err := h.client.GetOrganisationFromExternalID(orgID) + organizationID, err := h.client.GetOrganisationIDFromExternalID(orgID) if err != nil { return nil, errors.OrganisationNotFound(orgID, err) } - cloudAccounts, err := h.client.GetCustomerCloudAccounts(organization.ID(), []string{quota.RHACSMarketplaceQuotaID}) + cloudAccounts, err := h.client.GetCustomerCloudAccounts(organizationID, []string{quota.RHACSMarketplaceQuotaID}) if err != nil { return nil, errors.NewWithCause(errors.ErrorGeneral, err, "failed to fetch cloud accounts from AMS") } diff --git a/internal/dinosaur/pkg/handlers/cloud_accounts_test.go b/internal/dinosaur/pkg/handlers/cloud_accounts_test.go index 9c02d0b786..f2504f9f3e 100644 --- a/internal/dinosaur/pkg/handlers/cloud_accounts_test.go +++ b/internal/dinosaur/pkg/handlers/cloud_accounts_test.go @@ -31,9 +31,8 @@ func TestGetSuccess(t *testing.T) { Build() assert.NoError(t, err) c := ocm.ClientMock{ - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID("external-id").Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalID string) (string, error) { + return "external-id", nil }, GetCustomerCloudAccountsFunc: func(externalID string, quotaID []string) ([]*v1.CloudAccount, error) { return []*v1.CloudAccount{ @@ -66,10 +65,9 @@ func TestGetSuccess(t *testing.T) { func TestGetNoOrgId(t *testing.T) { timesClientCalled := 0 c := ocm.ClientMock{ - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { + GetOrganisationIDFromExternalIDFunc: func(externalID string) (string, error) { timesClientCalled++ - org, _ := v1.NewOrganization().ID("external-id").Build() - return org, nil + return "external-id", nil }, GetCustomerCloudAccountsFunc: func(externalID string, quotaID []string) ([]*v1.CloudAccount, error) { timesClientCalled++ @@ -102,8 +100,8 @@ func TestGetNoOrgId(t *testing.T) { func TestGetCannotGetOrganizationID(t *testing.T) { timesClientCalled := 0 c := ocm.ClientMock{ - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - return nil, errors.New("test failure") + GetOrganisationIDFromExternalIDFunc: func(externalID string) (string, error) { + return "", errors.New("test failure") }, GetCustomerCloudAccountsFunc: func(externalID string, quotaID []string) ([]*v1.CloudAccount, error) { timesClientCalled++ diff --git a/internal/dinosaur/pkg/migrations/202212150000_add_organisation_name.go b/internal/dinosaur/pkg/migrations/202212150000_add_organisation_name.go deleted file mode 100644 index 4d92925da8..0000000000 --- a/internal/dinosaur/pkg/migrations/202212150000_add_organisation_name.go +++ /dev/null @@ -1,128 +0,0 @@ -package migrations - -import ( - "time" - - "github.com/go-gormigrate/gormigrate/v2" - "github.com/pkg/errors" - "github.com/stackrox/acs-fleet-manager/pkg/api" - "github.com/stackrox/acs-fleet-manager/pkg/client/ocm" - "gorm.io/gorm" -) - -func newAMSClient(ocmConfig *ocm.OCMConfig) (ocm.Client, error) { - err := ocmConfig.ReadFiles() - if err != nil { - return nil, errors.Wrap(err, "reading OCM config files") - } - if !ocmConfig.EnableMock { - conn, _, err := ocm.NewOCMConnection(ocmConfig, ocmConfig.AmsURL) - if err != nil { - return nil, errors.Wrap(err, "creating OCM connection") - } - return ocm.NewClient(conn), nil - } - return nil, nil -} - -func fetchOrgName(amsClient ocm.Client, orgID string) (string, error) { - // Leave org name empty if client is not set. - if amsClient == nil { - return "", nil - } - org, err := amsClient.GetOrganisationFromExternalID(orgID) - return org.Name(), err -} - -func addOrganisationNameToCentralRequest(ocmConfig *ocm.OCMConfig) *gormigrate.Migration { - type AuthConfig struct { - ClientID string `json:"idp_client_id"` - ClientSecret string `json:"idp_client_secret"` - Issuer string `json:"idp_issuer"` - ClientOrigin string `json:"client_origin"` - } - - type CentralRequest struct { - api.Meta - Region string `json:"region"` - ClusterID string `json:"cluster_id" gorm:"index"` - CloudProvider string `json:"cloud_provider"` - CloudAccountID string `json:"cloud_account_id"` - MultiAZ bool `json:"multi_az"` - Name string `json:"name" gorm:"index"` - Status string `json:"status" gorm:"index"` - SubscriptionID string `json:"subscription_id"` - Owner string `json:"owner" gorm:"index"` - OwnerAccountID string `json:"owner_account_id"` - OwnerUserID string `json:"owner_user_id"` - Host string `json:"host"` - OrganisationID string `json:"organisation_id" gorm:"index"` - OrganisationName string `json:"organisation_name"` - FailedReason string `json:"failed_reason"` - PlacementID string `json:"placement_id"` - Central api.JSON `json:"central"` - Scanner api.JSON `json:"scanner"` - - DesiredCentralVersion string `json:"desired_central_version"` - ActualCentralVersion string `json:"actual_central_version"` - DesiredCentralOperatorVersion string `json:"desired_central_operator_version"` - ActualCentralOperatorVersion string `json:"actual_central_operator_version"` - CentralUpgrading bool `json:"central_upgrading"` - CentralOperatorUpgrading bool `json:"central_operator_upgrading"` - InstanceType string `json:"instance_type"` - QuotaType string `json:"quota_type"` - Routes api.JSON `json:"routes"` - RoutesCreated bool `json:"routes_created"` - Namespace string `json:"namespace"` - RoutesCreationID string `json:"routes_creation_id"` - DeletionTimestamp *time.Time `json:"deletionTimestamp"` - AuthConfig - } - - id := "202212150000" - return &gormigrate.Migration{ - ID: id, - Migrate: func(tx *gorm.DB) error { - if err := tx.Migrator().AddColumn(&CentralRequest{}, "OrganisationName"); err != nil { - return errors.Wrapf(err, "adding column OrganisationName in migration %s", id) - } - - amsClient, err := newAMSClient(ocmConfig) - if err != nil { - return errors.Wrapf(err, "creating AMS client in migration %s", id) - } - - rows, err := tx.Model(&CentralRequest{}).Where("organisation_name IS NULL").Rows() - if err != nil { - return errors.Wrapf(err, "fetching rows where organisation_name is NULL in migration %s", id) - } - defer func() { - if err := rows.Close(); err != nil { - panic(errors.Wrapf(err, "closing rows in migration %s", id)) - } - }() - - for rows.Next() { - var central CentralRequest - if err := tx.ScanRows(rows, ¢ral); err != nil { - return errors.Wrapf(err, "scanning rows in migration %s", id) - } - - orgName, err := fetchOrgName(amsClient, central.OrganisationID) - if err != nil { - return errors.Wrapf(err, "fetching organisation_name in migration %s", id) - } - if err = tx.Model(¢ral).Update("organisation_name", orgName).Error; err != nil { - return errors.Wrapf(err, "updating organisation_name in migration %s", id) - } - } - return nil - }, - Rollback: func(tx *gorm.DB) error { - if err := tx.Migrator().DropColumn(&CentralRequest{}, "OrganisationName"); err != nil { - return errors.Wrapf(err, "rolling back column OrganisationName in migration %s", id) - } - return nil - }, - } -} diff --git a/internal/dinosaur/pkg/migrations/migrations.go b/internal/dinosaur/pkg/migrations/migrations.go index 633c4b627d..884392bcbc 100644 --- a/internal/dinosaur/pkg/migrations/migrations.go +++ b/internal/dinosaur/pkg/migrations/migrations.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/go-gormigrate/gormigrate/v2" - "github.com/stackrox/acs-fleet-manager/pkg/client/ocm" "github.com/stackrox/acs-fleet-manager/pkg/db" ) @@ -25,33 +24,30 @@ import ( // // 4. Create one function in a separate file that returns your Migration. Add that single function call // to the end of this list. -func getMigrations(ocmConfig *ocm.OCMConfig) []*gormigrate.Migration { - return []*gormigrate.Migration{ - addCentralRequest(), - addClusters(), - addLeaderLease(), - sampleMigration(), - addOwnerUserIDToCentralRequest(), - addResourcesToCentralRequest(), - addAuthConfigToCentralRequest(), - addCentralAuthLease(), - addSkipSchedulingToClusters(), - addClientOriginToCentralRequest(), - changeCentralClientOrigin(), - addCloudAccountIDToCentralRequest(), - addOrganisationNameToCentralRequest(ocmConfig), - } +var migrations = []*gormigrate.Migration{ + addCentralRequest(), + addClusters(), + addLeaderLease(), + sampleMigration(), + addOwnerUserIDToCentralRequest(), + addResourcesToCentralRequest(), + addAuthConfigToCentralRequest(), + addCentralAuthLease(), + addSkipSchedulingToClusters(), + addClientOriginToCentralRequest(), + changeCentralClientOrigin(), + addCloudAccountIDToCentralRequest(), } // New ... -func New(dbConfig *db.DatabaseConfig, ocmConfig *ocm.OCMConfig) (*db.Migration, func(), error) { - migrations := getMigrations(ocmConfig) +func New(dbConfig *db.DatabaseConfig) (*db.Migration, func(), error) { m, f, err := db.NewMigration(dbConfig, &gormigrate.Options{ TableName: "migrations", IDColumnName: "id", IDColumnSize: 255, UseTransaction: false, }, migrations) + if err != nil { return m, f, fmt.Errorf("assembling database migration: %w", err) } diff --git a/internal/dinosaur/pkg/presenters/managedcentral.go b/internal/dinosaur/pkg/presenters/managedcentral.go index a09bb739fe..fe7720c56d 100644 --- a/internal/dinosaur/pkg/presenters/managedcentral.go +++ b/internal/dinosaur/pkg/presenters/managedcentral.go @@ -69,7 +69,6 @@ func (c *ManagedCentralPresenter) PresentManagedCentral(from *dbapi.CentralReque ClientSecret: from.AuthConfig.ClientSecret, // pragma: allowlist secret ClientOrigin: from.AuthConfig.ClientOrigin, OwnerOrgId: from.OrganisationID, - OwnerOrgName: from.OrganisationName, OwnerUserId: from.OwnerUserID, Issuer: from.AuthConfig.Issuer, }, diff --git a/internal/dinosaur/pkg/services/dinosaur.go b/internal/dinosaur/pkg/services/dinosaur.go index 91cc04c41f..793bc79ce6 100644 --- a/internal/dinosaur/pkg/services/dinosaur.go +++ b/internal/dinosaur/pkg/services/dinosaur.go @@ -22,7 +22,6 @@ import ( "github.com/stackrox/acs-fleet-manager/pkg/api" "github.com/stackrox/acs-fleet-manager/pkg/auth" "github.com/stackrox/acs-fleet-manager/pkg/client/aws" - "github.com/stackrox/acs-fleet-manager/pkg/client/ocm" "github.com/stackrox/acs-fleet-manager/pkg/db" "github.com/stackrox/acs-fleet-manager/pkg/errors" "github.com/stackrox/acs-fleet-manager/pkg/logger" @@ -122,11 +121,10 @@ type dinosaurService struct { authService authorization.Authorization dataplaneClusterConfig *config.DataplaneClusterConfig clusterPlacementStrategy ClusterPlacementStrategy - amsClient ocm.AMSClient } // NewDinosaurService ... -func NewDinosaurService(connectionFactory *db.ConnectionFactory, clusterService ClusterService, iamService sso.IAMService, dinosaurConfig *config.CentralConfig, dataplaneClusterConfig *config.DataplaneClusterConfig, awsConfig *config.AWSConfig, quotaServiceFactory QuotaServiceFactory, awsClientFactory aws.ClientFactory, authorizationService authorization.Authorization, clusterPlacementStrategy ClusterPlacementStrategy, amsClient ocm.AMSClient) *dinosaurService { +func NewDinosaurService(connectionFactory *db.ConnectionFactory, clusterService ClusterService, iamService sso.IAMService, dinosaurConfig *config.CentralConfig, dataplaneClusterConfig *config.DataplaneClusterConfig, awsConfig *config.AWSConfig, quotaServiceFactory QuotaServiceFactory, awsClientFactory aws.ClientFactory, authorizationService authorization.Authorization, clusterPlacementStrategy ClusterPlacementStrategy) *dinosaurService { return &dinosaurService{ connectionFactory: connectionFactory, clusterService: clusterService, @@ -138,7 +136,6 @@ func NewDinosaurService(connectionFactory *db.ConnectionFactory, clusterService authService: authorizationService, dataplaneClusterConfig: dataplaneClusterConfig, clusterPlacementStrategy: clusterPlacementStrategy, - amsClient: amsClient, } } @@ -331,23 +328,12 @@ func (k *dinosaurService) PrepareDinosaurRequest(dinosaurRequest *dbapi.CentralR return nil } - // Obtain organisation name from AMS to store in central request. - org, err := k.amsClient.GetOrganisationFromExternalID(dinosaurRequest.OrganisationID) - if err != nil { - return errors.OrganisationNotFound(dinosaurRequest.OrganisationID, err) - } - orgName := org.Name() - if orgName == "" { - return errors.OrganisationNameInvalid(dinosaurRequest.OrganisationID, orgName) - } - // Update the fields of the CentralRequest record in the database. updatedCentralRequest := &dbapi.CentralRequest{ Meta: api.Meta{ ID: dinosaurRequest.ID, }, - OrganisationName: orgName, - Status: dinosaurConstants.CentralRequestStatusProvisioning.String(), + Status: dinosaurConstants.CentralRequestStatusProvisioning.String(), } if err := k.Update(updatedCentralRequest); err != nil { return errors.NewWithCause(errors.ErrorGeneral, err, "failed to update central request") diff --git a/internal/dinosaur/pkg/services/quota/ams_quota_service.go b/internal/dinosaur/pkg/services/quota/ams_quota_service.go index ca692c6498..8ee97c1746 100644 --- a/internal/dinosaur/pkg/services/quota/ams_quota_service.go +++ b/internal/dinosaur/pkg/services/quota/ams_quota_service.go @@ -38,14 +38,14 @@ var supportedAMSBillingModels = map[string]struct{}{ // CheckIfQuotaIsDefinedForInstanceType ... func (q amsQuotaService) CheckIfQuotaIsDefinedForInstanceType(dinosaur *dbapi.CentralRequest, instanceType types.DinosaurInstanceType) (bool, *errors.ServiceError) { - org, err := q.amsClient.GetOrganisationFromExternalID(dinosaur.OrganisationID) + orgID, err := q.amsClient.GetOrganisationIDFromExternalID(dinosaur.OrganisationID) if err != nil { return false, errors.OrganisationNotFound(dinosaur.OrganisationID, err) } - hasQuota, err := q.hasConfiguredQuotaCost(org.ID(), instanceType.GetQuotaType()) + hasQuota, err := q.hasConfiguredQuotaCost(orgID, instanceType.GetQuotaType()) if err != nil { - return false, errors.NewWithCause(errors.ErrorGeneral, err, fmt.Sprintf("failed to get assigned quota of type %v for organization with external id %v and id %v", instanceType.GetQuotaType(), dinosaur.OrganisationID, org.ID())) + return false, errors.NewWithCause(errors.ErrorGeneral, err, fmt.Sprintf("failed to get assigned quota of type %v for organization with id %v", instanceType.GetQuotaType(), orgID)) } return hasQuota, nil @@ -132,11 +132,11 @@ func (q amsQuotaService) ReserveQuota(dinosaur *dbapi.CentralRequest, instanceTy rr := newBaseQuotaReservedResourceResourceBuilder() - org, err := q.amsClient.GetOrganisationFromExternalID(dinosaur.OrganisationID) + orgID, err := q.amsClient.GetOrganisationIDFromExternalID(dinosaur.OrganisationID) if err != nil { return "", errors.OrganisationNotFound(dinosaur.OrganisationID, err) } - bm, err := q.selectBillingModelFromDinosaurInstanceType(org.ID(), dinosaur.CloudProvider, dinosaur.CloudAccountID, instanceType) + bm, err := q.selectBillingModelFromDinosaurInstanceType(orgID, dinosaur.CloudProvider, dinosaur.CloudAccountID, instanceType) if err != nil { svcErr := errors.ToServiceError(err) return "", errors.NewWithCause(svcErr.Code, svcErr, "Error getting billing model") @@ -145,7 +145,7 @@ func (q amsQuotaService) ReserveQuota(dinosaur *dbapi.CentralRequest, instanceTy glog.Infof("Billing model of Central request %s with quota type %s has been set to %s.", dinosaur.ID, instanceType.GetQuotaType(), bm) if bm != string(amsv1.BillingModelStandard) { - if err := q.verifyCloudAccountInAMS(dinosaur, org.ID()); err != nil { + if err := q.verifyCloudAccountInAMS(dinosaur, orgID); err != nil { return "", err } rr.BillingMarketplaceAccount(dinosaur.CloudAccountID) diff --git a/internal/dinosaur/pkg/services/quota/ams_quota_service_test.go b/internal/dinosaur/pkg/services/quota/ams_quota_service_test.go index 2232749c6e..8772999fbe 100644 --- a/internal/dinosaur/pkg/services/quota/ams_quota_service_test.go +++ b/internal/dinosaur/pkg/services/quota/ams_quota_service_test.go @@ -55,9 +55,8 @@ func Test_AMSCheckQuota(t *testing.T) { cloudAuthorizationResp, _ := v1.NewClusterAuthorizationResponse().Allowed(true).Build() return cloudAuthorizationResp, nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { if product != string(ocm.RHACSProduct) { @@ -95,9 +94,8 @@ func Test_AMSCheckQuota(t *testing.T) { cloudAuthorizationResp, _ := v1.NewClusterAuthorizationResponse().Allowed(false).Build() return cloudAuthorizationResp, nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { if product != string(ocm.RHACSProduct) { @@ -131,9 +129,8 @@ func Test_AMSCheckQuota(t *testing.T) { cloudAuthorizationResp, _ := v1.NewClusterAuthorizationResponse().Allowed(false).Build() return cloudAuthorizationResp, nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { return []*v1.QuotaCost{}, nil @@ -160,9 +157,8 @@ func Test_AMSCheckQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return nil, fmt.Errorf("some errors") }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { if product != string(ocm.RHACSProduct) { @@ -236,9 +232,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSProduct)).ResourceName(resourceName).Cost(1) @@ -266,9 +261,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSProduct)).ResourceName(resourceName).Cost(1) @@ -299,9 +293,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSProduct)).ResourceName(resourceName).Cost(1) @@ -332,9 +325,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSTrialProduct)).ResourceName(resourceName).Cost(0) @@ -362,9 +354,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSProduct)).ResourceName(resourceName).Cost(1) @@ -393,9 +384,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { return []*v1.QuotaCost{}, nil @@ -418,9 +408,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string("unknownbillingmodelone")).Product(string(ocm.RHACSProduct)).ResourceName(resourceName).Cost(1) @@ -450,9 +439,8 @@ func Test_AMSReserveQuota(t *testing.T) { cloudAuthorizationResp, _ := v1.NewClusterAuthorizationResponse().Allowed(false).Build() return cloudAuthorizationResp, nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSProduct)).ResourceName(resourceName).Cost(1) @@ -478,9 +466,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSTrialProduct)).ResourceName(resourceName).Cost(0) @@ -506,9 +493,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSTrialProduct)).ResourceName(resourceName).Cost(0) @@ -541,9 +527,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSTrialProduct)).ResourceName(resourceName).Cost(0) @@ -576,9 +561,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSTrialProduct)).ResourceName(resourceName).Cost(0) @@ -615,9 +599,8 @@ func Test_AMSReserveQuota(t *testing.T) { ClusterAuthorizationFunc: func(cb *v1.ClusterAuthorizationRequest) (*v1.ClusterAuthorizationResponse, error) { return mockClusterAuthorizationResponse(), nil }, - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSTrialProduct)).ResourceName(resourceName).Cost(0) @@ -762,9 +745,8 @@ func Test_amsQuotaService_CheckIfQuotaIsDefinedForInstanceType(t *testing.T) { { name: "returns false if no quota cost exists for the dinosaur's organization", ocmClient: &ocm.ClientMock{ - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { return []*v1.QuotaCost{}, nil @@ -780,9 +762,8 @@ func Test_amsQuotaService_CheckIfQuotaIsDefinedForInstanceType(t *testing.T) { { name: "returns false if the quota cost billing model is not among the supported ones", ocmClient: &ocm.ClientMock{ - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel("unknownbillingmodel").Product(string(ocm.RHACSProduct)).ResourceName(resourceName).Cost(1) @@ -804,9 +785,8 @@ func Test_amsQuotaService_CheckIfQuotaIsDefinedForInstanceType(t *testing.T) { { name: "returns true if there is at least a 'standard' quota cost billing model", ocmClient: &ocm.ClientMock{ - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelStandard)).Product(string(ocm.RHACSProduct)).ResourceName(resourceName).Cost(1) @@ -828,9 +808,8 @@ func Test_amsQuotaService_CheckIfQuotaIsDefinedForInstanceType(t *testing.T) { { name: "returns true if there is at least a 'marketplace' quota cost billing model", ocmClient: &ocm.ClientMock{ - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel("unknownbillingmodel").Product(string(ocm.RHACSProduct)).ResourceName(resourceName).Cost(1) @@ -857,9 +836,8 @@ func Test_amsQuotaService_CheckIfQuotaIsDefinedForInstanceType(t *testing.T) { { name: "returns false if there is no supported billing model with an 'allowed' value greater than 0", ocmClient: &ocm.ClientMock{ - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { rrbq1 := v1.NewRelatedResource().BillingModel(string(v1.BillingModelMarketplace)).Product(string(ocm.RHACSProduct)).ResourceName(resourceName).Cost(1) @@ -885,8 +863,8 @@ func Test_amsQuotaService_CheckIfQuotaIsDefinedForInstanceType(t *testing.T) { { name: "returns an error if it fails retrieving the organization ID", ocmClient: &ocm.ClientMock{ - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - return nil, fmt.Errorf("error getting org") + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return "", fmt.Errorf("error getting org") }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { return []*v1.QuotaCost{}, nil @@ -901,9 +879,8 @@ func Test_amsQuotaService_CheckIfQuotaIsDefinedForInstanceType(t *testing.T) { { name: "returns an error if it fails retrieving quota costs", ocmClient: &ocm.ClientMock{ - GetOrganisationFromExternalIDFunc: func(externalId string) (*v1.Organization, error) { - org, _ := v1.NewOrganization().ID(fmt.Sprintf("fake-org-id-%s", externalId)).Build() - return org, nil + GetOrganisationIDFromExternalIDFunc: func(externalId string) (string, error) { + return fmt.Sprintf("fake-org-id-%s", externalId), nil }, GetQuotaCostsForProductFunc: func(organizationID, resourceName, product string) ([]*v1.QuotaCost, error) { return []*v1.QuotaCost{}, fmt.Errorf("error getting quota costs") diff --git a/openapi/fleet-manager-private.yaml b/openapi/fleet-manager-private.yaml index b837ff48f2..430b927e63 100644 --- a/openapi/fleet-manager-private.yaml +++ b/openapi/fleet-manager-private.yaml @@ -260,8 +260,6 @@ components: type: string ownerOrgId: type: string - ownerOrgName: - type: string issuer: type: string uiEndpoint: diff --git a/pkg/client/ocm/client.go b/pkg/client/ocm/client.go index 8d79472604..1179aa801e 100644 --- a/pkg/client/ocm/client.go +++ b/pkg/client/ocm/client.go @@ -54,7 +54,7 @@ type Client interface { FindSubscriptions(query string) (*amsv1.SubscriptionsListResponse, error) GetRequiresTermsAcceptance(username string) (termsRequired bool, redirectURL string, err error) GetExistingClusterMetrics(clusterID string) (*amsv1.SubscriptionMetrics, error) - GetOrganisationFromExternalID(externalID string) (*amsv1.Organization, error) + GetOrganisationIDFromExternalID(externalID string) (string, error) Connection() *sdkClient.Connection GetQuotaCostsForProduct(organizationID, resourceName, product string) ([]*amsv1.QuotaCost, error) GetCustomerCloudAccounts(organizationID string, quotaIDs []string) ([]*amsv1.CloudAccount, error) @@ -129,10 +129,6 @@ func (c *client) Close() { // CreateCluster ... func (c *client) CreateCluster(cluster *clustersmgmtv1.Cluster) (*clustersmgmtv1.Cluster, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - clusterResource := c.connection.ClustersMgmt().V1().Clusters() response, err := clusterResource.Add().Body(cluster).Send() if err != nil { @@ -145,10 +141,6 @@ func (c *client) CreateCluster(cluster *clustersmgmtv1.Cluster) (*clustersmgmtv1 // GetExistingClusterMetrics ... func (c *client) GetExistingClusterMetrics(clusterID string) (*amsv1.SubscriptionMetrics, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - subscriptions, err := c.connection.AccountsMgmt().V1().Subscriptions().List().Search(fmt.Sprintf("cluster_id='%s'", clusterID)).Send() if err != nil { return nil, fmt.Errorf("retrieving subscriptions: %w", err) @@ -174,32 +166,24 @@ func (c *client) GetExistingClusterMetrics(clusterID string) (*amsv1.Subscriptio return subscriptionsMetrics[0], nil } -// GetOrganisationFromExternalID takes the external org id as input, and returns the OCM org. -func (c *client) GetOrganisationFromExternalID(externalID string) (*amsv1.Organization, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - +// GetOrganisationIDFromExternalID ... +func (c *client) GetOrganisationIDFromExternalID(externalID string) (string, error) { request := c.connection.AccountsMgmt().V1().Organizations().List().Search(fmt.Sprintf("external_id='%s'", externalID)) res, err := request.Send() if err != nil { - return nil, fmt.Errorf("retrieving organizations: %w", err) + return "", fmt.Errorf("retrieving organizations: %w", err) } items := res.Items() if items.Len() < 1 { - return nil, serviceErrors.New(serviceErrors.ErrorNotFound, "organisation with external id '%s' not found", externalID) + return "", serviceErrors.New(serviceErrors.ErrorNotFound, "organisation with external id '%s' not found", externalID) } - return items.Get(0), nil + return items.Get(0).ID(), nil } // GetRequiresTermsAcceptance ... func (c *client) GetRequiresTermsAcceptance(username string) (termsRequired bool, redirectURL string, err error) { - if c.connection == nil { - return false, "", serviceErrors.InvalidOCMConnection() - } - // Check for Appendix 4 Terms request, err := v1.NewTermsReviewRequest().AccountUsername(username).SiteCode(TermsSitecode).EventCode(TermsEventcodeRegister).Build() if err != nil { @@ -222,10 +206,6 @@ func (c *client) GetRequiresTermsAcceptance(username string) (termsRequired bool // GetClusterIngresses sends a GET request to ocm to retrieve the ingresses of an OSD cluster func (c *client) GetClusterIngresses(clusterID string) (*clustersmgmtv1.IngressesListResponse, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - clusterIngresses := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterID).Ingresses() ingressList, err := clusterIngresses.List().Send() if err != nil { @@ -237,10 +217,6 @@ func (c *client) GetClusterIngresses(clusterID string) (*clustersmgmtv1.Ingresse // GetCluster ... func (c client) GetCluster(clusterID string) (*clustersmgmtv1.Cluster, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - resp, err := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterID).Get().Send() if err != nil { return nil, fmt.Errorf("sending get cluster request: %w", err) @@ -250,10 +226,6 @@ func (c client) GetCluster(clusterID string) (*clustersmgmtv1.Cluster, error) { // GetClusterStatus ... func (c client) GetClusterStatus(id string) (*clustersmgmtv1.ClusterStatus, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - resp, err := c.connection.ClustersMgmt().V1().Clusters().Cluster(id).Status().Get().Send() if err != nil { return nil, fmt.Errorf("sending cluster status request: %w", err) @@ -263,10 +235,6 @@ func (c client) GetClusterStatus(id string) (*clustersmgmtv1.ClusterStatus, erro // GetCloudProviders ... func (c *client) GetCloudProviders() (*clustersmgmtv1.CloudProviderList, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - providersCollection := c.connection.ClustersMgmt().V1().CloudProviders() providersResponse, err := providersCollection.List().Send() if err != nil { @@ -278,10 +246,6 @@ func (c *client) GetCloudProviders() (*clustersmgmtv1.CloudProviderList, error) // GetRegions ... func (c *client) GetRegions(provider *clustersmgmtv1.CloudProvider) (*clustersmgmtv1.CloudRegionList, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - regionsCollection := c.connection.ClustersMgmt().V1().CloudProviders().CloudProvider(provider.ID()).Regions() regionsResponse, err := regionsCollection.List().Send() if err != nil { @@ -294,10 +258,6 @@ func (c *client) GetRegions(provider *clustersmgmtv1.CloudProvider) (*clustersmg // CreateAddonWithParams ... func (c client) CreateAddonWithParams(clusterID string, addonID string, params []Parameter) (*clustersmgmtv1.AddOnInstallation, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - addon := clustersmgmtv1.NewAddOn().ID(addonID) addonParameters := newAddonParameterListBuilder(params) addonInstallationBuilder := clustersmgmtv1.NewAddOnInstallation().Addon(addon) @@ -322,10 +282,6 @@ func (c client) CreateAddon(clusterID string, addonID string) (*clustersmgmtv1.A // GetAddon ... func (c client) GetAddon(clusterID string, addonID string) (*clustersmgmtv1.AddOnInstallation, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - resp, err := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterID).Addons().List().Send() if err != nil { return nil, fmt.Errorf("sending AddOnInstallationList request: %w", err) @@ -345,10 +301,6 @@ func (c client) GetAddon(clusterID string, addonID string) (*clustersmgmtv1.AddO // UpdateAddonParameters ... func (c client) UpdateAddonParameters(clusterID string, addonInstallationID string, parameters []Parameter) (*clustersmgmtv1.AddOnInstallation, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - addonInstallationResp, err := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterID).Addons().Addoninstallation(addonInstallationID).Get().Send() if err != nil { return nil, fmt.Errorf("sending AddOnInstallationGet request: %w", err) @@ -402,10 +354,6 @@ func (c *client) GetClusterDNS(clusterID string) (string, error) { // CreateSyncSet ... func (c client) CreateSyncSet(clusterID string, syncset *clustersmgmtv1.Syncset) (*clustersmgmtv1.Syncset, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - clustersResource := c.connection.ClustersMgmt().V1().Clusters() response, syncsetErr := clustersResource.Cluster(clusterID). ExternalConfiguration(). @@ -422,10 +370,6 @@ func (c client) CreateSyncSet(clusterID string, syncset *clustersmgmtv1.Syncset) // UpdateSyncSet ... func (c client) UpdateSyncSet(clusterID string, syncSetID string, syncset *clustersmgmtv1.Syncset) (*clustersmgmtv1.Syncset, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - clustersResource := c.connection.ClustersMgmt().V1().Clusters() response, syncsetErr := clustersResource.Cluster(clusterID). ExternalConfiguration(). @@ -444,10 +388,6 @@ func (c client) UpdateSyncSet(clusterID string, syncSetID string, syncset *clust // CreateIdentityProvider ... func (c client) CreateIdentityProvider(clusterID string, identityProvider *clustersmgmtv1.IdentityProvider) (*clustersmgmtv1.IdentityProvider, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - clustersResource := c.connection.ClustersMgmt().V1().Clusters() response, identityProviderErr := clustersResource.Cluster(clusterID). IdentityProviders(). @@ -463,10 +403,6 @@ func (c client) CreateIdentityProvider(clusterID string, identityProvider *clust // GetIdentityProviderList ... func (c client) GetIdentityProviderList(clusterID string) (*clustersmgmtv1.IdentityProviderList, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - clusterResource := c.connection.ClustersMgmt().V1().Clusters() response, getIDPErr := clusterResource.Cluster(clusterID). IdentityProviders(). @@ -481,10 +417,6 @@ func (c client) GetIdentityProviderList(clusterID string) (*clustersmgmtv1.Ident // GetSyncSet ... func (c client) GetSyncSet(clusterID string, syncSetID string) (*clustersmgmtv1.Syncset, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - clustersResource := c.connection.ClustersMgmt().V1().Clusters() response, syncsetErr := clustersResource.Cluster(clusterID). ExternalConfiguration(). @@ -502,10 +434,6 @@ func (c client) GetSyncSet(clusterID string, syncSetID string) (*clustersmgmtv1. // DeleteSyncSet Status returns the response status code. func (c client) DeleteSyncSet(clusterID string, syncsetID string) (int, error) { - if c.connection == nil { - return 0, serviceErrors.InvalidOCMConnection() - } - clustersResource := c.connection.ClustersMgmt().V1().Clusters() response, syncsetErr := clustersResource.Cluster(clusterID). ExternalConfiguration(). @@ -528,10 +456,6 @@ func (c client) ScaleDownComputeNodes(clusterID string, decrement int) (*cluster // scaleComputeNodes scales the Compute nodes up or down by the value of `numNodes` func (c client) scaleComputeNodes(clusterID string, numNodes int) (*clustersmgmtv1.Cluster, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - clusterClient := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterID) cluster, err := clusterClient.Get().Send() @@ -561,10 +485,6 @@ func (c client) scaleComputeNodes(clusterID string, numNodes int) (*clustersmgmt // SetComputeNodes ... func (c client) SetComputeNodes(clusterID string, numNodes int) (*clustersmgmtv1.Cluster, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - clusterClient := c.connection.ClustersMgmt().V1().Clusters().Cluster(clusterID) patch, err := clustersmgmtv1.NewCluster().Nodes(clustersmgmtv1.NewClusterNodes().Compute(numNodes)). @@ -615,10 +535,6 @@ func sameParameters(parameterList *clustersmgmtv1.AddOnInstallationParameterList // DeleteCluster ... func (c client) DeleteCluster(clusterID string) (int, error) { - if c.connection == nil { - return 0, serviceErrors.InvalidOCMConnection() - } - clustersResource := c.connection.ClustersMgmt().V1().Clusters() response, deleteClusterError := clustersResource.Cluster(clusterID).Delete().Send() @@ -631,10 +547,6 @@ func (c client) DeleteCluster(clusterID string) (int, error) { // ClusterAuthorization ... func (c client) ClusterAuthorization(cb *amsv1.ClusterAuthorizationRequest) (*amsv1.ClusterAuthorizationResponse, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - glog.V(10).Infof("Sending request to OCM '%v'", *cb) r, err := c.connection.AccountsMgmt().V1(). @@ -650,10 +562,6 @@ func (c client) ClusterAuthorization(cb *amsv1.ClusterAuthorizationRequest) (*am // DeleteSubscription ... func (c client) DeleteSubscription(id string) (int, error) { - if c.connection == nil { - return 0, serviceErrors.InvalidOCMConnection() - } - r := c.connection.AccountsMgmt().V1().Subscriptions().Subscription(id).Delete() resp, err := r.Send() return resp.Status(), err @@ -661,10 +569,6 @@ func (c client) DeleteSubscription(id string) (int, error) { // FindSubscriptions ... func (c client) FindSubscriptions(query string) (*amsv1.SubscriptionsListResponse, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - r, err := c.connection.AccountsMgmt().V1().Subscriptions().List().Search(query).Send() if err != nil { return nil, fmt.Errorf("querying the accounts management service for subscriptions: %w", err) @@ -676,10 +580,6 @@ func (c client) FindSubscriptions(query string) (*amsv1.SubscriptionsListRespons // whose relatedResources contains at least a relatedResource that has the // given resourceName and product func (c client) GetQuotaCostsForProduct(organizationID, resourceName, product string) ([]*amsv1.QuotaCost, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - var res []*amsv1.QuotaCost organizationClient := c.connection.AccountsMgmt().V1().Organizations() quotaCostClient := organizationClient.Organization(organizationID).QuotaCost() @@ -703,10 +603,6 @@ func (c client) GetQuotaCostsForProduct(organizationID, resourceName, product st } func (c *client) GetCustomerCloudAccounts(organizationID string, quotaIDs []string) ([]*amsv1.CloudAccount, error) { - if c.connection == nil { - return nil, serviceErrors.InvalidOCMConnection() - } - var res []*amsv1.CloudAccount organizationClient := c.connection.AccountsMgmt().V1().Organizations() quotaCostClient := organizationClient.Organization(organizationID).QuotaCost() diff --git a/pkg/client/ocm/client_moq.go b/pkg/client/ocm/client_moq.go index b0e630331b..98f6be0a7f 100644 --- a/pkg/client/ocm/client_moq.go +++ b/pkg/client/ocm/client_moq.go @@ -80,8 +80,8 @@ var _ Client = &ClientMock{} // GetIdentityProviderListFunc: func(clusterID string) (*clustersmgmtv1.IdentityProviderList, error) { // panic("mock out the GetIdentityProviderList method") // }, -// GetOrganisationFromExternalIDFunc: func(externalID string) (*amsv1.Organization, error) { -// panic("mock out the GetOrganisationFromExternalID method") +// GetOrganisationIDFromExternalIDFunc: func(externalID string) (string, error) { +// panic("mock out the GetOrganisationIDFromExternalID method") // }, // GetQuotaCostsForProductFunc: func(organizationID string, resourceName string, product string) ([]*amsv1.QuotaCost, error) { // panic("mock out the GetQuotaCostsForProduct method") @@ -177,8 +177,8 @@ type ClientMock struct { // GetIdentityProviderListFunc mocks the GetIdentityProviderList method. GetIdentityProviderListFunc func(clusterID string) (*clustersmgmtv1.IdentityProviderList, error) - // GetOrganisationFromExternalIDFunc mocks the GetOrganisationFromExternalID method. - GetOrganisationFromExternalIDFunc func(externalID string) (*amsv1.Organization, error) + // GetOrganisationIDFromExternalIDFunc mocks the GetOrganisationIDFromExternalID method. + GetOrganisationIDFromExternalIDFunc func(externalID string) (string, error) // GetQuotaCostsForProductFunc mocks the GetQuotaCostsForProduct method. GetQuotaCostsForProductFunc func(organizationID string, resourceName string, product string) ([]*amsv1.QuotaCost, error) @@ -321,8 +321,8 @@ type ClientMock struct { // ClusterID is the clusterID argument value. ClusterID string } - // GetOrganisationFromExternalID holds details about calls to the GetOrganisationFromExternalID method. - GetOrganisationFromExternalID []struct { + // GetOrganisationIDFromExternalID holds details about calls to the GetOrganisationIDFromExternalID method. + GetOrganisationIDFromExternalID []struct { // ExternalID is the externalID argument value. ExternalID string } @@ -392,36 +392,36 @@ type ClientMock struct { Syncset *clustersmgmtv1.Syncset } } - lockClusterAuthorization sync.RWMutex - lockConnection sync.RWMutex - lockCreateAddon sync.RWMutex - lockCreateAddonWithParams sync.RWMutex - lockCreateCluster sync.RWMutex - lockCreateIdentityProvider sync.RWMutex - lockCreateSyncSet sync.RWMutex - lockDeleteCluster sync.RWMutex - lockDeleteSubscription sync.RWMutex - lockDeleteSyncSet sync.RWMutex - lockFindSubscriptions sync.RWMutex - lockGetAddon sync.RWMutex - lockGetCloudProviders sync.RWMutex - lockGetCluster sync.RWMutex - lockGetClusterDNS sync.RWMutex - lockGetClusterIngresses sync.RWMutex - lockGetClusterStatus sync.RWMutex - lockGetCustomerCloudAccounts sync.RWMutex - lockGetExistingClusterMetrics sync.RWMutex - lockGetIdentityProviderList sync.RWMutex - lockGetOrganisationFromExternalID sync.RWMutex - lockGetQuotaCostsForProduct sync.RWMutex - lockGetRegions sync.RWMutex - lockGetRequiresTermsAcceptance sync.RWMutex - lockGetSyncSet sync.RWMutex - lockScaleDownComputeNodes sync.RWMutex - lockScaleUpComputeNodes sync.RWMutex - lockSetComputeNodes sync.RWMutex - lockUpdateAddonParameters sync.RWMutex - lockUpdateSyncSet sync.RWMutex + lockClusterAuthorization sync.RWMutex + lockConnection sync.RWMutex + lockCreateAddon sync.RWMutex + lockCreateAddonWithParams sync.RWMutex + lockCreateCluster sync.RWMutex + lockCreateIdentityProvider sync.RWMutex + lockCreateSyncSet sync.RWMutex + lockDeleteCluster sync.RWMutex + lockDeleteSubscription sync.RWMutex + lockDeleteSyncSet sync.RWMutex + lockFindSubscriptions sync.RWMutex + lockGetAddon sync.RWMutex + lockGetCloudProviders sync.RWMutex + lockGetCluster sync.RWMutex + lockGetClusterDNS sync.RWMutex + lockGetClusterIngresses sync.RWMutex + lockGetClusterStatus sync.RWMutex + lockGetCustomerCloudAccounts sync.RWMutex + lockGetExistingClusterMetrics sync.RWMutex + lockGetIdentityProviderList sync.RWMutex + lockGetOrganisationIDFromExternalID sync.RWMutex + lockGetQuotaCostsForProduct sync.RWMutex + lockGetRegions sync.RWMutex + lockGetRequiresTermsAcceptance sync.RWMutex + lockGetSyncSet sync.RWMutex + lockScaleDownComputeNodes sync.RWMutex + lockScaleUpComputeNodes sync.RWMutex + lockSetComputeNodes sync.RWMutex + lockUpdateAddonParameters sync.RWMutex + lockUpdateSyncSet sync.RWMutex } // ClusterAuthorization calls ClusterAuthorizationFunc. @@ -1086,35 +1086,35 @@ func (mock *ClientMock) GetIdentityProviderListCalls() []struct { return calls } -// GetOrganisationFromExternalID calls GetOrganisationFromExternalIDFunc. -func (mock *ClientMock) GetOrganisationFromExternalID(externalID string) (*amsv1.Organization, error) { - if mock.GetOrganisationFromExternalIDFunc == nil { - panic("ClientMock.GetOrganisationFromExternalIDFunc: method is nil but Client.GetOrganisationFromExternalID was just called") +// GetOrganisationIDFromExternalID calls GetOrganisationIDFromExternalIDFunc. +func (mock *ClientMock) GetOrganisationIDFromExternalID(externalID string) (string, error) { + if mock.GetOrganisationIDFromExternalIDFunc == nil { + panic("ClientMock.GetOrganisationIDFromExternalIDFunc: method is nil but Client.GetOrganisationIDFromExternalID was just called") } callInfo := struct { ExternalID string }{ ExternalID: externalID, } - mock.lockGetOrganisationFromExternalID.Lock() - mock.calls.GetOrganisationFromExternalID = append(mock.calls.GetOrganisationFromExternalID, callInfo) - mock.lockGetOrganisationFromExternalID.Unlock() - return mock.GetOrganisationFromExternalIDFunc(externalID) + mock.lockGetOrganisationIDFromExternalID.Lock() + mock.calls.GetOrganisationIDFromExternalID = append(mock.calls.GetOrganisationIDFromExternalID, callInfo) + mock.lockGetOrganisationIDFromExternalID.Unlock() + return mock.GetOrganisationIDFromExternalIDFunc(externalID) } -// GetOrganisationFromExternalIDCalls gets all the calls that were made to GetOrganisationFromExternalID. +// GetOrganisationIDFromExternalIDCalls gets all the calls that were made to GetOrganisationIDFromExternalID. // Check the length with: // -// len(mockedClient.GetOrganisationFromExternalIDCalls()) -func (mock *ClientMock) GetOrganisationFromExternalIDCalls() []struct { +// len(mockedClient.GetOrganisationIDFromExternalIDCalls()) +func (mock *ClientMock) GetOrganisationIDFromExternalIDCalls() []struct { ExternalID string } { var calls []struct { ExternalID string } - mock.lockGetOrganisationFromExternalID.RLock() - calls = mock.calls.GetOrganisationFromExternalID - mock.lockGetOrganisationFromExternalID.RUnlock() + mock.lockGetOrganisationIDFromExternalID.RLock() + calls = mock.calls.GetOrganisationIDFromExternalID + mock.lockGetOrganisationIDFromExternalID.RUnlock() return calls } diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index e386813c2b..5185b33396 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -735,12 +735,6 @@ func InvalidCloudAccountID(reason string, values ...interface{}) *ServiceError { return New(ErrorInvalidCloudAccountID, message, values...) } -// InvalidOCMConnection is raised when the OCM connection is invalid. -// Root causes for an invalid connection are invalid credentials or OCM base URLs. -func InvalidOCMConnection() *ServiceError { - return New(ErrorGeneral, "invalid OCM connection") -} - // OrganisationNotFound converts the error to a ServiceError and returns a reason and hint for the user. func OrganisationNotFound(externalID string, err error) *ServiceError { svcErr := ToServiceError(err) @@ -753,11 +747,6 @@ func OrganisationNotFound(externalID string, err error) *ServiceError { return NewWithCause(svcErr.Code, svcErr, reason, externalID) } -// OrganisationNameInvalid indicates that OCM organisation was found, but its name is invalid. -func OrganisationNameInvalid(externalID string, name string) *ServiceError { - return New(ErrorGeneral, "organisation with external id %q has invalid name %q", externalID, name) -} - // FailedClusterAuthorization converts the error to a ServiceError and returns a reason and hint for the user. func FailedClusterAuthorization(err error) *ServiceError { svcErr := ToServiceError(err) diff --git a/templates/service-template.yml b/templates/service-template.yml index 0b5537360b..858b9572c6 100644 --- a/templates/service-template.yml +++ b/templates/service-template.yml @@ -1036,15 +1036,10 @@ objects: image: ${IMAGE_REGISTRY}/${IMAGE_REPOSITORY}:${IMAGE_TAG} imagePullPolicy: ${IMAGE_PULL_POLICY} volumeMounts: - - name: fleet-manager-credentials - mountPath: /secrets/fleet-manager-credentials - name: service mountPath: /secrets/service - name: rds mountPath: /secrets/rds - env: - - name: "OCM_ENV" - value: "${ENVIRONMENT}" command: - /usr/local/bin/fleet-manager - migrate @@ -1056,11 +1051,6 @@ objects: - --db-ssl-certificate-file=/secrets/rds/db.ca_cert - --db-sslmode=${DB_SSLMODE} - --db-max-open-connections=${DB_MAX_OPEN_CONNS} - - --ocm-client-id-file=/secrets/fleet-manager-credentials/ocm-service.clientId - - --ocm-client-secret-file=/secrets/fleet-manager-credentials/ocm-service.clientSecret - - --ocm-base-url=${OCM_URL} - - --ams-base-url=${AMS_URL} - - --ocm-debug=${OCM_DEBUG} - --enable-db-debug=${ENABLE_DB_DEBUG} - --alsologtostderr - -v=${GLOG_V}