instance

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: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NetworkInstanceCmd = &cobra.Command{
	Use:   "instance",
	Short: "Manage instances connected to a network",
	Long:  `Perform operations on instances connected to a network such as connect, disconnect, and list.`,
}

NetworkInstanceCmd is the root command for managing network instances.

View Source
var NetworkInstanceConnectCmd = &cobra.Command{
	Use:   "connect",
	Short: "Connect an instance to 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("instanceId"),
		)
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		token := cli.TokenFromContext(cmd.Context())
		zoneID := cli.ZoneIDFromContext(cmd.Context())
		if err := cli.LoadFromCobraFlags(cmd, &networkInstanceConnectOpt); err != nil {
			return err
		}
		httpClient := http.NewClient(token)
		resp, err := httpClient.ConnectInstanceToNetwork(zoneID, networkInstanceConnectOpt.NetworkID, networkInstanceConnectOpt.InstanceID)
		if err != nil {
			slog.Error("failed to connect instance to network", "error", err)
			return fmt.Errorf("failed to connect instance: %w", err)
		}
		if resp.Data.Success {
			slog.Info("instance connected to network successfully")
			fmt.Println("Instance successfully connected to network.")
		} else {
			slog.Error("connect instance to network unsuccessful", "response", resp)
			return fmt.Errorf("failed to connect instance to network")
		}
		return nil
	},
}

NetworkInstanceConnectCmd is the command for connecting an instance to a network.

View Source
var NetworkInstanceDisconnectCmd = &cobra.Command{
	Use:   "disconnect",
	Short: "Disconnect an instance from 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("instanceId"),
			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, &networkInstanceDisConnectOpt); err != nil {
			return err
		}
		httpClient := http.NewClient(token)
		resp, err := httpClient.DisconnectInstanceFromNetwork(zoneID, networkInstanceDisConnectOpt.NetworkID, networkInstanceDisConnectOpt.InstanceID, networkInstanceDisConnectOpt.InstanceNetworkID)
		if err != nil {
			if strings.Contains(err.Error(), "instance_network_id you dont have access") {
				slog.Error("invalid instance_network_id or no access", "error", err)
				return fmt.Errorf("the provided instance_network_id is invalid or you do not have access. Please check and try again")
			}
			slog.Error("failed to disconnect instance from network", "error", err)
			return fmt.Errorf("failed to disconnect instance: %w", err)
		}
		if resp.Data.Success {
			slog.Info("instance disconnected from network successfully")
			fmt.Println("Instance successfully disconnected from network.")
		} else {
			slog.Error("disconnect instance from network unsuccessful", "response", resp)
			return fmt.Errorf("failed to disconnect instance from network")
		}
		return nil
	},
}

NetworkInstanceDisconnectCmd is the command for disconnecting an instance from a network.

View Source
var NetworkInstanceListCmd = &cobra.Command{
	Use:   "list",
	Short: "List all instances connected to 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("instanceId"),
		)
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		token := cli.TokenFromContext(cmd.Context())
		zoneID := cli.ZoneIDFromContext(cmd.Context())
		if err := cli.LoadFromCobraFlags(cmd, &networkInstanceListOpt); err != nil {
			return err
		}
		httpClient := http.NewClient(token)
		resp, err := httpClient.ListNetworkInstances(zoneID, networkInstanceListOpt.NetworkID, networkInstanceListOpt.InstanceID)
		if err != nil {
			slog.Error("failed to list instances", "error", err)
			return fmt.Errorf("failed to list instances: %w", err)
		}
		if len(resp.Data) == 0 {
			fmt.Println("No instances connected to this network.")
			return nil
		}
		presenter.RenderInstanceNetworkList(resp.Data)
		return nil
	},
}

NetworkInstanceListCmd is the command for listing instances connected to a network.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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