forked from tych0/lxd-pkg-ubuntu
/
container_post.go
63 lines (49 loc) · 1.22 KB
/
container_post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
)
type containerPostBody struct {
Migration bool `json:"migration"`
Name string `json:"name"`
}
func containerPost(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
c, err := containerLoadByName(d, name)
if err != nil {
return SmartError(err)
}
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return InternalError(err)
}
body := containerPostBody{}
if err := json.Unmarshal(buf, &body); err != nil {
return BadRequest(err)
}
if body.Migration {
ws, err := NewMigrationSource(c)
if err != nil {
return InternalError(err)
}
resources := map[string][]string{}
resources["containers"] = []string{name}
op, err := operationCreate(operationClassWebsocket, resources, ws.Metadata(), ws.Do, nil, ws.Connect)
if err != nil {
return InternalError(err)
}
return OperationResponse(op)
}
run := func(*operation) error {
return c.Rename(body.Name)
}
resources := map[string][]string{}
resources["containers"] = []string{name}
op, err := operationCreate(operationClassTask, resources, nil, run, nil, nil)
if err != nil {
return InternalError(err)
}
return OperationResponse(op)
}