Browse code

api/types/container: create type for changes endpoint

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2023/04/16 16:40:24
Showing 12 changed files
... ...
@@ -1610,6 +1610,34 @@ definitions:
1610 1610
           "WorkDir": "/var/lib/docker/overlay2/ef749362d13333e65fc95c572eb525abbe0052e16e086cb64bc3b98ae9aa6d74/work"
1611 1611
         }
1612 1612
 
1613
+  FilesystemChange:
1614
+    description: |
1615
+      Change in the container's filesystem.
1616
+    type: "object"
1617
+    required: [Path, Kind]
1618
+    properties:
1619
+      Path:
1620
+        description: |
1621
+          Path to file or directory that has changed.
1622
+        type: "string"
1623
+        x-nullable: false
1624
+      Kind:
1625
+        $ref: "#/definitions/ChangeType"
1626
+
1627
+  ChangeType:
1628
+    description: |
1629
+      Kind of change
1630
+
1631
+      Can be one of:
1632
+
1633
+      - `0`: Modified ("C")
1634
+      - `1`: Added ("A")
1635
+      - `2`: Deleted ("D")
1636
+    type: "integer"
1637
+    format: "uint8"
1638
+    enum: [0, 1, 2]
1639
+    x-nullable: false
1640
+
1613 1641
   ImageInspect:
1614 1642
     description: |
1615 1643
       Information about an image in the local image cache.
... ...
@@ -6876,9 +6904,9 @@ paths:
6876 6876
         Returns which files in a container's filesystem have been added, deleted,
6877 6877
         or modified. The `Kind` of modification can be one of:
6878 6878
 
6879
-        - `0`: Modified
6880
-        - `1`: Added
6881
-        - `2`: Deleted
6879
+        - `0`: Modified ("C")
6880
+        - `1`: Added ("A")
6881
+        - `2`: Deleted ("D")
6882 6882
       operationId: "ContainerChanges"
6883 6883
       produces: ["application/json"]
6884 6884
       responses:
... ...
@@ -6887,22 +6915,7 @@ paths:
6887 6887
           schema:
6888 6888
             type: "array"
6889 6889
             items:
6890
-              type: "object"
6891
-              x-go-name: "ContainerChangeResponseItem"
6892
-              title: "ContainerChangeResponseItem"
6893
-              description: "change item in response to ContainerChanges operation"
6894
-              required: [Path, Kind]
6895
-              properties:
6896
-                Path:
6897
-                  description: "Path to file that has changed"
6898
-                  type: "string"
6899
-                  x-nullable: false
6900
-                Kind:
6901
-                  description: "Kind of change"
6902
-                  type: "integer"
6903
-                  format: "uint8"
6904
-                  enum: [0, 1, 2]
6905
-                  x-nullable: false
6890
+              $ref: "#/definitions/FilesystemChange"
6906 6891
           examples:
6907 6892
             application/json:
6908 6893
               - Path: "/dev"
6909 6894
new file mode 100644
... ...
@@ -0,0 +1,6 @@
0
+package container
1
+
2
+// ContainerChangeResponseItem change item in response to ContainerChanges operation
3
+//
4
+// Deprecated: use [FilesystemChange].
5
+type ContainerChangeResponseItem = FilesystemChange
0 6
new file mode 100644
... ...
@@ -0,0 +1,15 @@
0
+package container
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
+// ChangeType Kind of change
6
+//
7
+// Can be one of:
8
+//
9
+// - `0`: Modified ("C")
10
+// - `1`: Added ("A")
11
+// - `2`: Deleted ("D")
12
+//
13
+// swagger:model ChangeType
14
+type ChangeType uint8
0 15
new file mode 100644
... ...
@@ -0,0 +1,23 @@
0
+package container
1
+
2
+const (
3
+	// ChangeModify represents the modify operation.
4
+	ChangeModify ChangeType = 0
5
+	// ChangeAdd represents the add operation.
6
+	ChangeAdd ChangeType = 1
7
+	// ChangeDelete represents the delete operation.
8
+	ChangeDelete ChangeType = 2
9
+)
10
+
11
+func (ct ChangeType) String() string {
12
+	switch ct {
13
+	case ChangeModify:
14
+		return "C"
15
+	case ChangeAdd:
16
+		return "A"
17
+	case ChangeDelete:
18
+		return "D"
19
+	default:
20
+		return ""
21
+	}
22
+}
0 23
deleted file mode 100644
... ...
@@ -1,20 +0,0 @@
1
-package container // import "github.com/docker/docker/api/types/container"
2
-
3
-// ----------------------------------------------------------------------------
4
-// Code generated by `swagger generate operation`. DO NOT EDIT.
5
-//
6
-// See hack/generate-swagger-api.sh
7
-// ----------------------------------------------------------------------------
8
-
9
-// ContainerChangeResponseItem change item in response to ContainerChanges operation
10
-// swagger:model ContainerChangeResponseItem
11
-type ContainerChangeResponseItem struct {
12
-
13
-	// Kind of change
14
-	// Required: true
15
-	Kind uint8 `json:"Kind"`
16
-
17
-	// Path to file that has changed
18
-	// Required: true
19
-	Path string `json:"Path"`
20
-}
21 1
new file mode 100644
... ...
@@ -0,0 +1,19 @@
0
+package container
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
+// FilesystemChange Change in the container's filesystem.
6
+//
7
+// swagger:model FilesystemChange
8
+type FilesystemChange struct {
9
+
10
+	// kind
11
+	// Required: true
12
+	Kind ChangeType `json:"Kind"`
13
+
14
+	// Path to file or directory that has changed.
15
+	//
16
+	// Required: true
17
+	Path string `json:"Path"`
18
+}
... ...
@@ -9,8 +9,8 @@ import (
9 9
 )
10 10
 
11 11
 // ContainerDiff shows differences in a container filesystem since it was started.
12
-func (cli *Client) ContainerDiff(ctx context.Context, containerID string) ([]container.ContainerChangeResponseItem, error) {
13
-	var changes []container.ContainerChangeResponseItem
12
+func (cli *Client) ContainerDiff(ctx context.Context, containerID string) ([]container.FilesystemChange, error) {
13
+	var changes []container.FilesystemChange
14 14
 
15 15
 	serverResp, err := cli.get(ctx, "/containers/"+containerID+"/changes", url.Values{}, nil)
16 16
 	defer ensureReaderClosed(serverResp)
... ...
@@ -31,13 +31,13 @@ func TestContainerDiff(t *testing.T) {
31 31
 			if !strings.HasPrefix(req.URL.Path, expectedURL) {
32 32
 				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
33 33
 			}
34
-			b, err := json.Marshal([]container.ContainerChangeResponseItem{
34
+			b, err := json.Marshal([]container.FilesystemChange{
35 35
 				{
36
-					Kind: 0,
36
+					Kind: container.ChangeModify,
37 37
 					Path: "/path/1",
38 38
 				},
39 39
 				{
40
-					Kind: 1,
40
+					Kind: container.ChangeAdd,
41 41
 					Path: "/path/2",
42 42
 				},
43 43
 			})
... ...
@@ -48,7 +48,7 @@ type ContainerAPIClient interface {
48 48
 	ContainerAttach(ctx context.Context, container string, options types.ContainerAttachOptions) (types.HijackedResponse, error)
49 49
 	ContainerCommit(ctx context.Context, container string, options types.ContainerCommitOptions) (types.IDResponse, error)
50 50
 	ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *specs.Platform, containerName string) (container.CreateResponse, error)
51
-	ContainerDiff(ctx context.Context, container string) ([]container.ContainerChangeResponseItem, error)
51
+	ContainerDiff(ctx context.Context, container string) ([]container.FilesystemChange, error)
52 52
 	ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error)
53 53
 	ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error)
54 54
 	ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error)
... ...
@@ -20,7 +20,9 @@ swagger generate model -f api/swagger.yaml \
20 20
 	-t api -m types/container --skip-validator -C api/swagger-gen.yaml \
21 21
 	-n ContainerCreateResponse \
22 22
 	-n ContainerWaitResponse \
23
-	-n ContainerWaitExitError
23
+	-n ContainerWaitExitError \
24
+	-n ChangeType \
25
+	-n FilesystemChange
24 26
 
25 27
 swagger generate model -f api/swagger.yaml \
26 28
 	-t api -m types/volume --skip-validator -C api/swagger-gen.yaml \
... ...
@@ -32,7 +34,6 @@ swagger generate operation -f api/swagger.yaml \
32 32
 	-t api -a types -m types -C api/swagger-gen.yaml \
33 33
 	-T api/templates --skip-responses --skip-parameters --skip-validator \
34 34
 	-n Authenticate \
35
-	-n ContainerChanges \
36 35
 	-n ContainerTop \
37 36
 	-n ContainerUpdate \
38 37
 	-n ImageHistory
... ...
@@ -7,7 +7,6 @@ import (
7 7
 
8 8
 	containertypes "github.com/docker/docker/api/types/container"
9 9
 	"github.com/docker/docker/integration/internal/container"
10
-	"github.com/docker/docker/pkg/archive"
11 10
 	"gotest.tools/v3/assert"
12 11
 	"gotest.tools/v3/poll"
13 12
 	"gotest.tools/v3/skip"
... ...
@@ -25,15 +24,15 @@ func TestDiff(t *testing.T) {
25 25
 	// it will take a few seconds to exit. Also there's no way in Windows to
26 26
 	// differentiate between an Add or a Modify, and all files are under
27 27
 	// a "Files/" prefix.
28
-	expected := []containertypes.ContainerChangeResponseItem{
29
-		{Kind: archive.ChangeAdd, Path: "/foo"},
30
-		{Kind: archive.ChangeAdd, Path: "/foo/bar"},
28
+	expected := []containertypes.FilesystemChange{
29
+		{Kind: containertypes.ChangeAdd, Path: "/foo"},
30
+		{Kind: containertypes.ChangeAdd, Path: "/foo/bar"},
31 31
 	}
32 32
 	if testEnv.OSType == "windows" {
33 33
 		poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(60*time.Second))
34
-		expected = []containertypes.ContainerChangeResponseItem{
35
-			{Kind: archive.ChangeModify, Path: "Files/foo"},
36
-			{Kind: archive.ChangeModify, Path: "Files/foo/bar"},
34
+		expected = []containertypes.FilesystemChange{
35
+			{Kind: containertypes.ChangeModify, Path: "Files/foo"},
36
+			{Kind: containertypes.ChangeModify, Path: "Files/foo/bar"},
37 37
 		}
38 38
 	}
39 39
 
... ...
@@ -450,8 +450,8 @@ func testGraphDriver(ctx context.Context, t *testing.T, c client.APIClient, driv
450 450
 
451 451
 	diffs, err := c.ContainerDiff(ctx, id)
452 452
 	assert.NilError(t, err)
453
-	assert.Check(t, is.Contains(diffs, containertypes.ContainerChangeResponseItem{
454
-		Kind: archive.ChangeAdd,
453
+	assert.Check(t, is.Contains(diffs, containertypes.FilesystemChange{
454
+		Kind: containertypes.ChangeAdd,
455 455
 		Path: "/hello",
456 456
 	}), "diffs: %v", diffs)
457 457