Skip to content

Commit

Permalink
name external resources if no explicit name is set
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Nov 29, 2023
1 parent bb9c98a commit 402d2a3
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 31 deletions.
40 changes: 28 additions & 12 deletions loader/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
31 changes: 12 additions & 19 deletions transform/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
83 changes: 83 additions & 0 deletions transform/external_test.go
Original file line number Diff line number Diff line change
@@ -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",
})
}

0 comments on commit 402d2a3

Please sign in to comment.