-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4218 from utkarsh-pro/utkarsh-pro/feature/multi-c…
…ontext-support Multi-Context Support
- Loading branch information
Showing
36 changed files
with
4,405 additions
and
5,966 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/layer5io/meshery/models" | ||
) | ||
|
||
func (h *Handler) GetAllContexts(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) { | ||
token, ok := req.Context().Value(models.TokenCtxKey).(string) | ||
if !ok { | ||
http.Error(w, "failed to get token", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
q := req.URL.Query() | ||
|
||
vals, err := provider.GetK8sContexts(token, q.Get("page"), q.Get("pageSize"), q.Get("search"), q.Get("order")) | ||
if err != nil { | ||
http.Error(w, "failed to get contexts", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(vals); err != nil { | ||
http.Error(w, "failed to encode contexts", http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
func (h *Handler) GetContext(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) { | ||
if req.URL.Query().Get("current") != "" { | ||
context, ok := req.Context().Value(models.KubeContextKey).(*models.K8sContext) | ||
if !ok || context == nil { | ||
http.Error(w, "failed to get context", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(context); err != nil { | ||
http.Error(w, "failed to encode context", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
token, ok := req.Context().Value(models.TokenCtxKey).(string) | ||
if !ok { | ||
http.Error(w, "failed to get token", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
val, err := provider.GetK8sContext(token, mux.Vars(req)["id"]) | ||
if err != nil { | ||
http.Error(w, "failed to get context", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(val); err != nil { | ||
http.Error(w, "failed to encode context", http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
func (h *Handler) DeleteContext(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) { | ||
token, ok := req.Context().Value(models.TokenCtxKey).(string) | ||
if !ok { | ||
http.Error(w, "failed to get token", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
_, err := provider.DeleteK8sContext(token, mux.Vars(req)["id"]) | ||
if err != nil { | ||
http.Error(w, "failed to delete context", http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
func (h *Handler) GetCurrentContextHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) { | ||
token, ok := req.Context().Value(models.TokenCtxKey).(string) | ||
if !ok { | ||
http.Error(w, "failed to get token", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
val, err := h.GetCurrentContext(token, provider) | ||
if err != nil { | ||
http.Error(w, "failed to get current context", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(val); err != nil { | ||
http.Error(w, "failed to encode context", http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
func (h *Handler) SetCurrentContextHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) { | ||
token, ok := req.Context().Value(models.TokenCtxKey).(string) | ||
if !ok { | ||
http.Error(w, "failed to get token", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
_, err := provider.SetCurrentContext(token, mux.Vars(req)["id"]) | ||
if err != nil { | ||
http.Error(w, "failed to set current context", http.StatusInternalServerError) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.