refactor: split BMC data structures into pkg/bmc package

This commit is contained in:
Devon Bautista 2025-04-16 16:29:20 -06:00
parent 88bd791718
commit ad0708d2ad
No known key found for this signature in database
GPG key ID: E1AAD3D4444A3DA0
4 changed files with 78 additions and 51 deletions

45
pkg/bmc/bmc.go Normal file
View 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
}