Skip to content

Commit

Permalink
Merge pull request #45 from jafurlan/main
Browse files Browse the repository at this point in the history
Grendel v0.0.10
  • Loading branch information
aebruno authored Jan 16, 2024
2 parents b62a3d2 + 98a8d5e commit 651b661
Show file tree
Hide file tree
Showing 42 changed files with 2,436 additions and 1,774 deletions.
40 changes: 0 additions & 40 deletions bmc/bmc.go

This file was deleted.

64 changes: 64 additions & 0 deletions bmc/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package bmc

import (
"github.com/spf13/viper"
"github.com/stmcginnis/gofish"
)

type Redfish struct {
config gofish.ClientConfig
client *gofish.APIClient
service *gofish.Service
}

type System struct {
Name string `json:"name"`
BIOSVersion string `json:"bios_version"`
SerialNumber string `json:"serial_number"`
Manufacturer string `json:"manufacturer"`
PowerStatus string `json:"power_status"`
Health string `json:"health"`
TotalMemory float32 `json:"total_memory"`
ProcessorCount int `json:"processor_count"`
BootNext string `json:"boot_next"`
BootOrder []string `json:"boot_order"`
}

func NewRedfishClient(ip string) (*Redfish, error) {
user := viper.GetString("bmc.user")
pass := viper.GetString("bmc.password")
viper.SetDefault("bmc.insecure", true)
insecure := viper.GetBool("bmc.insecure")

endpoint := "https://" + ip

config := gofish.ClientConfig{
Endpoint: endpoint,
Username: user,
Password: pass,
Insecure: insecure,
}

client, err := gofish.Connect(config)
if err != nil {
e := ParseRedfishError(err)
// Try with default credentials
if e.Code == "401" {
config.Username = "root"
config.Password = "calvin"
client, err = gofish.Connect(config)
if err != nil {
log.Debug("default credentials failed")
return nil, err
}
} else {
return nil, err
}
}

return &Redfish{
config: config,
client: client,
service: client.GetService(),
}, nil
}
42 changes: 42 additions & 0 deletions bmc/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package bmc

import (
"encoding/json"
"regexp"
"strings"
)

type RedfishError struct {
Code string
Error struct {
MessageExtendedInfo []struct {
Message string
MessageArgs []string //?
MessageArgsCount int `json:"MessageArgs.@odata.count"`
MessageId string
RelatedProperties []string //?
RelatedPropertiesCount int `json:"RelatedProperties.@odata.count"`
Resolution string
Severity string
} `json:"@Message.ExtendedInfo"`
Code string `json:"code"`
Message string `json:"message"`
} `json:"error"`
}

func ParseRedfishError(err error) RedfishError {
var e RedfishError
if err == nil {
return e
}

reg := regexp.MustCompile(`^[0-9]{3}`)
e.Code = reg.FindString(err.Error())
if e.Code != "" {
test, _ := strings.CutPrefix(err.Error(), e.Code+":")

json.Unmarshal([]byte(test), &e)
}

return e
}
131 changes: 0 additions & 131 deletions bmc/ipmi.go

This file was deleted.

42 changes: 0 additions & 42 deletions bmc/ipmi_test.go

This file was deleted.

Loading

0 comments on commit 651b661

Please sign in to comment.