diff --git a/components/director/internal/domain/application/service.go b/components/director/internal/domain/application/service.go index 3fc67dd940..87bda5fd64 100644 --- a/components/director/internal/domain/application/service.go +++ b/components/director/internal/domain/application/service.go @@ -1177,17 +1177,7 @@ func (s *service) genericCreate(ctx context.Context, in model.ApplicationRegiste } log.C(ctx).Debugf("Loaded Application Tenant %s from context", appTenant) - applications, err := s.appRepo.ListAll(ctx, appTenant) - if err != nil { - return "", err - } - normalizedName := s.appNameNormalizer.Normalize(in.Name) - for _, app := range applications { - if normalizedName == s.appNameNormalizer.Normalize(app.Name) && in.SystemNumber == app.SystemNumber { - return "", apperrors.NewNotUniqueNameError(resource.Application) - } - } exists, err := s.ensureIntSysExists(ctx, in.IntegrationSystemID) if err != nil { diff --git a/components/director/internal/domain/application/service_test.go b/components/director/internal/domain/application/service_test.go index e578d0056a..f275432839 100644 --- a/components/director/internal/domain/application/service_test.go +++ b/components/director/internal/domain/application/service_test.go @@ -62,11 +62,6 @@ func TestService_Create(t *testing.T) { "applicationType": "test-app-with-ppms", "ppmsProductVersionId": "1", } - normalizedLabels := map[string]interface{}{ - "integrationSystemID": intSysID, - "label": "value", - "name": "mp-foo-bar-not", - } labelsWithoutIntSys := map[string]interface{}{ "name": "mp-test", } @@ -78,7 +73,6 @@ func TestService_Create(t *testing.T) { externalTnt := "external-tnt" appModel := modelFromInput(modelInput, tnt, id, applicationMatcher(modelInput.Name, modelInput.Description)) - normalizedAppModel := modelFromInput(normalizedModelInput, tnt, id, applicationMatcher(normalizedModelInput.Name, normalizedModelInput.Description)) appWithScenariosLabel := applicationRegisterInput() appWithScenariosLabel.Labels[model.ScenariosKey] = []string{testScenario} @@ -99,11 +93,10 @@ func TestService_Create(t *testing.T) { ExpectedErr error }{ { - Name: "Returns success when listing existing applications returns empty applications", + Name: "Returns success for normalized name", AppNameNormalizer: &normalizer.DefaultNormalizator{}, AppRepoFn: func() *automock.ApplicationRepository { repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{}, nil).Once() repo.On("Create", ctx, tnt, mock.MatchedBy(appModel.ApplicationMatcherFn)).Return(nil).Once() return repo }, @@ -135,118 +128,11 @@ func TestService_Create(t *testing.T) { Input: applicationRegisterInput(), ExpectedErr: nil, }, - { - Name: "Returns success when listing existing applications returns application with different name", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{{Name: modelInput.Name + "-test"}}, nil).Once() - repo.On("Create", ctx, tnt, mock.MatchedBy(appModel.ApplicationMatcherFn)).Return(nil).Once() - return repo - }, - WebhookRepoFn: func() *automock.WebhookRepository { - repo := &automock.WebhookRepository{} - repo.On("CreateMany", ctx, tnt, mock.Anything).Return(nil).Once() - return repo - }, - IntSysRepoFn: func() *automock.IntegrationSystemRepository { - repo := &automock.IntegrationSystemRepository{} - repo.On("Exists", ctx, intSysID).Return(true, nil).Once() - return repo - }, - LabelServiceFn: func() *automock.LabelService { - svc := &automock.LabelService{} - svc.On("UpsertMultipleLabels", ctx, tnt, model.ApplicationLabelableObject, id, labels).Return(nil).Once() - return svc - }, - BundleServiceFn: func() *automock.BundleService { - svc := &automock.BundleService{} - svc.On("CreateMultiple", ctx, resource.Application, id, modelInput.Bundles).Return(nil).Once() - return svc - }, - UIDServiceFn: func() *automock.UIDService { - svc := &automock.UIDService{} - svc.On("Generate").Return(id) - return svc - }, - Input: applicationRegisterInput(), - ExpectedErr: nil, - }, - { - Name: "Returns error when listing existing applications returns application with same name", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{{Name: modelInput.Name}}, nil).Once() - return repo - }, - WebhookRepoFn: UnusedWebhookRepository, - IntSysRepoFn: UnusedIntegrationSystemRepository, - LabelServiceFn: UnusedLabelService, - BundleServiceFn: UnusedBundleService, - UIDServiceFn: UnusedUIDService, - Input: applicationRegisterInput(), - ExpectedErr: apperrors.NewNotUniqueNameError(resource.Application), - }, - { - Name: "Returns success when listing existing applications returns application with different name and incoming name is already normalized", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{{Name: normalizedModelInput.Name + "-test"}}, nil).Once() - repo.On("Create", ctx, tnt, mock.MatchedBy(normalizedAppModel.ApplicationMatcherFn)).Return(nil).Once() - return repo - }, - WebhookRepoFn: func() *automock.WebhookRepository { - repo := &automock.WebhookRepository{} - repo.On("CreateMany", ctx, tnt, mock.Anything).Return(nil).Once() - return repo - }, - IntSysRepoFn: func() *automock.IntegrationSystemRepository { - repo := &automock.IntegrationSystemRepository{} - repo.On("Exists", ctx, intSysID).Return(true, nil).Once() - return repo - }, - LabelServiceFn: func() *automock.LabelService { - svc := &automock.LabelService{} - svc.On("UpsertMultipleLabels", ctx, tnt, model.ApplicationLabelableObject, id, normalizedLabels).Return(nil).Once() - return svc - }, - BundleServiceFn: func() *automock.BundleService { - svc := &automock.BundleService{} - svc.On("CreateMultiple", ctx, resource.Application, id, normalizedModelInput.Bundles).Return(nil).Once() - return svc - }, - UIDServiceFn: func() *automock.UIDService { - svc := &automock.UIDService{} - svc.On("Generate").Return(id) - return svc - }, - Input: normalizedModelInput, - ExpectedErr: nil, - }, - { - Name: "Returns error when listing existing applications returns application with same name and incoming name is already normalized", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{{Name: normalizedModelInput.Name}}, nil).Once() - return repo - }, - WebhookRepoFn: UnusedWebhookRepository, - IntSysRepoFn: UnusedIntegrationSystemRepository, - LabelServiceFn: UnusedLabelService, - BundleServiceFn: UnusedBundleService, - UIDServiceFn: UnusedUIDService, - Input: normalizedModelInput, - ExpectedErr: apperrors.NewNotUniqueNameError(resource.Application), - }, { Name: "Success when no labels provided", AppNameNormalizer: &normalizer.DefaultNormalizator{}, AppRepoFn: func() *automock.ApplicationRepository { repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() repo.On("Create", ctx, tnt, mock.MatchedBy(applicationMatcher("test", nil))).Return(nil).Once() return repo }, @@ -275,7 +161,6 @@ func TestService_Create(t *testing.T) { AppNameNormalizer: &normalizer.DefaultNormalizator{}, AppRepoFn: func() *automock.ApplicationRepository { repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() repo.On("Create", ctx, tnt, mock.MatchedBy(applicationMatcher("test", nil))).Return(nil).Once() return repo }, @@ -307,7 +192,6 @@ func TestService_Create(t *testing.T) { AppNameNormalizer: &normalizer.DefaultNormalizator{}, AppRepoFn: func() *automock.ApplicationRepository { repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() repo.On("Create", ctx, tnt, mock.MatchedBy(appModel.ApplicationMatcherFn)).Return(testErr).Once() return repo }, @@ -327,31 +211,11 @@ func TestService_Create(t *testing.T) { Input: applicationRegisterInput(), ExpectedErr: testErr, }, - { - Name: "Returns error when listing existing applications fails", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, testErr).Once() - return repo - }, - WebhookRepoFn: UnusedWebhookRepository, - IntSysRepoFn: UnusedIntegrationSystemRepository, - LabelServiceFn: UnusedLabelService, - BundleServiceFn: UnusedBundleService, - UIDServiceFn: UnusedUIDService, - Input: applicationRegisterInput(), - ExpectedErr: testErr, - }, { Name: "Returns error when integration system doesn't exist", AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() - return repo - }, - WebhookRepoFn: UnusedWebhookRepository, + AppRepoFn: UnusedApplicationRepository, + WebhookRepoFn: UnusedWebhookRepository, IntSysRepoFn: func() *automock.IntegrationSystemRepository { repo := &automock.IntegrationSystemRepository{} repo.On("Exists", ctx, intSysID).Return(false, nil).Once() @@ -366,12 +230,8 @@ func TestService_Create(t *testing.T) { { Name: "Returns error when checking for integration system fails", AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() - return repo - }, - WebhookRepoFn: UnusedWebhookRepository, + AppRepoFn: UnusedApplicationRepository, + WebhookRepoFn: UnusedWebhookRepository, IntSysRepoFn: func() *automock.IntegrationSystemRepository { repo := &automock.IntegrationSystemRepository{} repo.On("Exists", ctx, intSysID).Return(false, testErr).Once() @@ -388,7 +248,6 @@ func TestService_Create(t *testing.T) { AppNameNormalizer: &normalizer.DefaultNormalizator{}, AppRepoFn: func() *automock.ApplicationRepository { repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() repo.On("Create", ctx, tnt, mock.MatchedBy(appModel.ApplicationMatcherFn)).Return(nil).Once() return repo }, @@ -425,7 +284,6 @@ func TestService_Create(t *testing.T) { AppNameNormalizer: &normalizer.DefaultNormalizator{}, AppRepoFn: func() *automock.ApplicationRepository { repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{}, nil).Once() repo.On("Create", ctx, tnt, mock.MatchedBy(appModel.ApplicationMatcherFn)).Return(nil).Once() return repo }, @@ -510,23 +368,17 @@ func TestService_CreateFromTemplate(t *testing.T) { "applicationType": "test-app-with-ppms", "ppmsProductVersionId": "1", } - normalizedLabels := map[string]interface{}{ - "integrationSystemID": intSysID, - "label": "value", - "name": "mp-foo-bar-not", - } labelsWithoutIntSys := map[string]interface{}{ "name": "mp-test", } var nilLabels map[string]interface{} id := "foo" - appTemplteID := "test-app-template" + appTemplateID := "test-app-template" tnt := "tenant" externalTnt := "external-tnt" - appFromTemplateModel := modelFromInput(modelInput, tnt, id, applicationFromTemplateMatcher(modelInput.Name, modelInput.Description, &appTemplteID)) - normalizedAppModel := modelFromInput(normalizedModelInput, tnt, id, applicationFromTemplateMatcher(normalizedModelInput.Name, normalizedModelInput.Description, &appTemplteID)) + appFromTemplateModel := modelFromInput(modelInput, tnt, id, applicationFromTemplateMatcher(modelInput.Name, modelInput.Description, &appTemplateID)) ctx := context.TODO() ctx = tenant.SaveToContext(ctx, tnt, externalTnt) @@ -548,7 +400,6 @@ func TestService_CreateFromTemplate(t *testing.T) { AppNameNormalizer: &normalizer.DefaultNormalizator{}, AppRepoFn: func() *automock.ApplicationRepository { repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() repo.On("Create", ctx, tnt, mock.MatchedBy(appFromTemplateModel.ApplicationMatcherFn)).Return(nil).Once() return repo }, @@ -580,155 +431,11 @@ func TestService_CreateFromTemplate(t *testing.T) { Input: applicationRegisterInput(), ExpectedErr: nil, }, - { - Name: "Returns success when listing existing applications returns empty applications", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{}, nil).Once() - repo.On("Create", ctx, tnt, mock.MatchedBy(appFromTemplateModel.ApplicationMatcherFn)).Return(nil).Once() - return repo - }, - WebhookRepoFn: func() *automock.WebhookRepository { - repo := &automock.WebhookRepository{} - repo.On("CreateMany", ctx, tnt, mock.Anything).Return(nil).Once() - return repo - }, - IntSysRepoFn: func() *automock.IntegrationSystemRepository { - repo := &automock.IntegrationSystemRepository{} - repo.On("Exists", ctx, intSysID).Return(true, nil).Once() - return repo - }, - LabelServiceFn: func() *automock.LabelService { - svc := &automock.LabelService{} - svc.On("UpsertMultipleLabels", ctx, tnt, model.ApplicationLabelableObject, id, labels).Return(nil).Once() - return svc - }, - BundleServiceFn: func() *automock.BundleService { - svc := &automock.BundleService{} - svc.On("CreateMultiple", ctx, resource.Application, id, modelInput.Bundles).Return(nil).Once() - return svc - }, - UIDServiceFn: func() *automock.UIDService { - svc := &automock.UIDService{} - svc.On("Generate").Return(id) - return svc - }, - Input: applicationRegisterInput(), - ExpectedErr: nil, - }, - { - Name: "Returns success when listing existing applications returns application with different name", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{{Name: modelInput.Name + "-test"}}, nil).Once() - repo.On("Create", ctx, tnt, mock.MatchedBy(appFromTemplateModel.ApplicationMatcherFn)).Return(nil).Once() - return repo - }, - WebhookRepoFn: func() *automock.WebhookRepository { - repo := &automock.WebhookRepository{} - repo.On("CreateMany", ctx, tnt, mock.Anything).Return(nil).Once() - return repo - }, - IntSysRepoFn: func() *automock.IntegrationSystemRepository { - repo := &automock.IntegrationSystemRepository{} - repo.On("Exists", ctx, intSysID).Return(true, nil).Once() - return repo - }, - LabelServiceFn: func() *automock.LabelService { - svc := &automock.LabelService{} - svc.On("UpsertMultipleLabels", ctx, tnt, model.ApplicationLabelableObject, id, labels).Return(nil).Once() - return svc - }, - BundleServiceFn: func() *automock.BundleService { - svc := &automock.BundleService{} - svc.On("CreateMultiple", ctx, resource.Application, id, modelInput.Bundles).Return(nil).Once() - return svc - }, - UIDServiceFn: func() *automock.UIDService { - svc := &automock.UIDService{} - svc.On("Generate").Return(id) - return svc - }, - Input: applicationRegisterInput(), - ExpectedErr: nil, - }, - { - Name: "Returns error when listing existing applications returns application with same name", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{{Name: modelInput.Name}}, nil).Once() - return repo - }, - WebhookRepoFn: UnusedWebhookRepository, - IntSysRepoFn: UnusedIntegrationSystemRepository, - LabelServiceFn: UnusedLabelService, - BundleServiceFn: UnusedBundleService, - UIDServiceFn: UnusedUIDService, - Input: applicationRegisterInput(), - ExpectedErr: apperrors.NewNotUniqueNameError(resource.Application), - }, - { - Name: "Returns success when listing existing applications returns application with different name and incoming name is already normalized", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{{Name: normalizedModelInput.Name + "-test"}}, nil).Once() - repo.On("Create", ctx, tnt, mock.MatchedBy(normalizedAppModel.ApplicationMatcherFn)).Return(nil).Once() - return repo - }, - WebhookRepoFn: func() *automock.WebhookRepository { - repo := &automock.WebhookRepository{} - repo.On("CreateMany", ctx, tnt, mock.Anything).Return(nil).Once() - return repo - }, - IntSysRepoFn: func() *automock.IntegrationSystemRepository { - repo := &automock.IntegrationSystemRepository{} - repo.On("Exists", ctx, intSysID).Return(true, nil).Once() - return repo - }, - LabelServiceFn: func() *automock.LabelService { - svc := &automock.LabelService{} - svc.On("UpsertMultipleLabels", ctx, tnt, model.ApplicationLabelableObject, id, normalizedLabels).Return(nil).Once() - return svc - }, - BundleServiceFn: func() *automock.BundleService { - svc := &automock.BundleService{} - svc.On("CreateMultiple", ctx, resource.Application, id, normalizedModelInput.Bundles).Return(nil).Once() - return svc - }, - UIDServiceFn: func() *automock.UIDService { - svc := &automock.UIDService{} - svc.On("Generate").Return(id) - return svc - }, - Input: normalizedModelInput, - ExpectedErr: nil, - }, - { - Name: "Returns error when listing existing applications returns application with same name and incoming name is already normalized", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return([]*model.Application{{Name: normalizedModelInput.Name}}, nil).Once() - return repo - }, - WebhookRepoFn: UnusedWebhookRepository, - IntSysRepoFn: UnusedIntegrationSystemRepository, - LabelServiceFn: UnusedLabelService, - BundleServiceFn: UnusedBundleService, - UIDServiceFn: UnusedUIDService, - Input: normalizedModelInput, - ExpectedErr: apperrors.NewNotUniqueNameError(resource.Application), - }, { Name: "Success when no labels provided", AppNameNormalizer: &normalizer.DefaultNormalizator{}, AppRepoFn: func() *automock.ApplicationRepository { repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() repo.On("Create", ctx, tnt, mock.MatchedBy(applicationMatcher("test", nil))).Return(nil).Once() return repo }, @@ -757,7 +464,6 @@ func TestService_CreateFromTemplate(t *testing.T) { AppNameNormalizer: &normalizer.DefaultNormalizator{}, AppRepoFn: func() *automock.ApplicationRepository { repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() repo.On("Create", ctx, tnt, mock.MatchedBy(appFromTemplateModel.ApplicationMatcherFn)).Return(testErr).Once() return repo }, @@ -777,31 +483,11 @@ func TestService_CreateFromTemplate(t *testing.T) { Input: applicationRegisterInput(), ExpectedErr: testErr, }, - { - Name: "Returns error when listing existing applications fails", - AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, testErr).Once() - return repo - }, - WebhookRepoFn: UnusedWebhookRepository, - IntSysRepoFn: UnusedIntegrationSystemRepository, - LabelServiceFn: UnusedLabelService, - BundleServiceFn: UnusedBundleService, - UIDServiceFn: UnusedUIDService, - Input: applicationRegisterInput(), - ExpectedErr: testErr, - }, { Name: "Returns error when integration system doesn't exist", AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() - return repo - }, - WebhookRepoFn: UnusedWebhookRepository, + AppRepoFn: UnusedApplicationRepository, + WebhookRepoFn: UnusedWebhookRepository, IntSysRepoFn: func() *automock.IntegrationSystemRepository { repo := &automock.IntegrationSystemRepository{} repo.On("Exists", ctx, intSysID).Return(false, nil).Once() @@ -816,12 +502,8 @@ func TestService_CreateFromTemplate(t *testing.T) { { Name: "Returns error when checking for integration system fails", AppNameNormalizer: &normalizer.DefaultNormalizator{}, - AppRepoFn: func() *automock.ApplicationRepository { - repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() - return repo - }, - WebhookRepoFn: UnusedWebhookRepository, + AppRepoFn: UnusedApplicationRepository, + WebhookRepoFn: UnusedWebhookRepository, IntSysRepoFn: func() *automock.IntegrationSystemRepository { repo := &automock.IntegrationSystemRepository{} repo.On("Exists", ctx, intSysID).Return(false, testErr).Once() @@ -838,7 +520,6 @@ func TestService_CreateFromTemplate(t *testing.T) { AppNameNormalizer: &normalizer.DefaultNormalizator{}, AppRepoFn: func() *automock.ApplicationRepository { repo := &automock.ApplicationRepository{} - repo.On("ListAll", ctx, mock.Anything).Return(nil, nil).Once() repo.On("Create", ctx, tnt, mock.MatchedBy(appFromTemplateModel.ApplicationMatcherFn)).Return(nil).Once() return repo }, @@ -885,7 +566,7 @@ func TestService_CreateFromTemplate(t *testing.T) { svc.SetTimestampGen(func() time.Time { return timestamp }) // WHEN - result, err := svc.CreateFromTemplate(ctx, testCase.Input, &appTemplteID, false) + result, err := svc.CreateFromTemplate(ctx, testCase.Input, &appTemplateID, false) // then assert.IsType(t, "string", result) diff --git a/components/director/internal/domain/subscription/service.go b/components/director/internal/domain/subscription/service.go index 080a50eda3..c4ff4efaf3 100644 --- a/components/director/internal/domain/subscription/service.go +++ b/components/director/internal/domain/subscription/service.go @@ -370,7 +370,7 @@ func (s *service) SubscribeTenantToApplication(ctx context.Context, providerID, } } - appID, systemFieldDiscoveryValue, err := s.createApplicationFromTemplate(ctx, appTemplate, subscribedSubaccountID, consumerTenantID, subscribedAppName, subdomainValue, region, subscriptionID, subscriptionPayload) + appID, systemFieldDiscoveryValue, err := s.createApplicationFromTemplate(ctx, appTemplate, subscribedSubaccountID, consumerTenantID, subdomainValue, region, subscriptionID, subscriptionPayload) if err != nil { return false, systemFieldDiscoveryValue, "", "", err } @@ -445,28 +445,28 @@ func (s *service) DetermineSubscriptionFlow(ctx context.Context, providerID, reg return "", errors.Errorf("could not determine flow") } -func (s *service) createApplicationFromTemplate(ctx context.Context, appTemplate *model.ApplicationTemplate, subscribedSubaccountID, consumerTenantID, subscribedAppName, subdomain, region, subscriptionID string, subscriptionPayload string) (string, bool, error) { - log.C(ctx).Debugf("Preparing Values for Application Template with name %q", appTemplate.Name) +func (s *service) createApplicationFromTemplate(ctx context.Context, appTemplate *model.ApplicationTemplate, subscribedSubaccountID, consumerTenantID, subdomain, region, subscriptionID string, subscriptionPayload string) (string, bool, error) { + log.C(ctx).Debugf("Preparing Values for Application Template with name %q and ID %s", appTemplate.Name, appTemplate.ID) values, err := s.preparePlaceholderValues(appTemplate, subdomain, region, subscriptionPayload) if err != nil { - return "", false, errors.Wrapf(err, "while preparing the values for Application template %q", appTemplate.Name) + return "", false, errors.Wrapf(err, "while preparing the values for Application template %q and ID %s", appTemplate.Name, appTemplate.ID) } - log.C(ctx).Debugf("Preparing ApplicationCreateInput JSON from Application Template with name %q", appTemplate.Name) + log.C(ctx).Debugf("Preparing ApplicationCreateInput JSON from Application Template with name %q and ID %s", appTemplate.Name, appTemplate.ID) appCreateInputJSON, err := s.appTemplateSvc.PrepareApplicationCreateInputJSON(appTemplate, values) if err != nil { - return "", false, errors.Wrapf(err, "while preparing ApplicationCreateInput JSON from Application Template with name %q", appTemplate.Name) + return "", false, errors.Wrapf(err, "while preparing ApplicationCreateInput JSON from Application Template with name %q and ID %s", appTemplate.Name, appTemplate.ID) } - log.C(ctx).Debugf("Converting ApplicationCreateInput JSON to GraphQL ApplicationRegistrationInput from Application Template with name %q", appTemplate.Name) + log.C(ctx).Debugf("Converting ApplicationCreateInput JSON to GraphQL ApplicationRegistrationInput from Application Template with name %q and ID %s", appTemplate.Name, appTemplate.ID) appCreateInputGQL, err := s.appConv.CreateRegisterInputJSONToGQL(appCreateInputJSON) if err != nil { return "", false, errors.Wrapf(err, "while converting ApplicationCreateInput JSON to GraphQL ApplicationRegistrationInput from Application Template with name %q", appTemplate.Name) } - log.C(ctx).Infof("Validating GraphQL ApplicationRegistrationInput from Application Template with name %q", appTemplate.Name) + log.C(ctx).Infof("Validating GraphQL ApplicationRegistrationInput from Application Template with name %q and ID %s", appTemplate.Name, appTemplate.ID) if err := inputvalidation.Validate(appCreateInputGQL); err != nil { - return "", false, errors.Wrapf(err, "while validating application input from Application Template with name %q", appTemplate.Name) + return "", false, errors.Wrapf(err, "while validating application input from Application Template with name %q and ID %s", appTemplate.Name, appTemplate.ID) } appCreateInputModel, err := s.appConv.CreateInputFromGraphQL(ctx, appCreateInputGQL) @@ -490,21 +490,21 @@ func (s *service) createApplicationFromTemplate(ctx context.Context, appTemplate return "", false, err } else { if apperrors.IsNotFoundError(err) { - log.C(ctx).Infof("%s label for Application Template with ID %s is missing", SystemFieldDiscoveryLabelKey, appTemplate.ID) + log.C(ctx).Infof("%s label for Application Template with name %q and ID %s is missing", SystemFieldDiscoveryLabelKey, appTemplate.Name, appTemplate.ID) } else { systemFieldDiscoveryValue, ok = systemFieldDiscoveryLabel.Value.(bool) if !ok { - log.C(ctx).Infof("%s label for Application Template with ID %s is not a boolean", SystemFieldDiscoveryLabelKey, appTemplate.ID) + log.C(ctx).Infof("%s label for Application Template with name %q and ID %s is not a boolean", SystemFieldDiscoveryLabelKey, appTemplate.Name, appTemplate.ID) } } } - log.C(ctx).Infof("Creating an Application with name %q from Application Template with name %q", subscribedAppName, appTemplate.Name) + log.C(ctx).Infof("Creating an Application with name %q from Application Template with name %q and ID %s", appCreateInputModel.Name, appTemplate.Name, appTemplate.ID) appID, err := s.appSvc.CreateFromTemplate(ctx, appCreateInputModel, &appTemplate.ID, systemFieldDiscoveryValue) if err != nil { - return "", systemFieldDiscoveryValue, errors.Wrapf(err, "while creating an Application with name %s from Application Template with name %s", subscribedAppName, appTemplate.Name) + return "", systemFieldDiscoveryValue, errors.Wrapf(err, "while creating an Application with name %s from Application Template with name %q and ID %s", appCreateInputModel.Name, appTemplate.Name, appTemplate.ID) } - log.C(ctx).Infof("Successfully created an Application with id %q and name %q from Application Template with name %q", appID, subscribedAppName, appTemplate.Name) + log.C(ctx).Infof("Successfully created an Application with id %q and name %q from Application Template with name %q and ID %q", appID, appCreateInputModel.Name, appTemplate.Name, appTemplate.ID) return appID, systemFieldDiscoveryValue, nil } diff --git a/tests/director/tests/application/application_api_test.go b/tests/director/tests/application/application_api_test.go index 6db2b3f8a8..d7bade7054 100644 --- a/tests/director/tests/application/application_api_test.go +++ b/tests/director/tests/application/application_api_test.go @@ -178,95 +178,6 @@ func TestRegisterApplicationWithOrdWebhook(t *testing.T) { // assert.Equal(t, operation.OperationStatusSucceeded, opResponse.Status) // } -func TestRegisterApplicationNormalizationValidation(t *testing.T) { - // GIVEN - ctx := context.Background() - firstAppName := "app@wordpress" - - tenantId := tenant.TestTenants.GetDefaultTenantID() - - actualApp, err := fixtures.RegisterApplication(t, ctx, certSecuredGraphQLClient, firstAppName, tenantId) - defer fixtures.CleanupApplication(t, ctx, certSecuredGraphQLClient, tenantId, &actualApp) - require.NoError(t, err) - require.NotEmpty(t, actualApp.ID) - - //THEN - require.NotEmpty(t, actualApp.ID) - require.Equal(t, actualApp.Name, firstAppName) - - assert.Equal(t, graphql.ApplicationStatusConditionInitial, actualApp.Status.Condition) - - defer fixtures.DeleteFormation(t, ctx, certSecuredGraphQLClient, testScenario) - fixtures.CreateFormation(t, ctx, certSecuredGraphQLClient, testScenario) - - // SECOND APP WITH SAME APP NAME WHEN NORMALIZED - inSecond := graphql.ApplicationRegisterInput{ - Name: "app!wordpress", - ProviderName: ptr.String("provider name"), - Description: ptr.String("my first wordpress application"), - HealthCheckURL: ptr.String("http://mywordpress.com/health"), - Labels: graphql.Labels{ - "group": []interface{}{"production", "experimental"}, - }, - } - appSecondInputGQL, err := testctx.Tc.Graphqlizer.ApplicationRegisterInputToGQL(inSecond) - require.NoError(t, err) - actualSecondApp := graphql.ApplicationExt{} - - // WHEN - - request := fixtures.FixRegisterApplicationRequest(appSecondInputGQL) - err = testctx.Tc.RunOperation(ctx, certSecuredGraphQLClient, request, &actualSecondApp) - - //THEN - require.EqualError(t, err, "graphql: Object name is not unique [object=application]") - require.Empty(t, actualSecondApp.BaseEntity) - - // THIRD APP WITH DIFFERENT APP NAME WHEN NORMALIZED - actualThirdApp, err := fixtures.RegisterApplication(t, ctx, certSecuredGraphQLClient, "appwordpress", tenantId) - defer fixtures.CleanupApplication(t, ctx, certSecuredGraphQLClient, tenantId, &actualThirdApp) - require.NoError(t, err) - require.NotEmpty(t, actualThirdApp.ID) - - //THEN - require.NotEmpty(t, actualThirdApp.ID) - - assert.Equal(t, graphql.ApplicationStatusConditionInitial, actualThirdApp.Status.Condition) - - // FOURTH APP WITH DIFFERENT ALREADY NORMALIZED NAME WHICH MATCHES EXISTING APP WHEN NORMALIZED - inFourth := graphql.ApplicationRegisterInput{ - Name: "mp-appwordpress", - ProviderName: ptr.String("provider name"), - Description: ptr.String("my first wordpress application"), - HealthCheckURL: ptr.String("http://mywordpress.com/health"), - Labels: graphql.Labels{ - "group": []interface{}{"production", "experimental"}, - }, - } - appFourthInputGQL, err := testctx.Tc.Graphqlizer.ApplicationRegisterInputToGQL(inFourth) - require.NoError(t, err) - actualFourthApp := graphql.ApplicationExt{} - // WHEN - request = fixtures.FixRegisterApplicationRequest(appFourthInputGQL) - err = testctx.Tc.RunOperation(ctx, certSecuredGraphQLClient, request, &actualFourthApp) - //THEN - require.EqualError(t, err, "graphql: Object name is not unique [object=application]") - require.Empty(t, actualFourthApp.BaseEntity) - - // FIFTH APP WITH DIFFERENT ALREADY NORMALIZED NAME WHICH DOES NOT MATCH ANY EXISTING APP WHEN NORMALIZED - fifthAppName := "mp-application" - actualFifthApp, err := fixtures.RegisterApplication(t, ctx, certSecuredGraphQLClient, fifthAppName, tenantId) - defer fixtures.CleanupApplication(t, ctx, certSecuredGraphQLClient, tenantId, &actualFifthApp) - require.NoError(t, err) - require.NotEmpty(t, actualFifthApp.ID) - - //THEN - require.NotEmpty(t, actualFifthApp.ID) - require.Equal(t, actualFifthApp.Name, fifthAppName) - - assert.Equal(t, graphql.ApplicationStatusConditionInitial, actualFifthApp.Status.Condition) -} - func TestRegisterApplicationWithStatusCondition(t *testing.T) { // GIVEN ctx := context.Background()