-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b987d1
commit 5d5f22b
Showing
3 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package secrets | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/client/secret_service" | ||
"github.com/hashicorp/hcp/internal/commands/vaultsecrets/secrets/appname" | ||
"github.com/hashicorp/hcp/internal/pkg/cmd" | ||
"github.com/hashicorp/hcp/internal/pkg/flagvalue" | ||
"github.com/hashicorp/hcp/internal/pkg/format" | ||
"github.com/hashicorp/hcp/internal/pkg/heredoc" | ||
"github.com/hashicorp/hcp/internal/pkg/iostreams" | ||
"github.com/hashicorp/hcp/internal/pkg/profile" | ||
"github.com/posener/complete" | ||
) | ||
|
||
type ImportOpts struct { | ||
Ctx context.Context | ||
Profile *profile.Profile | ||
IO iostreams.IOStreams | ||
Output *format.Outputter | ||
|
||
AppName string | ||
ConfigFilePath string | ||
Client secret_service.ClientService | ||
} | ||
|
||
func NewCmdImport(ctx *cmd.Context, runF func(*ImportOpts) error) *cmd.Command { | ||
opts := &ImportOpts{ | ||
Ctx: ctx.ShutdownCtx, | ||
Profile: ctx.Profile, | ||
IO: ctx.IO, | ||
Output: ctx.Output, | ||
Client: secret_service.New(ctx.HCP, nil), | ||
} | ||
|
||
cmd := &cmd.Command{ | ||
Name: "import", | ||
ShortHelp: "Import new static secret.", | ||
LongHelp: heredoc.New(ctx.IO).Must(` | ||
The {{ template "mdCodeOrBold" "hcp vault-secrets secrets import" }} command imports static secrets and creates them under a Vault Secrets application. | ||
The configuration for importing your static secrets will be read from the provided HCL config file. | ||
`), | ||
Examples: []cmd.Example{ | ||
{ | ||
Preamble: `Import static secrets:`, | ||
Command: heredoc.New(ctx.IO, heredoc.WithPreserveNewlines()).Must(` | ||
$ hcp vault-secrets secrets import --config-file=--config-file=path/to/file/config.hcl | ||
`), | ||
}, | ||
}, | ||
Flags: cmd.Flags{ | ||
Local: []*cmd.Flag{ | ||
{ | ||
Name: "config-file", | ||
DisplayValue: "CONFIG_FILE", | ||
Description: "File path to read import config data.", | ||
Value: flagvalue.Simple("", &opts.ConfigFilePath), | ||
Required: true, | ||
Autocomplete: complete.PredictOr( | ||
complete.PredictFiles("*"), | ||
complete.PredictSet("-"), | ||
), | ||
}, | ||
}, | ||
}, | ||
RunF: func(c *cmd.Command, args []string) error { | ||
opts.AppName = appname.Get() | ||
|
||
if runF != nil { | ||
return runF(opts) | ||
} | ||
return importRun(opts) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func importRun(opts *ImportOpts) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package secrets | ||
|
||
import ( | ||
"context" | ||
"github.com/go-openapi/runtime/client" | ||
"github.com/hashicorp/hcp/internal/pkg/cmd" | ||
"github.com/hashicorp/hcp/internal/pkg/format" | ||
"github.com/hashicorp/hcp/internal/pkg/iostreams" | ||
"github.com/hashicorp/hcp/internal/pkg/profile" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestNewCmdImport(t *testing.T) { | ||
t.Parallel() | ||
|
||
testProfile := func(t *testing.T) *profile.Profile { | ||
tp := profile.TestProfile(t).SetOrgID("123").SetProjectID("456") | ||
return tp | ||
} | ||
|
||
cases := []struct { | ||
Name string | ||
Args []string | ||
Profile func(t *testing.T) *profile.Profile | ||
Error string | ||
Expect *ImportOpts | ||
}{ | ||
{ | ||
Name: "Good", | ||
Profile: testProfile, | ||
Args: []string{"--config-file", "path/to/file"}, | ||
Expect: &ImportOpts{ | ||
ConfigFilePath: "path/to/file", | ||
}, | ||
}, | ||
{ | ||
Name: "Failed: No config file specified", | ||
Profile: testProfile, | ||
Expect: &ImportOpts{ | ||
ConfigFilePath: "path/to/file", | ||
}, | ||
Error: "ERROR: missing required flag: --config-file=CONFIG_FILE", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
c := c | ||
t.Run(c.Name, func(t *testing.T) { | ||
t.Parallel() | ||
r := require.New(t) | ||
|
||
// Create a context. | ||
io := iostreams.Test() | ||
ctx := &cmd.Context{ | ||
IO: io, | ||
Profile: c.Profile(t), | ||
Output: format.New(io), | ||
HCP: &client.Runtime{}, | ||
ShutdownCtx: context.Background(), | ||
} | ||
|
||
var gotOpts *ImportOpts | ||
importCmd := NewCmdImport(ctx, func(o *ImportOpts) error { | ||
gotOpts = o | ||
return nil | ||
}) | ||
importCmd.SetIO(io) | ||
|
||
code := importCmd.Run(c.Args) | ||
if c.Error != "" { | ||
r.NotZero(code) | ||
r.Contains(io.Error.String(), c.Error) | ||
return | ||
} | ||
|
||
r.Zero(code, io.Error.String()) | ||
r.NotNil(gotOpts) | ||
r.Equal(c.Expect.ConfigFilePath, gotOpts.ConfigFilePath) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters