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 stack init cmd to influx cli #17448

Merged
merged 2 commits into from
Mar 27, 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 @@ -8,6 +8,7 @@
1. [17363](https://github.com/influxdata/influxdb/pull/17363): Telegraf config tokens can no longer be retrieved after creation, but new tokens can be created after a telegraf has been setup
1. [17400](https://github.com/influxdata/influxdb/pull/17400): Be able to delete bucket by name via cli
1. [17396](https://github.com/influxdata/influxdb/pull/17396): Add module to write line data to specified url, org, and bucket
1. [17448](https://github.com/influxdata/influxdb/pull/17448): Add foundation for pkger stacks, stateful package management

### Bug Fixes

Expand Down
34 changes: 34 additions & 0 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -88,6 +89,10 @@ func (o genericCLIOpts) newCmd(use string, runE func(*cobra.Command, []string) e
return cmd
}

func (o genericCLIOpts) writeJSON(v interface{}) error {
return writeJSON(o.w, v)
}

func (o genericCLIOpts) newTabWriter() *internal.TabWriter {
return internal.NewTabWriter(o.w)
}
Expand Down Expand Up @@ -436,12 +441,41 @@ func (f flagOpts) mustRegister(cmd *cobra.Command) {
cli.BindOptions(cmd, f)
}

func registerPrintOptions(cmd *cobra.Command, headersP, jsonOutP *bool) {
var opts flagOpts
if headersP != nil {
opts = append(opts, cli.Opt{
DestP: headersP,
Flag: "hide-headers",
EnvVar: "HIDE_HEADERS",
Desc: "Hide the table headers; defaults false",
Default: false,
})
}
if jsonOutP != nil {
opts = append(opts, cli.Opt{
DestP: jsonOutP,
Flag: "json",
EnvVar: "OUTPUT_JSON",
Desc: "Output data as json; defaults false",
Default: false,
})
}
opts.mustRegister(cmd)
}

func setViperOptions() {
viper.SetEnvPrefix("INFLUX")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
}

func writeJSON(w io.Writer, v interface{}) error {
enc := json.NewEncoder(w)
enc.SetIndent("", "\t")
return enc.Encode(v)
}

func newBucketService() (influxdb.BucketService, error) {
if flags.local {
return newLocalKVService()
Expand Down
114 changes: 101 additions & 13 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ type cmdPkgBuilder struct {
file string
files []string
filters []string
description string
disableColor bool
disableTableBorders bool
json bool
name string
org organization
quiet bool
recurse bool
Expand Down Expand Up @@ -82,7 +85,9 @@ func (b *cmdPkgBuilder) cmd() *cobra.Command {
b.cmdPkgExport(),
b.cmdPkgSummary(),
b.cmdPkgValidate(),
b.cmdStack(),
)

return cmd
}

Expand All @@ -92,10 +97,9 @@ func (b *cmdPkgBuilder) cmdPkgApply() *cobra.Command {

b.org.register(cmd, false)
b.registerPkgFileFlags(cmd)
b.registerPkgPrintOpts(cmd)
cmd.Flags().BoolVarP(&b.quiet, "quiet", "q", false, "Disable output printing")
cmd.Flags().StringVar(&b.applyOpts.force, "force", "", `TTY input, if package will have destructive changes, proceed if set "true"`)
cmd.Flags().BoolVarP(&b.disableColor, "disable-color", "c", false, "Disable color in output")
cmd.Flags().BoolVar(&b.disableTableBorders, "disable-table-borders", false, "Disable table borders")

b.applyOpts.secrets = []string{}
cmd.Flags().StringSliceVar(&b.applyOpts.secrets, "secret", nil, "Secrets to provide alongside the package; format should --secret=SECRET_KEY=SECRET_VALUE --secret=SECRET_KEY_2=SECRET_VALUE_2")
Expand Down Expand Up @@ -150,8 +154,8 @@ func (b *cmdPkgBuilder) pkgApplyRunEFn(cmd *cobra.Command, args []string) error
}
}

if !b.quiet {
b.printPkgDiff(diff)
if err := b.printPkgDiff(diff); err != nil {
return err
}

isForced, _ := strconv.ParseBool(b.applyOpts.force)
Expand All @@ -172,9 +176,7 @@ func (b *cmdPkgBuilder) pkgApplyRunEFn(cmd *cobra.Command, args []string) error
return err
}

if !b.quiet {
b.printPkgSummary(summary)
}
b.printPkgSummary(summary)

return nil
}
Expand Down Expand Up @@ -314,16 +316,14 @@ func (b *cmdPkgBuilder) cmdPkgSummary() *cobra.Command {
return err
}

b.printPkgSummary(pkg.Summary())
return nil
return b.printPkgSummary(pkg.Summary())
}

cmd := b.newCmd("summary", runE, false)
cmd.Short = "Summarize the provided package"

b.registerPkgFileFlags(cmd)
cmd.Flags().BoolVarP(&b.disableColor, "disable-color", "c", false, "Disable color in output")
cmd.Flags().BoolVar(&b.disableTableBorders, "disable-table-borders", false, "Disable table borders")
b.registerPkgPrintOpts(cmd)

return cmd
}
Expand All @@ -345,6 +345,74 @@ func (b *cmdPkgBuilder) cmdPkgValidate() *cobra.Command {
return cmd
}

func (b *cmdPkgBuilder) cmdStack() *cobra.Command {
cmd := b.newCmd("stack", nil, false)
cmd.Short = "Stack management commands"
cmd.AddCommand(b.cmdStackInit())
return cmd
}

func (b *cmdPkgBuilder) cmdStackInit() *cobra.Command {
cmd := b.newCmd("init", b.stackInitRunEFn, true)
cmd.Short = "Initialize a stack"

cmd.Flags().StringVarP(&b.name, "stack-name", "n", "", "Name given to created stack")
cmd.Flags().StringVarP(&b.description, "stack-description", "d", "", "Description given to created stack")
cmd.Flags().StringArrayVarP(&b.urls, "package-url", "u", nil, "Package urls to associate with new stack")
cmd.Flags().BoolVar(&b.json, "json", false, "Output data as json")

b.org.register(cmd, false)

return cmd
}

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

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

const fakeUserID = 0 // is 0 because user is pulled from token...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The userID can be inferred from the token if you use the auth service, but I'm not sure I understand what it's being used for here.

Is it a problem for the userID to be 0 in some cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the ID is real or not, the user assoicated with the token is what is used when creating resources at this time. That is pretty consistent across the API at this pint.

stack, err := pkgSVC.InitStack(context.Background(), fakeUserID, pkger.Stack{
OrgID: orgID,
Name: b.name,
Description: b.description,
URLs: b.urls,
})
if err != nil {
return err
}

if b.json {
return b.writeJSON(stack)
}

tabW := b.newTabWriter()
tabW.WriteHeaders("ID", "OrgID", "Name", "Description", "URLs", "Created At")
tabW.Write(map[string]interface{}{
"ID": stack.ID,
"OrgID": stack.OrgID,
"Name": stack.Name,
"Description": stack.Description,
"URLs": stack.URLs,
"Created At": stack.CreatedAt,
})
tabW.Flush()

return nil
}

func (b *cmdPkgBuilder) registerPkgPrintOpts(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&b.disableColor, "disable-color", "c", false, "Disable color in output")
cmd.Flags().BoolVar(&b.disableTableBorders, "disable-table-borders", false, "Disable table borders")
registerPrintOptions(cmd, nil, &b.json)
}

func (b *cmdPkgBuilder) registerPkgFileFlags(cmd *cobra.Command) {
cmd.Flags().StringSliceVarP(&b.files, "file", "f", nil, "Path to package file")
cmd.MarkFlagFilename("file", "yaml", "yml", "json", "jsonnet")
Expand Down Expand Up @@ -597,7 +665,15 @@ func newPkgerSVC() (pkger.SVC, influxdb.OrganizationService, error) {
return &pkger.HTTPRemoteService{Client: httpClient}, orgSvc, nil
}

func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) {
func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) error {
if b.quiet {
return nil
}

if b.json {
return b.writeJSON(diff)
}

red := color.New(color.FgRed).SprintFunc()
green := color.New(color.FgHiGreen, color.Bold).SprintFunc()

Expand Down Expand Up @@ -786,9 +862,19 @@ func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) {
}
})
}

return nil
}

func (b *cmdPkgBuilder) printPkgSummary(sum pkger.Summary) {
func (b *cmdPkgBuilder) printPkgSummary(sum pkger.Summary) error {
if b.quiet {
return nil
}

if b.json {
return b.writeJSON(sum)
}

tablePrintFn := b.tablePrinterGen()
if labels := sum.Labels; len(labels) > 0 {
headers := []string{"ID", "Name", "Description", "Color"}
Expand Down Expand Up @@ -930,6 +1016,8 @@ func (b *cmdPkgBuilder) printPkgSummary(sum pkger.Summary) {
return []string{secrets[i]}
})
}

return nil
}

func (b *cmdPkgBuilder) tablePrinterGen() func(table string, headers []string, count int, rowFn func(i int) []string) {
Expand Down
Loading