Implement docker volume with standalone client lib.
Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
parent
8c9c9e137c
commit
73bca058ae
2 changed files with 68 additions and 40 deletions
62
api/client/lib/volume.go
Normal file
62
api/client/lib/volume.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/pkg/parsers/filters"
|
||||
)
|
||||
|
||||
// VolumeList returns the volumes configured in the docker host.
|
||||
func (cli *Client) VolumeList(filter filters.Args) (types.VolumesListResponse, error) {
|
||||
var volumes types.VolumesListResponse
|
||||
var query url.Values
|
||||
|
||||
if filter.Len() > 0 {
|
||||
filterJSON, err := filters.ToParam(filter)
|
||||
if err != nil {
|
||||
return volumes, err
|
||||
}
|
||||
query.Set("filters", filterJSON)
|
||||
}
|
||||
resp, err := cli.GET("/volumes", query, nil)
|
||||
if err != nil {
|
||||
return volumes, err
|
||||
}
|
||||
defer ensureReaderClosed(resp)
|
||||
|
||||
err = json.NewDecoder(resp.body).Decode(&volumes)
|
||||
return volumes, err
|
||||
}
|
||||
|
||||
// VolumeInspect returns the information about a specific volume in the docker host.
|
||||
func (cli *Client) VolumeInspect(volumeID string) (types.Volume, error) {
|
||||
var volume types.Volume
|
||||
resp, err := cli.GET("/volumes"+volumeID, nil, nil)
|
||||
if err != nil {
|
||||
return volume, err
|
||||
}
|
||||
defer ensureReaderClosed(resp)
|
||||
err = json.NewDecoder(resp.body).Decode(&volume)
|
||||
return volume, err
|
||||
}
|
||||
|
||||
// VolumeCreate creates a volume in the docker host.
|
||||
func (cli *Client) VolumeCreate(options types.VolumeCreateRequest) (types.Volume, error) {
|
||||
var volume types.Volume
|
||||
resp, err := cli.POST("/volumes/create", nil, options, nil)
|
||||
if err != nil {
|
||||
return volume, err
|
||||
}
|
||||
defer ensureReaderClosed(resp)
|
||||
err = json.NewDecoder(resp.body).Decode(&volume)
|
||||
return volume, err
|
||||
}
|
||||
|
||||
// VolumeRemove removes a volume from the docker host.
|
||||
func (cli *Client) VolumeRemove(volumeID string) error {
|
||||
resp, err := cli.DELETE("/volumes"+volumeID, nil, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
|
@ -5,8 +5,6 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"text/tabwriter"
|
||||
"text/template"
|
||||
|
||||
|
@ -64,25 +62,11 @@ func (cli *DockerCli) CmdVolumeLs(args ...string) error {
|
|||
}
|
||||
}
|
||||
|
||||
v := url.Values{}
|
||||
if volFilterArgs.Len() > 0 {
|
||||
filterJSON, err := filters.ToParam(volFilterArgs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.Set("filters", filterJSON)
|
||||
}
|
||||
|
||||
resp, err := cli.call("GET", "/volumes?"+v.Encode(), nil, nil)
|
||||
volumes, err := cli.client.VolumeList(volFilterArgs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var volumes types.VolumesListResponse
|
||||
if err := json.NewDecoder(resp.body).Decode(&volumes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
||||
if !*quiet {
|
||||
fmt.Fprintf(w, "DRIVER \tVOLUME NAME")
|
||||
|
@ -127,18 +111,8 @@ func (cli *DockerCli) CmdVolumeInspect(args ...string) error {
|
|||
var volumes []*types.Volume
|
||||
|
||||
for _, name := range cmd.Args() {
|
||||
resp, err := cli.call("GET", "/volumes/"+name, nil, nil)
|
||||
volume, err := cli.client.VolumeInspect(name)
|
||||
if err != nil {
|
||||
if resp.statusCode != http.StatusNotFound {
|
||||
return err
|
||||
}
|
||||
status = 1
|
||||
fmt.Fprintf(cli.err, "Error: No such volume: %s\n", name)
|
||||
continue
|
||||
}
|
||||
|
||||
var volume types.Volume
|
||||
if err := json.NewDecoder(resp.body).Decode(&volume); err != nil {
|
||||
fmt.Fprintf(cli.err, "Unable to read inspect data: %v\n", err)
|
||||
status = 1
|
||||
break
|
||||
|
@ -192,24 +166,17 @@ func (cli *DockerCli) CmdVolumeCreate(args ...string) error {
|
|||
cmd.Require(flag.Exact, 0)
|
||||
cmd.ParseFlags(args, true)
|
||||
|
||||
volReq := &types.VolumeCreateRequest{
|
||||
volReq := types.VolumeCreateRequest{
|
||||
Driver: *flDriver,
|
||||
DriverOpts: flDriverOpts.GetAll(),
|
||||
Name: *flName,
|
||||
}
|
||||
|
||||
if *flName != "" {
|
||||
volReq.Name = *flName
|
||||
}
|
||||
|
||||
resp, err := cli.call("POST", "/volumes/create", volReq, nil)
|
||||
vol, err := cli.client.VolumeCreate(volReq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var vol types.Volume
|
||||
if err := json.NewDecoder(resp.body).Decode(&vol); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(cli.out, "%s\n", vol.Name)
|
||||
return nil
|
||||
}
|
||||
|
@ -224,8 +191,7 @@ func (cli *DockerCli) CmdVolumeRm(args ...string) error {
|
|||
|
||||
var status = 0
|
||||
for _, name := range cmd.Args() {
|
||||
_, err := cli.call("DELETE", "/volumes/"+name, nil, nil)
|
||||
if err != nil {
|
||||
if err := cli.client.VolumeRemove(name); err != nil {
|
||||
fmt.Fprintf(cli.err, "%s\n", err)
|
||||
status = 1
|
||||
continue
|
||||
|
|
Loading…
Reference in a new issue