Browse code

Merge pull request #49444 from thaJeztah/commit_and_exect_response

api/types/container: introduce CommitResponse, ExecCreateResponse types

Sebastiaan van Stijn authored on 2025/02/11 20:53:09
Showing 14 changed files
... ...
@@ -71,7 +71,7 @@ func (c *containerRouter) postCommit(ctx context.Context, w http.ResponseWriter,
71 71
 		return err
72 72
 	}
73 73
 
74
-	return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{ID: imgID})
74
+	return httputils.WriteJSON(w, http.StatusCreated, &container.CommitResponse{ID: imgID})
75 75
 }
76 76
 
77 77
 func (c *containerRouter) getContainersJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
... ...
@@ -61,7 +61,7 @@ func (c *containerRouter) postContainerExecCreate(ctx context.Context, w http.Re
61 61
 		return err
62 62
 	}
63 63
 
64
-	return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{
64
+	return httputils.WriteJSON(w, http.StatusCreated, &container.ExecCreateResponse{
65 65
 		ID: id,
66 66
 	})
67 67
 }
... ...
@@ -2908,9 +2908,10 @@ definitions:
2908 2908
     example:
2909 2909
       message: "Something went wrong."
2910 2910
 
2911
-  IdResponse:
2911
+  IDResponse:
2912 2912
     description: "Response to an API call that returns just an Id"
2913 2913
     type: "object"
2914
+    x-go-name: "IDResponse"
2914 2915
     required: ["Id"]
2915 2916
     properties:
2916 2917
       Id:
... ...
@@ -10233,7 +10234,7 @@ paths:
10233 10233
         201:
10234 10234
           description: "no error"
10235 10235
           schema:
10236
-            $ref: "#/definitions/IdResponse"
10236
+            $ref: "#/definitions/IDResponse"
10237 10237
         404:
10238 10238
           description: "no such container"
10239 10239
           schema:
... ...
@@ -10627,7 +10628,7 @@ paths:
10627 10627
         201:
10628 10628
           description: "no error"
10629 10629
           schema:
10630
-            $ref: "#/definitions/IdResponse"
10630
+            $ref: "#/definitions/IDResponse"
10631 10631
         404:
10632 10632
           description: "no such container"
10633 10633
           schema:
... ...
@@ -13107,7 +13108,7 @@ paths:
13107 13107
         201:
13108 13108
           description: "no error"
13109 13109
           schema:
13110
-            $ref: "#/definitions/IdResponse"
13110
+            $ref: "#/definitions/IDResponse"
13111 13111
         409:
13112 13112
           description: "name conflicts with an existing object"
13113 13113
           schema:
... ...
@@ -13314,7 +13315,7 @@ paths:
13314 13314
         201:
13315 13315
           description: "no error"
13316 13316
           schema:
13317
-            $ref: "#/definitions/IdResponse"
13317
+            $ref: "#/definitions/IDResponse"
13318 13318
         409:
13319 13319
           description: "name conflicts with an existing object"
13320 13320
           schema:
13321 13321
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+package common
1
+
2
+// This file was generated by the swagger tool.
3
+// Editing this file might prove futile when you re-run the swagger generate command
4
+
5
+// IDResponse Response to an API call that returns just an Id
6
+// swagger:model IDResponse
7
+type IDResponse struct {
8
+
9
+	// The id of the newly created object.
10
+	// Required: true
11
+	ID string `json:"Id"`
12
+}
0 13
new file mode 100644
... ...
@@ -0,0 +1,7 @@
0
+package container
1
+
2
+import "github.com/docker/docker/api/types/common"
3
+
4
+// CommitResponse response for the commit API call, containing the ID of the
5
+// image that was produced.
6
+type CommitResponse = common.IDResponse
... ...
@@ -1,5 +1,13 @@
1 1
 package container
2 2
 
3
+import "github.com/docker/docker/api/types/common"
4
+
5
+// ExecCreateResponse is the response for a successful exec-create request.
6
+// It holds the ID of the exec that was created.
7
+//
8
+// TODO(thaJeztah): make this a distinct type.
9
+type ExecCreateResponse = common.IDResponse
10
+
3 11
 // ExecOptions is a small subset of the Config struct that holds the configuration
4 12
 // for the exec feature of docker.
5 13
 type ExecOptions struct {
6 14
deleted file mode 100644
... ...
@@ -1,13 +0,0 @@
1
-package types
2
-
3
-// This file was generated by the swagger tool.
4
-// Editing this file might prove futile when you re-run the swagger generate command
5
-
6
-// IDResponse Response to an API call that returns just an Id
7
-// swagger:model IdResponse
8
-type IDResponse struct {
9
-
10
-	// The id of the newly created object.
11
-	// Required: true
12
-	ID string `json:"Id"`
13
-}
... ...
@@ -3,11 +3,15 @@ package types
3 3
 import (
4 4
 	"context"
5 5
 
6
+	"github.com/docker/docker/api/types/common"
6 7
 	"github.com/docker/docker/api/types/container"
7 8
 	"github.com/docker/docker/api/types/image"
8 9
 	"github.com/docker/docker/api/types/storage"
9 10
 )
10 11
 
12
+// IDResponse Response to an API call that returns just an Id
13
+type IDResponse = common.IDResponse
14
+
11 15
 // ContainerJSONBase contains response of Engine API GET "/containers/{name:.*}/json"
12 16
 // for API version 1.18 and older.
13 17
 //
... ...
@@ -69,11 +69,11 @@ type HijackDialer interface {
69 69
 // ContainerAPIClient defines API client methods for the containers
70 70
 type ContainerAPIClient interface {
71 71
 	ContainerAttach(ctx context.Context, container string, options container.AttachOptions) (types.HijackedResponse, error)
72
-	ContainerCommit(ctx context.Context, container string, options container.CommitOptions) (types.IDResponse, error)
72
+	ContainerCommit(ctx context.Context, container string, options container.CommitOptions) (container.CommitResponse, error)
73 73
 	ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error)
74 74
 	ContainerDiff(ctx context.Context, container string) ([]container.FilesystemChange, error)
75 75
 	ContainerExecAttach(ctx context.Context, execID string, options container.ExecAttachOptions) (types.HijackedResponse, error)
76
-	ContainerExecCreate(ctx context.Context, container string, options container.ExecOptions) (types.IDResponse, error)
76
+	ContainerExecCreate(ctx context.Context, container string, options container.ExecOptions) (container.ExecCreateResponse, error)
77 77
 	ContainerExecInspect(ctx context.Context, execID string) (container.ExecInspect, error)
78 78
 	ContainerExecResize(ctx context.Context, execID string, options container.ResizeOptions) error
79 79
 	ContainerExecStart(ctx context.Context, execID string, options container.ExecStartOptions) error
... ...
@@ -7,26 +7,25 @@ import (
7 7
 	"net/url"
8 8
 
9 9
 	"github.com/distribution/reference"
10
-	"github.com/docker/docker/api/types"
11 10
 	"github.com/docker/docker/api/types/container"
12 11
 )
13 12
 
14 13
 // ContainerCommit applies changes to a container and creates a new tagged image.
15
-func (cli *Client) ContainerCommit(ctx context.Context, containerID string, options container.CommitOptions) (types.IDResponse, error) {
14
+func (cli *Client) ContainerCommit(ctx context.Context, containerID string, options container.CommitOptions) (container.CommitResponse, error) {
16 15
 	containerID, err := trimID("container", containerID)
17 16
 	if err != nil {
18
-		return types.IDResponse{}, err
17
+		return container.CommitResponse{}, err
19 18
 	}
20 19
 
21 20
 	var repository, tag string
22 21
 	if options.Reference != "" {
23 22
 		ref, err := reference.ParseNormalizedNamed(options.Reference)
24 23
 		if err != nil {
25
-			return types.IDResponse{}, err
24
+			return container.CommitResponse{}, err
26 25
 		}
27 26
 
28 27
 		if _, isCanonical := ref.(reference.Canonical); isCanonical {
29
-			return types.IDResponse{}, errors.New("refusing to create a tag with a digest reference")
28
+			return container.CommitResponse{}, errors.New("refusing to create a tag with a digest reference")
30 29
 		}
31 30
 		ref = reference.TagNameOnly(ref)
32 31
 
... ...
@@ -49,7 +48,7 @@ func (cli *Client) ContainerCommit(ctx context.Context, containerID string, opti
49 49
 		query.Set("pause", "0")
50 50
 	}
51 51
 
52
-	var response types.IDResponse
52
+	var response container.CommitResponse
53 53
 	resp, err := cli.post(ctx, "/commit", query, options.Config, nil)
54 54
 	defer ensureReaderClosed(resp)
55 55
 	if err != nil {
... ...
@@ -10,7 +10,6 @@ import (
10 10
 	"strings"
11 11
 	"testing"
12 12
 
13
-	"github.com/docker/docker/api/types"
14 13
 	"github.com/docker/docker/api/types/container"
15 14
 	"github.com/docker/docker/errdefs"
16 15
 	"gotest.tools/v3/assert"
... ...
@@ -77,7 +76,7 @@ func TestContainerCommit(t *testing.T) {
77 77
 			if len(changes) != len(expectedChanges) {
78 78
 				return nil, fmt.Errorf("expected container changes size to be '%d', got %d", len(expectedChanges), len(changes))
79 79
 			}
80
-			b, err := json.Marshal(types.IDResponse{
80
+			b, err := json.Marshal(container.CommitResponse{
81 81
 				ID: "new_container_id",
82 82
 			})
83 83
 			if err != nil {
... ...
@@ -11,10 +11,10 @@ import (
11 11
 )
12 12
 
13 13
 // ContainerExecCreate creates a new exec configuration to run an exec process.
14
-func (cli *Client) ContainerExecCreate(ctx context.Context, containerID string, options container.ExecOptions) (types.IDResponse, error) {
14
+func (cli *Client) ContainerExecCreate(ctx context.Context, containerID string, options container.ExecOptions) (container.ExecCreateResponse, error) {
15 15
 	containerID, err := trimID("container", containerID)
16 16
 	if err != nil {
17
-		return types.IDResponse{}, err
17
+		return container.ExecCreateResponse{}, err
18 18
 	}
19 19
 
20 20
 	// Make sure we negotiated (if the client is configured to do so),
... ...
@@ -23,11 +23,11 @@ func (cli *Client) ContainerExecCreate(ctx context.Context, containerID string,
23 23
 	// Normally, version-negotiation (if enabled) would not happen until
24 24
 	// the API request is made.
25 25
 	if err := cli.checkVersion(ctx); err != nil {
26
-		return types.IDResponse{}, err
26
+		return container.ExecCreateResponse{}, err
27 27
 	}
28 28
 
29 29
 	if err := cli.NewVersionError(ctx, "1.25", "env"); len(options.Env) != 0 && err != nil {
30
-		return types.IDResponse{}, err
30
+		return container.ExecCreateResponse{}, err
31 31
 	}
32 32
 	if versions.LessThan(cli.ClientVersion(), "1.42") {
33 33
 		options.ConsoleSize = nil
... ...
@@ -36,10 +36,10 @@ func (cli *Client) ContainerExecCreate(ctx context.Context, containerID string,
36 36
 	resp, err := cli.post(ctx, "/containers/"+containerID+"/exec", nil, options, nil)
37 37
 	defer ensureReaderClosed(resp)
38 38
 	if err != nil {
39
-		return types.IDResponse{}, err
39
+		return container.ExecCreateResponse{}, err
40 40
 	}
41 41
 
42
-	var response types.IDResponse
42
+	var response container.ExecCreateResponse
43 43
 	err = json.NewDecoder(resp.body).Decode(&response)
44 44
 	return response, err
45 45
 }
... ...
@@ -10,7 +10,6 @@ import (
10 10
 	"strings"
11 11
 	"testing"
12 12
 
13
-	"github.com/docker/docker/api/types"
14 13
 	"github.com/docker/docker/api/types/container"
15 14
 	"github.com/docker/docker/errdefs"
16 15
 	"gotest.tools/v3/assert"
... ...
@@ -67,7 +66,7 @@ func TestContainerExecCreate(t *testing.T) {
67 67
 			if execConfig.User != "user" {
68 68
 				return nil, fmt.Errorf("expected an execConfig with User == 'user', got %v", execConfig)
69 69
 			}
70
-			b, err := json.Marshal(types.IDResponse{
70
+			b, err := json.Marshal(container.ExecCreateResponse{
71 71
 				ID: "exec_id",
72 72
 			})
73 73
 			if err != nil {
... ...
@@ -4,7 +4,6 @@ set -eu
4 4
 swagger generate model -f api/swagger.yaml \
5 5
 	-t api -m types --skip-validator -C api/swagger-gen.yaml \
6 6
 	-n ErrorResponse \
7
-	-n IdResponse \
8 7
 	-n Plugin \
9 8
 	-n PluginDevice \
10 9
 	-n PluginMount \
... ...
@@ -12,6 +11,10 @@ swagger generate model -f api/swagger.yaml \
12 12
 	-n PluginInterfaceType
13 13
 
14 14
 swagger generate model -f api/swagger.yaml \
15
+	-t api -m types/common --skip-validator -C api/swagger-gen.yaml \
16
+	-n IDResponse
17
+
18
+swagger generate model -f api/swagger.yaml \
15 19
 	-t api -m types/storage --skip-validator -C api/swagger-gen.yaml \
16 20
 	-n DriverData
17 21