|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/kernel/hypeman-go" |
| 11 | + "github.com/kernel/hypeman-go/option" |
| 12 | + "github.com/tidwall/gjson" |
| 13 | + "github.com/urfave/cli/v3" |
| 14 | +) |
| 15 | + |
| 16 | +var autoStandbyCmd = cli.Command{ |
| 17 | + Name: "auto-standby", |
| 18 | + Aliases: []string{"autostandby"}, |
| 19 | + Usage: "Inspect auto-standby configuration and status", |
| 20 | + Commands: []*cli.Command{ |
| 21 | + &autoStandbyStatusCmd, |
| 22 | + }, |
| 23 | + HideHelpCommand: true, |
| 24 | +} |
| 25 | + |
| 26 | +var autoStandbyStatusCmd = cli.Command{ |
| 27 | + Name: "status", |
| 28 | + Usage: "Get auto-standby status for an instance", |
| 29 | + ArgsUsage: "<instance>", |
| 30 | + Action: handleAutoStandbyStatus, |
| 31 | + HideHelpCommand: true, |
| 32 | +} |
| 33 | + |
| 34 | +func handleAutoStandbyStatus(ctx context.Context, cmd *cli.Command) error { |
| 35 | + args := cmd.Args().Slice() |
| 36 | + if len(args) < 1 { |
| 37 | + return fmt.Errorf("instance ID or name required\nUsage: hypeman auto-standby status <instance>") |
| 38 | + } |
| 39 | + |
| 40 | + client := hypeman.NewClient(getDefaultRequestOptions(cmd)...) |
| 41 | + instanceID, err := ResolveInstance(ctx, &client, args[0]) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + var opts []option.RequestOption |
| 47 | + if cmd.Root().Bool("debug") { |
| 48 | + opts = append(opts, debugMiddlewareOption) |
| 49 | + } |
| 50 | + |
| 51 | + var res []byte |
| 52 | + opts = append(opts, option.WithResponseBodyInto(&res)) |
| 53 | + _, err = client.Instances.AutoStandby.Status(ctx, instanceID, opts...) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + format := cmd.Root().String("format") |
| 59 | + transform := cmd.Root().String("transform") |
| 60 | + return ShowJSON(os.Stdout, "auto-standby status", gjson.ParseBytes(res), format, transform) |
| 61 | +} |
| 62 | + |
| 63 | +func buildAutoStandbyPolicy(cmd *cli.Command, prefix string) (hypeman.AutoStandbyPolicyParam, bool, error) { |
| 64 | + var policy hypeman.AutoStandbyPolicyParam |
| 65 | + |
| 66 | + enabledFlag := prefix + "enabled" |
| 67 | + idleTimeoutFlag := prefix + "idle-timeout" |
| 68 | + ignoreDestinationPortFlag := prefix + "ignore-destination-port" |
| 69 | + ignoreSourceCIDRFlag := prefix + "ignore-source-cidr" |
| 70 | + |
| 71 | + enabledSet := cmd.IsSet(enabledFlag) |
| 72 | + idleTimeout := cmd.String(idleTimeoutFlag) |
| 73 | + ignoreSourceCIDRs := cleanStringValues(cmd.StringSlice(ignoreSourceCIDRFlag)) |
| 74 | + ignoreDestinationPorts, err := parseAutoStandbyPorts(cmd.StringSlice(ignoreDestinationPortFlag), ignoreDestinationPortFlag) |
| 75 | + if err != nil { |
| 76 | + return hypeman.AutoStandbyPolicyParam{}, false, err |
| 77 | + } |
| 78 | + |
| 79 | + if !enabledSet && idleTimeout == "" && len(ignoreDestinationPorts) == 0 && len(ignoreSourceCIDRs) == 0 { |
| 80 | + return hypeman.AutoStandbyPolicyParam{}, false, nil |
| 81 | + } |
| 82 | + |
| 83 | + if enabledSet { |
| 84 | + policy.Enabled = hypeman.Opt(cmd.Bool(enabledFlag)) |
| 85 | + } else { |
| 86 | + policy.Enabled = hypeman.Opt(true) |
| 87 | + } |
| 88 | + |
| 89 | + if idleTimeout != "" { |
| 90 | + policy.IdleTimeout = hypeman.Opt(idleTimeout) |
| 91 | + } |
| 92 | + if len(ignoreDestinationPorts) > 0 { |
| 93 | + policy.IgnoreDestinationPorts = ignoreDestinationPorts |
| 94 | + } |
| 95 | + if len(ignoreSourceCIDRs) > 0 { |
| 96 | + policy.IgnoreSourceCidrs = ignoreSourceCIDRs |
| 97 | + } |
| 98 | + |
| 99 | + return policy, true, nil |
| 100 | +} |
| 101 | + |
| 102 | +func parseAutoStandbyPorts(rawPorts []string, flagName string) ([]int64, error) { |
| 103 | + ports := make([]int64, 0, len(rawPorts)) |
| 104 | + for _, rawPort := range rawPorts { |
| 105 | + value := strings.TrimSpace(rawPort) |
| 106 | + if value == "" { |
| 107 | + continue |
| 108 | + } |
| 109 | + |
| 110 | + port, err := strconv.ParseInt(value, 10, 64) |
| 111 | + if err != nil { |
| 112 | + return nil, fmt.Errorf("invalid %s value %q: %w", flagName, rawPort, err) |
| 113 | + } |
| 114 | + if port < 1 || port > 65535 { |
| 115 | + return nil, fmt.Errorf("%s must be between 1 and 65535: %q", flagName, rawPort) |
| 116 | + } |
| 117 | + |
| 118 | + ports = append(ports, port) |
| 119 | + } |
| 120 | + |
| 121 | + return ports, nil |
| 122 | +} |
| 123 | + |
| 124 | +func cleanStringValues(values []string) []string { |
| 125 | + cleaned := make([]string, 0, len(values)) |
| 126 | + for _, value := range values { |
| 127 | + value = strings.TrimSpace(value) |
| 128 | + if value == "" { |
| 129 | + continue |
| 130 | + } |
| 131 | + |
| 132 | + cleaned = append(cleaned, value) |
| 133 | + } |
| 134 | + |
| 135 | + return cleaned |
| 136 | +} |
0 commit comments