示例#1
0
func (s *S) TestInstallHostList(c *check.C) {
	token := userWithPermission(c, permission.Permission{
		Scheme:  permission.PermInstallManage,
		Context: permission.Context(permission.CtxGlobal, ""),
	})
	host1 := &install.Host{Name: "my-host-1", DriverName: "amazonec2", Driver: make(map[string]interface{})}
	err := install.AddHost(host1)
	c.Assert(err, check.IsNil)
	host2 := &install.Host{Name: "my-host-2", DriverName: "amazonec2", Driver: make(map[string]interface{})}
	err = install.AddHost(host2)
	c.Assert(err, check.IsNil)
	defer s.conn.InstallHosts().RemoveAll(nil)
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("GET", "/install/hosts", nil)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+token.GetValue())
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	m := RunServer(true)
	m.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "application/json")
	hosts := []install.Host{}
	err = json.Unmarshal(recorder.Body.Bytes(), &hosts)
	c.Assert(err, check.IsNil)
	c.Assert(hosts, check.DeepEquals, []install.Host{*host1, *host2})
}
示例#2
0
文件: install.go 项目: tsuru/tsuru
// title: add install host
// path: /install/hosts
// method: POST
// consume: application/x-www-form-urlencoded
// produce: application/json
// responses:
//   201: Host added
//   401: Unauthorized
func installHostAdd(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
	allowed := permission.Check(t, permission.PermInstallManage)
	if !allowed {
		return permission.ErrUnauthorized
	}
	err = r.ParseForm()
	if err != nil {
		return err
	}
	dec := form.NewDecoder(nil)
	dec.IgnoreUnknownKeys(true)
	dec.IgnoreCase(true)
	var host *install.Host
	err = dec.DecodeValues(&host, r.Form)
	if err != nil {
		return err
	}
	evt, err := event.New(&event.Opts{
		Target:     event.Target{Type: event.TargetTypeInstallHost, Value: host.Name},
		Kind:       permission.PermInstallManage,
		Owner:      t,
		CustomData: event.FormToCustomData(r.Form),
		Allowed:    event.Allowed(permission.PermInstallManage),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	var rawDriver map[string]interface{}
	err = json.Unmarshal([]byte(r.Form.Get("driver")), &rawDriver)
	if err != nil {
		return err
	}
	host.Driver = rawDriver
	err = install.AddHost(host)
	if err != nil {
		return err
	}
	w.WriteHeader(http.StatusCreated)
	return nil
}