2016-09-06 14:46:37 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-02 20:43:32 -04:00
|
|
|
|
2017-09-08 12:04:34 -04:00
|
|
|
"net/http"
|
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-11-21 21:42:55 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
2016-11-21 21:42:55 -05:00
|
|
|
// errConnectionFailed implements an error returned when connection failed.
|
|
|
|
type errConnectionFailed struct {
|
|
|
|
host string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a string representation of an errConnectionFailed
|
|
|
|
func (err errConnectionFailed) Error() string {
|
|
|
|
if err.host == "" {
|
|
|
|
return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrConnectionFailed returns true if the error is caused by connection failed.
|
|
|
|
func IsErrConnectionFailed(err error) bool {
|
|
|
|
_, ok := errors.Cause(err).(errConnectionFailed)
|
|
|
|
return ok
|
|
|
|
}
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
// ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
|
|
|
|
func ErrorConnectionFailed(host string) error {
|
2016-11-21 21:42:55 -05:00
|
|
|
return errConnectionFailed{host: host}
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type notFound interface {
|
|
|
|
error
|
|
|
|
NotFound() bool // Is the error a NotFound error
|
|
|
|
}
|
|
|
|
|
2017-09-07 12:22:11 -04:00
|
|
|
// IsErrNotFound returns true if the error is a NotFound error, which is returned
|
|
|
|
// by the API when some object is not found.
|
2016-09-06 14:46:37 -04:00
|
|
|
func IsErrNotFound(err error) bool {
|
|
|
|
te, ok := err.(notFound)
|
|
|
|
return ok && te.NotFound()
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:04:34 -04:00
|
|
|
type objectNotFoundError struct {
|
|
|
|
object string
|
|
|
|
id string
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-09-08 12:04:34 -04:00
|
|
|
func (e objectNotFoundError) NotFound() bool {
|
2016-09-06 14:46:37 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:04:34 -04:00
|
|
|
func (e objectNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("Error: No such %s: %s", e.object, e.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrapResponseError(err error, resp serverResponse, object, id string) error {
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return nil
|
|
|
|
case resp.statusCode == http.StatusNotFound:
|
|
|
|
return objectNotFoundError{object: object, id: id}
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrImageNotFound returns true if the error is caused
|
|
|
|
// when an image is not found in the docker host.
|
2017-09-07 12:22:11 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use IsErrNotFound
|
2016-09-06 14:46:37 -04:00
|
|
|
func IsErrImageNotFound(err error) bool {
|
|
|
|
return IsErrNotFound(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrContainerNotFound returns true if the error is caused
|
|
|
|
// when a container is not found in the docker host.
|
2017-09-07 12:22:11 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use IsErrNotFound
|
2016-09-06 14:46:37 -04:00
|
|
|
func IsErrContainerNotFound(err error) bool {
|
|
|
|
return IsErrNotFound(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrNetworkNotFound returns true if the error is caused
|
|
|
|
// when a network is not found in the docker host.
|
2017-09-07 12:22:11 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use IsErrNotFound
|
2016-09-06 14:46:37 -04:00
|
|
|
func IsErrNetworkNotFound(err error) bool {
|
|
|
|
return IsErrNotFound(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrVolumeNotFound returns true if the error is caused
|
|
|
|
// when a volume is not found in the docker host.
|
2017-09-07 12:22:11 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use IsErrNotFound
|
2016-09-06 14:46:37 -04:00
|
|
|
func IsErrVolumeNotFound(err error) bool {
|
|
|
|
return IsErrNotFound(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unauthorizedError represents an authorization error in a remote registry.
|
|
|
|
type unauthorizedError struct {
|
|
|
|
cause error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a string representation of an unauthorizedError
|
|
|
|
func (u unauthorizedError) Error() string {
|
|
|
|
return u.cause.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrUnauthorized returns true if the error is caused
|
|
|
|
// when a remote registry authentication fails
|
|
|
|
func IsErrUnauthorized(err error) bool {
|
|
|
|
_, ok := err.(unauthorizedError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrNodeNotFound returns true if the error is caused
|
|
|
|
// when a node is not found.
|
2017-09-07 12:22:11 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use IsErrNotFound
|
2016-09-06 14:46:37 -04:00
|
|
|
func IsErrNodeNotFound(err error) bool {
|
2017-09-08 12:04:34 -04:00
|
|
|
return IsErrNotFound(err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrServiceNotFound returns true if the error is caused
|
|
|
|
// when a service is not found.
|
2017-09-07 12:22:11 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use IsErrNotFound
|
2016-09-06 14:46:37 -04:00
|
|
|
func IsErrServiceNotFound(err error) bool {
|
2017-09-08 12:04:34 -04:00
|
|
|
return IsErrNotFound(err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrTaskNotFound returns true if the error is caused
|
|
|
|
// when a task is not found.
|
2017-09-07 12:22:11 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use IsErrNotFound
|
2016-09-06 14:46:37 -04:00
|
|
|
func IsErrTaskNotFound(err error) bool {
|
2017-09-08 12:04:34 -04:00
|
|
|
return IsErrNotFound(err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type pluginPermissionDenied struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e pluginPermissionDenied) Error() string {
|
|
|
|
return "Permission denied while installing plugin " + e.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrPluginPermissionDenied returns true if the error is caused
|
|
|
|
// when a user denies a plugin's permissions
|
|
|
|
func IsErrPluginPermissionDenied(err error) bool {
|
|
|
|
_, ok := err.(pluginPermissionDenied)
|
|
|
|
return ok
|
|
|
|
}
|
2016-11-02 20:43:32 -04:00
|
|
|
|
|
|
|
// NewVersionError returns an error if the APIVersion required
|
|
|
|
// if less than the current supported version
|
|
|
|
func (cli *Client) NewVersionError(APIrequired, feature string) error {
|
2017-06-30 01:08:42 -04:00
|
|
|
if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
|
2017-01-16 09:35:27 -05:00
|
|
|
return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
|
2016-11-02 20:43:32 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-10-19 12:22:02 -04:00
|
|
|
|
|
|
|
// IsErrSecretNotFound returns true if the error is caused
|
|
|
|
// when a secret is not found.
|
2017-09-07 12:22:11 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use IsErrNotFound
|
2016-10-19 12:22:02 -04:00
|
|
|
func IsErrSecretNotFound(err error) bool {
|
2017-09-08 12:04:34 -04:00
|
|
|
return IsErrNotFound(err)
|
2017-03-15 18:04:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrConfigNotFound returns true if the error is caused
|
|
|
|
// when a config is not found.
|
2017-09-07 12:22:11 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use IsErrNotFound
|
2017-03-15 18:04:32 -04:00
|
|
|
func IsErrConfigNotFound(err error) bool {
|
2017-09-08 12:04:34 -04:00
|
|
|
return IsErrNotFound(err)
|
2016-11-29 20:31:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrPluginNotFound returns true if the error is caused
|
|
|
|
// when a plugin is not found in the docker host.
|
2017-09-07 12:22:11 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use IsErrNotFound
|
2016-11-29 20:31:29 -05:00
|
|
|
func IsErrPluginNotFound(err error) bool {
|
|
|
|
return IsErrNotFound(err)
|
|
|
|
}
|