Skip to content

Commit 3e14302

Browse files
authored
Merge pull request #138 from LalitDeore/distribution
Environments distribution to suborgs
2 parents 370df19 + e815f0f commit 3e14302

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

db-connector.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5991,6 +5991,23 @@ func GetEnvironments(ctx context.Context, orgId string) ([]Environment, error) {
59915991
}
59925992
}
59935993

5994+
//Check if this is suborg and get parent org environments if it distributed
5995+
foundOrg, err := GetOrg(ctx, orgId)
5996+
if err == nil && len(foundOrg.ChildOrgs) == 0 && len(foundOrg.CreatorOrg) > 0 && foundOrg.CreatorOrg != orgId {
5997+
parentOrg, err := GetOrg(ctx, foundOrg.CreatorOrg)
5998+
if err == nil {
5999+
parentEnvs, err := GetEnvironments(ctx, parentOrg.Id)
6000+
if err == nil {
6001+
for _, parentEnv := range parentEnvs {
6002+
if !ArrayContains(parentEnv.SuborgDistribution, orgId) {
6003+
continue
6004+
}
6005+
environments = append(environments, parentEnv)
6006+
}
6007+
}
6008+
}
6009+
}
6010+
59946011
// Fixing environment return search problems
59956012
timenow := time.Now().Unix()
59966013
for envIndex, env := range environments {

shared.go

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2602,7 +2602,7 @@ func HandleSetEnvironments(resp http.ResponseWriter, request *http.Request) {
26022602
}
26032603

26042604
item.RunningIp = ""
2605-
if item.OrgId != user.ActiveOrg.Id {
2605+
if item.OrgId != user.ActiveOrg.Id && len(item.SuborgDistribution) == 0 {
26062606
item.OrgId = user.ActiveOrg.Id
26072607
}
26082608

@@ -3323,6 +3323,10 @@ func HandleGetEnvironments(resp http.ResponseWriter, request *http.Request) {
33233323
newEnvironments[envIndex].Licensed = false
33243324
newEnvironments[envIndex].DataLake.Enabled = false
33253325
}
3326+
3327+
if len(env.SuborgDistribution) != 0 {
3328+
newEnvironments[envIndex].SuborgDistribution = env.SuborgDistribution
3329+
}
33263330
}
33273331

33283332
newjson, err := json.Marshal(newEnvironments)
@@ -28303,6 +28307,113 @@ func HandleGetenvStats(resp http.ResponseWriter, request *http.Request) {
2830328307
resp.Write([]byte(fmt.Sprintf(`{"success": true}`)))
2830428308
}
2830528309

28310+
func HandleSetenvConfig(resp http.ResponseWriter, request *http.Request) {
28311+
28312+
cors := HandleCors(resp, request)
28313+
if cors {
28314+
return
28315+
}
28316+
28317+
user, err := HandleApiAuthentication(resp, request)
28318+
if err != nil {
28319+
log.Printf("[AUDIT] Api authentication failed in set env config: %s", err)
28320+
resp.WriteHeader(401)
28321+
resp.Write([]byte(`{"success": false}`))
28322+
return
28323+
}
28324+
28325+
if user.Role != "admin" {
28326+
log.Printf("[AUDIT] User isn't admin during set env config")
28327+
resp.WriteHeader(409)
28328+
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Must be admin to perform this action"}`)))
28329+
return
28330+
}
28331+
28332+
ctx := GetContext(request)
28333+
28334+
var environmentId string
28335+
location := strings.Split(request.URL.String(), "/")
28336+
if location[1] == "api" {
28337+
if len(location) <= 4 {
28338+
log.Printf("Path too short: %d", len(location))
28339+
resp.WriteHeader(401)
28340+
resp.Write([]byte(`{"success": false}`))
28341+
return
28342+
}
28343+
28344+
environmentId = location[4]
28345+
}
28346+
28347+
if len(environmentId) == 0 {
28348+
log.Printf("[Error] No environment ID found in path")
28349+
resp.WriteHeader(401)
28350+
resp.Write([]byte(`{"success": false}`))
28351+
return
28352+
}
28353+
28354+
type environmentConfig struct {
28355+
Action string `json:"action"`
28356+
SelectedSuborg []string `json:"selected_suborgs"`
28357+
}
28358+
28359+
var config environmentConfig
28360+
28361+
body, err := ioutil.ReadAll(request.Body)
28362+
if err != nil {
28363+
log.Printf("[Error] Failed reading body in set env config: %s", err)
28364+
resp.WriteHeader(500)
28365+
resp.Write([]byte(`{"success": false}`))
28366+
return
28367+
}
28368+
28369+
err = json.Unmarshal(body, &config)
28370+
if err != nil {
28371+
log.Printf("[Error] Failed unmarshalling body in set env config: %s", err)
28372+
resp.WriteHeader(500)
28373+
resp.Write([]byte(`{"success": false}`))
28374+
return
28375+
}
28376+
28377+
environment, err := GetEnvironment(ctx, environmentId, user.ActiveOrg.Id)
28378+
if err != nil {
28379+
log.Printf("[Error] Failed getting environment in set env config: %s for Id: %s", err, environmentId)
28380+
resp.WriteHeader(500)
28381+
resp.Write([]byte(`{"success": false}`))
28382+
return
28383+
}
28384+
28385+
if config.Action == "suborg_distribute" {
28386+
28387+
if len(config.SelectedSuborg) == 0 {
28388+
environment.SuborgDistribution = []string{}
28389+
} else {
28390+
environment.SuborgDistribution = config.SelectedSuborg
28391+
}
28392+
28393+
err = SetEnvironment(ctx, environment)
28394+
if err != nil {
28395+
log.Printf("[Error] Failed setting environment in set env config: %s", err)
28396+
resp.WriteHeader(500)
28397+
resp.Write([]byte(`{"success": false}`))
28398+
return
28399+
}
28400+
28401+
foundOrg, err := GetOrg(ctx, user.ActiveOrg.Id)
28402+
if err == nil {
28403+
for _, childOrg := range foundOrg.ChildOrgs {
28404+
nameKey := "Environments"
28405+
cacheKey := fmt.Sprintf("%s_%s", nameKey, childOrg.Id)
28406+
DeleteCache(ctx, cacheKey)
28407+
}
28408+
}
28409+
28410+
log.Printf("[INFO] Successfully updated environment in set env config for environment id: %s", environmentId)
28411+
resp.WriteHeader(200)
28412+
resp.Write([]byte(`{"success": true, "reason" : "Successfully updated environment"}`))
28413+
28414+
}
28415+
}
28416+
2830628417
func GetWorkflowSuggestions(ctx context.Context, user User, org *Org, orgUpdated bool, amount int) (*Org, bool) {
2830728418
// Loop workflows
2830828419

structs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ type Environment struct {
496496
Licensed bool `json:"licensed" datastore:"licensed"`
497497
RunType string `json:"run_type" datastore:"run_type"`
498498
DataLake LakeConfig `json:"data_lake" datastore:"data_lake"`
499+
500+
SuborgDistribution []string `json:"suborg_distribution" datastore:"suborg_distribution"`
499501
}
500502

501503
type LakeConfig struct {

0 commit comments

Comments
 (0)