Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions cmd/uncloud/machine/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package machine

import (
"fmt"
"strings"

"github.com/psviderski/uncloud/internal/cli"
"github.com/spf13/cobra"
)

func newLabelCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "label",
Short: "Manage machine labels",
RunE: func(cmd *cobra.Command, args []string) error {
return fmt.Errorf("a valid subcommand is required")
},
}
cmd.AddCommand(
newLabelAddCmd(),
newLabelRmCmd(),
newLabelLsCmd(),
)
return cmd
}

func newLabelAddCmd() *cobra.Command {
return &cobra.Command{
Use: "add <machine> <key=value> [key=value...]",
Short: "Add one or more labels to a machine",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
machineNameOrID := args[0]
labelArgs := args[1:]

labels := make(map[string]string)
for _, arg := range labelArgs {
parts := strings.SplitN(arg, "=", 2)
key := parts[0]
val := ""
if len(parts) > 1 {
val = parts[1]
}
labels[key] = val
}

client, err := uncli.ConnectCluster(cmd.Context())
if err != nil {
return err
}
defer client.Close()

if _, err = client.AddMachineLabels(cmd.Context(), machineNameOrID, labels); err != nil {
return err
}

fmt.Printf("Label(s) added to machine %q.\n", machineNameOrID)
return nil
},
}
}

func newLabelRmCmd() *cobra.Command {
return &cobra.Command{
Use: "rm <machine> <key> [key...]",
Short: "Remove one or more labels from a machine",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
machineNameOrID := args[0]
labels := args[1:]

client, err := uncli.ConnectCluster(cmd.Context())
if err != nil {
return err
}
defer client.Close()

if _, err = client.RemoveMachineLabels(cmd.Context(), machineNameOrID, labels); err != nil {
return err
}

fmt.Printf("Label(s) removed from machine %q.\n", machineNameOrID)
return nil
},
}
}

func newLabelLsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ls <machine>",
Short: "List labels on a machine",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
machineNameOrID := args[0]

client, err := uncli.ConnectCluster(cmd.Context())
if err != nil {
return err
}
defer client.Close()

labels, err := client.GetMachineLabels(cmd.Context(), machineNameOrID)
if err != nil {
return fmt.Errorf("list labels for machine: %w", err)
}

if len(labels) == 0 {
fmt.Printf("No labels found for machine %q.\n", machineNameOrID)
return nil
}

fmt.Printf("Labels for machine %q:\n", machineNameOrID)
for key, val := range labels {
if val == "" {
fmt.Printf(" %s\n", key)
} else {
fmt.Printf(" %s=%s\n", key, val)
}
}
return nil
},
}
return cmd
}
1 change: 1 addition & 0 deletions cmd/uncloud/machine/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func NewRootCommand() *cobra.Command {
NewRmCommand(),
NewUpdateCommand(),
NewTokenCommand(),
newLabelCmd(),
)
return cmd
}
316 changes: 242 additions & 74 deletions internal/machine/api/pb/cluster.pb.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions internal/machine/api/pb/cluster.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ service Cluster {
rpc GetDomain(google.protobuf.Empty) returns (Domain);
rpc ReleaseDomain(google.protobuf.Empty) returns (Domain);
rpc CreateDomainRecords(CreateDomainRecordsRequest) returns (CreateDomainRecordsResponse);
rpc AddMachineLabels(AddMachineLabelsRequest) returns (UpdateMachineResponse);
rpc RemoveMachineLabels(RemoveMachineLabelsRequest) returns (UpdateMachineResponse);
}

message AddMachineRequest {
Expand Down Expand Up @@ -96,3 +98,13 @@ message DNSRecord {
RecordType type = 2;
repeated string values = 3;
}

message AddMachineLabelsRequest {
string machine_id = 1;
map<string, string> labels = 2;
}

message RemoveMachineLabelsRequest {
string machine_id = 1;
repeated string labels = 2;
}
76 changes: 76 additions & 0 deletions internal/machine/api/pb/cluster_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading