summaryrefslogtreecommitdiff
path: root/pkg/api/handlers/libpod/containers_create.go
blob: 38a341a89763d676d624d7c41a2a111d389d3327 (plain)
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
package libpod

import (
	"encoding/json"
	"net/http"

	"github.com/containers/libpod/libpod"
	"github.com/containers/libpod/pkg/api/handlers/utils"
	"github.com/containers/libpod/pkg/specgen"
	"github.com/containers/libpod/pkg/specgen/generate"
	"github.com/pkg/errors"
)

// CreateContainer takes a specgenerator and makes a container. It returns
// the new container ID on success along with any warnings.
func CreateContainer(w http.ResponseWriter, r *http.Request) {
	runtime := r.Context().Value("runtime").(*libpod.Runtime)
	var sg specgen.SpecGenerator
	if err := json.NewDecoder(r.Body).Decode(&sg); err != nil {
		utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "Decode()"))
		return
	}
	if err := generate.CompleteSpec(r.Context(), runtime, &sg); err != nil {
		utils.InternalServerError(w, err)
		return
	}
	ctr, err := generate.MakeContainer(runtime, &sg)
	if err != nil {
		utils.InternalServerError(w, err)
		return
	}
	response := utils.ContainerCreateResponse{ID: ctr.ID()}
	utils.WriteJSON(w, http.StatusCreated, response)
}