lb

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NetworkLbAssignCmd = &cobra.Command{
	Use:   "assign",
	Short: "Assign instances to a load balancing rule",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if err := cli.Preflight(true)(cmd, args); err != nil {
			return err
		}
		return cli.Validate(cmd,
			cli.Required("networkId"),
			cli.Required("ruleId"),
			cli.Required("instanceNetworkIds"),
		)
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		token := cli.TokenFromContext(cmd.Context())
		zoneID := cli.ZoneIDFromContext(cmd.Context())
		if err := cli.LoadFromCobraFlags(cmd, &lbAssignOpts); err != nil {
			return err
		}
		instanceIds := strings.Split(lbAssignOpts.InstanceNetworkIds, ",")
		for i := range instanceIds {
			instanceIds[i] = strings.TrimSpace(instanceIds[i])
			if instanceIds[i] == "" {
				return fmt.Errorf("instanceNetworkIds contains empty value")
			}
		}
		httpClient := http.NewClient(token)
		resp, err := httpClient.AssignLoadBalancerRule(zoneID, lbAssignOpts.NetworkID, lbAssignOpts.RuleID, instanceIds)
		if err != nil {
			slog.Error("failed to assign instances to load balancer rule", "error", err)
			return fmt.Errorf("error: %w", err)
		}
		if resp.Data.Success {
			slog.Info("instances assigned to load balancer rule successfully")
			fmt.Println("Instances assigned to load balancer rule successfully.")
		} else {
			slog.Error("assign instances to load balancer rule unsuccessful", "response", resp)
			return fmt.Errorf("failed to assign instances to load balancer rule")
		}
		return nil
	},
}

NetworkLbAssignCmd is the command for assigning instances to a load balancing rule.

View Source
var NetworkLbCmd = &cobra.Command{
	Use:     "loadbalance",
	Aliases: []string{"lb", "load-balancer"},
	Short:   "Manage network load balancer rules",
}

NetworkLbCmd is the root command for network load balancer operations.

View Source
var NetworkLbCreateCmd = &cobra.Command{
	Use:   "create",
	Short: "Create a new load balancing rule for a network",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if err := cli.Preflight(true)(cmd, args); err != nil {
			return err
		}
		return cli.Validate(cmd,
			cli.Required("networkId"),
			cli.Required("publicIpId"),
			cli.Required("name"),
			cli.Required("algorithm"),
			cli.Required("publicPort"),
			cli.Required("privatePort"),
		)
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		token := cli.TokenFromContext(cmd.Context())
		zoneID := cli.ZoneIDFromContext(cmd.Context())
		if err := cli.LoadFromCobraFlags(cmd, &lbCreateOpts); err != nil {
			return err
		}
		httpClient := http.NewClient(token)
		_, err := httpClient.CreateLoadBalancerRule(
			zoneID,
			lbCreateOpts.NetworkID,
			lbCreateOpts.PublicIPID,
			lbCreateOpts.Name,
			lbCreateOpts.Algorithm,
			lbCreateOpts.PublicPort,
			lbCreateOpts.PrivatePort,
		)
		if err != nil {
			slog.Error("failed to create load balancer rule", "error", err)
			return fmt.Errorf("error: %w", err)
		}
		slog.Info("load balancer rule created successfully")
		fmt.Println("Load balancer rule created successfully")
		return nil
	},
}

NetworkLbCreateCmd is the command for creating a new load balancing rule.

View Source
var NetworkLbDeassignCmd = &cobra.Command{
	Use:   "deassign",
	Short: "De-assign an instance from a load balancing rule",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if err := cli.Preflight(true)(cmd, args); err != nil {
			return err
		}
		return cli.Validate(cmd,
			cli.Required("networkId"),
			cli.Required("ruleId"),
			cli.Required("instanceNetworkId"),
		)
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		token := cli.TokenFromContext(cmd.Context())
		zoneID := cli.ZoneIDFromContext(cmd.Context())
		if err := cli.LoadFromCobraFlags(cmd, &lbDeassignOpts); err != nil {
			return err
		}
		httpClient := http.NewClient(token)
		resp, err := httpClient.DeassignLoadBalancerRule(zoneID, lbDeassignOpts.NetworkID, lbDeassignOpts.RuleID, lbDeassignOpts.InstanceNetworkID)
		if err != nil {
			slog.Error("failed to de-assign instance from load balancer rule", "error", err)
			return fmt.Errorf("error: %w", err)
		}
		if resp.Data.Success {
			slog.Info("instance de-assigned from load balancer rule successfully")
			fmt.Println("Instance de-assigned from load balancer rule successfully.")
		} else {
			slog.Error("failed to de-assign instance from load balancer rule", "response", resp)
			return fmt.Errorf("de-assign failed")
		}
		return nil
	},
}

NetworkLbDeassignCmd is the command for de-assigning an instance from a load balancing rule.

View Source
var NetworkLbDeleteCmd = &cobra.Command{
	Use:   "delete",
	Short: "Delete a load balancing rule by its ID",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if err := cli.Preflight(true)(cmd, args); err != nil {
			return err
		}
		return cli.Validate(cmd,
			cli.Required("networkId"),
			cli.Required("ruleId"),
		)
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		token := cli.TokenFromContext(cmd.Context())
		zoneID := cli.ZoneIDFromContext(cmd.Context())
		if err := cli.LoadFromCobraFlags(cmd, &lbDeleteOpts); err != nil {
			return err
		}
		httpClient := http.NewClient(token)
		resp, err := httpClient.DeleteLoadBalancerRule(zoneID, lbDeleteOpts.NetworkID, lbDeleteOpts.RuleID)
		if err != nil {
			slog.Error("failed to delete load balancer rule", "error", err)
			return fmt.Errorf("error: %w", err)
		}
		if resp.Data.Success {
			slog.Info("load balancer rule deleted successfully")
			fmt.Println("Load balancer rule deleted successfully.")
		} else {
			slog.Error("load balancer rule deletion unsuccessful", "response", resp)
			return fmt.Errorf("failed to delete load balancer rule")
		}
		return nil
	},
}

NetworkLbDeleteCmd is the command for deleting a load balancing rule.

View Source
var NetworkLbListCmd = &cobra.Command{
	Use:   "list",
	Short: "List all load balancing rules for a network",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if err := cli.Preflight(true)(cmd, args); err != nil {
			return err
		}
		return cli.Validate(cmd,
			cli.Required("networkId"),
		)
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		token := cli.TokenFromContext(cmd.Context())
		zoneID := cli.ZoneIDFromContext(cmd.Context())
		if err := cli.LoadFromCobraFlags(cmd, &lbListOpts); err != nil {
			return err
		}
		httpClient := http.NewClient(token)
		resp, err := httpClient.ListLoadBalancerRules(zoneID, lbListOpts.NetworkID)
		if err != nil {
			slog.Error("failed to list load balancer rules", "error", err)
			return fmt.Errorf("error: %w", err)
		}
		if len(resp.Data) == 0 {
			fmt.Println("No load balancer rules found.")
			return nil
		}
		table := tablewriter.NewWriter(os.Stdout)
		table.SetHeader([]string{"ID", "Name", "Algorithm", "PublicPort", "PrivatePort", "Status"})
		for _, rule := range resp.Data {
			table.Append([]string{
				rule.ID,
				rule.Name,
				rule.Algorithm,
				fmt.Sprintf("%d", rule.PublicPort),
				fmt.Sprintf("%d", rule.PrivatePort),
				rule.Status,
			})
		}
		table.Render()
		return nil
	},
}

NetworkLbListCmd is the command for listing load balancing rules.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL