From f993eb1a5dafd29849b0c7a6aecfc428ee9571db Mon Sep 17 00:00:00 2001 From: Oleksandr Sokolov Date: Wed, 15 Feb 2023 19:16:51 +0200 Subject: [PATCH] refactoring --- rest/workspaces.go | 87 ++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 54 deletions(-) diff --git a/rest/workspaces.go b/rest/workspaces.go index 0babe61..20e5668 100644 --- a/rest/workspaces.go +++ b/rest/workspaces.go @@ -1,19 +1,21 @@ package rest import ( + "bytes" "encoding/json" "fmt" + "net/http" + "os" + "path/filepath" + + "github.com/gorilla/mux" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" - "net/http" - "os" - "path/filepath" "github.com/equinor/flowify-workflows-server/pkg/workspace" "github.com/equinor/flowify-workflows-server/user" - "github.com/gorilla/mux" ) func RegisterWorkspaceRoutes(r *mux.Route) { @@ -27,7 +29,7 @@ func RegisterWorkspaceRoutes(r *mux.Route) { s.Use(SetContentTypeMiddleware(outtype)) s.HandleFunc("/workspaces/", WorkspacesListHandler()).Methods(http.MethodGet) - s.HandleFunc("/workspaces/", WorkspacesCreate()).Methods(http.MethodPost) + s.HandleFunc("/workspaces/", WorkspacesCreateHandler()).Methods(http.MethodPost) } func WorkspacesListHandler() http.HandlerFunc { @@ -55,18 +57,19 @@ func WorkspacesListHandler() http.HandlerFunc { } type WorkspaceCreateInputData struct { - Name string + Name string + Roles []string } -func WorkspacesCreate() http.HandlerFunc { +func WorkspacesCreateHandler() http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - - var p WorkspaceCreateInputData - err := json.NewDecoder(r.Body).Decode(&p) + var creationData WorkspaceCreateInputData + err := json.NewDecoder(r.Body).Decode(&creationData) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return + WriteResponse(w, http.StatusInternalServerError, nil, struct { + Error string + }{Error: fmt.Sprintf("error decoding the input data: %v\n", err)}, "workspace") } userHomeDir, err := os.UserHomeDir() @@ -89,29 +92,16 @@ func WorkspacesCreate() http.HandlerFunc { WriteResponse(w, http.StatusInternalServerError, nil, nil, "workspace") } - //nsName := &corev1.Namespace{ - // ObjectMeta: metav1.ObjectMeta{ - // Name: "my-new-namespace1", - // }, - //} - // - //namespace, err := clientset.CoreV1().Namespaces().Create(context.Background(), nsName, metav1.CreateOptions{}) - //if err != nil { - // WriteResponse(w, http.StatusInternalServerError, nil, struct { - // Error error - // }{Error: err}, "workspace") - //} - - //apiVersion: v1 - //kind: ConfigMap - //metadata: - //labels: - // app.kubernetes.io/component: "workspace-config" - // app.kubernetes.io/part-of: "flowify" - //name: "sandbox-project-a" - //namespace: "argo" - //data: - //roles: "[[\"sandbox-developer\"]]" + var strBuffer bytes.Buffer + + strBuffer.WriteString("[") + for _, role := range creationData.Roles { + strBuffer.WriteString("\"") + strBuffer.WriteString(role) + strBuffer.WriteString("\", ") + } + roles := strBuffer.String() + roles = roles[:len(roles)-2] + "]" cm := corev1.ConfigMap{ TypeMeta: metav1.TypeMeta{ @@ -119,11 +109,14 @@ func WorkspacesCreate() http.HandlerFunc { APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: "workspace-test", + Name: creationData.Name, Namespace: "argo", - Labels: map[string]string{"app.kubernetes.io/component": "workspace-config", "app.kubernetes.io/part-of": "flowify"}, + Labels: map[string]string{ + "app.kubernetes.io/component": "workspace-config", + "app.kubernetes.io/part-of": "flowify", + }, }, - Data: map[string]string{"roles": "[\"sandbox-developer\"]", "projectName": "example-workspace"}, + Data: map[string]string{"roles": roles, "projectName": creationData.Name}, } CMOpt := metav1.CreateOptions{} @@ -135,24 +128,10 @@ func WorkspacesCreate() http.HandlerFunc { }{Error: fmt.Sprintf("error getting user home dir: %v\n", err)}, "workspace") } - //configMaps, err := clientset.CoreV1().ConfigMaps("argo").List(r.Context(), metav1.ListOptions{}) - //if err != nil { - // WriteResponse(w, http.StatusInternalServerError, nil, struct { - // Error string - // }{Error: fmt.Sprintf("error getting user home dir: %v\n", err)}, "workspace") - //} - //var cmlist []string - //for _, cm := range configMaps.Items { - // str, _ := json.Marshal(cm) - // cmlist = append(cmlist, fmt.Sprintf("CM %s", str)) - //} - WriteResponse(w, http.StatusOK, nil, struct { - NamespaceName string - CM []string + Workspace string }{ - //NamespaceName: namespace.Name, - //CM: cmlist, + Workspace: fmt.Sprintf("The Workspace has been created %s", creationData.Name), }, "workspace") }) }