diff --git a/loader/normalize.go b/loader/normalize.go index f5c87f44..858fc7bf 100644 --- a/loader/normalize.go +++ b/loader/normalize.go @@ -191,31 +191,47 @@ func setIfMissing(d types.DependsOnConfig, service string, dep types.ServiceDepe // Resources with no explicit name are actually named by their key in map func setNameFromKey(project *types.Project) { - for i, n := range project.Networks { + for key, n := range project.Networks { if n.Name == "" { - n.Name = fmt.Sprintf("%s_%s", project.Name, i) - project.Networks[i] = n + if n.External { + n.Name = key + } else { + n.Name = fmt.Sprintf("%s_%s", project.Name, key) + } + project.Networks[key] = n } } - for i, v := range project.Volumes { + for key, v := range project.Volumes { if v.Name == "" { - v.Name = fmt.Sprintf("%s_%s", project.Name, i) - project.Volumes[i] = v + if v.External { + v.Name = key + } else { + v.Name = fmt.Sprintf("%s_%s", project.Name, key) + } + project.Volumes[key] = v } } - for i, c := range project.Configs { + for key, c := range project.Configs { if c.Name == "" { - c.Name = fmt.Sprintf("%s_%s", project.Name, i) - project.Configs[i] = c + if c.External { + c.Name = key + } else { + c.Name = fmt.Sprintf("%s_%s", project.Name, key) + } + project.Configs[key] = c } } - for i, s := range project.Secrets { + for key, s := range project.Secrets { if s.Name == "" { - s.Name = fmt.Sprintf("%s_%s", project.Name, i) - project.Secrets[i] = s + if s.External { + s.Name = key + } else { + s.Name = fmt.Sprintf("%s_%s", project.Name, key) + } + project.Secrets[key] = s } } } diff --git a/transform/external.go b/transform/external.go index 1e50701b..9e24eb83 100644 --- a/transform/external.go +++ b/transform/external.go @@ -33,28 +33,21 @@ func transformMaybeExternal(data any, p tree.Path) (any, error) { } if ext, ok := resource["external"]; ok { - external, ok := ext.(map[string]any) - if !ok { - return resource, nil - } name, named := resource["name"] - extname, extNamed := external["name"] - if extNamed { - logrus.Warnf("%s: external.name is deprecated. Please set name and external: true", p) - if named && extname != name { - return nil, fmt.Errorf("%s: name and external.name conflict; only use name", p) - } - } - if !named { - if extNamed { - // adopt (deprecated) external.name if set - resource["name"] = extname - } else { - // otherwise, just replicate the mapping key for convenience - resource["name"] = p + if external, ok := ext.(map[string]any); ok { + resource["external"] = true + if extname, extNamed := external["name"]; extNamed { + logrus.Warnf("%s: external.name is deprecated. Please set name and external: true", p) + if named && extname != name { + return nil, fmt.Errorf("%s: name and external.name conflict; only use name", p) + } + if !named { + // adopt (deprecated) external.name if set + resource["name"] = extname + return resource, nil + } } } - resource["external"] = true } return resource, nil diff --git a/transform/external_test.go b/transform/external_test.go new file mode 100644 index 00000000..56fa33a7 --- /dev/null +++ b/transform/external_test.go @@ -0,0 +1,83 @@ +/* + Copyright 2020 The Compose Specification Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package transform + +import ( + "testing" + + "github.com/compose-spec/compose-go/v2/tree" + "gotest.tools/v3/assert" +) + +func TestNotExternal(t *testing.T) { + ssh, err := transformMaybeExternal(map[string]any{ + "driver": "foo", + }, tree.NewPath("resources.test")) + assert.NilError(t, err) + assert.DeepEqual(t, ssh, map[string]any{ + "driver": "foo", + }) +} + +func TestExternalNamed(t *testing.T) { + ssh, err := transformMaybeExternal(map[string]any{ + "external": true, + "name": "foo", + }, tree.NewPath("resources.test")) + assert.NilError(t, err) + assert.DeepEqual(t, ssh, map[string]any{ + "external": true, + "name": "foo", + }) +} + +func TestExternalUnnamed(t *testing.T) { + ssh, err := transformMaybeExternal(map[string]any{ + "external": true, + }, tree.NewPath("resources.test")) + assert.NilError(t, err) + assert.DeepEqual(t, ssh, map[string]any{ + "external": true, + }) +} + +func TestExternalLegacy(t *testing.T) { + ssh, err := transformMaybeExternal(map[string]any{ + "external": map[string]any{ + "name": "foo", + }, + }, tree.NewPath("resources.test")) + assert.NilError(t, err) + assert.DeepEqual(t, ssh, map[string]any{ + "external": true, + "name": "foo", + }) +} + +func TestExternalLegacyNamed(t *testing.T) { + ssh, err := transformMaybeExternal(map[string]any{ + "external": map[string]any{ + "name": "foo", + }, + "name": "foo", + }, tree.NewPath("resources.test")) + assert.NilError(t, err) + assert.DeepEqual(t, ssh, map[string]any{ + "external": true, + "name": "foo", + }) +}