Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pkger): add ability to export by stack #18322

Merged
merged 2 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

1. [18279](https://github.com/influxdata/influxdb/pull/18279): Make all pkg applications stateful via stacks
1. [18322](https://github.com/influxdata/influxdb/pull/18322): Add ability to export a stack's existing (as they are in the platform) resource state as a pkg

## v2.0.0-beta.11 [2020-05-26]

Expand Down
52 changes: 47 additions & 5 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ func (b *cmdPkgBuilder) pkgApplyRunEFn(cmd *cobra.Command, args []string) error
func (b *cmdPkgBuilder) cmdPkgExport() *cobra.Command {
cmd := b.newCmd("export", b.pkgExportRunEFn, true)
cmd.Short = "Export existing resources as a package"
cmd.AddCommand(b.cmdPkgExportAll())
cmd.AddCommand(
b.cmdPkgExportAll(),
b.cmdPkgExportStack(),
)

cmd.Flags().StringVarP(&b.file, "file", "f", "", "output file for created pkg; defaults to std out if no file provided; the extension of provided file (.yml/.json) will dictate encoding")
cmd.Flags().StringVar(&b.exportOpts.resourceType, "resource-type", "", "The resource type provided will be associated with all IDs via stdin.")
Expand Down Expand Up @@ -250,7 +253,7 @@ func (b *cmdPkgBuilder) pkgExportRunEFn(cmd *cobra.Command, args []string) error
}

if b.exportOpts.resourceType == "" {
return b.writePkg(cmd.OutOrStdout(), pkgSVC, b.file, opts...)
return b.exportPkg(cmd.OutOrStdout(), pkgSVC, b.file, opts...)
}

kind := pkger.Kind(b.exportOpts.resourceType)
Expand All @@ -270,7 +273,7 @@ func (b *cmdPkgBuilder) pkgExportRunEFn(cmd *cobra.Command, args []string) error
return err
}

return b.writePkg(cmd.OutOrStdout(), pkgSVC, b.file, append(opts, resTypeOpt)...)
return b.exportPkg(cmd.OutOrStdout(), pkgSVC, b.file, append(opts, resTypeOpt)...)
}

func (b *cmdPkgBuilder) cmdPkgExportAll() *cobra.Command {
Expand Down Expand Up @@ -324,7 +327,42 @@ func (b *cmdPkgBuilder) pkgExportAllRunEFn(cmd *cobra.Command, args []string) er
LabelNames: labelNames,
ResourceKinds: resourceKinds,
})
return b.writePkg(cmd.OutOrStdout(), pkgSVC, b.file, orgOpt)
return b.exportPkg(cmd.OutOrStdout(), pkgSVC, b.file, orgOpt)
}

func (b *cmdPkgBuilder) cmdPkgExportStack() *cobra.Command {
cmd := b.newCmd("stack $STACK_ID", b.pkgExportStackRunEFn, true)
cmd.Short = "Export all existing resources for an organization as a package"
cmd.Args = cobra.ExactValidArgs(1)

cmd.Flags().StringVarP(&b.file, "file", "f", "", "output file for created pkg; defaults to std out if no file provided; the extension of provided file (.yml/.json) will dictate encoding")
b.org.register(cmd, false)

return cmd
}

func (b *cmdPkgBuilder) pkgExportStackRunEFn(cmd *cobra.Command, args []string) error {
pkgSVC, orgSVC, err := b.svcFn()
if err != nil {
return err
}

stackID, err := influxdb.IDFromString(args[0])
if err != nil {
return err
}

orgID, err := b.org.getID(orgSVC)
if err != nil {
return err
}

pkg, err := pkgSVC.ExportStack(context.Background(), orgID, *stackID)
if err != nil {
return err
}

return b.writePkg(b.w, b.file, pkg)
}

func (b *cmdPkgBuilder) cmdPkgSummary() *cobra.Command {
Expand Down Expand Up @@ -609,12 +647,16 @@ func (b *cmdPkgBuilder) registerPkgFileFlags(cmd *cobra.Command) {
cmd.MarkFlagFilename("encoding", "yaml", "yml", "json", "jsonnet")
}

func (b *cmdPkgBuilder) writePkg(w io.Writer, pkgSVC pkger.SVC, outPath string, opts ...pkger.CreatePkgSetFn) error {
func (b *cmdPkgBuilder) exportPkg(w io.Writer, pkgSVC pkger.SVC, outPath string, opts ...pkger.CreatePkgSetFn) error {
pkg, err := pkgSVC.CreatePkg(context.Background(), opts...)
if err != nil {
return err
}

return b.writePkg(w, outPath, pkg)
}

func (b *cmdPkgBuilder) writePkg(w io.Writer, outPath string, pkg *pkger.Pkg) error {
buf, err := createPkgBuf(pkg, outPath)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions cmd/influx/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,10 @@ func (f *fakePkgSVC) DeleteStack(ctx context.Context, identifiers struct{ OrgID,
panic("not implemented")
}

func (f *fakePkgSVC) ExportStack(ctx context.Context, orgID, stackID influxdb.ID) (*pkger.Pkg, error) {
panic("not implemented")
}

func (f *fakePkgSVC) CreatePkg(ctx context.Context, setters ...pkger.CreatePkgSetFn) (*pkger.Pkg, error) {
if f.createFn != nil {
return f.createFn(ctx, setters...)
Expand Down
Loading