github.com/45cali/docker@v1.11.1/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,
    26  	}, nil
    27  }
    28  
    29  func (a *volumeDriverAdapter) Remove(v volume.Volume) error {
    30  	return a.proxy.Remove(v.Name())
    31  }
    32  
    33  func (a *volumeDriverAdapter) List() ([]volume.Volume, error) {
    34  	ls, err := a.proxy.List()
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	var out []volume.Volume
    40  	for _, vp := range ls {
    41  		out = append(out, &volumeAdapter{
    42  			proxy:      a.proxy,
    43  			name:       vp.Name,
    44  			driverName: a.name,
    45  			eMount:     vp.Mountpoint,
    46  		})
    47  	}
    48  	return out, nil
    49  }
    50  
    51  func (a *volumeDriverAdapter) Get(name string) (volume.Volume, error) {
    52  	v, err := a.proxy.Get(name)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	// plugin may have returned no volume and no error
    58  	if v == nil {
    59  		return nil, fmt.Errorf("no such volume")
    60  	}
    61  
    62  	return &volumeAdapter{
    63  		proxy:      a.proxy,
    64  		name:       v.Name,
    65  		driverName: a.Name(),
    66  		eMount:     v.Mountpoint,
    67  	}, nil
    68  }
    69  
    70  type volumeAdapter struct {
    71  	proxy      *volumeDriverProxy
    72  	name       string
    73  	driverName string
    74  	eMount     string // ephemeral host volume path
    75  }
    76  
    77  type proxyVolume struct {
    78  	Name       string
    79  	Mountpoint string
    80  }
    81  
    82  func (a *volumeAdapter) Name() string {
    83  	return a.name
    84  }
    85  
    86  func (a *volumeAdapter) DriverName() string {
    87  	return a.driverName
    88  }
    89  
    90  func (a *volumeAdapter) Path() string {
    91  	if len(a.eMount) > 0 {
    92  		return a.eMount
    93  	}
    94  	m, _ := a.proxy.Path(a.name)
    95  	return m
    96  }
    97  
    98  func (a *volumeAdapter) Mount() (string, error) {
    99  	var err error
   100  	a.eMount, err = a.proxy.Mount(a.name)
   101  	return a.eMount, err
   102  }
   103  
   104  func (a *volumeAdapter) Unmount() error {
   105  	return a.proxy.Unmount(a.name)
   106  }