func CreateInstanceHandler(w http.ResponseWriter, r *http.Request, u *auth.User) error { log.Print("Receiving request to create a service instance") b, err := ioutil.ReadAll(r.Body) if err != nil { log.Print("Got error while reading request body:") log.Print(err.Error()) return &errors.Http{Code: http.StatusInternalServerError, Message: err.Error()} } var sJson map[string]string err = json.Unmarshal(b, &sJson) if err != nil { log.Print("Got a problem while unmarshalling request's json:") log.Print(err.Error()) return &errors.Http{Code: http.StatusInternalServerError, Message: err.Error()} } var s service.Service err = validateInstanceForCreation(&s, sJson, u) if err != nil { log.Print("Got error while validation:") log.Print(err.Error()) return err } var teamNames []string teams, err := u.Teams() if err != nil { return err } for _, t := range teams { if s.HasTeam(&t) || !s.IsRestricted { teamNames = append(teamNames, t.Name) } } si := service.ServiceInstance{ Name: sJson["name"], ServiceName: sJson["service_name"], Teams: teamNames, } if err = s.ProductionEndpoint().Create(&si); err != nil { log.Print("Error while calling create action from service api.") log.Print(err.Error()) return err } err = si.Create() if err != nil { return err } fmt.Fprint(w, "success") return nil }
func createServiceInstance(w http.ResponseWriter, r *http.Request, t *auth.Token) error { b, err := ioutil.ReadAll(r.Body) if err != nil { return err } var sJson map[string]string err = json.Unmarshal(b, &sJson) if err != nil { return err } u, err := t.User() if err != nil { return err } rec.Log(u.Email, "create-service-instance", string(b)) var s service.Service err = validateInstanceForCreation(&s, sJson, u) if err != nil { return err } var teamNames []string teams, err := u.Teams() if err != nil { return err } for _, t := range teams { if s.HasTeam(&t) || !s.IsRestricted { teamNames = append(teamNames, t.Name) } } si := service.ServiceInstance{ Name: sJson["name"], ServiceName: sJson["service_name"], Teams: teamNames, } err = service.CreateInstance(&si) if err != nil { return err } fmt.Fprint(w, "success") return nil }