github.com/SophiaGitHub/hello@v1.7.1-rc3/volume/drivers/proxy.go (about)

     1  package volumedrivers
     2  
     3  import "fmt"
     4  
     5  // currently created by hand. generation tool would generate this like:
     6  // $ rpc-gen volume/drivers/api.go VolumeDriver > volume/drivers/proxy.go
     7  
     8  type volumeDriverRequest struct {
     9  	Name string
    10  }
    11  
    12  type volumeDriverResponse struct {
    13  	Mountpoint string `json:",ommitempty"`
    14  	Err        error  `json:",ommitempty"`
    15  }
    16  
    17  type volumeDriverProxy struct {
    18  	c client
    19  }
    20  
    21  func (pp *volumeDriverProxy) Create(name string) error {
    22  	args := volumeDriverRequest{name}
    23  	var ret volumeDriverResponse
    24  	err := pp.c.Call("VolumeDriver.Create", args, &ret)
    25  	if err != nil {
    26  		return pp.fmtError(name, err)
    27  	}
    28  	return pp.fmtError(name, ret.Err)
    29  }
    30  
    31  func (pp *volumeDriverProxy) Remove(name string) error {
    32  	args := volumeDriverRequest{name}
    33  	var ret volumeDriverResponse
    34  	err := pp.c.Call("VolumeDriver.Remove", args, &ret)
    35  	if err != nil {
    36  		return pp.fmtError(name, err)
    37  	}
    38  	return pp.fmtError(name, ret.Err)
    39  }
    40  
    41  func (pp *volumeDriverProxy) Path(name string) (string, error) {
    42  	args := volumeDriverRequest{name}
    43  	var ret volumeDriverResponse
    44  	if err := pp.c.Call("VolumeDriver.Path", args, &ret); err != nil {
    45  		return "", pp.fmtError(name, err)
    46  	}
    47  	return ret.Mountpoint, pp.fmtError(name, ret.Err)
    48  }
    49  
    50  func (pp *volumeDriverProxy) Mount(name string) (string, error) {
    51  	args := volumeDriverRequest{name}
    52  	var ret volumeDriverResponse
    53  	if err := pp.c.Call("VolumeDriver.Mount", args, &ret); err != nil {
    54  		return "", pp.fmtError(name, err)
    55  	}
    56  	return ret.Mountpoint, pp.fmtError(name, ret.Err)
    57  }
    58  
    59  func (pp *volumeDriverProxy) Unmount(name string) error {
    60  	args := volumeDriverRequest{name}
    61  	var ret volumeDriverResponse
    62  	err := pp.c.Call("VolumeDriver.Unmount", args, &ret)
    63  	if err != nil {
    64  		return pp.fmtError(name, err)
    65  	}
    66  	return pp.fmtError(name, ret.Err)
    67  }
    68  
    69  func (pp *volumeDriverProxy) fmtError(name string, err error) error {
    70  	if err == nil {
    71  		return nil
    72  	}
    73  	return fmt.Errorf("External volume driver request failed for %s: %v", name, err)
    74  }