Skip to content

Commit

Permalink
Merge pull request #601 from ava-labs/avalanche-academy
Browse files Browse the repository at this point in the history
add control commands to best support avalanche academy course
  • Loading branch information
felipemadero authored Jul 21, 2023
2 parents 36ece61 + 8fe9b2e commit 85f7c45
Show file tree
Hide file tree
Showing 7 changed files with 1,749 additions and 278 deletions.
40 changes: 40 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ type Client interface {
LoadSnapshot(ctx context.Context, snapshotName string, opts ...OpOption) (*rpcpb.LoadSnapshotResponse, error)
RemoveSnapshot(ctx context.Context, snapshotName string) (*rpcpb.RemoveSnapshotResponse, error)
GetSnapshotNames(ctx context.Context) ([]string, error)
ListSubnets(ctx context.Context) ([]string, error)
ListBlockchains(ctx context.Context) ([]*rpcpb.CustomChainInfo, error)
ListRpcs(ctx context.Context) ([]*rpcpb.BlockchainRpcs, error)
VMID(ctx context.Context, vmName string) (string, error)
}

type client struct {
Expand Down Expand Up @@ -392,6 +396,42 @@ func (c *client) GetSnapshotNames(ctx context.Context) ([]string, error) {
return resp.SnapshotNames, nil
}

func (c *client) ListSubnets(ctx context.Context) ([]string, error) {
c.log.Info("list subnets")
resp, err := c.controlc.ListSubnets(ctx, &rpcpb.ListSubnetsRequest{})
if err != nil {
return nil, err
}
return resp.SubnetIds, nil
}

func (c *client) ListBlockchains(ctx context.Context) ([]*rpcpb.CustomChainInfo, error) {
c.log.Info("list blockchains")
resp, err := c.controlc.ListBlockchains(ctx, &rpcpb.ListBlockchainsRequest{})
if err != nil {
return nil, err
}
return resp.Blockchains, nil
}

func (c *client) ListRpcs(ctx context.Context) ([]*rpcpb.BlockchainRpcs, error) {
c.log.Info("list rpcs")
resp, err := c.controlc.ListRpcs(ctx, &rpcpb.ListRpcsRequest{})
if err != nil {
return nil, err
}
return resp.BlockchainsRpcs, nil
}

func (c *client) VMID(ctx context.Context, vmName string) (string, error) {
c.log.Info("vmud")
resp, err := c.controlc.VMID(ctx, &rpcpb.VMIDRequest{VmName: vmName})
if err != nil {
return "", err
}
return resp.VmId, nil
}

func (c *client) Close() error {
c.closeOnce.Do(func() {
close(c.closed)
Expand Down
135 changes: 135 additions & 0 deletions cmd/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func NewCommand() *cobra.Command {
newLoadSnapshotCommand(),
newRemoveSnapshotCommand(),
newGetSnapshotNamesCommand(),
newVMIDCommand(),
newListSubnetsCommand(),
newListBlockchainsCommand(),
newListRPCsCommand(),
)

return cmd
Expand Down Expand Up @@ -1343,6 +1347,137 @@ func getSnapshotNamesFunc(*cobra.Command, []string) error {
return nil
}

func newVMIDCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "vmid vm-name",
Short: "Returns the vm id associated to the given vm name.",
RunE: VMIDFunc,
Args: cobra.ExactArgs(1),
}
return cmd
}

func VMIDFunc(_ *cobra.Command, args []string) error {
vmName := args[0]
cli, err := newClient()
if err != nil {
return err
}
defer cli.Close()

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
vmID, err := cli.VMID(ctx, vmName)
cancel()
if err != nil {
return err
}

ux.Print(log, logging.Green.Wrap("VMID: %s"), vmID)
return nil
}

func newListSubnetsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list-subnets",
Short: "Lists all subnet ids of the network.",
RunE: listSubnetsFunc,
Args: cobra.ExactArgs(0),
}
return cmd
}

func listSubnetsFunc(*cobra.Command, []string) error {
cli, err := newClient()
if err != nil {
return err
}
defer cli.Close()

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
subnetIDs, err := cli.ListSubnets(ctx)
cancel()
if err != nil {
return err
}

for _, subnetID := range subnetIDs {
ux.Print(log, logging.Green.Wrap("Subnet ID: %s"), subnetID)
}

return nil
}

func newListBlockchainsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list-blockchains",
Short: "Lists all blockchain ids of the network.",
RunE: listBlockchainsFunc,
Args: cobra.ExactArgs(0),
}
return cmd
}

func listBlockchainsFunc(*cobra.Command, []string) error {
cli, err := newClient()
if err != nil {
return err
}
defer cli.Close()

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
resp, err := cli.ListBlockchains(ctx)
cancel()
if err != nil {
return err
}

ux.Print(log, "")
for _, blockchain := range resp {
ux.Print(log, logging.Green.Wrap("Blockchain ID: %s"), blockchain.ChainId)
ux.Print(log, logging.Green.Wrap(" VM Name: %s"), blockchain.ChainName)
ux.Print(log, logging.Green.Wrap(" VM ID: %s"), blockchain.VmId)
ux.Print(log, logging.Green.Wrap(" Subnet ID: %s"), blockchain.SubnetId)
ux.Print(log, "")
}

return nil
}

func newListRPCsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list-rpcs",
Short: "Lists rpcs for all blockchain of the network.",
RunE: listRPCsFunc,
Args: cobra.ExactArgs(0),
}
return cmd
}

func listRPCsFunc(*cobra.Command, []string) error {
cli, err := newClient()
if err != nil {
return err
}
defer cli.Close()

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
resp, err := cli.ListRpcs(ctx)
cancel()
if err != nil {
return err
}

ux.Print(log, "")
for _, blockchainRpcs := range resp {
ux.Print(log, logging.Green.Wrap("Blockchain ID: %s"), blockchainRpcs.BlockchainId)
for _, rpc := range blockchainRpcs.Rpcs {
ux.Print(log, logging.Green.Wrap(" %s: %s"), rpc.NodeName, rpc.Rpc)
}
ux.Print(log, "")
}
return nil
}

func newClient() (client.Client, error) {
if err := setLogs(); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 85f7c45

Please sign in to comment.