Alexander Belanger 5 лет назад
Родитель
Сommit
0ec9ef6d29
3 измененных файлов с 124 добавлено и 52 удалено
  1. 46 52
      server/api/chart_handler.go
  2. 77 0
      server/api/chart_handler_test.go
  3. 1 0
      server/router/router.go

+ 46 - 52
server/api/chart_handler.go

@@ -2,7 +2,6 @@ package api
 
 import (
 	"encoding/json"
-	"fmt"
 	"net/http"
 	"net/url"
 	"strconv"
@@ -197,65 +196,64 @@ func (app *App) HandleListChartHistory(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
-// HandleUpdateChart upgrades a chart with new values.yaml
-// func (app *App) HandleUpdateChart(w http.ResponseWriter, r *http.Request) {
-// 	session, err := app.store.Get(r, app.cookieName)
-
-// 	if err != nil {
-// 		app.handleErrorFormDecoding(err, ErrChartDecode, w)
-// 		return
-// 	}
+// HandleUpgradeChart upgrades a chart with new values.yaml
+func (app *App) HandleUpgradeChart(w http.ResponseWriter, r *http.Request) {
+	session, err := app.store.Get(r, app.cookieName)
 
-// 	name := chi.URLParam(r, "name")
+	if err != nil {
+		app.handleErrorFormDecoding(err, ErrChartDecode, w)
+		return
+	}
 
-// 	// get the filter options
-// 	form := &forms.ListChartHistoryForm{
-// 		ChartForm: &forms.ChartForm{
-// 			Form: &helm.Form{},
-// 		},
-// 		Name: name,
-// 	}
+	name := chi.URLParam(r, "name")
 
-// 	vals, err := url.ParseQuery(r.URL.RawQuery)
+	// get the filter options
+	form := &forms.UpgradeChartForm{
+		ChartForm: &forms.ChartForm{
+			Form: &helm.Form{},
+		},
+		Name: name,
+	}
 
-// 	if err != nil {
-// 		app.handleErrorFormDecoding(err, ErrChartDecode, w)
-// 		return
-// 	}
+	// decode from JSON to form value
+	if err := json.NewDecoder(r.Body).Decode(form); err != nil {
+		app.handleErrorFormDecoding(err, ErrUserDecode, w)
+		return
+	}
 
-// 	form.PopulateHelmOptionsFromQueryParams(vals)
+	if sessID, ok := session.Values["user_id"].(uint); ok {
+		form.PopulateHelmOptionsFromUserID(sessID, app.repo.User)
+	}
 
-// 	if sessID, ok := session.Values["user_id"].(uint); ok {
-// 		form.PopulateHelmOptionsFromUserID(sessID, app.repo.User)
-// 	}
+	// validate the form
+	if err := app.validator.Struct(form); err != nil {
+		app.handleErrorFormValidation(err, ErrChartValidateFields, w)
+		return
+	}
 
-// 	// validate the form
-// 	if err := app.validator.Struct(form); err != nil {
-// 		app.handleErrorFormValidation(err, ErrChartValidateFields, w)
-// 		return
-// 	}
+	// create a new agent
+	var agent *helm.Agent
 
-// 	// create a new agent
-// 	var agent *helm.Agent
+	if app.testing {
+		agent = app.TestAgents.HelmAgent
+	} else {
+		agent, err = helm.GetAgentOutOfClusterConfig(form.ChartForm.Form, app.logger)
+	}
 
-// 	if app.testing {
-// 		agent = app.TestAgents.HelmAgent
-// 	} else {
-// 		agent, err = helm.GetAgentOutOfClusterConfig(form.ChartForm.Form, app.logger)
-// 	}
+	rel, err := agent.UpgradeChart(form.Name, form.Values)
 
-// 	release, err := agent.GetReleaseHistory(form.Name)
+	if err != nil {
+		app.handleErrorInternal(err, w)
+		return
+	}
 
-// 	if err != nil {
-// 		app.handleErrorFormValidation(err, ErrChartValidateFields, w)
-// 		return
-// 	}
+	w.WriteHeader(http.StatusOK)
 
-// 	if err := json.NewEncoder(w).Encode(release); err != nil {
-// 		app.handleErrorFormDecoding(err, ErrChartDecode, w)
-// 		return
-// 	}
-// }
+	if err := json.NewEncoder(w).Encode(rel); err != nil {
+		app.handleErrorFormDecoding(err, ErrChartDecode, w)
+		return
+	}
+}
 
 // HandleRollbackChart rolls a release back to a specified revision
 func (app *App) HandleRollbackChart(w http.ResponseWriter, r *http.Request) {
@@ -310,9 +308,5 @@ func (app *App) HandleRollbackChart(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	release, err := agent.GetRelease("wordpress", 3)
-
-	fmt.Println("RELEASE IS", release)
-
 	w.WriteHeader(http.StatusOK)
 }

+ 77 - 0
server/api/chart_handler_test.go

@@ -197,6 +197,83 @@ func TestHandleListChartHistory(t *testing.T) {
 	testChartRequests(t, listChartHistoryTests, true)
 }
 
+var upgradeChartTests = []*chartTest{
+	&chartTest{
+		initializers: []func(tester *tester){
+			initHistoryCharts,
+		},
+		msg:       "Upgrade relase",
+		method:    "POST",
+		namespace: "default",
+		endpoint:  "/api/charts/wordpress/upgrade",
+		body: `
+			{
+				"namespace": "default",
+				"context": "context-test",
+				"storage": "memory",
+				"values": "\nfoo: bar\n"
+			}
+		`,
+		expStatus: http.StatusOK,
+		expBody:   ``,
+		useCookie: true,
+		validators: []func(c *chartTest, tester *tester, t *testing.T){
+			func(c *chartTest, tester *tester, t *testing.T) {
+				req, err := http.NewRequest(
+					"GET",
+					"/api/charts/wordpress/3?"+url.Values{
+						"namespace": []string{"default"},
+						"context":   []string{"context-test"},
+						"storage":   []string{"memory"},
+					}.Encode(),
+					strings.NewReader(""),
+				)
+
+				req.AddCookie(tester.cookie)
+
+				if err != nil {
+					t.Fatal(err)
+				}
+
+				rr2 := httptest.NewRecorder()
+				tester.router.ServeHTTP(rr2, req)
+
+				gotBody := &release.Release{}
+				expBody := &release.Release{}
+
+				expBodyJSON := releaseStubToChartJSON(releaseStub{"wordpress", "default", 3, "1.0.2", release.StatusDeployed})
+
+				json.Unmarshal(rr2.Body.Bytes(), gotBody)
+				json.Unmarshal([]byte(expBodyJSON), expBody)
+
+				// just check name and version match, other items will be different
+				if gotBody.Name != expBody.Name {
+					t.Errorf("%s, validation wrong body: got %v want %v",
+						c.msg, gotBody.Name, expBody.Name)
+				}
+
+				if gotBody.Version != expBody.Version {
+					t.Errorf("%s, validation wrong body: got %v want %v",
+						c.msg, gotBody.Version, expBody.Version)
+				}
+
+				expConfig := map[string]interface{}{
+					"foo": "bar",
+				}
+
+				if !reflect.DeepEqual(gotBody.Config, expConfig) {
+					t.Errorf("%s, validation wrong config: got %v want %v",
+						c.msg, gotBody.Config, expConfig)
+				}
+			},
+		},
+	},
+}
+
+func TestUpgradeChart(t *testing.T) {
+	testChartRequests(t, upgradeChartTests, true)
+}
+
 var rollbackChartTests = []*chartTest{
 	&chartTest{
 		initializers: []func(tester *tester){

+ 1 - 0
server/router/router.go

@@ -30,6 +30,7 @@ func New(a *api.App, store sessions.Store, cookieName string) *chi.Mux {
 		// /api/charts routes
 		r.Method("GET", "/charts", auth.BasicAuthenticate(requestlog.NewHandler(a.HandleListCharts, l)))
 		r.Method("GET", "/charts/{name}/history", auth.BasicAuthenticate(requestlog.NewHandler(a.HandleListChartHistory, l)))
+		r.Method("POST", "/charts/{name}/upgrade", auth.BasicAuthenticate(requestlog.NewHandler(a.HandleUpgradeChart, l)))
 		r.Method("GET", "/charts/{name}/{revision}", auth.BasicAuthenticate(requestlog.NewHandler(a.HandleGetChart, l)))
 		r.Method("POST", "/charts/rollback/{name}/{revision}", auth.BasicAuthenticate(requestlog.NewHandler(a.HandleRollbackChart, l)))