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

Refactor IAS adapter to be async #3784

Merged
merged 10 commits into from
Apr 9, 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
2 changes: 1 addition & 1 deletion chart/compass/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ global:
name: compass-hydrator
ias_adapter:
dir: dev/incubator/
version: "PR-3768"
version: "PR-3784"
name: compass-ias-adapter
kyma_adapter:
dir: dev/incubator/
Expand Down

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

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

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 @@ -10,7 +10,6 @@ import (
)

//go:generate mockery --name=HealthService --output=automock --outpkg=automock --case=underscore --disable-version-string

type HealthService interface {
CheckHealth(ctx context.Context) (types.HealthStatus, error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import (
"strings"

"github.com/gin-gonic/gin"

"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/types"
)

const (
S4SAPManagedCommunicationScenario = "SAP_COM_1002"
locationHeader = "Location"
)

//go:generate mockery --name=TenantMappingsService --output=automock --outpkg=automock --case=underscore --disable-version-string
Expand All @@ -26,8 +25,14 @@ type TenantMappingsService interface {
RemoveTenantMapping(ctx context.Context, tenantMapping types.TenantMapping) error
}

//go:generate mockery --name=AsyncProcessor --output=automock --outpkg=automock --case=underscore --disable-version-string
type AsyncProcessor interface {
ProcessTMRequest(ctx context.Context, tenantMapping types.TenantMapping)
}

type TenantMappingsHandler struct {
Service TenantMappingsService
Service TenantMappingsService
AsyncProcessor AsyncProcessor
}

func (h TenantMappingsHandler) Patch(ctx *gin.Context) {
Expand All @@ -38,7 +43,7 @@ func (h TenantMappingsHandler) Patch(ctx *gin.Context) {
return
}

if err := tenantMapping.AssignedTenants[0].SetConfiguration(ctx); err != nil {
if err := tenantMapping.AssignedTenant.SetConfiguration(ctx); err != nil {
err = errors.Newf("failed to set assigned tenant configuration: %w", err)
internal.RespondWithError(ctx, http.StatusBadRequest, err)
return
Expand All @@ -55,53 +60,16 @@ func (h TenantMappingsHandler) Patch(ctx *gin.Context) {
tenantMapping.ReceiverTenant.ApplicationURL = "https://" + tenantMapping.ReceiverTenant.ApplicationURL
}

reverseAssignmentState := tenantMapping.AssignedTenants[0].ReverseAssignmentState
if tenantMapping.AssignedTenants[0].Operation == types.OperationAssign {
if reverseAssignmentState != types.StateInitial && reverseAssignmentState != types.StateReady {
errMsgf := "skipped processing tenant mapping notification with $.assignedTenants[0].reverseAssignmentState '%s'"
err := errors.Newf(errMsgf, reverseAssignmentState)
internal.RespondWithError(ctx, internal.IncompleteStatusCode, err)
return
}
}
if err := h.Service.ProcessTenantMapping(ctx, tenantMapping); err != nil {
err = errors.Newf("failed to process tenant mapping notification: %w", err)
operation := tenantMapping.AssignedTenants[0].Operation

if operation == types.OperationAssign {
if errors.Is(err, errors.IASApplicationNotFound) {
internal.RespondWithError(ctx, internal.NotFoundStatusCode, err)
return
}

if errors.Is(err, errors.S4CertificateNotFound) {
logger.FromContext(ctx).Info().Msgf("S/4 certificate not provided. Responding with CONFIG_PENDING.")
s4Config := &types.TenantMappingConfiguration{
Credentials: types.Credentials{
OutboundCommunicationCredentials: types.CommunicationCredentials{
OAuth2mTLSAuthentication: types.OAuth2mTLSAuthentication{
CorrelationIds: []string{S4SAPManagedCommunicationScenario},
},
},
},
}
internal.RespondWithConfigPending(ctx, s4Config)
return
}
}

internal.RespondWithError(ctx, internal.ErrorStatusCode, err)
return
}

ctx.Status(http.StatusOK)
ctx.AbortWithStatus(http.StatusAccepted)
ctx.Set(locationHeader, ctx.GetHeader(locationHeader))
h.AsyncProcessor.ProcessTMRequest(ctx, tenantMapping)
}

func (h TenantMappingsHandler) handleValidateError(ctx *gin.Context, err error, tenantMapping *types.TenantMapping) {
operation := tenantMapping.AssignedTenants[0].Operation
operation := tenantMapping.Operation
if operation != types.OperationUnassign ||
errors.Is(err, types.ErrInvalidFormationID) ||
errors.Is(err, types.ErrInvalidAssignedTenantID) {
errors.Is(err, types.ErrInvalidAssignedTenantAppID) {
internal.RespondWithError(ctx, http.StatusBadRequest, err)
return
}
Expand Down
Loading