Skip to content

Commit

Permalink
Merge pull request #181 from Cray-HPE/ultimate-kree
Browse files Browse the repository at this point in the history
check all providers, create a map of init commands
  • Loading branch information
jacobsalmela authored Dec 28, 2023
2 parents a380d0a + 2c4cabb commit b2e84c7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 31 deletions.
48 changes: 21 additions & 27 deletions cmd/session/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

root "github.com/Cray-HPE/cani/cmd"
"github.com/Cray-HPE/cani/cmd/taxonomy"
"github.com/Cray-HPE/cani/internal/provider/csm"
"github.com/Cray-HPE/cani/internal/domain"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
Expand All @@ -42,10 +42,10 @@ var (
ignoreValidationMessage = "Ignore validation failures. Use this to allow unconventional configurations."
forceInit bool

ProviderCmd = &cobra.Command{}
ProviderInitCmds = map[string]*cobra.Command{}
// BootstapCmd is used to start a session with a specific provider and allows the provider to define
// how the real init command is defined using their custom business logic
BootstrapCmd = &cobra.Command{
SessionInitCmd = &cobra.Command{
Use: "init PROVIDER",
Short: taxonomy.InitShort,
Long: taxonomy.InitLong,
Expand All @@ -56,38 +56,32 @@ var (
)

func init() {
// init is run once, and this is where the flags get set
// since flags vary by provider, create a variable for each
var err error
for _, provider := range taxonomy.SupportedProviders {
switch provider {
case taxonomy.CSM:
ProviderCmd, err = csm.NewSessionInitCommand()
default:
log.Debug().Msgf("skipping provider: %s", provider)
}
// Define the bare minimum needed to determine who the provider for the session will be
SessionInitCmd.Flags().BoolVar(&ignoreExternalValidation, "ignore-validation", false, ignoreValidationMessage)
SessionInitCmd.Flags().BoolVarP(&forceInit, "force", "f", false, "Overwrite the existing session with a new session")

for _, p := range domain.GetProviders() {
// Create a domain object to interact with the datastore and the provider
providerCmd, err := domain.NewSessionInitCommand(p.Slug())
if err != nil {
log.Error().Msgf("unable to get cmd from provider: %v", err)
log.Error().Msgf("unable to get provider init command: %v", err)
os.Exit(1)
}
ProviderCmd.Use = "init"
}
providerCmd.Use = "init"

// Define the bare minimum needed to determine who the provider for the session will be
BootstrapCmd.Flags().BoolVar(&ignoreExternalValidation, "ignore-validation", false, ignoreValidationMessage)
BootstrapCmd.Flags().BoolVarP(&forceInit, "force", "f", false, "Overwrite the existing session with a new session")
// all flags should be set in init(). you can set flags after the fact, but it is much easier to work with everything up front
// this will set existing variables for each provider
err = root.MergeProviderFlags(SessionInitCmd, providerCmd)
if err != nil {
log.Error().Msgf("unable to get flags from provider: %v", err)
os.Exit(1)
}

// all flags should be set in init(). you can set flags after the fact, but it is much easier to work with everything up front
// this will set existing variables for each provider
err = root.MergeProviderFlags(BootstrapCmd, ProviderCmd)
if err != nil {
log.Error().Msgf("unable to get flags from provider: %v", err)
os.Exit(1)
ProviderInitCmds[p.Slug()] = providerCmd
}

// Add session commands to root commands
root.SessionCmd.AddCommand(BootstrapCmd)
BootstrapCmd.AddCommand(ProviderCmd)
root.SessionCmd.AddCommand(SessionInitCmd)
root.SessionCmd.AddCommand(SessionApplyCmd)
root.SessionCmd.AddCommand(SessionStatusCmd)
root.SessionCmd.AddCommand(SessionSummaryCmd)
Expand Down
16 changes: 12 additions & 4 deletions cmd/session/session_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ import (
)

func initSessionWithProviderCmd(cmd *cobra.Command, args []string) (err error) {
// Create a domain object to interact with the datastore and the provider
root.D, err = domain.New(cmd, args)
if err != nil {
return err
for _, p := range domain.GetProviders() {
// if the provider matches the arg requested, a session can begin
if p.Slug() == args[0] {
providerInitCmd, exists := ProviderInitCmds[p.Slug()]
if exists {
// Create a domain object to interact with the datastore and the provider
root.D, err = domain.New(providerInitCmd, args)
if err != nil {
return err
}
}
}
}

// Set the datastore
Expand Down
7 changes: 7 additions & 0 deletions internal/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,10 @@ type UpdatedHardwareResult struct {
DatastoreValidationErrors map[uuid.UUID]inventory.ValidateResult // TODO
ProviderValidationErrors map[uuid.UUID]provider.HardwareValidationResult
}

func GetProviders() []provider.InventoryProvider {
supportedProviders := []provider.InventoryProvider{
&csm.CSM{},
}
return supportedProviders
}
11 changes: 11 additions & 0 deletions internal/domain/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ import (
"github.com/spf13/cobra"
)

func NewSessionInitCommand(p string) (providerCmd *cobra.Command, err error) {
switch p {
case "csm":
providerCmd, err = csm.NewSessionInitCommand()
}
if err != nil {
return providerCmd, err
}
return providerCmd, nil
}

// NewProviderCmd returns the appropriate command to the cmd layer
func NewProviderCmd(bootstrapCmd *cobra.Command, availableDomains map[string]*Domain) (providerCmd *cobra.Command, err error) {
providerCmd = &cobra.Command{}
Expand Down
8 changes: 8 additions & 0 deletions internal/provider/csm/csm.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ import (
sls_client "github.com/Cray-HPE/cani/pkg/sls-client"
)

const (
CsmSlug = "csm"
)

type CSM struct {
// Clients
slsClient *sls_client.APIClient
Expand Down Expand Up @@ -163,6 +167,10 @@ func New(cmd *cobra.Command, args []string, hwlib *hardwaretypes.Library, opts i
// return csm, nil
}

func (csm *CSM) Slug() string {
return CsmSlug
}

func (csm *CSM) setupClients() (err error) {
// Setup HTTP client and context using csm options
httpClient, _, err := csm.newClient()
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ type InventoryProvider interface {

// Print
PrintHardware(cmd *cobra.Command, args []string, filtered map[uuid.UUID]inventory.Hardware) error

// Provider's name
Slug() string
}

type HardwareValidationResult struct {
Expand Down

0 comments on commit b2e84c7

Please sign in to comment.