diff options
Diffstat (limited to 'pkg/api')
-rw-r--r-- | pkg/api/handlers/compat/networks.go | 37 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/networks.go | 26 | ||||
-rw-r--r-- | pkg/api/server/register_networks.go | 54 |
3 files changed, 108 insertions, 9 deletions
diff --git a/pkg/api/handlers/compat/networks.go b/pkg/api/handlers/compat/networks.go index 64ddebf9c..c74cdb840 100644 --- a/pkg/api/handlers/compat/networks.go +++ b/pkg/api/handlers/compat/networks.go @@ -289,25 +289,44 @@ func CreateNetwork(w http.ResponseWriter, r *http.Request) { func RemoveNetwork(w http.ResponseWriter, r *http.Request) { runtime := r.Context().Value("runtime").(*libpod.Runtime) - config, err := runtime.GetConfig() - if err != nil { - utils.InternalServerError(w, err) + ic := abi.ContainerEngine{Libpod: runtime} + + query := struct { + Force bool `schema:"force"` + }{ + // This is where you can override the golang default value for one of fields + } + + decoder := r.Context().Value("decoder").(*schema.Decoder) + if err := decoder.Decode(&query, r.URL.Query()); err != nil { + utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) return } + + options := entities.NetworkRmOptions{ + Force: query.Force, + } + name := utils.GetName(r) - exists, err := network.Exists(config, name) + reports, err := ic.NetworkRm(r.Context(), []string{name}, options) if err != nil { - utils.InternalServerError(w, err) + utils.Error(w, "remove Network failed", http.StatusInternalServerError, err) return } - if !exists { - utils.Error(w, "network not found", http.StatusNotFound, define.ErrNoSuchNetwork) + if len(reports) == 0 { + utils.Error(w, "remove Network failed", http.StatusInternalServerError, errors.Errorf("internal error")) return } - if err := network.RemoveNetwork(config, name); err != nil { - utils.InternalServerError(w, err) + report := reports[0] + if report.Err != nil { + if errors.Cause(report.Err) == define.ErrNoSuchNetwork { + utils.Error(w, "network not found", http.StatusNotFound, define.ErrNoSuchNetwork) + return + } + utils.InternalServerError(w, report.Err) return } + utils.WriteResponse(w, http.StatusNoContent, "") } diff --git a/pkg/api/handlers/libpod/networks.go b/pkg/api/handlers/libpod/networks.go index 78e525f1f..f1578f829 100644 --- a/pkg/api/handlers/libpod/networks.go +++ b/pkg/api/handlers/libpod/networks.go @@ -131,3 +131,29 @@ func InspectNetwork(w http.ResponseWriter, r *http.Request) { } utils.WriteResponse(w, http.StatusOK, reports) } + +// Connect adds a container to a network +func Connect(w http.ResponseWriter, r *http.Request) { + runtime := r.Context().Value("runtime").(*libpod.Runtime) + + var netConnect entities.NetworkConnectOptions + if err := json.NewDecoder(r.Body).Decode(&netConnect); err != nil { + utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "Decode()")) + return + } + name := utils.GetName(r) + err := runtime.ConnectContainerToNetwork(netConnect.Container, name, netConnect.Aliases) + if err != nil { + if errors.Cause(err) == define.ErrNoSuchCtr { + utils.ContainerNotFound(w, netConnect.Container, err) + return + } + if errors.Cause(err) == define.ErrNoSuchNetwork { + utils.Error(w, "network not found", http.StatusNotFound, err) + return + } + utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err) + return + } + utils.WriteResponse(w, http.StatusOK, "OK") +} diff --git a/pkg/api/server/register_networks.go b/pkg/api/server/register_networks.go index 6222006e5..ea169cbdf 100644 --- a/pkg/api/server/register_networks.go +++ b/pkg/api/server/register_networks.go @@ -253,5 +253,59 @@ func (s *APIServer) registerNetworkHandlers(r *mux.Router) error { // 500: // $ref: "#/responses/InternalError" r.HandleFunc(VersionedPath("/libpod/networks/create"), s.APIHandler(libpod.CreateNetwork)).Methods(http.MethodPost) + // swagger:operation POST /libpod/networks/{name}/connect libpod libpodConnectNetwork + // --- + // tags: + // - networks + // summary: Connect container to network + // description: Connect a container to a network. + // produces: + // - application/json + // parameters: + // - in: path + // name: name + // type: string + // required: true + // description: the name of the network + // - in: body + // name: create + // description: attributes for connecting a container to a network + // schema: + // $ref: "#/definitions/NetworkConnectRequest" + // responses: + // 200: + // description: OK + // 404: + // $ref: "#/responses/NoSuchNetwork" + // 500: + // $ref: "#/responses/InternalError" + r.HandleFunc(VersionedPath("/libpod/networks/{name}/connect"), s.APIHandler(libpod.Connect)).Methods(http.MethodPost) + // swagger:operation POST /libpod/networks/{name}/disconnect libpod libpodDisconnectNetwork + // --- + // tags: + // - networks + // summary: Disconnect container from network + // description: Disconnect a container from a network. + // produces: + // - application/json + // parameters: + // - in: path + // name: name + // type: string + // required: true + // description: the name of the network + // - in: body + // name: create + // description: attributes for disconnecting a container from a network + // schema: + // $ref: "#/definitions/NetworkDisconnectRequest" + // responses: + // 200: + // description: OK + // 404: + // $ref: "#/responses/NoSuchNetwork" + // 500: + // $ref: "#/responses/InternalError" + r.HandleFunc(VersionedPath("/libpod/networks/{name}/disconnect"), s.APIHandler(compat.Disconnect)).Methods(http.MethodPost) return nil } |