Merge pull request #51324 from thaJeztah/network_connect_disconnect

api/types/network: define `ConnectRequest` and `DisconnectRequest`
This commit is contained in:
Paweł Gronowski
2025-10-29 12:56:15 +01:00
committed by GitHub
14 changed files with 176 additions and 65 deletions

View File

@@ -2842,6 +2842,42 @@ definitions:
type: "string"
x-nullable: false
NetworkConnectRequest:
description: |
NetworkConnectRequest represents the data to be used to connect a container to a network.
type: "object"
x-go-name: "ConnectRequest"
required: ["Container"]
properties:
Container:
type: "string"
description: "The ID or name of the container to connect to the network."
x-nullable: false
example: "3613f73ba0e4"
EndpointConfig:
$ref: "#/definitions/EndpointSettings"
x-nullable: true
NetworkDisconnectRequest:
description: |
NetworkDisconnectRequest represents the data to be used to disconnect a container from a network.
type: "object"
x-go-name: "DisconnectRequest"
required: ["Container"]
properties:
Container:
type: "string"
description: "The ID or name of the container to disconnect from the network."
x-nullable: false
example: "3613f73ba0e4"
Force:
type: "boolean"
description: "Force the container to disconnect from the network."
default: false
x-nullable: false
x-omitempty: false
example: false
EndpointSettings:
description: "Configuration for a network endpoint."
type: "object"
@@ -11359,22 +11395,7 @@ paths:
in: "body"
required: true
schema:
type: "object"
title: "NetworkConnectRequest"
properties:
Container:
type: "string"
description: "The ID or name of the container to connect to the network."
EndpointConfig:
$ref: "#/definitions/EndpointSettings"
example:
Container: "3613f73ba0e4"
EndpointConfig:
IPAMConfig:
IPv4Address: "172.24.56.89"
IPv6Address: "2001:db8::5689"
MacAddress: "02:42:ac:12:05:02"
Priority: 100
$ref: "#/definitions/NetworkConnectRequest"
tags: ["Network"]
/networks/{id}/disconnect:
@@ -11408,17 +11429,7 @@ paths:
in: "body"
required: true
schema:
type: "object"
title: "NetworkDisconnectRequest"
properties:
Container:
type: "string"
description: |
The ID or name of the container to disconnect from the network.
Force:
type: "boolean"
description: |
Force the container to disconnect from the network.
$ref: "#/definitions/NetworkDisconnectRequest"
tags: ["Network"]
/networks/prune:
post:

View File

@@ -2842,6 +2842,42 @@ definitions:
type: "string"
x-nullable: false
NetworkConnectRequest:
description: |
NetworkConnectRequest represents the data to be used to connect a container to a network.
type: "object"
x-go-name: "ConnectRequest"
required: ["Container"]
properties:
Container:
type: "string"
description: "The ID or name of the container to connect to the network."
x-nullable: false
example: "3613f73ba0e4"
EndpointConfig:
$ref: "#/definitions/EndpointSettings"
x-nullable: true
NetworkDisconnectRequest:
description: |
NetworkDisconnectRequest represents the data to be used to disconnect a container from a network.
type: "object"
x-go-name: "DisconnectRequest"
required: ["Container"]
properties:
Container:
type: "string"
description: "The ID or name of the container to disconnect from the network."
x-nullable: false
example: "3613f73ba0e4"
Force:
type: "boolean"
description: "Force the container to disconnect from the network."
default: false
x-nullable: false
x-omitempty: false
example: false
EndpointSettings:
description: "Configuration for a network endpoint."
type: "object"
@@ -11359,22 +11395,7 @@ paths:
in: "body"
required: true
schema:
type: "object"
title: "NetworkConnectRequest"
properties:
Container:
type: "string"
description: "The ID or name of the container to connect to the network."
EndpointConfig:
$ref: "#/definitions/EndpointSettings"
example:
Container: "3613f73ba0e4"
EndpointConfig:
IPAMConfig:
IPv4Address: "172.24.56.89"
IPv6Address: "2001:db8::5689"
MacAddress: "02:42:ac:12:05:02"
Priority: 100
$ref: "#/definitions/NetworkConnectRequest"
tags: ["Network"]
/networks/{id}/disconnect:
@@ -11408,17 +11429,7 @@ paths:
in: "body"
required: true
schema:
type: "object"
title: "NetworkDisconnectRequest"
properties:
Container:
type: "string"
description: |
The ID or name of the container to disconnect from the network.
Force:
type: "boolean"
description: |
Force the container to disconnect from the network.
$ref: "#/definitions/NetworkDisconnectRequest"
tags: ["Network"]
/networks/prune:
post:

View File

@@ -0,0 +1,20 @@
// Code generated by go-swagger; DO NOT EDIT.
package network
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
// ConnectRequest NetworkConnectRequest represents the data to be used to connect a container to a network.
//
// swagger:model ConnectRequest
type ConnectRequest struct {
// The ID or name of the container to connect to the network.
// Example: 3613f73ba0e4
// Required: true
Container string `json:"Container"`
// endpoint config
EndpointConfig *EndpointSettings `json:"EndpointConfig,omitempty"`
}

View File

@@ -0,0 +1,21 @@
// Code generated by go-swagger; DO NOT EDIT.
package network
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
// DisconnectRequest NetworkDisconnectRequest represents the data to be used to disconnect a container from a network.
//
// swagger:model DisconnectRequest
type DisconnectRequest struct {
// The ID or name of the container to disconnect from the network.
// Example: 3613f73ba0e4
// Required: true
Container string `json:"Container"`
// Force the container to disconnect from the network.
// Example: false
Force bool `json:"Force"`
}

View File

@@ -18,11 +18,11 @@ func (cli *Client) NetworkConnect(ctx context.Context, networkID, containerID st
return err
}
nc := NetworkConnectOptions{
req := network.ConnectRequest{
Container: containerID,
EndpointConfig: config,
}
resp, err := cli.post(ctx, "/networks/"+networkID+"/connect", nil, nc, nil)
resp, err := cli.post(ctx, "/networks/"+networkID+"/connect", nil, req, nil)
defer ensureReaderClosed(resp)
return err
}

View File

@@ -38,7 +38,7 @@ func TestNetworkConnectEmptyNilEndpointSettings(t *testing.T) {
return nil, err
}
var connect NetworkConnectOptions
var connect network.ConnectRequest
if err := json.NewDecoder(req.Body).Decode(&connect); err != nil {
return nil, err
}

View File

@@ -2,6 +2,8 @@ package client
import (
"context"
"github.com/moby/moby/api/types/network"
)
// NetworkDisconnect disconnects a container from an existent network in the docker host.
@@ -16,11 +18,11 @@ func (cli *Client) NetworkDisconnect(ctx context.Context, networkID, containerID
return err
}
nd := NetworkDisconnectOptions{
req := network.DisconnectRequest{
Container: containerID,
Force: force,
}
resp, err := cli.post(ctx, "/networks/"+networkID+"/disconnect", nil, nd, nil)
resp, err := cli.post(ctx, "/networks/"+networkID+"/disconnect", nil, req, nil)
defer ensureReaderClosed(resp)
return err
}

View File

@@ -8,6 +8,7 @@ import (
"testing"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/moby/api/types/network"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
@@ -37,7 +38,7 @@ func TestNetworkDisconnect(t *testing.T) {
return nil, err
}
var disconnect NetworkDisconnectOptions
var disconnect network.DisconnectRequest
if err := json.NewDecoder(req.Body).Decode(&disconnect); err != nil {
return nil, err
}

View File

@@ -65,7 +65,9 @@ generate_model types/network --keep-spec-order --additional-initialism=IPAM <<-
EndpointResource
IPAMStatus
Network
NetworkConnectRequest
NetworkCreateResponse
NetworkDisconnectRequest
NetworkInspect
NetworkStatus
NetworkSummary

View File

@@ -236,7 +236,7 @@ func createNetwork(t *testing.T, config network.CreateRequest, expectedStatusCod
}
func connectNetwork(t *testing.T, nid, cid string) {
resp, _, err := request.Post(testutil.GetContext(t), "/networks/"+nid+"/connect", request.JSONBody(client.NetworkConnectOptions{
resp, _, err := request.Post(testutil.GetContext(t), "/networks/"+nid+"/connect", request.JSONBody(network.ConnectRequest{
Container: cid,
}))
assert.NilError(t, err)

View File

@@ -0,0 +1,20 @@
// Code generated by go-swagger; DO NOT EDIT.
package network
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
// ConnectRequest NetworkConnectRequest represents the data to be used to connect a container to a network.
//
// swagger:model ConnectRequest
type ConnectRequest struct {
// The ID or name of the container to connect to the network.
// Example: 3613f73ba0e4
// Required: true
Container string `json:"Container"`
// endpoint config
EndpointConfig *EndpointSettings `json:"EndpointConfig,omitempty"`
}

View File

@@ -0,0 +1,21 @@
// Code generated by go-swagger; DO NOT EDIT.
package network
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
// DisconnectRequest NetworkDisconnectRequest represents the data to be used to disconnect a container from a network.
//
// swagger:model DisconnectRequest
type DisconnectRequest struct {
// The ID or name of the container to disconnect from the network.
// Example: 3613f73ba0e4
// Required: true
Container string `json:"Container"`
// Force the container to disconnect from the network.
// Example: false
Force bool `json:"Force"`
}

View File

@@ -18,11 +18,11 @@ func (cli *Client) NetworkConnect(ctx context.Context, networkID, containerID st
return err
}
nc := NetworkConnectOptions{
req := network.ConnectRequest{
Container: containerID,
EndpointConfig: config,
}
resp, err := cli.post(ctx, "/networks/"+networkID+"/connect", nil, nc, nil)
resp, err := cli.post(ctx, "/networks/"+networkID+"/connect", nil, req, nil)
defer ensureReaderClosed(resp)
return err
}

View File

@@ -2,6 +2,8 @@ package client
import (
"context"
"github.com/moby/moby/api/types/network"
)
// NetworkDisconnect disconnects a container from an existent network in the docker host.
@@ -16,11 +18,11 @@ func (cli *Client) NetworkDisconnect(ctx context.Context, networkID, containerID
return err
}
nd := NetworkDisconnectOptions{
req := network.DisconnectRequest{
Container: containerID,
Force: force,
}
resp, err := cli.post(ctx, "/networks/"+networkID+"/disconnect", nil, nd, nil)
resp, err := cli.post(ctx, "/networks/"+networkID+"/disconnect", nil, req, nil)
defer ensureReaderClosed(resp)
return err
}