Add --template-driver option for secrets/configs

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2018-02-21 19:45:34 +01:00
parent 50b229f533
commit d11b5ccdfa
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 111 additions and 27 deletions

View File

@ -16,9 +16,10 @@ import (
) )
type createOptions struct { type createOptions struct {
name string name string
file string templateDriver string
labels opts.ListOpts file string
labels opts.ListOpts
} }
func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command { func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
@ -38,6 +39,8 @@ func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
} }
flags := cmd.Flags() flags := cmd.Flags()
flags.VarP(&createOpts.labels, "label", "l", "Config labels") flags.VarP(&createOpts.labels, "label", "l", "Config labels")
flags.StringVar(&createOpts.templateDriver, "template-driver", "", "Template driver")
flags.SetAnnotation("driver", "version", []string{"1.37"})
return cmd return cmd
} }
@ -68,7 +71,11 @@ func runConfigCreate(dockerCli command.Cli, options createOptions) error {
}, },
Data: configData, Data: configData,
} }
if options.templateDriver != "" {
spec.Templating = &swarm.Driver{
Name: options.templateDriver,
}
}
r, err := client.ConfigCreate(ctx, spec) r, err := client.ConfigCreate(ctx, spec)
if err != nil { if err != nil {
return err return err

View File

@ -82,14 +82,21 @@ func TestConfigCreateWithLabels(t *testing.T) {
} }
name := "foo" name := "foo"
data, err := ioutil.ReadFile(filepath.Join("testdata", configDataFile))
assert.NoError(t, err)
expected := swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: name,
Labels: expectedLabels,
},
Data: data,
}
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
if spec.Name != name { if !reflect.DeepEqual(spec, expected) {
return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.ConfigCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
}
if !reflect.DeepEqual(spec.Labels, expectedLabels) {
return types.ConfigCreateResponse{}, errors.Errorf("expected labels %v, got %v", expectedLabels, spec.Labels)
} }
return types.ConfigCreateResponse{ return types.ConfigCreateResponse{
@ -105,3 +112,32 @@ func TestConfigCreateWithLabels(t *testing.T) {
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String())) assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
} }
func TestConfigCreateWithTemplatingDriver(t *testing.T) {
expectedDriver := &swarm.Driver{
Name: "template-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("template-driver", expectedDriver.Name)
assert.NoError(t, cmd.Execute())
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
}

View File

@ -16,10 +16,11 @@ import (
) )
type createOptions struct { type createOptions struct {
name string name string
driver string driver string
file string templateDriver string
labels opts.ListOpts file string
labels opts.ListOpts
} }
func newSecretCreateCommand(dockerCli command.Cli) *cobra.Command { func newSecretCreateCommand(dockerCli command.Cli) *cobra.Command {
@ -43,6 +44,8 @@ func newSecretCreateCommand(dockerCli command.Cli) *cobra.Command {
flags.VarP(&options.labels, "label", "l", "Secret labels") flags.VarP(&options.labels, "label", "l", "Secret labels")
flags.StringVarP(&options.driver, "driver", "d", "", "Secret driver") flags.StringVarP(&options.driver, "driver", "d", "", "Secret driver")
flags.SetAnnotation("driver", "version", []string{"1.31"}) flags.SetAnnotation("driver", "version", []string{"1.31"})
flags.StringVar(&options.templateDriver, "template-driver", "", "Template driver")
flags.SetAnnotation("driver", "version", []string{"1.37"})
return cmd return cmd
} }
@ -71,7 +74,11 @@ func runSecretCreate(dockerCli command.Cli, options createOptions) error {
Name: options.driver, Name: options.driver,
} }
} }
if options.templateDriver != "" {
spec.Templating = &swarm.Driver{
Name: options.templateDriver,
}
}
r, err := client.SecretCreate(ctx, spec) r, err := client.SecretCreate(ctx, spec)
if err != nil { if err != nil {
return err return err

View File

@ -11,7 +11,6 @@ import (
"github.com/docker/cli/internal/test/testutil" "github.com/docker/cli/internal/test/testutil"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/gotestyourself/gotestyourself/golden"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -52,15 +51,22 @@ func TestSecretCreateErrors(t *testing.T) {
func TestSecretCreateWithName(t *testing.T) { func TestSecretCreateWithName(t *testing.T) {
name := "foo" name := "foo"
var actual []byte data, err := ioutil.ReadFile(filepath.Join("testdata", secretDataFile))
assert.NoError(t, err)
expected := swarm.SecretSpec{
Annotations: swarm.Annotations{
Name: name,
Labels: make(map[string]string),
},
Data: data,
}
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if spec.Name != name { if !reflect.DeepEqual(spec, expected) {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.SecretCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
} }
actual = spec.Data
return types.SecretCreateResponse{ return types.SecretCreateResponse{
ID: "ID-" + spec.Name, ID: "ID-" + spec.Name,
}, nil }, nil
@ -70,7 +76,6 @@ func TestSecretCreateWithName(t *testing.T) {
cmd := newSecretCreateCommand(cli) cmd := newSecretCreateCommand(cli)
cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)}) cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
golden.Assert(t, string(actual), secretDataFile)
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String())) assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
} }
@ -86,7 +91,7 @@ func TestSecretCreateWithDriver(t *testing.T) {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) 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) return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
} }
@ -103,6 +108,35 @@ func TestSecretCreateWithDriver(t *testing.T) {
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String())) assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
} }
func TestSecretCreateWithTemplatingDriver(t *testing.T) {
expectedDriver := &swarm.Driver{
Name: "template-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("template-driver", expectedDriver.Name)
assert.NoError(t, cmd.Execute())
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
}
func TestSecretCreateWithLabels(t *testing.T) { func TestSecretCreateWithLabels(t *testing.T) {
expectedLabels := map[string]string{ expectedLabels := map[string]string{
"lbl1": "Label-foo", "lbl1": "Label-foo",

View File

@ -16,13 +16,13 @@ keywords: ["secret, create"]
# secret create # secret create
```Markdown ```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 Create a secret from a file or STDIN as content
Options: Options:
--help Print usage -l, --label list Secret labels
-l, --label list Secret labels (default []) --template-driver string Template driver
``` ```
## Description ## Description