@@ -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+
2830628417func GetWorkflowSuggestions(ctx context.Context, user User, org *Org, orgUpdated bool, amount int) (*Org, bool) {
2830728418 // Loop workflows
2830828419
0 commit comments