// title: machine destroy // path: /iaas/machines/{machine_id} // method: DELETE // responses: // 200: OK // 400: Invalid data // 401: Unauthorized // 404: Not found func machineDestroy(w http.ResponseWriter, r *http.Request, token auth.Token) (err error) { r.ParseForm() machineID := r.URL.Query().Get(":machine_id") if machineID == "" { return &errors.HTTP{Code: http.StatusBadRequest, Message: "machine id is required"} } m, err := iaas.FindMachineById(machineID) if err != nil { if err == mgo.ErrNotFound { return &errors.HTTP{Code: http.StatusNotFound, Message: "machine not found"} } return err } iaasCtx := permission.Context(permission.CtxIaaS, m.Iaas) allowed := permission.Check(token, permission.PermMachineDelete, iaasCtx) if !allowed { return permission.ErrUnauthorized } evt, err := event.New(&event.Opts{ Target: event.Target{Type: event.TargetTypeIaas, Value: m.Iaas}, Kind: permission.PermMachineDelete, Owner: token, CustomData: event.FormToCustomData(r.Form), Allowed: event.Allowed(permission.PermMachineReadEvents, iaasCtx), }) if err != nil { return err } defer func() { evt.Done(err) }() return m.Destroy() }
func (runBs) destroyMachine(id string) { if id != "" { machine, err := iaas.FindMachineById(id) if err != nil { log.Errorf("failed to remove machine %q: %s", id, err) return } err = machine.Destroy() if err != nil { log.Errorf("failed to remove machine %q: %s", id, err) return } } }
func machineDestroy(w http.ResponseWriter, r *http.Request, token auth.Token) error { machineId := r.URL.Query().Get(":machine_id") if machineId == "" { return &errors.HTTP{Code: http.StatusBadRequest, Message: "machine id is required"} } m, err := iaas.FindMachineById(machineId) if err != nil { if err == mgo.ErrNotFound { return &errors.HTTP{Code: http.StatusNotFound, Message: "machine not found"} } return err } return m.Destroy() }
func (s *HandlersSuite) TestRemoveNodeHandlerRemoveIaaS(c *check.C) { iaas.RegisterIaasProvider("my-xxx-iaas", newTestIaaS) machine, err := iaas.CreateMachineForIaaS("my-xxx-iaas", map[string]string{}) c.Assert(err, check.IsNil) mainDockerProvisioner.cluster, err = cluster.New(nil, &cluster.MapStorage{}) c.Assert(err, check.IsNil) _, err = mainDockerProvisioner.getCluster().Register(fmt.Sprintf("http://%s:2375", machine.Address), nil) c.Assert(err, check.IsNil) b := bytes.NewBufferString(fmt.Sprintf(`{"address": "http://%s:2375", "remove_iaas": "true"}`, machine.Address)) req, err := http.NewRequest("POST", "/node/remove", b) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() err = removeNodeHandler(rec, req, nil) c.Assert(err, check.IsNil) nodes, err := mainDockerProvisioner.getCluster().Nodes() c.Assert(len(nodes), check.Equals, 0) _, err = iaas.FindMachineById(machine.Id) c.Assert(err, check.Equals, mgo.ErrNotFound) }
func machineDestroy(w http.ResponseWriter, r *http.Request, token auth.Token) error { machineId := r.URL.Query().Get(":machine_id") if machineId == "" { return &errors.HTTP{Code: http.StatusBadRequest, Message: "machine id is required"} } m, err := iaas.FindMachineById(machineId) if err != nil { if err == mgo.ErrNotFound { return &errors.HTTP{Code: http.StatusNotFound, Message: "machine not found"} } return err } allowed := permission.Check(token, permission.PermMachineDelete, permission.Context(permission.CtxIaaS, m.Iaas), ) if !allowed { return permission.ErrUnauthorized } return m.Destroy() }
func (s *HandlersSuite) TestRemoveNodeHandlerWithoutRemoveIaaS(c *gocheck.C) { iaas.RegisterIaasProvider("some-iaas", TestIaaS{}) machine, err := iaas.CreateMachineForIaaS("some-iaas", map[string]string{}) c.Assert(err, gocheck.IsNil) dCluster, err = cluster.New(nil, &cluster.MapStorage{}) c.Assert(err, gocheck.IsNil) err = dCluster.Register(fmt.Sprintf("http://%s:4243", machine.Address), nil) c.Assert(err, gocheck.IsNil) b := bytes.NewBufferString(fmt.Sprintf(`{"address": "http://%s:4243", "remove_iaas": "false"}`, machine.Address)) req, err := http.NewRequest("POST", "/node/remove", b) c.Assert(err, gocheck.IsNil) rec := httptest.NewRecorder() err = removeNodeHandler(rec, req, nil) c.Assert(err, gocheck.IsNil) nodes, err := dCluster.Nodes() c.Assert(len(nodes), gocheck.Equals, 0) dbM, err := iaas.FindMachineById(machine.Id) c.Assert(err, gocheck.IsNil) c.Assert(dbM.Id, gocheck.Equals, machine.Id) }
func (s *S) TestRemoveNodeHandlerWithRemoveIaaS(c *check.C) { iaas.RegisterIaasProvider("some-iaas", newTestIaaS) machine, err := iaas.CreateMachineForIaaS("some-iaas", map[string]string{"id": "m1"}) c.Assert(err, check.IsNil) err = s.provisioner.AddNode(provision.AddNodeOptions{ Address: fmt.Sprintf("http://%s:2375", machine.Address), }) c.Assert(err, check.IsNil) u := fmt.Sprintf("/node/http://%s:2375?remove-iaas=true", machine.Address) req, err := http.NewRequest("DELETE", u, nil) c.Assert(err, check.IsNil) req.Header.Set("Authorization", "bearer "+s.token.GetValue()) rec := httptest.NewRecorder() server := RunServer(true) server.ServeHTTP(rec, req) c.Assert(rec.Body.String(), check.Equals, "rebalancing...remove done!") c.Assert(rec.Code, check.Equals, http.StatusOK) nodes, err := s.provisioner.ListNodes(nil) c.Assert(err, check.IsNil) c.Assert(nodes, check.HasLen, 0) _, err = iaas.FindMachineById(machine.Id) c.Assert(err, check.Equals, mgo.ErrNotFound) }