github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/libpod/networks.go (about) 1 package libpod 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 "github.com/containers/podman/v2/libpod" 8 "github.com/containers/podman/v2/libpod/define" 9 "github.com/containers/podman/v2/libpod/network" 10 "github.com/containers/podman/v2/pkg/api/handlers/utils" 11 "github.com/containers/podman/v2/pkg/domain/entities" 12 "github.com/containers/podman/v2/pkg/domain/infra/abi" 13 "github.com/gorilla/schema" 14 "github.com/pkg/errors" 15 ) 16 17 func CreateNetwork(w http.ResponseWriter, r *http.Request) { 18 runtime := r.Context().Value("runtime").(*libpod.Runtime) 19 decoder := r.Context().Value("decoder").(*schema.Decoder) 20 options := entities.NetworkCreateOptions{} 21 if err := json.NewDecoder(r.Body).Decode(&options); err != nil { 22 utils.Error(w, "unable to marshall input", http.StatusInternalServerError, errors.Wrap(err, "Decode()")) 23 return 24 } 25 query := struct { 26 Name string `schema:"name"` 27 }{ 28 // override any golang type defaults 29 } 30 if err := decoder.Decode(&query, r.URL.Query()); err != nil { 31 utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, 32 errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) 33 return 34 } 35 if len(options.Driver) < 1 { 36 options.Driver = network.DefaultNetworkDriver 37 } 38 ic := abi.ContainerEngine{Libpod: runtime} 39 report, err := ic.NetworkCreate(r.Context(), query.Name, options) 40 if err != nil { 41 utils.InternalServerError(w, err) 42 return 43 } 44 utils.WriteResponse(w, http.StatusOK, report) 45 46 } 47 func ListNetworks(w http.ResponseWriter, r *http.Request) { 48 runtime := r.Context().Value("runtime").(*libpod.Runtime) 49 decoder := r.Context().Value("decoder").(*schema.Decoder) 50 query := struct { 51 Filter string `schema:"filter"` 52 }{ 53 // override any golang type defaults 54 } 55 if err := decoder.Decode(&query, r.URL.Query()); err != nil { 56 utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, 57 errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) 58 return 59 } 60 61 options := entities.NetworkListOptions{ 62 Filter: query.Filter, 63 } 64 ic := abi.ContainerEngine{Libpod: runtime} 65 reports, err := ic.NetworkList(r.Context(), options) 66 if err != nil { 67 utils.InternalServerError(w, err) 68 return 69 } 70 utils.WriteResponse(w, http.StatusOK, reports) 71 } 72 73 func RemoveNetwork(w http.ResponseWriter, r *http.Request) { 74 runtime := r.Context().Value("runtime").(*libpod.Runtime) 75 decoder := r.Context().Value("decoder").(*schema.Decoder) 76 query := struct { 77 Force bool `schema:"force"` 78 }{ 79 // override any golang type defaults 80 } 81 if err := decoder.Decode(&query, r.URL.Query()); err != nil { 82 utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, 83 errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) 84 return 85 } 86 name := utils.GetName(r) 87 88 options := entities.NetworkRmOptions{ 89 Force: query.Force, 90 } 91 ic := abi.ContainerEngine{Libpod: runtime} 92 reports, err := ic.NetworkRm(r.Context(), []string{name}, options) 93 if err != nil { 94 utils.InternalServerError(w, err) 95 return 96 } 97 if reports[0].Err != nil { 98 // If the network cannot be found, we return a 404. 99 if errors.Cause(reports[0].Err) == define.ErrNoSuchNetwork { 100 utils.Error(w, "Something went wrong", http.StatusNotFound, reports[0].Err) 101 return 102 } 103 } 104 utils.WriteResponse(w, http.StatusOK, reports) 105 } 106 107 func InspectNetwork(w http.ResponseWriter, r *http.Request) { 108 runtime := r.Context().Value("runtime").(*libpod.Runtime) 109 decoder := r.Context().Value("decoder").(*schema.Decoder) 110 query := struct { 111 }{ 112 // override any golang type defaults 113 } 114 if err := decoder.Decode(&query, r.URL.Query()); err != nil { 115 utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, 116 errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) 117 return 118 } 119 name := utils.GetName(r) 120 options := entities.InspectOptions{} 121 ic := abi.ContainerEngine{Libpod: runtime} 122 reports, errs, err := ic.NetworkInspect(r.Context(), []string{name}, options) 123 // If the network cannot be found, we return a 404. 124 if len(errs) > 0 { 125 utils.Error(w, "Something went wrong", http.StatusNotFound, define.ErrNoSuchNetwork) 126 return 127 } 128 if err != nil { 129 utils.InternalServerError(w, err) 130 return 131 } 132 utils.WriteResponse(w, http.StatusOK, reports) 133 } 134 135 // Connect adds a container to a network 136 func Connect(w http.ResponseWriter, r *http.Request) { 137 runtime := r.Context().Value("runtime").(*libpod.Runtime) 138 139 var netConnect entities.NetworkConnectOptions 140 if err := json.NewDecoder(r.Body).Decode(&netConnect); err != nil { 141 utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "Decode()")) 142 return 143 } 144 name := utils.GetName(r) 145 err := runtime.ConnectContainerToNetwork(netConnect.Container, name, netConnect.Aliases) 146 if err != nil { 147 if errors.Cause(err) == define.ErrNoSuchCtr { 148 utils.ContainerNotFound(w, netConnect.Container, err) 149 return 150 } 151 if errors.Cause(err) == define.ErrNoSuchNetwork { 152 utils.Error(w, "network not found", http.StatusNotFound, err) 153 return 154 } 155 utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err) 156 return 157 } 158 utils.WriteResponse(w, http.StatusOK, "OK") 159 }