mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
refactor: split BMC data structures into pkg/bmc package
This commit is contained in:
parent
88bd791718
commit
ad0708d2ad
4 changed files with 78 additions and 51 deletions
45
pkg/bmc/bmc.go
Normal file
45
pkg/bmc/bmc.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package bmc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/OpenCHAMI/magellan/pkg/secrets"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type BMCCredentials struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func GetBMCCredentials(store secrets.SecretStore, id string) (BMCCredentials, error) {
|
||||
var creds BMCCredentials
|
||||
if uriCreds, err := store.GetSecretByID(id); err != nil {
|
||||
// Specific credentials for URI not found, fetch default.
|
||||
log.Warn().Str("id", id).Msg("specific credentials not found, falling back to default")
|
||||
defaultSecret, err := store.GetSecretByID(secrets.DEFAULT_KEY)
|
||||
if err != nil {
|
||||
// We've exhausted all options, the credentials will be blank unless
|
||||
// overridden by a CLI flag.
|
||||
log.Warn().Str("id", id).Err(err).Msg("no default credentials were set, they will be blank unless overridden by CLI flags")
|
||||
} else {
|
||||
// Default credentials found, use them.
|
||||
if err = json.Unmarshal([]byte(defaultSecret), &creds); err != nil {
|
||||
log.Warn().Str("id", id).Err(err).Msg("failed to unmarshal default secrets store credentials")
|
||||
return creds, err
|
||||
} else {
|
||||
log.Info().Str("id", id).Msg("default credentials found, using")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Specific URI credentials found, use them.
|
||||
if err = json.Unmarshal([]byte(uriCreds), &creds); err != nil {
|
||||
log.Warn().Str("id", id).Err(err).Msg("failed to unmarshal specific credentials")
|
||||
return creds, err
|
||||
} else {
|
||||
log.Info().Str("id", id).Msg("specific credentials found, using")
|
||||
}
|
||||
}
|
||||
|
||||
return creds, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue