github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/cmd/ndm_daemonset/app/command/device-list.go (about)

     1  /*
     2  Copyright 2018 The OpenEBS Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package command
    18  
    19  import (
    20  	"fmt"
    21  	"html/template"
    22  	"os"
    23  
    24  	"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  /*
    29  defaultDeviceList template use to print list of device
    30  This template looks like below -
    31  
    32  when disk resources present
    33  root@instance-1:~#ndm device list
    34  NAME                                         PATH      CAPACITY       STATUS    SERIAL                   MODEL               VENDOR
    35  disk-ccc636c88bd9ab09dde9de476309058d        /dev/sda  10737418240    Inactive  instance-1               PersistentDisk      Google
    36  disk-ce41f8f5fa22acb79ec56292441dc207        /dev/sdb  10737418240    Active    disk-1                   PersistentDisk      Google
    37  
    38  when no resource present
    39  root@instance-1:~#ndm device list
    40  No disk resource present.
    41  */
    42  const defaultDeviceList = `
    43  {{- if .Items}}
    44  	{{- printf "%-45s" "NAME"}}
    45  	{{- printf "%-10s" "PATH"}}
    46  	{{- printf "%-15s" "CAPACITY"}}
    47  	{{- printf "%-10s" "STATUS"}}
    48  	{{- printf "%-25s" "SERIAL"}}
    49  	{{- printf "%-20s" "MODEL"}}
    50  	{{- printf "%-20s" "VENDOR"}}
    51  {{range .Items}}
    52  	{{- printf "%-45s" .ObjectMeta.Name}}
    53  	{{- printf "%-10s" .Spec.Path}}
    54  	{{- printf "%-15d" .Spec.Capacity.Storage}}
    55  	{{- printf "%-10s" .Status.State}}
    56  	{{- printf "%-25s" .Spec.Details.Serial}}
    57  	{{- printf "%-20s" .Spec.Details.Model}}
    58  	{{- printf "%-20s" .Spec.Details.Vendor}}
    59  {{end}}
    60  {{- else}}
    61  	{{- printf "%s" "No disk resource present."}}
    62  {{end}}`
    63  
    64  // NewSubCmdListBlockDevice is to list block device is created
    65  func NewSubCmdListBlockDevice() *cobra.Command {
    66  	getCmd := &cobra.Command{
    67  		Use:   "list",
    68  		Short: "List block devices",
    69  		Long: `the set of block devices on the node
    70  		can be listed via 'ndm device list' command`,
    71  		Run: func(cmd *cobra.Command, args []string) {
    72  			err := deviceList()
    73  			if err != nil {
    74  				fmt.Println(err)
    75  				os.Exit(1)
    76  			}
    77  		},
    78  	}
    79  
    80  	return getCmd
    81  }
    82  
    83  // deviceList prints list of devices using defaultDeviceList template
    84  func deviceList() error {
    85  	ctrl, err := controller.NewController()
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	err = ctrl.SetControllerOptions(options)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	// TODO @akhilerm should pass the filter as args to List, so that all devices will be listed
    96  	diskList, err := ctrl.ListBlockDeviceResource(false)
    97  	if err != nil {
    98  		return err
    99  	}
   100  	diskListTemplate := template.Must(template.New("defaultDeviceList").Parse(defaultDeviceList))
   101  	err = diskListTemplate.Execute(os.Stdout, diskList)
   102  	return err
   103  }