client: refactor `ContainerRename` to wrap options/result structs
| ... | ... |
@@ -66,7 +66,7 @@ type ContainerAPIClient interface {
|
| 66 | 66 |
ContainerLogs(ctx context.Context, container string, options ContainerLogsOptions) (ContainerLogsResult, error) |
| 67 | 67 |
ContainerPause(ctx context.Context, container string, options ContainerPauseOptions) (ContainerPauseResult, error) |
| 68 | 68 |
ContainerRemove(ctx context.Context, container string, options ContainerRemoveOptions) (ContainerRemoveResult, error) |
| 69 |
- ContainerRename(ctx context.Context, container, newContainerName string) error |
|
| 69 |
+ ContainerRename(ctx context.Context, container string, options ContainerRenameOptions) (ContainerRenameResult, error) |
|
| 70 | 70 |
ContainerResize(ctx context.Context, container string, options ContainerResizeOptions) (ContainerResizeResult, error) |
| 71 | 71 |
ContainerRestart(ctx context.Context, container string, options ContainerRestartOptions) (ContainerRestartResult, error) |
| 72 | 72 |
ContainerStatPath(ctx context.Context, container, path string) (container.PathStat, error) |
| ... | ... |
@@ -5,16 +5,26 @@ import ( |
| 5 | 5 |
"net/url" |
| 6 | 6 |
) |
| 7 | 7 |
|
| 8 |
+// ContainerRenameOptions represents the options for renaming a container. |
|
| 9 |
+type ContainerRenameOptions struct {
|
|
| 10 |
+ NewName string |
|
| 11 |
+} |
|
| 12 |
+ |
|
| 13 |
+// ContainerRenameResult represents the result of a container rename operation. |
|
| 14 |
+type ContainerRenameResult struct {
|
|
| 15 |
+ // This struct can be expanded in the future if needed |
|
| 16 |
+} |
|
| 17 |
+ |
|
| 8 | 18 |
// ContainerRename changes the name of a given container. |
| 9 |
-func (cli *Client) ContainerRename(ctx context.Context, containerID, newContainerName string) error {
|
|
| 19 |
+func (cli *Client) ContainerRename(ctx context.Context, containerID string, options ContainerRenameOptions) (ContainerRenameResult, error) {
|
|
| 10 | 20 |
containerID, err := trimID("container", containerID)
|
| 11 | 21 |
if err != nil {
|
| 12 |
- return err |
|
| 22 |
+ return ContainerRenameResult{}, err
|
|
| 13 | 23 |
} |
| 14 | 24 |
|
| 15 | 25 |
query := url.Values{}
|
| 16 |
- query.Set("name", newContainerName)
|
|
| 26 |
+ query.Set("name", options.NewName)
|
|
| 17 | 27 |
resp, err := cli.post(ctx, "/containers/"+containerID+"/rename", query, nil, nil) |
| 18 | 28 |
defer ensureReaderClosed(resp) |
| 19 |
- return err |
|
| 29 |
+ return ContainerRenameResult{}, err
|
|
| 20 | 30 |
} |
| ... | ... |
@@ -14,14 +14,14 @@ import ( |
| 14 | 14 |
func TestContainerRenameError(t *testing.T) {
|
| 15 | 15 |
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error"))) |
| 16 | 16 |
assert.NilError(t, err) |
| 17 |
- err = client.ContainerRename(context.Background(), "nothing", "newNothing") |
|
| 17 |
+ _, err = client.ContainerRename(context.Background(), "nothing", ContainerRenameOptions{NewName: "newNothing"})
|
|
| 18 | 18 |
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal)) |
| 19 | 19 |
|
| 20 |
- err = client.ContainerRename(context.Background(), "", "newNothing") |
|
| 20 |
+ _, err = client.ContainerRename(context.Background(), "", ContainerRenameOptions{NewName: "newNothing"})
|
|
| 21 | 21 |
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument)) |
| 22 | 22 |
assert.Check(t, is.ErrorContains(err, "value is empty")) |
| 23 | 23 |
|
| 24 |
- err = client.ContainerRename(context.Background(), " ", "newNothing") |
|
| 24 |
+ _, err = client.ContainerRename(context.Background(), " ", ContainerRenameOptions{NewName: "newNothing"})
|
|
| 25 | 25 |
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument)) |
| 26 | 26 |
assert.Check(t, is.ErrorContains(err, "value is empty")) |
| 27 | 27 |
} |
| ... | ... |
@@ -40,6 +40,6 @@ func TestContainerRename(t *testing.T) {
|
| 40 | 40 |
})) |
| 41 | 41 |
assert.NilError(t, err) |
| 42 | 42 |
|
| 43 |
- err = client.ContainerRename(context.Background(), "container_id", "newName") |
|
| 43 |
+ _, err = client.ContainerRename(context.Background(), "container_id", ContainerRenameOptions{NewName: "newName"})
|
|
| 44 | 44 |
assert.NilError(t, err) |
| 45 | 45 |
} |
| ... | ... |
@@ -27,7 +27,7 @@ func TestRenameLinkedContainer(t *testing.T) {
|
| 27 | 27 |
aID := container.Run(ctx, t, apiClient, container.WithName(aName)) |
| 28 | 28 |
bID := container.Run(ctx, t, apiClient, container.WithName(bName), container.WithLinks(aName)) |
| 29 | 29 |
|
| 30 |
- err := apiClient.ContainerRename(ctx, aID, "a1"+t.Name()) |
|
| 30 |
+ _, err := apiClient.ContainerRename(ctx, aID, client.ContainerRenameOptions{NewName: "a1" + t.Name()})
|
|
| 31 | 31 |
assert.NilError(t, err) |
| 32 | 32 |
|
| 33 | 33 |
container.Run(ctx, t, apiClient, container.WithName(aName)) |
| ... | ... |
@@ -54,7 +54,7 @@ func TestRenameStoppedContainer(t *testing.T) {
|
| 54 | 54 |
assert.Check(t, is.Equal("/"+oldName, inspect.Container.Name))
|
| 55 | 55 |
|
| 56 | 56 |
newName := "new_name" + cID // using cID as random suffix |
| 57 |
- err = apiClient.ContainerRename(ctx, oldName, newName) |
|
| 57 |
+ _, err = apiClient.ContainerRename(ctx, oldName, client.ContainerRenameOptions{NewName: newName})
|
|
| 58 | 58 |
assert.NilError(t, err) |
| 59 | 59 |
|
| 60 | 60 |
inspect, err = apiClient.ContainerInspect(ctx, cID, client.ContainerInspectOptions{})
|
| ... | ... |
@@ -70,7 +70,7 @@ func TestRenameRunningContainerAndReuse(t *testing.T) {
|
| 70 | 70 |
cID := container.Run(ctx, t, apiClient, container.WithName(oldName)) |
| 71 | 71 |
|
| 72 | 72 |
newName := "new_name" + cID // using cID as random suffix |
| 73 |
- err := apiClient.ContainerRename(ctx, oldName, newName) |
|
| 73 |
+ _, err := apiClient.ContainerRename(ctx, oldName, client.ContainerRenameOptions{NewName: newName})
|
|
| 74 | 74 |
assert.NilError(t, err) |
| 75 | 75 |
|
| 76 | 76 |
inspect, err := apiClient.ContainerInspect(ctx, cID, client.ContainerInspectOptions{})
|
| ... | ... |
@@ -94,7 +94,7 @@ func TestRenameInvalidName(t *testing.T) {
|
| 94 | 94 |
oldName := "first_name" + t.Name() |
| 95 | 95 |
cID := container.Run(ctx, t, apiClient, container.WithName(oldName)) |
| 96 | 96 |
|
| 97 |
- err := apiClient.ContainerRename(ctx, oldName, "new:invalid") |
|
| 97 |
+ _, err := apiClient.ContainerRename(ctx, oldName, client.ContainerRenameOptions{NewName: "new:invalid"})
|
|
| 98 | 98 |
assert.Check(t, is.ErrorContains(err, "Invalid container name")) |
| 99 | 99 |
|
| 100 | 100 |
inspect, err := apiClient.ContainerInspect(ctx, oldName, client.ContainerInspectOptions{})
|
| ... | ... |
@@ -125,7 +125,7 @@ func TestRenameAnonymousContainer(t *testing.T) {
|
| 125 | 125 |
}) |
| 126 | 126 |
|
| 127 | 127 |
container1Name := "container1" + t.Name() |
| 128 |
- err = apiClient.ContainerRename(ctx, cID, container1Name) |
|
| 128 |
+ _, err = apiClient.ContainerRename(ctx, cID, client.ContainerRenameOptions{NewName: container1Name})
|
|
| 129 | 129 |
assert.NilError(t, err) |
| 130 | 130 |
// Stop/Start the container to get registered |
| 131 | 131 |
// FIXME(vdemeester) this is a really weird behavior as it fails otherwise |
| ... | ... |
@@ -158,9 +158,9 @@ func TestRenameContainerWithSameName(t *testing.T) {
|
| 158 | 158 |
|
| 159 | 159 |
oldName := "old" + t.Name() |
| 160 | 160 |
cID := container.Run(ctx, t, apiClient, container.WithName(oldName)) |
| 161 |
- err := apiClient.ContainerRename(ctx, oldName, oldName) |
|
| 161 |
+ _, err := apiClient.ContainerRename(ctx, oldName, client.ContainerRenameOptions{NewName: oldName})
|
|
| 162 | 162 |
assert.Check(t, is.ErrorContains(err, "Renaming a container with the same name")) |
| 163 |
- err = apiClient.ContainerRename(ctx, cID, oldName) |
|
| 163 |
+ _, err = apiClient.ContainerRename(ctx, cID, client.ContainerRenameOptions{NewName: oldName})
|
|
| 164 | 164 |
assert.Check(t, is.ErrorContains(err, "Renaming a container with the same name")) |
| 165 | 165 |
} |
| 166 | 166 |
|
| ... | ... |
@@ -183,7 +183,7 @@ func TestRenameContainerWithLinkedContainer(t *testing.T) {
|
| 183 | 183 |
app2Name := "app2" + t.Name() |
| 184 | 184 |
container.Run(ctx, t, apiClient, container.WithName(app1Name), container.WithLinks(db1Name+":/mysql")) |
| 185 | 185 |
|
| 186 |
- err := apiClient.ContainerRename(ctx, app1Name, app2Name) |
|
| 186 |
+ _, err := apiClient.ContainerRename(ctx, app1Name, client.ContainerRenameOptions{NewName: app2Name})
|
|
| 187 | 187 |
assert.NilError(t, err) |
| 188 | 188 |
|
| 189 | 189 |
inspect, err := apiClient.ContainerInspect(ctx, app2Name+"/mysql", client.ContainerInspectOptions{})
|
| ... | ... |
@@ -204,11 +204,11 @@ func TestRenameContainerTwice(t *testing.T) {
|
| 204 | 204 |
}) |
| 205 | 205 |
}() |
| 206 | 206 |
|
| 207 |
- err := apiClient.ContainerRename(ctx, "c0", "c1") |
|
| 207 |
+ _, err := apiClient.ContainerRename(ctx, "c0", client.ContainerRenameOptions{NewName: "c1"})
|
|
| 208 | 208 |
assert.NilError(t, err) |
| 209 | 209 |
ctrName = "c1" |
| 210 | 210 |
|
| 211 |
- err = apiClient.ContainerRename(ctx, "c1", "c2") |
|
| 211 |
+ _, err = apiClient.ContainerRename(ctx, "c1", client.ContainerRenameOptions{NewName: "c2"})
|
|
| 212 | 212 |
assert.NilError(t, err) |
| 213 | 213 |
ctrName = "c2" |
| 214 | 214 |
} |
| ... | ... |
@@ -66,7 +66,7 @@ type ContainerAPIClient interface {
|
| 66 | 66 |
ContainerLogs(ctx context.Context, container string, options ContainerLogsOptions) (ContainerLogsResult, error) |
| 67 | 67 |
ContainerPause(ctx context.Context, container string, options ContainerPauseOptions) (ContainerPauseResult, error) |
| 68 | 68 |
ContainerRemove(ctx context.Context, container string, options ContainerRemoveOptions) (ContainerRemoveResult, error) |
| 69 |
- ContainerRename(ctx context.Context, container, newContainerName string) error |
|
| 69 |
+ ContainerRename(ctx context.Context, container string, options ContainerRenameOptions) (ContainerRenameResult, error) |
|
| 70 | 70 |
ContainerResize(ctx context.Context, container string, options ContainerResizeOptions) (ContainerResizeResult, error) |
| 71 | 71 |
ContainerRestart(ctx context.Context, container string, options ContainerRestartOptions) (ContainerRestartResult, error) |
| 72 | 72 |
ContainerStatPath(ctx context.Context, container, path string) (container.PathStat, error) |
| ... | ... |
@@ -5,16 +5,26 @@ import ( |
| 5 | 5 |
"net/url" |
| 6 | 6 |
) |
| 7 | 7 |
|
| 8 |
+// ContainerRenameOptions represents the options for renaming a container. |
|
| 9 |
+type ContainerRenameOptions struct {
|
|
| 10 |
+ NewName string |
|
| 11 |
+} |
|
| 12 |
+ |
|
| 13 |
+// ContainerRenameResult represents the result of a container rename operation. |
|
| 14 |
+type ContainerRenameResult struct {
|
|
| 15 |
+ // This struct can be expanded in the future if needed |
|
| 16 |
+} |
|
| 17 |
+ |
|
| 8 | 18 |
// ContainerRename changes the name of a given container. |
| 9 |
-func (cli *Client) ContainerRename(ctx context.Context, containerID, newContainerName string) error {
|
|
| 19 |
+func (cli *Client) ContainerRename(ctx context.Context, containerID string, options ContainerRenameOptions) (ContainerRenameResult, error) {
|
|
| 10 | 20 |
containerID, err := trimID("container", containerID)
|
| 11 | 21 |
if err != nil {
|
| 12 |
- return err |
|
| 22 |
+ return ContainerRenameResult{}, err
|
|
| 13 | 23 |
} |
| 14 | 24 |
|
| 15 | 25 |
query := url.Values{}
|
| 16 |
- query.Set("name", newContainerName)
|
|
| 26 |
+ query.Set("name", options.NewName)
|
|
| 17 | 27 |
resp, err := cli.post(ctx, "/containers/"+containerID+"/rename", query, nil, nil) |
| 18 | 28 |
defer ensureReaderClosed(resp) |
| 19 |
- return err |
|
| 29 |
+ return ContainerRenameResult{}, err
|
|
| 20 | 30 |
} |