github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/volume/drivers/adapter.go (about)

     1  package volumedrivers
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/docker/volume"
     7  )
     8  
     9  type volumeDriverAdapter struct {
    10  	name  string
    11  	proxy *volumeDriverProxy
    12  }
    13  
    14  func (a *volumeDriverAdapter) Name() string {
    15  	return a.name
    16  }
    17  
    18  func (a *volumeDriverAdapter) Create(name string, opts map[string]string) (volume.Volume, error) {
    19  	if err := a.proxy.Create(name, opts); err != nil {
    20  		return nil, err
    21  	}
    22  	return &volumeAdapter{
    23  		proxy:      a.proxy,
    24  		name:       name,
    25  		driverName: a.name}, nil
    26  }
    27  
    28  func (a *volumeDriverAdapter) Remove(v volume.Volume) error {
    29  	return a.proxy.Remove(v.Name())
    30  }
    31  
    32  func (a *volumeDriverAdapter) List() ([]volume.Volume, error) {
    33  	ls, err := a.proxy.List()
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	var out []volume.Volume
    39  	for _, vp := range ls {
    40  		out = append(out, &volumeAdapter{
    41  			proxy:      a.proxy,
    42  			name:       vp.Name,
    43  			driverName: a.name,
    44  			eMount:     vp.Mountpoint,
    45  		})
    46  	}
    47  	return out, nil
    48  }
    49  
    50  func (a *volumeDriverAdapter) Get(name string) (volume.Volume, error) {
    51  	v, err := a.proxy.Get(name)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	// plugin may have returned no volume and no error
    57  	if v == nil {
    58  		return nil, fmt.Errorf("no such volume")
    59  	}
    60  
    61  	return &volumeAdapter{
    62  		proxy:      a.proxy,
    63  		name:       v.Name,
    64  		driverName: a.Name(),
    65  		eMount:     v.Mountpoint,
    66  	}, nil
    67  }
    68  
    69  type volumeAdapter struct {
    70  	proxy      *volumeDriverProxy
    71  	name       string
    72  	driverName string
    73  	eMount     string // ephemeral host volume path
    74  }
    75  
    76  type proxyVolume struct {
    77  	Name       string
    78  	Mountpoint string
    79  }
    80  
    81  func (a *volumeAdapter) Name() string {
    82  	return a.name
    83  }
    84  
    85  func (a *volumeAdapter) DriverName() string {
    86  	return a.driverName
    87  }
    88  
    89  func (a *volumeAdapter) Path() string {
    90  	if len(a.eMount) > 0 {
    91  		return a.eMount
    92  	}
    93  	m, _ := a.proxy.Path(a.name)
    94  	return m
    95  }
    96  
    97  func (a *volumeAdapter) Mount() (string, error) {
    98  	var err error
    99  	a.eMount, err = a.proxy.Mount(a.name)
   100  	return a.eMount, err
   101  }
   102  
   103  func (a *volumeAdapter) Unmount() error {
   104  	return a.proxy.Unmount(a.name)
   105  }