Explorar o código

gorm migration issue

Alexander Belanger %!s(int64=5) %!d(string=hai) anos
pai
achega
1fbb01379a

+ 1 - 1
cmd/app/main.go

@@ -34,7 +34,7 @@ func main() {
 
 	validator := vr.New()
 
-	a := api.New(logger, repo, validator, store, appConf.Server.CookieName)
+	a := api.New(logger, repo, validator, store, &appConf.Helm, appConf.Server.CookieName)
 
 	appRouter := router.New(a, store, appConf.Server.CookieName)
 

+ 2 - 1
internal/forms/chart.go

@@ -20,7 +20,8 @@ func (lcf *ListChartForm) PopulateHelmOptions(repo repository.UserRepository) er
 		return err
 	}
 
-	lcf.HelmOptions.AllowedContexts = user.Contexts
+	lcf.HelmOptions.AllowedContexts = user.ContextToSlice()
+
 	lcf.HelmOptions.KubeConfig = user.RawKubeConfig
 	return nil
 }

+ 6 - 2
internal/forms/user.go

@@ -1,6 +1,8 @@
 package forms
 
 import (
+	"strings"
+
 	"github.com/porter-dev/porter/internal/kubernetes"
 	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/repository"
@@ -85,7 +87,7 @@ func (uuf *UpdateUserForm) ToUser(repo repository.UserRepository) (*models.User,
 
 	// if the allowedContexts is nil, query the DB for a non-nil one
 	if uuf.AllowedContexts == nil {
-		contexts = savedUser.Contexts
+		contexts = savedUser.ContextToSlice()
 	}
 
 	if len(rawBytes) > 0 {
@@ -106,11 +108,13 @@ func (uuf *UpdateUserForm) ToUser(repo repository.UserRepository) (*models.User,
 		}
 	}
 
+	contextsJoin := strings.Join(contexts, ",")
+
 	return &models.User{
 		Model: gorm.Model{
 			ID: uuf.ID,
 		},
-		Contexts:      contexts,
+		Contexts:      contextsJoin,
 		RawKubeConfig: rawBytes,
 	}, nil
 }

+ 18 - 11
internal/models/user.go

@@ -1,6 +1,8 @@
 package models
 
 import (
+	"strings"
+
 	"gorm.io/gorm"
 )
 
@@ -8,10 +10,10 @@ import (
 type User struct {
 	gorm.Model
 
-	Email         string   `json:"email" gorm:"unique"`
-	Password      string   `json:"password"`
-	Contexts      []string `json:"contexts"`
-	RawKubeConfig []byte   `json:"rawKubeConfig"`
+	Email         string `json:"email" gorm:"unique"`
+	Password      string `json:"password"`
+	Contexts      string `json:"contexts"`
+	RawKubeConfig []byte `json:"rawKubeConfig"`
 }
 
 // UserExternal represents the User type that is sent over REST
@@ -24,16 +26,21 @@ type UserExternal struct {
 
 // Externalize generates an external User to be shared over REST
 func (u *User) Externalize() *UserExternal {
-	contexts := u.Contexts
-
-	if contexts == nil {
-		contexts = []string{}
-	}
-
 	return &UserExternal{
 		ID:            u.ID,
 		Email:         u.Email,
-		Contexts:      contexts,
+		Contexts:      u.ContextToSlice(),
 		RawKubeConfig: string(u.RawKubeConfig),
 	}
 }
+
+// ContextToSlice converts the serialized context string to an array of strings
+func (u *User) ContextToSlice() []string {
+	contexts := strings.Split(u.Contexts, ",")
+
+	if u.Contexts == "" {
+		contexts = make([]string, 0)
+	}
+
+	return contexts
+}

+ 1 - 1
internal/models/user_test.go

@@ -15,7 +15,7 @@ func TestUserExternalize(t *testing.T) {
 		},
 		Email:         "testing@testing.com",
 		Password:      "testing123",
-		Contexts:      []string{"test"},
+		Contexts:      "test",
 		RawKubeConfig: []byte{},
 	}
 

+ 1 - 1
server/api/user_handler.go

@@ -162,7 +162,7 @@ func (app *App) HandleReadUserContexts(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	contexts, err := kubernetes.GetContextsFromBytes(user.RawKubeConfig, user.Contexts)
+	contexts, err := kubernetes.GetContextsFromBytes(user.RawKubeConfig, user.ContextToSlice())
 
 	if err != nil {
 		app.handleErrorFormDecoding(err, ErrUserDecode, w)

+ 4 - 1
server/api/user_handler_test.go

@@ -2,6 +2,7 @@ package api_test
 
 import (
 	"encoding/json"
+	"fmt"
 	"net/http"
 	"net/http/httptest"
 	"reflect"
@@ -378,6 +379,8 @@ var updateUserTests = []*userTest{
 				json.Unmarshal(rr2.Body.Bytes(), gotBody)
 				json.Unmarshal([]byte(`{"id":1,"email":"belanger@getporter.dev","contexts":[],"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n    server: https://localhost\n  name: cluster-test\ncontexts:\n- context:\n    cluster: cluster-test\n    user: test-admin\n  name: context-test\nusers:\n- name: test-admin"}`), expBody)
 
+				fmt.Println(rr2.Body.String())
+
 				if !reflect.DeepEqual(gotBody, expBody) {
 					t.Errorf("%s, handler returned wrong body: got %v want %v",
 						"validator failed", gotBody, expBody)
@@ -636,7 +639,7 @@ func initUserWithContexts(tester *tester) {
 	initUserDefault(tester)
 
 	user, _ := tester.repo.User.ReadUserByEmail("belanger@getporter.dev")
-	user.Contexts = []string{"context-test"}
+	user.Contexts = "context-test"
 
 	user.RawKubeConfig = []byte("apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n    server: https://localhost\n  name: cluster-test\ncontexts:\n- context:\n    cluster: cluster-test\n    user: test-admin\n  name: context-test\nusers:\n- name: test-admin")