forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_instance.go
68 lines (56 loc) · 1.51 KB
/
new_instance.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
package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/provisioner"
"github.com/play-with-docker/play-with-docker/pwd/types"
)
func NewInstance(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]
body := types.InstanceConfig{PlaygroundFQDN: req.Host}
json.NewDecoder(req.Body).Decode(&body)
s, err := core.SessionGet(sessionId)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
playground := core.PlaygroundGet(s.PlaygroundId)
if playground == nil {
log.Printf("Playground with id %s for session %s was not found!", s.PlaygroundId, s.Id)
rw.WriteHeader(http.StatusBadRequest)
return
}
if body.Type == "windows" && !playground.AllowWindowsInstances {
rw.WriteHeader(http.StatusUnauthorized)
return
}
instances, err := core.InstanceFindBySession(s)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
if playground.MaxInstances > 0 && len(instances) >= playground.MaxInstances {
log.Println(err)
rw.WriteHeader(http.StatusConflict)
return
}
i, err := core.InstanceNew(s, body)
if err != nil {
if provisioner.OutOfCapacity(err) {
rw.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintln(rw, `{"error": "out_of_capacity"}`)
return
}
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
//TODO: Set a status error
} else {
json.NewEncoder(rw).Encode(i)
}
}