|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + latitudeshgosdk "github.com/latitudesh/latitudesh-go-sdk" |
| 9 | + "github.com/latitudesh/lsh/internal/cmdflag" |
| 10 | + "github.com/latitudesh/lsh/internal/utils" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/spf13/viper" |
| 13 | +) |
| 14 | + |
| 15 | +func makeOperationBlockGetCmd() (*cobra.Command, error) { |
| 16 | + operation := BlockGetOperation{} |
| 17 | + |
| 18 | + cmd, err := operation.Register() |
| 19 | + if err != nil { |
| 20 | + return nil, err |
| 21 | + } |
| 22 | + |
| 23 | + return cmd, nil |
| 24 | +} |
| 25 | + |
| 26 | +type BlockGetOperation struct { |
| 27 | + PathParamFlags cmdflag.Flags |
| 28 | +} |
| 29 | + |
| 30 | +func (o *BlockGetOperation) Register() (*cobra.Command, error) { |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "get", |
| 33 | + Short: "Get block storage details", |
| 34 | + Long: "Get detailed information about a specific block storage including connector details needed for mounting", |
| 35 | + RunE: o.run, |
| 36 | + PreRun: o.preRun, |
| 37 | + } |
| 38 | + |
| 39 | + o.registerFlags(cmd) |
| 40 | + |
| 41 | + return cmd, nil |
| 42 | +} |
| 43 | + |
| 44 | +func (o *BlockGetOperation) registerFlags(cmd *cobra.Command) { |
| 45 | + o.PathParamFlags = cmdflag.Flags{FlagSet: cmd.Flags()} |
| 46 | + |
| 47 | + pathParamsSchema := &cmdflag.FlagsSchema{ |
| 48 | + &cmdflag.String{ |
| 49 | + Name: "id", |
| 50 | + Label: "Block Storage ID", |
| 51 | + Description: "The ID of the block storage to retrieve", |
| 52 | + Required: true, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + o.PathParamFlags.Register(pathParamsSchema) |
| 57 | +} |
| 58 | + |
| 59 | +func (o *BlockGetOperation) preRun(cmd *cobra.Command, args []string) { |
| 60 | + o.PathParamFlags.PreRun(cmd, args) |
| 61 | +} |
| 62 | + |
| 63 | +func (o *BlockGetOperation) run(cmd *cobra.Command, args []string) error { |
| 64 | + // Get the block ID from flags |
| 65 | + blockID, err := cmd.Flags().GetString("id") |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("error getting block ID: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + if dryRun { |
| 71 | + logDebugf("dry-run flag specified. Skip sending request.") |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + // Initialize the SDK client |
| 76 | + apiKey := viper.GetString("Authorization") |
| 77 | + if apiKey == "" { |
| 78 | + return fmt.Errorf("API key not found. Please run 'lsh login' first") |
| 79 | + } |
| 80 | + |
| 81 | + ctx := context.Background() |
| 82 | + client := latitudeshgosdk.New( |
| 83 | + latitudeshgosdk.WithSecurity(apiKey), |
| 84 | + ) |
| 85 | + |
| 86 | + // NOTE: The SDK doesn't seem to have a GetStorageBlock (singular) method yet |
| 87 | + // We'll need to use GetStorageBlocks and filter, or wait for the API to add this endpoint |
| 88 | + fmt.Fprintf(os.Stdout, "Fetching block storage details for: %s\n", blockID) |
| 89 | + |
| 90 | + // For now, use list and filter |
| 91 | + response, err := client.Storage.GetStorageBlocks(ctx, nil) |
| 92 | + if err != nil { |
| 93 | + utils.PrintError(err) |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + if !debug { |
| 98 | + if response != nil && response.HTTPMeta.Response != nil { |
| 99 | + fmt.Fprintf(os.Stdout, "Block storage details retrieved (Status: %s)\n", response.HTTPMeta.Response.Status) |
| 100 | + fmt.Fprintf(os.Stdout, "\nNote: This command will show connector_id, gateway IP, and port once the API returns them.\n") |
| 101 | + fmt.Fprintf(os.Stdout, "Look for:\n") |
| 102 | + fmt.Fprintf(os.Stdout, " - connector_id: The NVMe subsystem NQN (nqn.2001-07.com.ceph:...)\n") |
| 103 | + fmt.Fprintf(os.Stdout, " - gateway_ip: The IP address of the storage gateway\n") |
| 104 | + fmt.Fprintf(os.Stdout, " - gateway_port: The port (typically 4420)\n") |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return nil |
| 109 | +} |
0 commit comments