Skip to content

Commit d11b5cc

Browse files
committed
Add --template-driver option for secrets/configs
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 50b229f commit d11b5cc

File tree

5 files changed

+109
-25
lines changed

5 files changed

+109
-25
lines changed

cli/command/config/create.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import (
1616
)
1717

1818
type createOptions struct {
19-
name string
20-
file string
21-
labels opts.ListOpts
19+
name string
20+
templateDriver string
21+
file string
22+
labels opts.ListOpts
2223
}
2324

2425
func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
@@ -38,6 +39,8 @@ func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
3839
}
3940
flags := cmd.Flags()
4041
flags.VarP(&createOpts.labels, "label", "l", "Config labels")
42+
flags.StringVar(&createOpts.templateDriver, "template-driver", "", "Template driver")
43+
flags.SetAnnotation("driver", "version", []string{"1.37"})
4144

4245
return cmd
4346
}
@@ -68,7 +71,11 @@ func runConfigCreate(dockerCli command.Cli, options createOptions) error {
6871
},
6972
Data: configData,
7073
}
71-
74+
if options.templateDriver != "" {
75+
spec.Templating = &swarm.Driver{
76+
Name: options.templateDriver,
77+
}
78+
}
7279
r, err := client.ConfigCreate(ctx, spec)
7380
if err != nil {
7481
return err

cli/command/config/create_test.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,51 @@ func TestConfigCreateWithLabels(t *testing.T) {
8282
}
8383
name := "foo"
8484

85+
data, err := ioutil.ReadFile(filepath.Join("testdata", configDataFile))
86+
assert.NoError(t, err)
87+
88+
expected := swarm.ConfigSpec{
89+
Annotations: swarm.Annotations{
90+
Name: name,
91+
Labels: expectedLabels,
92+
},
93+
Data: data,
94+
}
95+
96+
cli := test.NewFakeCli(&fakeClient{
97+
configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
98+
if !reflect.DeepEqual(spec, expected) {
99+
return types.ConfigCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
100+
}
101+
102+
return types.ConfigCreateResponse{
103+
ID: "ID-" + spec.Name,
104+
}, nil
105+
},
106+
})
107+
108+
cmd := newConfigCreateCommand(cli)
109+
cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)})
110+
cmd.Flags().Set("label", "lbl1=Label-foo")
111+
cmd.Flags().Set("label", "lbl2=Label-bar")
112+
assert.NoError(t, cmd.Execute())
113+
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
114+
}
115+
116+
func TestConfigCreateWithTemplatingDriver(t *testing.T) {
117+
expectedDriver := &swarm.Driver{
118+
Name: "template-driver",
119+
}
120+
name := "foo"
121+
85122
cli := test.NewFakeCli(&fakeClient{
86123
configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
87124
if spec.Name != name {
88125
return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
89126
}
90127

91-
if !reflect.DeepEqual(spec.Labels, expectedLabels) {
92-
return types.ConfigCreateResponse{}, errors.Errorf("expected labels %v, got %v", expectedLabels, spec.Labels)
128+
if spec.Templating.Name != expectedDriver.Name {
129+
return types.ConfigCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
93130
}
94131

95132
return types.ConfigCreateResponse{
@@ -100,8 +137,7 @@ func TestConfigCreateWithLabels(t *testing.T) {
100137

101138
cmd := newConfigCreateCommand(cli)
102139
cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)})
103-
cmd.Flags().Set("label", "lbl1=Label-foo")
104-
cmd.Flags().Set("label", "lbl2=Label-bar")
140+
cmd.Flags().Set("template-driver", expectedDriver.Name)
105141
assert.NoError(t, cmd.Execute())
106142
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
107143
}

cli/command/secret/create.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ import (
1616
)
1717

1818
type createOptions struct {
19-
name string
20-
driver string
21-
file string
22-
labels opts.ListOpts
19+
name string
20+
driver string
21+
templateDriver string
22+
file string
23+
labels opts.ListOpts
2324
}
2425

2526
func newSecretCreateCommand(dockerCli command.Cli) *cobra.Command {
@@ -43,6 +44,8 @@ func newSecretCreateCommand(dockerCli command.Cli) *cobra.Command {
4344
flags.VarP(&options.labels, "label", "l", "Secret labels")
4445
flags.StringVarP(&options.driver, "driver", "d", "", "Secret driver")
4546
flags.SetAnnotation("driver", "version", []string{"1.31"})
47+
flags.StringVar(&options.templateDriver, "template-driver", "", "Template driver")
48+
flags.SetAnnotation("driver", "version", []string{"1.37"})
4649

4750
return cmd
4851
}
@@ -71,7 +74,11 @@ func runSecretCreate(dockerCli command.Cli, options createOptions) error {
7174
Name: options.driver,
7275
}
7376
}
74-
77+
if options.templateDriver != "" {
78+
spec.Templating = &swarm.Driver{
79+
Name: options.templateDriver,
80+
}
81+
}
7582
r, err := client.SecretCreate(ctx, spec)
7683
if err != nil {
7784
return err

cli/command/secret/create_test.go

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/docker/cli/internal/test/testutil"
1212
"github.com/docker/docker/api/types"
1313
"github.com/docker/docker/api/types/swarm"
14-
"github.com/gotestyourself/gotestyourself/golden"
1514
"github.com/pkg/errors"
1615
"github.com/stretchr/testify/assert"
1716
)
@@ -52,15 +51,22 @@ func TestSecretCreateErrors(t *testing.T) {
5251

5352
func TestSecretCreateWithName(t *testing.T) {
5453
name := "foo"
55-
var actual []byte
54+
data, err := ioutil.ReadFile(filepath.Join("testdata", secretDataFile))
55+
assert.NoError(t, err)
56+
57+
expected := swarm.SecretSpec{
58+
Annotations: swarm.Annotations{
59+
Name: name,
60+
Labels: make(map[string]string),
61+
},
62+
Data: data,
63+
}
64+
5665
cli := test.NewFakeCli(&fakeClient{
5766
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
58-
if spec.Name != name {
59-
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
67+
if !reflect.DeepEqual(spec, expected) {
68+
return types.SecretCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
6069
}
61-
62-
actual = spec.Data
63-
6470
return types.SecretCreateResponse{
6571
ID: "ID-" + spec.Name,
6672
}, nil
@@ -70,7 +76,6 @@ func TestSecretCreateWithName(t *testing.T) {
7076
cmd := newSecretCreateCommand(cli)
7177
cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)})
7278
assert.NoError(t, cmd.Execute())
73-
golden.Assert(t, string(actual), secretDataFile)
7479
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
7580
}
7681

@@ -86,7 +91,7 @@ func TestSecretCreateWithDriver(t *testing.T) {
8691
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
8792
}
8893

89-
if !reflect.DeepEqual(spec.Driver.Name, expectedDriver.Name) {
94+
if spec.Driver.Name != expectedDriver.Name {
9095
return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
9196
}
9297

@@ -103,6 +108,35 @@ func TestSecretCreateWithDriver(t *testing.T) {
103108
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
104109
}
105110

111+
func TestSecretCreateWithTemplatingDriver(t *testing.T) {
112+
expectedDriver := &swarm.Driver{
113+
Name: "template-driver",
114+
}
115+
name := "foo"
116+
117+
cli := test.NewFakeCli(&fakeClient{
118+
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
119+
if spec.Name != name {
120+
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
121+
}
122+
123+
if spec.Templating.Name != expectedDriver.Name {
124+
return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
125+
}
126+
127+
return types.SecretCreateResponse{
128+
ID: "ID-" + spec.Name,
129+
}, nil
130+
},
131+
})
132+
133+
cmd := newSecretCreateCommand(cli)
134+
cmd.SetArgs([]string{name})
135+
cmd.Flags().Set("template-driver", expectedDriver.Name)
136+
assert.NoError(t, cmd.Execute())
137+
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
138+
}
139+
106140
func TestSecretCreateWithLabels(t *testing.T) {
107141
expectedLabels := map[string]string{
108142
"lbl1": "Label-foo",

docs/reference/commandline/secret_create.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ keywords: ["secret, create"]
1616
# secret create
1717

1818
```Markdown
19-
Usage: docker secret create [OPTIONS] SECRET file|-
19+
Usage: docker secret create [OPTIONS] SECRET [file|-]
2020

2121
Create a secret from a file or STDIN as content
2222

2323
Options:
24-
--help Print usage
25-
-l, --label list Secret labels (default [])
24+
-l, --label list Secret labels
25+
--template-driver string Template driver
2626
```
2727

2828
## Description

0 commit comments

Comments
 (0)