From ddb43694b9e0285eda128a5e45da7404b80cfb1c Mon Sep 17 00:00:00 2001 From: Drew Walters Date: Tue, 28 Apr 2020 16:26:23 +0000 Subject: [PATCH] Add label support to baremetal power commands The power commands support interacting with hosts by providing the host document name as an argument. This change adds support for filtering hosts using a labels flag (-l or --labels). This change also adds a name flag to support filtering host documents by name and removes the argument requirement. Change-Id: I54d487a35fb36f26fe8e9931f57bbda25375e714 Signed-off-by: Drew Walters --- cmd/baremetal/baremetal.go | 8 ++++++++ cmd/baremetal/poweroff.go | 11 ++++++++--- cmd/baremetal/poweron.go | 13 +++++++++---- cmd/baremetal/powerstatus.go | 11 ++++++++--- cmd/baremetal/reboot.go | 11 ++++++++--- .../baremetal-poweroff-with-help.golden | 8 +++++--- .../baremetal-poweron-with-help.golden | 8 +++++--- .../baremetal-powerstatus-with-help.golden | 8 +++++--- .../baremetal-reboot-with-help.golden | 8 +++++--- 9 files changed, 61 insertions(+), 25 deletions(-) diff --git a/cmd/baremetal/baremetal.go b/cmd/baremetal/baremetal.go index 8551850e0..cb9f805d2 100644 --- a/cmd/baremetal/baremetal.go +++ b/cmd/baremetal/baremetal.go @@ -21,6 +21,14 @@ import ( ) const ( + flagLabel = "labels" + flagLabelShort = "l" + flagLabelDescription = "Label(s) to filter desired baremetal host documents" + + flagName = "name" + flagNameShort = "n" + flagNameDescription = "Name to filter desired baremetal host document" + flagPhase = "phase" flagPhaseDescription = "airshipctl phase that contains the desired baremetal host document(s)" ) diff --git a/cmd/baremetal/poweroff.go b/cmd/baremetal/poweroff.go index 48643d362..9c1f652d8 100644 --- a/cmd/baremetal/poweroff.go +++ b/cmd/baremetal/poweroff.go @@ -26,13 +26,16 @@ import ( // NewPowerOffCommand provides a command to shutdown a remote host. func NewPowerOffCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { + var labels string + var name string var phase string + cmd := &cobra.Command{ - Use: "poweroff BAREMETAL_HOST_DOC_NAME", + Use: "poweroff", Short: "Shutdown a baremetal host", - Args: cobra.ExactArgs(1), + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - m, err := remote.NewManager(rootSettings, phase, remote.ByName(args[0])) + m, err := remote.NewManager(rootSettings, phase, remote.ByLabel(labels), remote.ByName(name)) if err != nil { return err } @@ -50,6 +53,8 @@ func NewPowerOffCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Com } flags := cmd.Flags() + flags.StringVarP(&labels, flagLabel, flagLabelShort, "", flagLabelDescription) + flags.StringVarP(&name, flagName, flagNameShort, "", flagNameDescription) flags.StringVar(&phase, flagPhase, config.BootstrapPhase, flagPhaseDescription) return cmd diff --git a/cmd/baremetal/poweron.go b/cmd/baremetal/poweron.go index 7a90b3aff..3a4ec5335 100644 --- a/cmd/baremetal/poweron.go +++ b/cmd/baremetal/poweron.go @@ -26,13 +26,16 @@ import ( // NewPowerOnCommand provides a command with the capability to power on baremetal hosts. func NewPowerOnCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { + var labels string + var name string var phase string + cmd := &cobra.Command{ - Use: "poweron BAREMETAL_HOST_DOC_NAME", + Use: "poweron", Short: "Power on a host", - Args: cobra.ExactArgs(1), + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - m, err := remote.NewManager(rootSettings, phase, remote.ByName(args[0])) + m, err := remote.NewManager(rootSettings, phase, remote.ByLabel(labels), remote.ByName(name)) if err != nil { return err } @@ -42,7 +45,7 @@ func NewPowerOnCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comm return err } - fmt.Fprintf(cmd.OutOrStdout(), "Powered on remote host %s\n", args[0]) + fmt.Fprintf(cmd.OutOrStdout(), "Powered on remote host %s\n", host.HostName) } return nil @@ -50,6 +53,8 @@ func NewPowerOnCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comm } flags := cmd.Flags() + flags.StringVarP(&labels, flagLabel, flagLabelShort, "", flagLabelDescription) + flags.StringVarP(&name, flagName, flagNameShort, "", flagNameDescription) flags.StringVar(&phase, flagPhase, config.BootstrapPhase, flagPhaseDescription) return cmd diff --git a/cmd/baremetal/powerstatus.go b/cmd/baremetal/powerstatus.go index d6e9dc52d..ae12e3958 100644 --- a/cmd/baremetal/powerstatus.go +++ b/cmd/baremetal/powerstatus.go @@ -26,13 +26,16 @@ import ( // NewPowerStatusCommand provides a command to retrieve the power status of a baremetal host. func NewPowerStatusCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { + var labels string + var name string var phase string + cmd := &cobra.Command{ - Use: "powerstatus BAREMETAL_HOST_DOC_NAME", + Use: "powerstatus", Short: "Retrieve the power status of a baremetal host", - Args: cobra.ExactArgs(1), + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - m, err := remote.NewManager(rootSettings, phase, remote.ByName(args[0])) + m, err := remote.NewManager(rootSettings, phase, remote.ByLabel(labels), remote.ByName(name)) if err != nil { return err } @@ -52,6 +55,8 @@ func NewPowerStatusCommand(rootSettings *environment.AirshipCTLSettings) *cobra. } flags := cmd.Flags() + flags.StringVarP(&labels, flagLabel, flagLabelShort, "", flagLabelDescription) + flags.StringVarP(&name, flagName, flagNameShort, "", flagNameDescription) flags.StringVar(&phase, flagPhase, config.BootstrapPhase, flagPhaseDescription) return cmd diff --git a/cmd/baremetal/reboot.go b/cmd/baremetal/reboot.go index a271099bd..b5395cc09 100644 --- a/cmd/baremetal/reboot.go +++ b/cmd/baremetal/reboot.go @@ -26,13 +26,16 @@ import ( // NewRebootCommand provides a command with the capability to reboot baremetal hosts. func NewRebootCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { + var labels string + var name string var phase string + cmd := &cobra.Command{ - Use: "reboot BAREMETAL_HOST_DOC_NAME", + Use: "reboot", Short: "Reboot a host", - Args: cobra.ExactArgs(1), + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - m, err := remote.NewManager(rootSettings, phase, remote.ByName(args[0])) + m, err := remote.NewManager(rootSettings, phase, remote.ByLabel(labels), remote.ByName(name)) if err != nil { return err } @@ -50,6 +53,8 @@ func NewRebootCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma } flags := cmd.Flags() + flags.StringVarP(&labels, flagLabel, flagLabelShort, "", flagLabelDescription) + flags.StringVarP(&name, flagName, flagNameShort, "", flagNameDescription) flags.StringVar(&phase, flagPhase, config.BootstrapPhase, flagPhaseDescription) return cmd diff --git a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-poweroff-with-help.golden b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-poweroff-with-help.golden index ebb3c13ef..af65bb181 100644 --- a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-poweroff-with-help.golden +++ b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-poweroff-with-help.golden @@ -1,8 +1,10 @@ Shutdown a baremetal host Usage: - poweroff BAREMETAL_HOST_DOC_NAME [flags] + poweroff [flags] Flags: - -h, --help help for poweroff - --phase string airshipctl phase that contains the desired baremetal host document(s) (default "bootstrap") + -h, --help help for poweroff + -l, --labels string Label(s) to filter desired baremetal host documents + -n, --name string Name to filter desired baremetal host document + --phase string airshipctl phase that contains the desired baremetal host document(s) (default "bootstrap") diff --git a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-poweron-with-help.golden b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-poweron-with-help.golden index 3d7ab695d..f58abcf1c 100644 --- a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-poweron-with-help.golden +++ b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-poweron-with-help.golden @@ -1,8 +1,10 @@ Power on a host Usage: - poweron BAREMETAL_HOST_DOC_NAME [flags] + poweron [flags] Flags: - -h, --help help for poweron - --phase string airshipctl phase that contains the desired baremetal host document(s) (default "bootstrap") + -h, --help help for poweron + -l, --labels string Label(s) to filter desired baremetal host documents + -n, --name string Name to filter desired baremetal host document + --phase string airshipctl phase that contains the desired baremetal host document(s) (default "bootstrap") diff --git a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-powerstatus-with-help.golden b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-powerstatus-with-help.golden index 149b13476..f5242d5a8 100644 --- a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-powerstatus-with-help.golden +++ b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-powerstatus-with-help.golden @@ -1,8 +1,10 @@ Retrieve the power status of a baremetal host Usage: - powerstatus BAREMETAL_HOST_DOC_NAME [flags] + powerstatus [flags] Flags: - -h, --help help for powerstatus - --phase string airshipctl phase that contains the desired baremetal host document(s) (default "bootstrap") + -h, --help help for powerstatus + -l, --labels string Label(s) to filter desired baremetal host documents + -n, --name string Name to filter desired baremetal host document + --phase string airshipctl phase that contains the desired baremetal host document(s) (default "bootstrap") diff --git a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-reboot-with-help.golden b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-reboot-with-help.golden index bd7c5c678..579533816 100644 --- a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-reboot-with-help.golden +++ b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-reboot-with-help.golden @@ -1,8 +1,10 @@ Reboot a host Usage: - reboot BAREMETAL_HOST_DOC_NAME [flags] + reboot [flags] Flags: - -h, --help help for reboot - --phase string airshipctl phase that contains the desired baremetal host document(s) (default "bootstrap") + -h, --help help for reboot + -l, --labels string Label(s) to filter desired baremetal host documents + -n, --name string Name to filter desired baremetal host document + --phase string airshipctl phase that contains the desired baremetal host document(s) (default "bootstrap")