api: add types/volume.ListOptions for a more consistent API

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-11-01 16:11:35 +01:00
parent e60bddcc60
commit 340711db3d
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
8 changed files with 20 additions and 16 deletions

View file

@ -176,7 +176,7 @@ type VolumeAPIClient interface {
VolumeCreate(ctx context.Context, options volume.CreateOptions) (volume.Volume, error) VolumeCreate(ctx context.Context, options volume.CreateOptions) (volume.Volume, error)
VolumeInspect(ctx context.Context, volumeID string) (volume.Volume, error) VolumeInspect(ctx context.Context, volumeID string) (volume.Volume, error)
VolumeInspectWithRaw(ctx context.Context, volumeID string) (volume.Volume, []byte, error) VolumeInspectWithRaw(ctx context.Context, volumeID string) (volume.Volume, []byte, error)
VolumeList(ctx context.Context, filter filters.Args) (volume.ListResponse, error) VolumeList(ctx context.Context, options volume.ListOptions) (volume.ListResponse, error)
VolumeRemove(ctx context.Context, volumeID string, force bool) error VolumeRemove(ctx context.Context, volumeID string, force bool) error
VolumesPrune(ctx context.Context, pruneFilter filters.Args) (types.VolumesPruneReport, error) VolumesPrune(ctx context.Context, pruneFilter filters.Args) (types.VolumesPruneReport, error)
VolumeUpdate(ctx context.Context, volumeID string, version swarm.Version, options volume.UpdateOptions) error VolumeUpdate(ctx context.Context, volumeID string, version swarm.Version, options volume.UpdateOptions) error

View file

@ -10,13 +10,13 @@ import (
) )
// VolumeList returns the volumes configured in the docker host. // VolumeList returns the volumes configured in the docker host.
func (cli *Client) VolumeList(ctx context.Context, filter filters.Args) (volume.ListResponse, error) { func (cli *Client) VolumeList(ctx context.Context, options volume.ListOptions) (volume.ListResponse, error) {
var volumes volume.ListResponse var volumes volume.ListResponse
query := url.Values{} query := url.Values{}
if filter.Len() > 0 { if options.Filters.Len() > 0 {
//nolint:staticcheck // ignore SA1019 for old code //nolint:staticcheck // ignore SA1019 for old code
filterJSON, err := filters.ToParamWithVersion(cli.version, filter) filterJSON, err := filters.ToParamWithVersion(cli.version, options.Filters)
if err != nil { if err != nil {
return volumes, err return volumes, err
} }

View file

@ -20,7 +20,7 @@ func TestVolumeListError(t *testing.T) {
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.VolumeList(context.Background(), filters.NewArgs()) _, err := client.VolumeList(context.Background(), volume.ListOptions{})
if !errdefs.IsSystem(err) { if !errdefs.IsSystem(err) {
t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err) t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
} }
@ -87,7 +87,7 @@ func TestVolumeList(t *testing.T) {
}), }),
} }
volumeResponse, err := client.VolumeList(context.Background(), listCase.filters) volumeResponse, err := client.VolumeList(context.Background(), volume.ListOptions{Filters: listCase.filters})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -8,6 +8,7 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/integration/internal/container" "github.com/docker/docker/integration/internal/container"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp" is "gotest.tools/v3/assert/cmp"
@ -72,7 +73,9 @@ func TestRemoveContainerWithVolume(t *testing.T) {
}) })
assert.NilError(t, err) assert.NilError(t, err)
volumes, err := client.VolumeList(ctx, filters.NewArgs(filters.Arg("name", volName))) volumes, err := client.VolumeList(ctx, volume.ListOptions{
Filters: filters.NewArgs(filters.Arg("name", volName)),
})
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Equal(0, len(volumes.Volumes))) assert.Check(t, is.Equal(0, len(volumes.Volumes)))
} }

View file

@ -105,7 +105,7 @@ func TestAuthZPluginV2RejectVolumeRequests(t *testing.T) {
assert.Assert(t, err != nil) assert.Assert(t, err != nil)
assert.Assert(t, strings.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))) assert.Assert(t, strings.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag)))
_, err = c.VolumeList(context.Background(), filters.Args{}) _, err = c.VolumeList(context.Background(), volume.ListOptions{})
assert.Assert(t, err != nil) assert.Assert(t, err != nil)
assert.Assert(t, strings.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))) assert.Assert(t, strings.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag)))

View file

@ -9,7 +9,6 @@ import (
"time" "time"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume" "github.com/docker/docker/api/types/volume"
"github.com/docker/docker/integration/internal/container" "github.com/docker/docker/integration/internal/container"
"github.com/docker/docker/testutil/request" "github.com/docker/docker/testutil/request"
@ -43,7 +42,7 @@ func TestVolumesCreateAndList(t *testing.T) {
} }
assert.Check(t, is.DeepEqual(vol, expected, cmpopts.EquateEmpty())) assert.Check(t, is.DeepEqual(vol, expected, cmpopts.EquateEmpty()))
volList, err := client.VolumeList(ctx, filters.Args{}) volList, err := client.VolumeList(ctx, volume.ListOptions{})
assert.NilError(t, err) assert.NilError(t, err)
assert.Assert(t, len(volList.Volumes) > 0) assert.Assert(t, len(volList.Volumes) > 0)

View file

@ -8,6 +8,7 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/docker/errdefs" "github.com/docker/docker/errdefs"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
@ -124,7 +125,7 @@ func removeImage(ctx context.Context, t testing.TB, apiclient client.ImageAPICli
func deleteAllVolumes(t testing.TB, c client.VolumeAPIClient, protectedVolumes map[string]struct{}) { func deleteAllVolumes(t testing.TB, c client.VolumeAPIClient, protectedVolumes map[string]struct{}) {
t.Helper() t.Helper()
volumes, err := c.VolumeList(context.Background(), filters.Args{}) volumes, err := c.VolumeList(context.Background(), volume.ListOptions{})
assert.Check(t, err, "failed to list volumes") assert.Check(t, err, "failed to list volumes")
for _, v := range volumes.Volumes { for _, v := range volumes.Volumes {

View file

@ -6,6 +6,7 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/errdefs" "github.com/docker/docker/errdefs"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
) )
@ -195,8 +196,8 @@ func getExistingPlugins(t testing.TB, testEnv *Execution) []string {
// ProtectVolume adds the specified volume(s) to be protected in case of clean // ProtectVolume adds the specified volume(s) to be protected in case of clean
func (e *Execution) ProtectVolume(t testing.TB, volumes ...string) { func (e *Execution) ProtectVolume(t testing.TB, volumes ...string) {
t.Helper() t.Helper()
for _, volume := range volumes { for _, vol := range volumes {
e.protectedElements.volumes[volume] = struct{}{} e.protectedElements.volumes[vol] = struct{}{}
} }
} }
@ -211,12 +212,12 @@ func ProtectVolumes(t testing.TB, testEnv *Execution) {
func getExistingVolumes(t testing.TB, testEnv *Execution) []string { func getExistingVolumes(t testing.TB, testEnv *Execution) []string {
t.Helper() t.Helper()
client := testEnv.APIClient() client := testEnv.APIClient()
volumeList, err := client.VolumeList(context.Background(), filters.Args{}) volumeList, err := client.VolumeList(context.Background(), volume.ListOptions{})
assert.NilError(t, err, "failed to list volumes") assert.NilError(t, err, "failed to list volumes")
var volumes []string var volumes []string
for _, volume := range volumeList.Volumes { for _, vol := range volumeList.Volumes {
volumes = append(volumes, volume.Name) volumes = append(volumes, vol.Name)
} }
return volumes return volumes
} }