Skip to content

Commit

Permalink
feat(influx): add pkger remove|rm|uninstall stacks command
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed May 7, 2020
1 parent e9ebe1c commit a3f6005
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
1. [17934](https://github.com/influxdata/influxdb/pull/17934): Add ability to delete a stack and all the resources associated with it
1. [17941](https://github.com/influxdata/influxdb/pull/17941): Enforce DNS name compliance on all pkger resources' metadata.name field
1. [17989](https://github.com/influxdata/influxdb/pull/17989): Add stateful pkg management with stacks
1. [18007](https://github.com/influxdata/influxdb/pull/18007): Add remove and list pkger stack commands to influx CLI

### Bug Fixes

Expand Down
94 changes: 94 additions & 0 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ func (b *cmdPkgBuilder) cmdStack() *cobra.Command {
cmd.AddCommand(
b.cmdStackInit(),
b.cmdStackList(),
b.cmdStackRemove(),
)
return cmd
}
Expand Down Expand Up @@ -498,6 +499,99 @@ func (b *cmdPkgBuilder) stackListRunEFn(cmd *cobra.Command, args []string) error
return nil
}

func (b *cmdPkgBuilder) cmdStackRemove() *cobra.Command {
cmd := b.newCmd("remove [--stack-id=ID1 --stack-id=ID2]", b.stackRemoveRunEFn, true)
cmd.Short = "Remove a stack(s) and all associated resources"
cmd.Aliases = []string{"rm", "uninstall"}

cmd.Flags().StringArrayVar(&b.stackIDs, "stack-id", nil, "Stack IDs to be removed")
cmd.MarkFlagRequired("stack-id")
registerPrintOptions(cmd, &b.hideHeaders, &b.json)

b.org.register(cmd, false)

return cmd
}

func (b *cmdPkgBuilder) stackRemoveRunEFn(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
}

var stackIDs []influxdb.ID
for _, rawID := range b.stackIDs {
id, err := influxdb.IDFromString(rawID)
if err != nil {
return err
}
stackIDs = append(stackIDs, *id)
}

stacks, err := pkgSVC.ListStacks(context.Background(), orgID, pkger.ListFilter{
StackIDs: stackIDs,
})
if err != nil {
return err
}

printStack := func(stack pkger.Stack) error {
if b.json {
return b.writeJSON(stack)
}

tabW := b.newTabWriter()
defer func() {
tabW.Flush()
// add a breather line between confirm and printout
fmt.Fprintln(b.w)
}()

tabW.HideHeaders(b.hideHeaders)

tabW.WriteHeaders("ID", "OrgID", "Name", "Description", "Num Resources", "URLs", "Created At")
tabW.Write(map[string]interface{}{
"ID": stack.ID,
"OrgID": stack.OrgID,
"Name": stack.Name,
"Description": stack.Description,
"Num Resources": len(stack.Resources),
"URLs": stack.URLs,
"Created At": stack.CreatedAt,
})

return nil
}

for _, stack := range stacks {
if err := printStack(stack); err != nil {
return err
}

msg := fmt.Sprintf("Confirm removal of the stack[%s] and all associated resources (y/n)", stack.ID)
confirm := b.getInput(msg, "n")
if confirm != "y" {
continue
}

err := pkgSVC.DeleteStack(context.Background(), struct{ OrgID, UserID, StackID influxdb.ID }{
OrgID: orgID,
UserID: 0,
StackID: stack.ID,
})
if err != nil {
return err
}
}

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")
Expand Down

0 comments on commit a3f6005

Please sign in to comment.