Merge pull request #145 from dnephin/improve-compose-secrets-errors

Compose: Improve error messages when resource create/update fails
This commit is contained in:
Aaron Lehmann 2017-06-07 18:51:10 +03:00 committed by GitHub
commit 9d12d6fc87
3 changed files with 52 additions and 69 deletions

View File

@ -196,17 +196,18 @@ func createSecrets(
for _, secretSpec := range secrets {
secret, _, err := client.SecretInspectWithRaw(ctx, secretSpec.Name)
if err == nil {
switch {
case err == nil:
// secret already exists, then we update that
if err := client.SecretUpdate(ctx, secret.ID, secret.Meta.Version, secretSpec); err != nil {
return err
return errors.Wrapf(err, "failed to update secret %s", secretSpec.Name)
}
} else if apiclient.IsErrSecretNotFound(err) {
case apiclient.IsErrSecretNotFound(err):
// secret does not exist, then we create a new one.
if _, err := client.SecretCreate(ctx, secretSpec); err != nil {
return err
return errors.Wrapf(err, "failed to create secret %s", secretSpec.Name)
}
} else {
default:
return err
}
}
@ -222,17 +223,18 @@ func createConfigs(
for _, configSpec := range configs {
config, _, err := client.ConfigInspectWithRaw(ctx, configSpec.Name)
if err == nil {
switch {
case err == nil:
// config already exists, then we update that
if err := client.ConfigUpdate(ctx, config.ID, config.Meta.Version, configSpec); err != nil {
return err
errors.Wrapf(err, "failed to update config %s", configSpec.Name)
}
} else if apiclient.IsErrConfigNotFound(err) {
case apiclient.IsErrConfigNotFound(err):
// config does not exist, then we create a new one.
if _, err := client.ConfigCreate(ctx, configSpec); err != nil {
return err
errors.Wrapf(err, "failed to create config %s", configSpec.Name)
}
} else {
default:
return err
}
}
@ -269,10 +271,9 @@ func createNetworks(
fmt.Fprintf(dockerCli.Out(), "Creating network %s\n", name)
if _, err := client.NetworkCreate(ctx, name, createOpts); err != nil {
return err
return errors.Wrapf(err, "failed to create network %s", internalName)
}
}
return nil
}
@ -312,19 +313,15 @@ func deployServices(
if service, exists := existingServiceMap[name]; exists {
fmt.Fprintf(out, "Updating service %s (id: %s)\n", name, service.ID)
updateOpts := types.ServiceUpdateOptions{}
if sendAuth {
updateOpts.EncodedRegistryAuth = encodedAuth
}
response, err := apiClient.ServiceUpdate(
ctx,
service.ID,
service.Version,
serviceSpec,
updateOpts,
types.ServiceUpdateOptions{EncodedRegistryAuth: encodedAuth},
)
if err != nil {
return err
return errors.Wrapf(err, "failed to update service %s", name)
}
for _, warning := range response.Warnings {
@ -333,15 +330,11 @@ func deployServices(
} else {
fmt.Fprintf(out, "Creating service %s\n", name)
createOpts := types.ServiceCreateOptions{}
if sendAuth {
createOpts.EncodedRegistryAuth = encodedAuth
}
createOpts := types.ServiceCreateOptions{EncodedRegistryAuth: encodedAuth}
if _, err := apiClient.ServiceCreate(ctx, serviceSpec, createOpts); err != nil {
return err
return errors.Wrapf(err, "failed to create service %s", name)
}
}
}
return nil
}

View File

@ -195,7 +195,7 @@ func transform(source map[string]interface{}, target interface{}) error {
data := mapstructure.Metadata{}
config := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
transformHook,
createTransformHook(),
mapstructure.StringToTimeDurationHookFunc()),
Result: target,
Metadata: &data,
@ -207,46 +207,33 @@ func transform(source map[string]interface{}, target interface{}) error {
return decoder.Decode(source)
}
func transformHook(
source reflect.Type,
target reflect.Type,
data interface{},
) (interface{}, error) {
switch target {
case reflect.TypeOf(types.External{}):
return transformExternal(data)
case reflect.TypeOf(types.HealthCheckTest{}):
return transformHealthCheckTest(data)
case reflect.TypeOf(types.ShellCommand{}):
return transformShellCommand(data)
case reflect.TypeOf(types.StringList{}):
return transformStringList(data)
case reflect.TypeOf(map[string]string{}):
return transformMapStringString(data)
case reflect.TypeOf(types.UlimitsConfig{}):
return transformUlimits(data)
case reflect.TypeOf(types.UnitBytes(0)):
return transformSize(data)
case reflect.TypeOf([]types.ServicePortConfig{}):
return transformServicePort(data)
case reflect.TypeOf(types.ServiceSecretConfig{}):
return transformStringSourceMap(data)
case reflect.TypeOf(types.ServiceConfigObjConfig{}):
return transformStringSourceMap(data)
case reflect.TypeOf(types.StringOrNumberList{}):
return transformStringOrNumberList(data)
case reflect.TypeOf(map[string]*types.ServiceNetworkConfig{}):
return transformServiceNetworkMap(data)
case reflect.TypeOf(types.MappingWithEquals{}):
return transformMappingOrList(data, "=", true), nil
case reflect.TypeOf(types.Labels{}):
return transformMappingOrList(data, "=", false), nil
case reflect.TypeOf(types.MappingWithColon{}):
return transformMappingOrList(data, ":", false), nil
case reflect.TypeOf(types.ServiceVolumeConfig{}):
return transformServiceVolumeConfig(data)
func createTransformHook() mapstructure.DecodeHookFuncType {
transforms := map[reflect.Type]func(interface{}) (interface{}, error){
reflect.TypeOf(types.External{}): transformExternal,
reflect.TypeOf(types.HealthCheckTest{}): transformHealthCheckTest,
reflect.TypeOf(types.ShellCommand{}): transformShellCommand,
reflect.TypeOf(types.StringList{}): transformStringList,
reflect.TypeOf(map[string]string{}): transformMapStringString,
reflect.TypeOf(types.UlimitsConfig{}): transformUlimits,
reflect.TypeOf(types.UnitBytes(0)): transformSize,
reflect.TypeOf([]types.ServicePortConfig{}): transformServicePort,
reflect.TypeOf(types.ServiceSecretConfig{}): transformStringSourceMap,
reflect.TypeOf(types.ServiceConfigObjConfig{}): transformStringSourceMap,
reflect.TypeOf(types.StringOrNumberList{}): transformStringOrNumberList,
reflect.TypeOf(map[string]*types.ServiceNetworkConfig{}): transformServiceNetworkMap,
reflect.TypeOf(types.MappingWithEquals{}): transformMappingOrListFunc("=", true),
reflect.TypeOf(types.Labels{}): transformMappingOrListFunc("=", false),
reflect.TypeOf(types.MappingWithColon{}): transformMappingOrListFunc(":", false),
reflect.TypeOf(types.ServiceVolumeConfig{}): transformServiceVolumeConfig,
}
return func(_ reflect.Type, target reflect.Type, data interface{}) (interface{}, error) {
transform, ok := transforms[target]
if !ok {
return data, nil
}
return transform(data)
}
}
// keys needs to be converted to strings for jsonschema
@ -617,6 +604,12 @@ func transformStringList(data interface{}) (interface{}, error) {
}
}
func transformMappingOrListFunc(sep string, allowNil bool) func(interface{}) (interface{}, error) {
return func(data interface{}) (interface{}, error) {
return transformMappingOrList(data, sep, allowNil), nil
}
}
func transformMappingOrList(mappingOrList interface{}, sep string, allowNil bool) interface{} {
switch value := mappingOrList.(type) {
case map[string]interface{}:
@ -658,7 +651,7 @@ func transformHealthCheckTest(data interface{}) (interface{}, error) {
}
}
func transformSize(value interface{}) (int64, error) {
func transformSize(value interface{}) (interface{}, error) {
switch value := value.(type) {
case int:
return int64(value), nil

View File

@ -114,8 +114,5 @@ func isFilePath(source string) bool {
// Windows absolute path
first, next := utf8.DecodeRuneInString(source)
if unicode.IsLetter(first) && source[next] == ':' {
return true
}
return false
return unicode.IsLetter(first) && source[next] == ':'
}