func createAppHelper(instance *app.App, u *auth.User, units uint) ([]byte, error) { teams, err := u.Teams() if err != nil { return nil, err } if len(teams) < 1 { msg := "In order to create an app, you should be member of at least one team" return nil, &errors.Http{Code: http.StatusForbidden, Message: msg} } instance.SetTeams(teams) err = app.CreateApp(instance, units) if err != nil { log.Printf("Got error while creating app: %s", err) if e, ok := err.(*app.ValidationError); ok { return nil, &errors.Http{Code: http.StatusPreconditionFailed, Message: e.Message} } if strings.Contains(err.Error(), "key error") { msg := fmt.Sprintf(`There is already an app named "%s".`, instance.Name) return nil, &errors.Http{Code: http.StatusConflict, Message: msg} } return nil, err } msg := map[string]string{ "status": "success", "repository_url": repository.GetUrl(instance.Name), } return json.Marshal(msg) }
func (a *App) MarshalJSON() ([]byte, error) { result := make(map[string]interface{}) result["Name"] = a.Name result["State"] = a.State result["Framework"] = a.Framework result["Teams"] = a.Teams result["Units"] = a.Units result["Repository"] = repository.GetUrl(a.Name) return json.Marshal(&result) }
// MarshalJSON marshals the app in json format. It returns a JSON object with // the following keys: Name, Framework, Teams, Units, Repository and Ip. func (app *App) MarshalJSON() ([]byte, error) { result := make(map[string]interface{}) result["name"] = app.Name result["framework"] = app.Platform result["teams"] = app.Teams result["units"] = app.Units result["repository"] = repository.GetUrl(app.Name) result["ip"] = app.Ip result["cname"] = app.CName return json.Marshal(&result) }
func createApp(w http.ResponseWriter, r *http.Request, t *auth.Token) error { var a app.App var japp jsonApp defer r.Body.Close() body, err := ioutil.ReadAll(r.Body) if err != nil { return err } if err = json.Unmarshal(body, &japp); err != nil { return err } a.Name = japp.Name a.Platform = japp.Platform if japp.Units == 0 { japp.Units = 1 } u, err := t.User() if err != nil { return err } rec.Log(u.Email, "create-app", "name="+japp.Name, "platform="+japp.Platform, fmt.Sprintf("units=%d", japp.Units)) teams, err := u.Teams() if err != nil { return err } if len(teams) < 1 { msg := "In order to create an app, you should be member of at least one team" return &errors.Http{Code: http.StatusForbidden, Message: msg} } err = app.CreateApp(&a, japp.Units, teams) if err != nil { log.Printf("Got error while creating app: %s", err) if e, ok := err.(*errors.ValidationError); ok { return &errors.Http{Code: http.StatusBadRequest, Message: e.Message} } if strings.Contains(err.Error(), "key error") { msg := fmt.Sprintf(`There is already an app named "%s".`, a.Name) return &errors.Http{Code: http.StatusConflict, Message: msg} } return err } msg := map[string]string{ "status": "success", "repository_url": repository.GetUrl(a.Name), } jsonMsg, err := json.Marshal(msg) if err != nil { return err } fmt.Fprintf(w, "%s", jsonMsg) return nil }
func (s *S) TestAppMarshalJson(c *C) { app := App{ Name: "Name", State: "State", Framework: "Framework", Teams: []string{"team1"}, } expected := make(map[string]interface{}) expected["Name"] = "Name" expected["State"] = "State" expected["Framework"] = "Framework" expected["Repository"] = repository.GetUrl(app.Name) expected["Teams"] = []interface{}{"team1"} expected["Units"] = interface{}(nil) data, err := app.MarshalJSON() c.Assert(err, IsNil) result := make(map[string]interface{}) err = json.Unmarshal(data, &result) c.Assert(err, IsNil) c.Assert(result, DeepEquals, expected) }
func (s *S) TestAppMarshalJson(c *gocheck.C) { app := App{ Name: "name", Framework: "Framework", Teams: []string{"team1"}, Ip: "10.10.10.1", CName: "name.mycompany.com", } expected := make(map[string]interface{}) expected["Name"] = "name" expected["Framework"] = "Framework" expected["Repository"] = repository.GetUrl(app.Name) expected["Teams"] = []interface{}{"team1"} expected["Units"] = nil expected["Ip"] = "10.10.10.1" expected["CName"] = "name.mycompany.com" data, err := app.MarshalJSON() c.Assert(err, gocheck.IsNil) result := make(map[string]interface{}) err = json.Unmarshal(data, &result) c.Assert(err, gocheck.IsNil) c.Assert(result, gocheck.DeepEquals, expected) }