From 54f98d93457dfef0dca3ea735b901dce572dfa4b Mon Sep 17 00:00:00 2001 From: ZdravkoGyurov Date: Wed, 10 Apr 2024 09:59:25 +0300 Subject: [PATCH] remove leftover sync status report from IAS Adapter --- .../handlers/automock/async_processor.go | 7 +++++ .../api/internal/handlers/tenant_mapping.go | 8 ++++-- .../internal/handlers/tenant_mapping_test.go | 16 +++++------- .../internal/service/processor/processor.go | 26 +++++-------------- .../internal/types/tenant_mapping.go | 14 ++++++++++ 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/components/ias-adapter/internal/api/internal/handlers/automock/async_processor.go b/components/ias-adapter/internal/api/internal/handlers/automock/async_processor.go index 23fe3a3119..9e0bf031a2 100644 --- a/components/ias-adapter/internal/api/internal/handlers/automock/async_processor.go +++ b/components/ias-adapter/internal/api/internal/handlers/automock/async_processor.go @@ -8,6 +8,8 @@ import ( mock "github.com/stretchr/testify/mock" types "github.com/kyma-incubator/compass/components/ias-adapter/internal/types" + + ucl "github.com/kyma-incubator/compass/components/ias-adapter/internal/service/ucl" ) // AsyncProcessor is an autogenerated mock type for the AsyncProcessor type @@ -20,6 +22,11 @@ func (_m *AsyncProcessor) ProcessTMRequest(ctx context.Context, tenantMapping ty _m.Called(ctx, tenantMapping) } +// ReportStatus provides a mock function with given fields: ctx, statusReport +func (_m *AsyncProcessor) ReportStatus(ctx context.Context, statusReport ucl.StatusReport) { + _m.Called(ctx, statusReport) +} + type mockConstructorTestingTNewAsyncProcessor interface { mock.TestingT Cleanup(func()) diff --git a/components/ias-adapter/internal/api/internal/handlers/tenant_mapping.go b/components/ias-adapter/internal/api/internal/handlers/tenant_mapping.go index 86ff7a46bd..4b9be0043a 100644 --- a/components/ias-adapter/internal/api/internal/handlers/tenant_mapping.go +++ b/components/ias-adapter/internal/api/internal/handlers/tenant_mapping.go @@ -11,6 +11,7 @@ import ( "github.com/kyma-incubator/compass/components/ias-adapter/internal/api/internal" "github.com/kyma-incubator/compass/components/ias-adapter/internal/errors" "github.com/kyma-incubator/compass/components/ias-adapter/internal/logger" + "github.com/kyma-incubator/compass/components/ias-adapter/internal/service/ucl" "github.com/kyma-incubator/compass/components/ias-adapter/internal/types" ) @@ -28,6 +29,7 @@ type TenantMappingsService interface { //go:generate mockery --name=AsyncProcessor --output=automock --outpkg=automock --case=underscore --disable-version-string type AsyncProcessor interface { ProcessTMRequest(ctx context.Context, tenantMapping types.TenantMapping) + ReportStatus(ctx context.Context, statusReport ucl.StatusReport) } type TenantMappingsHandler struct { @@ -36,6 +38,8 @@ type TenantMappingsHandler struct { } func (h TenantMappingsHandler) Patch(ctx *gin.Context) { + ctx.Set(locationHeader, ctx.GetHeader(locationHeader)) + var tenantMapping types.TenantMapping if err := json.NewDecoder(ctx.Request.Body).Decode(&tenantMapping); err != nil { err = errors.Newf("failed to decode tenant mapping body: %w", err) @@ -61,7 +65,6 @@ func (h TenantMappingsHandler) Patch(ctx *gin.Context) { } ctx.AbortWithStatus(http.StatusAccepted) - ctx.Set(locationHeader, ctx.GetHeader(locationHeader)) h.AsyncProcessor.ProcessTMRequest(ctx, tenantMapping) } @@ -94,7 +97,8 @@ func (h TenantMappingsHandler) handleValidateError(ctx *gin.Context, err error, } logger.FromContext(ctx).Info().Msgf("%s. Responding OK as assignment is safe to remove", err.Error()) - ctx.Status(http.StatusOK) + ctx.AbortWithStatus(http.StatusAccepted) + h.AsyncProcessor.ReportStatus(ctx, ucl.StatusReport{State: types.ReadyState(operation)}) } func logProcessing(ctx context.Context, tenantMapping types.TenantMapping) { diff --git a/components/ias-adapter/internal/api/internal/handlers/tenant_mapping_test.go b/components/ias-adapter/internal/api/internal/handlers/tenant_mapping_test.go index bff73081aa..043e4324d5 100644 --- a/components/ias-adapter/internal/api/internal/handlers/tenant_mapping_test.go +++ b/components/ias-adapter/internal/api/internal/handlers/tenant_mapping_test.go @@ -8,6 +8,7 @@ import ( "github.com/kyma-incubator/compass/components/ias-adapter/internal/api/internal/handlers/automock" "github.com/kyma-incubator/compass/components/ias-adapter/internal/errors" + "github.com/kyma-incubator/compass/components/ias-adapter/internal/service/ucl" "github.com/kyma-incubator/compass/components/ias-adapter/internal/types" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -28,12 +29,6 @@ var _ = Describe("Tenant Mapping Handler", func() { Expect(responseBody).To(ContainSubstring(url.QueryEscape(expectedMessage))) Expect(w.Code).To(Equal(expectedCode)) } - expectSuccess = func(w *httptest.ResponseRecorder, expectedMessage string) { - responseBody, err := io.ReadAll(w.Body) - Expect(err).Error().ToNot(HaveOccurred()) - Expect(responseBody).To(ContainSubstring(url.QueryEscape(expectedMessage))) - Expect(w.Code).To(Equal(http.StatusOK)) - } ) BeforeEach(func() { @@ -121,13 +116,16 @@ var _ = Describe("Tenant Mapping Handler", func() { expectError(w, http.StatusInternalServerError, errExpected.Error()) }) - It("Should succeed if tenantMappings are less then 2", func() { + It("Should succeed if tenantMappings are less than 2", func() { mockService.On("CanSafelyRemoveTenantMapping", mock.Anything, mock.Anything).Return(true, nil) mockService.On("RemoveTenantMapping", mock.Anything, mock.Anything).Return(nil) - + expectedStatusReport := ucl.StatusReport{State: types.StateDeleteReady} + mockAsyncProcessor.On("ReportStatus", mock.Anything, expectedStatusReport).Return() w, ctx := createTestRequest(tenantMapping) + handler.Patch(ctx) - expectSuccess(w, "") + Expect(w.Code).To(Equal(http.StatusAccepted)) + Expect(mockAsyncProcessor.AssertNumberOfCalls(test, "ReportStatus", 1)).To(BeTrue()) }) }) }) diff --git a/components/ias-adapter/internal/service/processor/processor.go b/components/ias-adapter/internal/service/processor/processor.go index 4f727abd49..fc1ff1bca0 100644 --- a/components/ias-adapter/internal/service/processor/processor.go +++ b/components/ias-adapter/internal/service/processor/processor.go @@ -45,7 +45,7 @@ func (p AsyncProcessor) ProcessTMRequest(ctx context.Context, tenantMapping type if reverseAssignmentState != types.StateInitial && reverseAssignmentState != types.StateReady { log.Warn().Msgf("skipping processing tenant mapping notification with $.assignedTenant.state '%s'", reverseAssignmentState) - p.reportStatus(ctx, ucl.StatusReport{State: types.StateConfigPending}) + p.ReportStatus(ctx, ucl.StatusReport{State: types.StateConfigPending}) return } } @@ -57,41 +57,27 @@ func (p AsyncProcessor) ProcessTMRequest(ctx context.Context, tenantMapping type if operation == types.OperationAssign { if errors.Is(err, errors.IASApplicationNotFound) { - p.reportStatus(ctx, ucl.StatusReport{State: errorState(operation), Error: err.Error()}) + p.ReportStatus(ctx, ucl.StatusReport{State: types.ErrorState(operation), Error: err.Error()}) return } if errors.Is(err, errors.S4CertificateNotFound) { log.Info().Msgf("S/4 certificate not provided. Responding with CONFIG_PENDING.") - p.reportStatus(ctx, ucl.StatusReport{State: types.StateConfigPending, Configuration: s4Config}) + p.ReportStatus(ctx, ucl.StatusReport{State: types.StateConfigPending, Configuration: s4Config}) return } } - p.reportStatus(ctx, ucl.StatusReport{State: errorState(operation), Error: err.Error()}) + p.ReportStatus(ctx, ucl.StatusReport{State: types.ErrorState(operation), Error: err.Error()}) return } - p.reportStatus(ctx, ucl.StatusReport{State: readyState(operation)}) + p.ReportStatus(ctx, ucl.StatusReport{State: types.ReadyState(operation)}) } -func (p AsyncProcessor) reportStatus(ctx context.Context, statusReport ucl.StatusReport) { +func (p AsyncProcessor) ReportStatus(ctx context.Context, statusReport ucl.StatusReport) { statusReportURL := ctx.Value(locationHeader).(string) if err := p.UCLService.ReportStatus(ctx, statusReportURL, statusReport); err != nil { logger.FromContext(ctx).Error().Msgf("failed to report status to '%s': %s", statusReportURL, err) } } - -func readyState(operation types.Operation) types.State { - if operation == types.OperationAssign { - return types.StateCreateReady - } - return types.StateDeleteReady -} - -func errorState(operation types.Operation) types.State { - if operation == types.OperationAssign { - return types.StateCreateError - } - return types.StateDeleteError -} diff --git a/components/ias-adapter/internal/types/tenant_mapping.go b/components/ias-adapter/internal/types/tenant_mapping.go index fd138eed18..d72c9666db 100644 --- a/components/ias-adapter/internal/types/tenant_mapping.go +++ b/components/ias-adapter/internal/types/tenant_mapping.go @@ -60,6 +60,20 @@ const ( S4ApplicationNamespace ApplicationNamespace = "sap.s4" ) +func ReadyState(operation Operation) State { + if operation == OperationAssign { + return StateCreateReady + } + return StateDeleteReady +} + +func ErrorState(operation Operation) State { + if operation == OperationAssign { + return StateCreateError + } + return StateDeleteError +} + type AssignedTenant struct { AppID string `json:"uclSystemTenantId"` AppNamespace ApplicationNamespace `json:"applicationNamespace"`