Commit 8e1bdafc authored by Kunal Taitkar's avatar Kunal Taitkar
Browse files

Merge branch 'ab_Add_CallByBranch' of...

Merge branch 'ab_Add_CallByBranch' of https://corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2 into ab_Add_CallByBranch
parents 20ec34f1 f73848b9
Branches
Tags
2 merge requests!220Release v2.0.0 alpha,!219Add: Support for branches
Showing with 44 additions and 6 deletions
......@@ -73,4 +73,9 @@ const (
// CONDITIONAL - CONDITIONAL
CONDITIONAL = "CONDITIONAL"
// BranchSeparator separates entity name and branch name ex. <entityName>_<branchName>
Branch_Separator = "_"
// Branch_Main represents default "main" branch
Branch_Main = "main"
)
......@@ -35,6 +35,9 @@ func Init(o, r, c *routing.RouteGroup, JWTKey string) {
func commonHandler(c *routing.Context, isRestricted, isRoleBased, heavyDataActivity bool, principalObj servicebuildermdl.Principal) error {
serviceHeader := string(c.Request.Header.Peek("Service-Header"))
branch := CleanBranch(
strings.TrimSpace(c.Request.Header.Get(Header_Branch)),
)
services := strings.Split(serviceHeader, ",")
versionError := appVersioning(c)
if versionError != nil {
......
......@@ -36,6 +36,9 @@ func Init(o, r, c *gin.RouterGroup, JWTKey string) {
func commonHandler(c *gin.Context, isRestricted, isRoleBased, heavyDataActivity bool, principalObj servicebuildermdl.Principal) {
serviceHeader := c.Request.Header.Get("Service-Header")
branch := CleanBranch(
strings.TrimSpace(c.Request.Header.Get(Header_Branch)),
)
services := strings.Split(serviceHeader, ",")
versionError := appVersioning(c)
if versionError != nil {
......@@ -60,7 +63,7 @@ func commonHandler(c *gin.Context, isRestricted, isRoleBased, heavyDataActivity
for i := 0; i < len(services); i++ {
responseDataObj := responseData{}
service := services[i]
result, ab, isCompressed, errorCode, err := executeService(service, []byte(requestBody.Get(service).String()), isRestricted, isRoleBased, heavyDataActivity, principalObj)
result, ab, isCompressed, errorCode, err := executeServiceWithBranch(service, branch, []byte(requestBody.Get(service).String()), isRestricted, isRoleBased, heavyDataActivity, principalObj)
if errormdl.CheckErr1(err) != nil {
if ab == nil {
responseDataObj.ErrorCode = errorCode
......
......@@ -6,6 +6,7 @@ import (
"strings"
"time"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/constantmdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/utiliymdl/guidmdl"
"github.com/tidwall/sjson"
......@@ -19,6 +20,24 @@ import (
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/servicebuildermdl"
)
const (
Header_Branch = "branch"
)
// CleanBranch returns "main" branch if the branch string is empty after trimming all spaces. if Non-empty returns same string(including spaces along with characters).
func CleanBranch(branch string) string {
if strings.TrimSpace(branch) == "" {
branch = constantmdl.Branch_Main
}
return branch
}
func ConcatenateEntityWithBranch(name, branch string) string {
return name + constantmdl.Branch_Separator + CleanBranch(branch)
}
func setResponseHeader(serviceName string) responseData {
rd := responseData{}
val, ok := GetResponseHeader(serviceName)
......@@ -28,7 +47,7 @@ func setResponseHeader(serviceName string) responseData {
return rd
}
func executeService(name string, data []byte, isRestricted, isRoleBased, heavyDataActivity bool, principalObj servicebuildermdl.Principal) (interface{}, *servicebuildermdl.AbstractBusinessLogicHolder, bool, int, error) {
func executeServiceWithBranch(name, branch string, data []byte, isRestricted, isRoleBased, heavyDataActivity bool, principalObj servicebuildermdl.Principal) (interface{}, *servicebuildermdl.AbstractBusinessLogicHolder, bool, int, error) {
start := time.Now()
var isCompressed bool
......@@ -36,18 +55,20 @@ func executeService(name string, data []byte, isRestricted, isRoleBased, heavyDa
var ab *servicebuildermdl.AbstractBusinessLogicHolder
var found bool
activity := ConcatenateEntityWithBranch(name, branch)
if isRestricted {
if isRoleBased {
service, found = roleBasedServices.Get(name)
service, found = roleBasedServices.Get(activity)
} else {
service, found = restrictedServices.Get(name)
service, found = restrictedServices.Get(activity)
}
} else {
service, found = openServices.Get(name)
service, found = openServices.Get(activity)
}
if !found {
loggermdl.LogError("Service Not Found: " + name)
loggermdl.LogError("Service Not Found: " + activity)
return nil, ab, isCompressed, errormdl.SERVICENOTFOUND, errormdl.Wrap("Service Not Found: " + name)
}
......@@ -103,6 +124,12 @@ func executeService(name string, data []byte, isRestricted, isRoleBased, heavyDa
return result, ab, isCompressed, errormdl.EXPECTATIONFAILED, serviceError
}
// executeService is same as `executeServiceWithBranch` but works for `main` branch
func executeService(name string, data []byte, isRestricted, isRoleBased, heavyDataActivity bool, principalObj servicebuildermdl.Principal) (interface{}, *servicebuildermdl.AbstractBusinessLogicHolder, bool, int, error) {
return executeServiceWithBranch(name, constantmdl.Branch_Main, data, isRestricted, isRoleBased, heavyDataActivity, principalObj)
}
func validateRoleFromToken(principalObj servicebuildermdl.Principal, service ServiceCache) bool {
// check if group from request is present in groups associated with the service.
for _, g := range service.Groups {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment