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

govc: Add -file flag to cluster.module.rm to delete a list of cluster modules read from a file #3194

Merged
merged 1 commit into from
Aug 22, 2023
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
4 changes: 4 additions & 0 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,14 @@ Usage: govc cluster.module.rm [OPTIONS] ID

Delete cluster module ID.

If ID is "-", read a list from stdin.

Examples:
govc cluster.module.rm module_id
govc cluster.module.rm - < input-file.txt

Options:
-ignore-not-found=false Treat "404 Not Found" as a successful delete.
```

## cluster.module.vm.add
Expand Down
38 changes: 36 additions & 2 deletions govc/cluster/module/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ limitations under the License.
package module

import (
"bufio"
"context"
"flag"
"os"
"strings"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
Expand All @@ -27,6 +30,7 @@ import (

type rm struct {
*flags.ClientFlag
ignoreNotFound bool
}

func init() {
Expand All @@ -36,6 +40,8 @@ func init() {
func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)

f.BoolVar(&cmd.ignoreNotFound, "ignore-not-found", false, "Treat \"404 Not Found\" as a successful delete.")
}

func (cmd *rm) Usage() string {
Expand All @@ -45,8 +51,11 @@ func (cmd *rm) Usage() string {
func (cmd *rm) Description() string {
return `Delete cluster module ID.

If ID is "-", read a list from stdin.

Examples:
govc cluster.module.rm module_id`
govc cluster.module.rm module_id
govc cluster.module.rm - < input-file.txt`
}

func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
Expand All @@ -60,6 +69,31 @@ func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
if err != nil {
return err
}
m := cluster.NewManager(c)

if moduleID == "-" {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
moduleID := scanner.Text()
if moduleID == "" {
continue
}
if err := cmd.deleteModule(ctx, m, moduleID); err != nil {
return err
Copy link
Member

Choose a reason for hiding this comment

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

Consider whether you want another flag to continue processing modules on error, or to ignore "unknown module" errors.

Noting because if you end up with a failure somewhere in the 10k, you cannot simply resubmit the file but have to edit it to remove all modules that have already been deleted.

Not blocking, but a nice refinement. If needing to test this the simulator has module support, see this line

}
}
chrischdi marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

return cluster.NewManager(c).DeleteModule(ctx, moduleID)
return cmd.deleteModule(ctx, m, moduleID)
}

func (cmd *rm) deleteModule(ctx context.Context, m *cluster.Manager, moduleID string) error {
if err := m.DeleteModule(ctx, moduleID); err != nil {
if cmd.ignoreNotFound && strings.HasSuffix(err.Error(), "404 Not Found") {
Copy link
Member Author

Choose a reason for hiding this comment

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

Wondering if we should instead export a function at the library to match the error via a typecast or similar.

Copy link
Member

Choose a reason for hiding this comment

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

Meant to add, we have an issue open for this: #2521

return nil
}
return err
}
return nil
}
24 changes: 24 additions & 0 deletions govc/test/cluster.bats
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,30 @@ _EOF_

run govc cluster.module.ls -id $id
assert_failure

run govc cluster.module.rm "does_not_exist"
assert_failure

run govc cluster.module.rm --ignore-not-found "does_not_exist"
assert_success

run govc cluster.module.create -cluster DC0_C0
assert_success

id="$output"

run govc cluster.module.create -cluster DC0_C0
assert_success

id2="$output"

run govc cluster.module.rm --ignore-not-found "-" <<_EOF_
$id
does_not_exist
$id2
_EOF_
assert_success

}

@test "cluster.mv" {
Expand Down