Add back compat for volume drivers Get and Ls

Use a back-compat struct to handle listing volumes for volumes we know
about (because, presumably, they are being used by a container) for
volume drivers which don't yet support `List`.

Adds a fall-back for the volume driver `Get` call, which will use
`Create` when the driver returns a `404` for `Get`. The old behavior was
to always use `Create` to get a volume reference.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2016-01-20 22:31:46 -05:00
parent f87775b873
commit 1cb28d7bbe
2 changed files with 36 additions and 4 deletions

View file

@ -3,7 +3,6 @@ package plugins
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -124,7 +123,7 @@ func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool)
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("%s: %s", serviceMethod, err) return nil, &statusError{resp.StatusCode, serviceMethod, err.Error()}
} }
// Plugins' Response(s) should have an Err field indicating what went // Plugins' Response(s) should have an Err field indicating what went
@ -136,11 +135,11 @@ func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool)
remoteErr := responseErr{} remoteErr := responseErr{}
if err := json.Unmarshal(b, &remoteErr); err == nil { if err := json.Unmarshal(b, &remoteErr); err == nil {
if remoteErr.Err != "" { if remoteErr.Err != "" {
return nil, fmt.Errorf("%s: %s", serviceMethod, remoteErr.Err) return nil, &statusError{resp.StatusCode, serviceMethod, remoteErr.Err}
} }
} }
// old way... // old way...
return nil, fmt.Errorf("%s: %s", serviceMethod, string(b)) return nil, &statusError{resp.StatusCode, serviceMethod, string(b)}
} }
return resp.Body, nil return resp.Body, nil
} }

33
plugins/errors.go Normal file
View file

@ -0,0 +1,33 @@
package plugins
import (
"fmt"
"net/http"
)
type statusError struct {
status int
method string
err string
}
// Error returns a formated string for this error type
func (e *statusError) Error() string {
return fmt.Sprintf("%s: %v", e.method, e.err)
}
// IsNotFound indicates if the passed in error is from an http.StatusNotFound from the plugin
func IsNotFound(err error) bool {
return isStatusError(err, http.StatusNotFound)
}
func isStatusError(err error, status int) bool {
if err == nil {
return false
}
e, ok := err.(*statusError)
if !ok {
return false
}
return e.status == status
}