Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjustments in for preparation for hpcm/ngi providers #186

Merged
merged 3 commits into from
Jan 8, 2024
Merged
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
7 changes: 4 additions & 3 deletions cmd/blade/add_blade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -42,11 +42,12 @@ import (

// AddBladeCmd represents the blade add command
var AddBladeCmd = &cobra.Command{
Use: "blade",
Use: "blade PROVIDER",
Short: "Add blades to the inventory.",
Long: `Add blades to the inventory.`,
PreRunE: validHardware, // Hardware can only be valid if defined in the hardware library
RunE: addBlade, // Add a blade when this sub-command is called
Args: cobra.ExactArgs(1),
RunE: addBlade, // Add a blade when this sub-command is called
}

// addBlade adds a blade to the inventory
Expand Down
21 changes: 19 additions & 2 deletions cmd/blade/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand All @@ -26,7 +26,12 @@
package blade

import (
"os"

root "github.com/Cray-HPE/cani/cmd"
"github.com/Cray-HPE/cani/internal/domain"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var (
Expand All @@ -40,7 +45,8 @@ var (
sortBy string
)

func init() {
func Init() {
log.Trace().Msgf("%+v", "github.com/Cray-HPE/cani/cmd/blade.init")
// Add blade variants to root commands
root.AddCmd.AddCommand(AddBladeCmd)
root.ListCmd.AddCommand(ListBladeCmd)
Expand All @@ -63,4 +69,15 @@ func init() {
RemoveBladeCmd.Flags().BoolVarP(&recursion, "recursive", "R", false, "Recursively delete child hardware")
ListBladeCmd.Flags().StringVarP(&format, "format", "f", "pretty", "Format output")
ListBladeCmd.Flags().StringVarP(&sortBy, "sort", "s", "location", "Sort by a specific key")

// Register all provider commands during init()
for _, p := range domain.GetProviders() {
for _, c := range []*cobra.Command{AddBladeCmd, ListBladeCmd} {
err := root.RegisterProviderCommand(p, c)
if err != nil {
log.Error().Msgf("Unable to get command '%s %s' from provider %s ", c.Parent().Name(), c.Name(), p.Slug())
os.Exit(1)
}
}
}
}
6 changes: 3 additions & 3 deletions cmd/blade/list_blade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -35,10 +35,10 @@ import (

// ListBladeCmd represents the blade list command
var ListBladeCmd = &cobra.Command{
Use: "blade",
Use: "blade PROVIDER",
Short: "List blades in the inventory.",
Long: `List blades in the inventory.`,
Args: cobra.ArbitraryArgs,
Args: cobra.ExactArgs(1),
RunE: listBlade,
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/blade/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -31,13 +31,11 @@ import (
"os"

root "github.com/Cray-HPE/cani/cmd"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

// validHardware checks that the hardware type is valid by comparing it against the list of hardware types
func validHardware(cmd *cobra.Command, args []string) (err error) {
log.Debug().Msgf("Validating hardware %+v", root.D)
if cmd.Flags().Changed("list-supported-types") {
cmd.SetOut(os.Stdout)
for _, hw := range root.BladeTypes {
Expand Down
11 changes: 5 additions & 6 deletions cmd/cabinet/add_cabinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -42,11 +42,12 @@ import (

// AddCabinetCmd represents the cabinet add command
var AddCabinetCmd = &cobra.Command{
Use: "cabinet",
Use: "cabinet PROVIDER",
Short: "Add cabinets to the inventory.",
Long: `Add cabinets to the inventory.`,
PreRunE: validHardware, // Hardware can only be valid if defined in the hardware library
RunE: addCabinet, // Add a cabinet when this sub-command is called
Args: cobra.ExactArgs(1),
RunE: addCabinet,
}

// addCabinet adds a cabinet to the inventory
Expand Down Expand Up @@ -75,7 +76,7 @@ func addCabinet(cmd *cobra.Command, args []string) (err error) {
}

// log the provider recommendations to the screen
recommendations.Print()
root.D.PrintRecommendations(cmd, args, recommendations)
}

// Add the cabinet to the inventory using domain methods
Expand All @@ -98,8 +99,6 @@ func addCabinet(cmd *cobra.Command, args []string) (err error) {
var filtered = make(map[uuid.UUID]inventory.Hardware, 0)
for _, result := range result.AddedHardware {
if result.Hardware.Type == hardwaretypes.Cabinet {
log.Debug().Msgf("%s added at %s with parent %s (%s)", result.Hardware.Type, result.Location.String(), hardwaretypes.System, result.Hardware.Parent)
log.Info().Str("status", "SUCCESS").Msgf("%s %d was successfully staged to be added to the system", hardwaretypes.Cabinet, recommendations.CabinetOrdinal)
filtered[result.Hardware.ID] = result.Hardware
}
}
Expand Down
38 changes: 18 additions & 20 deletions cmd/cabinet/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand All @@ -29,22 +29,20 @@ import (
"os"

root "github.com/Cray-HPE/cani/cmd"
"github.com/Cray-HPE/cani/internal/domain"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var (
cabinetNumber int
auto bool
accept bool
format string
sortBy string
ProviderAddCabinetCmd = &cobra.Command{}
ProviderListCabinetCmd = &cobra.Command{}
auto bool
accept bool
format string
sortBy string
)

func init() {
var err error
func Init() {
log.Trace().Msgf("%+v", "github.com/Cray-HPE/cani/cmd/cabinet.init")

// Add subcommands to root commands
root.AddCmd.AddCommand(AddCabinetCmd)
Expand All @@ -53,22 +51,22 @@ func init() {

// Common 'add cabinet' flags and then merge with provider-specified command
AddCabinetCmd.Flags().BoolP("list-supported-types", "L", false, "List supported hardware types.")
AddCabinetCmd.Flags().IntVar(&cabinetNumber, "cabinet", 1001, "Cabinet number.")
AddCabinetCmd.Flags().BoolVar(&auto, "auto", false, "Automatically recommend and assign required flags.")
AddCabinetCmd.MarkFlagsMutuallyExclusive("auto")
AddCabinetCmd.Flags().BoolVarP(&accept, "accept", "y", false, "Automatically accept recommended values.")
err = root.MergeProviderCommand(AddCabinetCmd)
if err != nil {
log.Error().Msgf("%+v", err)
os.Exit(1)
}

// Common 'list cabinet' flags and then merge with provider-specified command
ListCabinetCmd.Flags().StringVarP(&format, "format", "f", "pretty", "Format out output")
ListCabinetCmd.Flags().StringVarP(&sortBy, "sort", "s", "location", "Sort by a specific key")
err = root.MergeProviderCommand(ListCabinetCmd)
if err != nil {
log.Error().Msgf("%+v", err)
os.Exit(1)

// Register all provider commands during init()
for _, p := range domain.GetProviders() {
for _, c := range []*cobra.Command{AddCabinetCmd, ListCabinetCmd} {
err := root.RegisterProviderCommand(p, c)
if err != nil {
log.Error().Msgf("Unable to get command '%s %s' from provider %s ", c.Parent().Name(), c.Name(), p.Slug())
os.Exit(1)
}
}
}
}
6 changes: 3 additions & 3 deletions cmd/cabinet/list_cabinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -35,10 +35,10 @@ import (

// ListCabinetCmd represents the cabinet list command
var ListCabinetCmd = &cobra.Command{
Use: "cabinet",
Use: "cabinet PROVIDER",
Short: "List cabinets in the inventory.",
Long: `List cabinets in the inventory.`,
Args: cobra.ArbitraryArgs,
Args: cobra.ExactArgs(1),
RunE: listCabinet,
}

Expand Down
30 changes: 1 addition & 29 deletions cmd/cabinet/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -72,33 +72,5 @@ func validHardware(cmd *cobra.Command, args []string) (err error) {
}
}

err = validFlagCombos(cmd, args)
if err != nil {
return err
}

return nil
}

// validFlagCombos has additional flag logic to account for overiding required flags with the --auto flag
func validFlagCombos(cmd *cobra.Command, args []string) error {
cabinetSet := cmd.Flags().Changed("cabinet")
vlanIdSet := cmd.Flags().Changed("vlan-id")
autoSet := cmd.Flags().Changed("auto")
// if auto is set, the values are recommended and the required flags are bypassed
if autoSet {
return nil
} else {
if !cabinetSet && !vlanIdSet {
return errors.New("required flag(s) \"cabinet\", \"vlan-id\" not set")
}
if cabinetSet && !vlanIdSet {
return errors.New("required flag(s) \"vlan-id\" not set")
}
if !cabinetSet && vlanIdSet {
return errors.New("required flag(s) \"cabinet\" not set")
}
}

return nil
}
6 changes: 4 additions & 2 deletions cmd/cdu/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand All @@ -27,14 +27,16 @@ package cdu

import (
root "github.com/Cray-HPE/cani/cmd"
"github.com/rs/zerolog/log"
)

var (
hwType string
supportedHw []string
)

func init() {
func Init() {
log.Trace().Msgf("%+v", "github.com/Cray-HPE/cani/cmd/cdu.init")
// Add variants to root commands
root.AddCmd.AddCommand(AddCduCmd)
root.ListCmd.AddCommand(ListCduCmd)
Expand Down
6 changes: 4 additions & 2 deletions cmd/chassis/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand All @@ -27,6 +27,7 @@ package chassis

import (
root "github.com/Cray-HPE/cani/cmd"
"github.com/rs/zerolog/log"
)

var (
Expand All @@ -36,7 +37,8 @@ var (
sortBy string
)

func init() {
func Init() {
log.Trace().Msgf("%+v", "github.com/Cray-HPE/cani/cmd/chassis.init")
// Add variants to root commands
root.AddCmd.AddCommand(AddChassisCmd)
root.ListCmd.AddCommand(ListChassisCmd)
Expand Down
Loading
Loading