Skip to content

Commit

Permalink
Add --templating-driver option for secrets/configs
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Feb 21, 2018
1 parent a160ad8 commit 57abb70
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 13 deletions.
15 changes: 11 additions & 4 deletions cli/command/config/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
)

type createOptions struct {
name string
file string
labels opts.ListOpts
name string
templatingDriver string
file string
labels opts.ListOpts
}

func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
Expand All @@ -38,6 +39,8 @@ func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
}
flags := cmd.Flags()
flags.VarP(&createOpts.labels, "label", "l", "Config labels")
flags.StringVar(&createOpts.templatingDriver, "templating-driver", "", "Templating driver")
flags.SetAnnotation("driver", "version", []string{"1.37"})

return cmd
}
Expand Down Expand Up @@ -68,7 +71,11 @@ func runConfigCreate(dockerCli command.Cli, options createOptions) error {
},
Data: configData,
}

if options.templatingDriver != "" {
spec.Templating = &swarm.Driver{
Name: options.templatingDriver,
}
}
r, err := client.ConfigCreate(ctx, spec)
if err != nil {
return err
Expand Down
29 changes: 29 additions & 0 deletions cli/command/config/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,32 @@ func TestConfigCreateWithLabels(t *testing.T) {
assert.NoError(t, cmd.Execute())
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
}

func TestConfigCreateWithTemplatingDriver(t *testing.T) {
expectedDriver := &swarm.Driver{
Name: "templating-driver",
}
name := "foo"

cli := test.NewFakeCli(&fakeClient{
configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
if spec.Name != name {
return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
}

if spec.Templating.Name != expectedDriver.Name {
return types.ConfigCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
}

return types.ConfigCreateResponse{
ID: "ID-" + spec.Name,
}, nil
},
})

cmd := newConfigCreateCommand(cli)
cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)})
cmd.Flags().Set("templating-driver", expectedDriver.Name)
assert.NoError(t, cmd.Execute())
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
}
17 changes: 12 additions & 5 deletions cli/command/secret/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
)

type createOptions struct {
name string
driver string
file string
labels opts.ListOpts
name string
driver string
templatingDriver string
file string
labels opts.ListOpts
}

func newSecretCreateCommand(dockerCli command.Cli) *cobra.Command {
Expand All @@ -43,6 +44,8 @@ func newSecretCreateCommand(dockerCli command.Cli) *cobra.Command {
flags.VarP(&options.labels, "label", "l", "Secret labels")
flags.StringVarP(&options.driver, "driver", "d", "", "Secret driver")
flags.SetAnnotation("driver", "version", []string{"1.31"})
flags.StringVar(&options.templatingDriver, "templating-driver", "", "Templating driver")
flags.SetAnnotation("driver", "version", []string{"1.37"})

return cmd
}
Expand Down Expand Up @@ -71,7 +74,11 @@ func runSecretCreate(dockerCli command.Cli, options createOptions) error {
Name: options.driver,
}
}

if options.templatingDriver != "" {
spec.Templating = &swarm.Driver{
Name: options.templatingDriver,
}
}
r, err := client.SecretCreate(ctx, spec)
if err != nil {
return err
Expand Down
31 changes: 30 additions & 1 deletion cli/command/secret/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestSecretCreateWithDriver(t *testing.T) {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
}

if !reflect.DeepEqual(spec.Driver.Name, expectedDriver.Name) {
if spec.Driver.Name != expectedDriver.Name {
return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
}

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

func TestSecretCreateWithTemplatingDriver(t *testing.T) {
expectedDriver := &swarm.Driver{
Name: "templating-driver",
}
name := "foo"

cli := test.NewFakeCli(&fakeClient{
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if spec.Name != name {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
}

if spec.Templating.Name != expectedDriver.Name {
return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
}

return types.SecretCreateResponse{
ID: "ID-" + spec.Name,
}, nil
},
})

cmd := newSecretCreateCommand(cli)
cmd.SetArgs([]string{name})
cmd.Flags().Set("templating-driver", expectedDriver.Name)
assert.NoError(t, cmd.Execute())
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
}

func TestSecretCreateWithLabels(t *testing.T) {
expectedLabels := map[string]string{
"lbl1": "Label-foo",
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/commandline/secret_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ keywords: ["secret, create"]
# secret create

```Markdown
Usage: docker secret create [OPTIONS] SECRET file|-
Usage: docker secret create [OPTIONS] SECRET [file|-]

Create a secret from a file or STDIN as content

Options:
--help Print usage
-l, --label list Secret labels (default [])
-l, --label list Secret labels
--templating-driver string Templating driver
```

## Description
Expand Down

0 comments on commit 57abb70

Please sign in to comment.