github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/api/handlers/utils/containers.go (about) 1 package utils 2 3 import ( 4 "context" 5 "net/http" 6 "time" 7 8 "github.com/containers/libpod/cmd/podman/shared" 9 "github.com/containers/libpod/libpod" 10 "github.com/containers/libpod/libpod/define" 11 createconfig "github.com/containers/libpod/pkg/spec" 12 "github.com/gorilla/schema" 13 "github.com/pkg/errors" 14 ) 15 16 // ContainerCreateResponse is the response struct for creating a container 17 type ContainerCreateResponse struct { 18 // ID of the container created 19 ID string `json:"Id"` 20 // Warnings during container creation 21 Warnings []string `json:"Warnings"` 22 } 23 24 func WaitContainer(w http.ResponseWriter, r *http.Request) (int32, error) { 25 var ( 26 err error 27 interval time.Duration 28 ) 29 runtime := r.Context().Value("runtime").(*libpod.Runtime) 30 decoder := r.Context().Value("decoder").(*schema.Decoder) 31 query := struct { 32 Interval string `schema:"interval"` 33 Condition string `schema:"condition"` 34 }{ 35 // Override golang default values for types 36 } 37 if err := decoder.Decode(&query, r.URL.Query()); err != nil { 38 Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "Failed to parse parameters for %s", r.URL.String())) 39 return 0, err 40 } 41 if _, found := r.URL.Query()["interval"]; found { 42 interval, err = time.ParseDuration(query.Interval) 43 if err != nil { 44 InternalServerError(w, err) 45 return 0, err 46 } 47 } else { 48 interval, err = time.ParseDuration("250ms") 49 if err != nil { 50 InternalServerError(w, err) 51 return 0, err 52 } 53 } 54 condition := define.ContainerStateStopped 55 if _, found := r.URL.Query()["condition"]; found { 56 condition, err = define.StringToContainerStatus(query.Condition) 57 if err != nil { 58 InternalServerError(w, err) 59 return 0, err 60 } 61 } 62 name := GetName(r) 63 con, err := runtime.LookupContainer(name) 64 if err != nil { 65 ContainerNotFound(w, name, err) 66 return 0, err 67 } 68 return con.WaitForConditionWithInterval(interval, condition) 69 } 70 71 // GenerateFilterFuncsFromMap is used to generate un-executed functions that can be used to filter 72 // containers. It is specifically designed for the RESTFUL API input. 73 func GenerateFilterFuncsFromMap(r *libpod.Runtime, filters map[string][]string) ([]libpod.ContainerFilter, error) { 74 var ( 75 filterFuncs []libpod.ContainerFilter 76 ) 77 for k, v := range filters { 78 for _, val := range v { 79 f, err := shared.GenerateContainerFilterFuncs(k, val, r) 80 if err != nil { 81 return filterFuncs, err 82 } 83 filterFuncs = append(filterFuncs, f) 84 } 85 } 86 return filterFuncs, nil 87 } 88 89 func CreateContainer(ctx context.Context, w http.ResponseWriter, runtime *libpod.Runtime, cc *createconfig.CreateConfig) { 90 var pod *libpod.Pod 91 ctr, err := shared.CreateContainerFromCreateConfig(runtime, cc, ctx, pod) 92 if err != nil { 93 Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "CreateContainerFromCreateConfig()")) 94 return 95 } 96 97 response := ContainerCreateResponse{ 98 ID: ctr.ID(), 99 Warnings: []string{}} 100 101 WriteResponse(w, http.StatusCreated, response) 102 }