|
| 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 | + |
| 61 | + obj := gjson.ParseBytes(res) |
| 62 | + |
| 63 | + if format == "auto" { |
| 64 | + status := obj.Get("status").String() |
| 65 | + enabled := obj.Get("enabled").Bool() |
| 66 | + configured := obj.Get("configured").Bool() |
| 67 | + supported := obj.Get("supported").Bool() |
| 68 | + idleTimeout := obj.Get("idle_timeout").String() |
| 69 | + reason := obj.Get("reason").String() |
| 70 | + trackingMode := obj.Get("tracking_mode").String() |
| 71 | + connections := obj.Get("active_inbound_connections").Int() |
| 72 | + |
| 73 | + if idleTimeout == "" { |
| 74 | + idleTimeout = "-" |
| 75 | + } |
| 76 | + if reason == "" { |
| 77 | + reason = "-" |
| 78 | + } |
| 79 | + |
| 80 | + fmt.Printf("%-14s %s\n", "STATUS", status) |
| 81 | + fmt.Printf("%-14s %t\n", "ENABLED", enabled) |
| 82 | + fmt.Printf("%-14s %t\n", "CONFIGURED", configured) |
| 83 | + fmt.Printf("%-14s %t\n", "SUPPORTED", supported) |
| 84 | + fmt.Printf("%-14s %s\n", "IDLE TIMEOUT", idleTimeout) |
| 85 | + fmt.Printf("%-14s %s\n", "REASON", reason) |
| 86 | + fmt.Printf("%-14s %s\n", "TRACKING", trackingMode) |
| 87 | + fmt.Printf("%-14s %d\n", "CONNECTIONS", connections) |
| 88 | + |
| 89 | + if idleSince := obj.Get("idle_since").String(); idleSince != "" { |
| 90 | + fmt.Printf("%-14s %s\n", "IDLE SINCE", idleSince) |
| 91 | + } |
| 92 | + if nextStandby := obj.Get("next_standby_at").String(); nextStandby != "" { |
| 93 | + fmt.Printf("%-14s %s\n", "NEXT STANDBY", nextStandby) |
| 94 | + } |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + return ShowJSON(os.Stdout, "auto-standby status", obj, format, transform) |
| 99 | +} |
| 100 | + |
| 101 | +func buildAutoStandbyPolicy(cmd *cli.Command, prefix string) (hypeman.AutoStandbyPolicyParam, bool, error) { |
| 102 | + var policy hypeman.AutoStandbyPolicyParam |
| 103 | + |
| 104 | + enabledFlag := prefix + "enabled" |
| 105 | + idleTimeoutFlag := prefix + "idle-timeout" |
| 106 | + ignoreDestinationPortFlag := prefix + "ignore-destination-port" |
| 107 | + ignoreSourceCIDRFlag := prefix + "ignore-source-cidr" |
| 108 | + |
| 109 | + enabledSet := cmd.IsSet(enabledFlag) |
| 110 | + idleTimeout := cmd.String(idleTimeoutFlag) |
| 111 | + ignoreSourceCIDRs := cleanStringValues(cmd.StringSlice(ignoreSourceCIDRFlag)) |
| 112 | + ignoreDestinationPorts, err := parseAutoStandbyPorts(cmd.StringSlice(ignoreDestinationPortFlag), ignoreDestinationPortFlag) |
| 113 | + if err != nil { |
| 114 | + return hypeman.AutoStandbyPolicyParam{}, false, err |
| 115 | + } |
| 116 | + |
| 117 | + if !enabledSet && idleTimeout == "" && len(ignoreDestinationPorts) == 0 && len(ignoreSourceCIDRs) == 0 { |
| 118 | + return hypeman.AutoStandbyPolicyParam{}, false, nil |
| 119 | + } |
| 120 | + |
| 121 | + if enabledSet { |
| 122 | + policy.Enabled = hypeman.Opt(cmd.Bool(enabledFlag)) |
| 123 | + } else { |
| 124 | + policy.Enabled = hypeman.Opt(true) |
| 125 | + } |
| 126 | + |
| 127 | + if idleTimeout != "" { |
| 128 | + policy.IdleTimeout = hypeman.Opt(idleTimeout) |
| 129 | + } |
| 130 | + if len(ignoreDestinationPorts) > 0 { |
| 131 | + policy.IgnoreDestinationPorts = ignoreDestinationPorts |
| 132 | + } |
| 133 | + if len(ignoreSourceCIDRs) > 0 { |
| 134 | + policy.IgnoreSourceCidrs = ignoreSourceCIDRs |
| 135 | + } |
| 136 | + |
| 137 | + return policy, true, nil |
| 138 | +} |
| 139 | + |
| 140 | +func parseAutoStandbyPorts(rawPorts []string, flagName string) ([]int64, error) { |
| 141 | + ports := make([]int64, 0, len(rawPorts)) |
| 142 | + for _, rawPort := range rawPorts { |
| 143 | + value := strings.TrimSpace(rawPort) |
| 144 | + if value == "" { |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + port, err := strconv.ParseInt(value, 10, 64) |
| 149 | + if err != nil { |
| 150 | + return nil, fmt.Errorf("invalid %s value %q: %w", flagName, rawPort, err) |
| 151 | + } |
| 152 | + if port < 1 || port > 65535 { |
| 153 | + return nil, fmt.Errorf("%s must be between 1 and 65535: %q", flagName, rawPort) |
| 154 | + } |
| 155 | + |
| 156 | + ports = append(ports, port) |
| 157 | + } |
| 158 | + |
| 159 | + return ports, nil |
| 160 | +} |
| 161 | + |
| 162 | +func cleanStringValues(values []string) []string { |
| 163 | + cleaned := make([]string, 0, len(values)) |
| 164 | + for _, value := range values { |
| 165 | + value = strings.TrimSpace(value) |
| 166 | + if value == "" { |
| 167 | + continue |
| 168 | + } |
| 169 | + |
| 170 | + cleaned = append(cleaned, value) |
| 171 | + } |
| 172 | + |
| 173 | + return cleaned |
| 174 | +} |
0 commit comments