Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Remove leftover sync status report from IAS Adapter #3798

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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

Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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())
})
})
})
Expand Down
26 changes: 6 additions & 20 deletions components/ias-adapter/internal/service/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}
14 changes: 14 additions & 0 deletions components/ias-adapter/internal/types/tenant_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down