forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_post.go
85 lines (67 loc) · 1.69 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
)
func containerPost(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
c, err := containerLoadByName(d, name)
if err != nil {
return SmartError(err)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return InternalError(err)
}
rdr1 := ioutil.NopCloser(bytes.NewBuffer(body))
rdr2 := ioutil.NopCloser(bytes.NewBuffer(body))
reqRaw := shared.Jmap{}
err = json.NewDecoder(rdr1).Decode(&reqRaw)
if err != nil {
return BadRequest(err)
}
req := api.ContainerPost{}
err = json.NewDecoder(rdr2).Decode(&req)
if err != nil {
return BadRequest(err)
}
// Check if stateful (backward compatibility)
stateful := true
_, err = reqRaw.GetBool("live")
if err == nil {
stateful = req.Live
}
if req.Migration {
ws, err := NewMigrationSource(c, stateful, req.ContainerOnly)
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)
}
// Check that the name isn't already in use
id, _ := dbContainerId(d.db, req.Name)
if id > 0 {
return Conflict
}
run := func(*operation) error {
return c.Rename(req.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)
}