Initial commit

This commit is contained in:
David J. Allen 2023-08-28 08:42:26 -06:00
commit b6fe201f66
9 changed files with 789 additions and 0 deletions

74
api/dora/dora.go Normal file
View file

@ -0,0 +1,74 @@
package dora
import (
"davidallendj/magellan/api"
"encoding/json"
"fmt"
"github.com/jmoiron/sqlx"
)
const (
Host = "http://localhost"
DbType = "sqlite3"
DbPath = "../data/assets.db"
BaseEndpoint = "/v1"
Port = 8000
)
type ScannedResult struct {
id string
site any
cidr string
ip string
port int
protocol string
scanner string
state string
updated string
}
func makeEndpointUrl(endpoint string) string {
return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint
}
// Scan for BMC assets uing dora scanner
func ScanForAssets() error {
return nil
}
// Query dora API to get scanned ports
func QueryScannedPorts() error {
// Perform scan and collect from dora server
url := makeEndpointUrl("/scanned_ports")
_, body, err := api.MakeRequest(url, "GET", nil)
if err != nil {
return fmt.Errorf("could not discover assets: %v", err)
}
// get data from JSON
var res map[string]any
if err := json.Unmarshal(body, &res); err != nil {
return fmt.Errorf("could not unmarshal response body: %v", err)
}
data := res["data"]
fmt.Println(data)
return nil
}
// Loads scanned ports directly from DB
func LoadScannedPortsFromDB(dbPath string, dbType string) {
db, _ := sqlx.Open(dbType, dbPath)
sql := `SELECT * FROM scanned_port WHERE state='open'`
rows, _ := db.Query(sql)
for rows.Next() {
var r ScannedResult
rows.Scan(
&r.id, &r.site, &r.cidr, &r.ip, &r.port, &r.protocol, &r.scanner,
&r.state, &r.updated,
)
}
}

57
api/smd/smd.go Normal file
View file

@ -0,0 +1,57 @@
package smd
// See ref for API docs:
// https://github.com/Cray-HPE/hms-smd/blob/master/docs/examples.adoc
// https://github.com/alexlovelltroy/hms-smd
import (
"davidallendj/magellan/api"
"fmt"
)
const (
Host = "http://localhost"
BaseEndpoint = "/hsm/v2"
Port = 27779
)
func makeEndpointUrl(endpoint string) string {
return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint
}
func GetRedfishEndpoints() error {
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
_, body, err := api.MakeRequest(url, "GET", nil)
if err != nil {
return fmt.Errorf("could not get endpoint: %v", err)
}
// fmt.Println(res)
fmt.Println(string(body))
return nil
}
func GetComponentEndpoint(xname string) error {
url := makeEndpointUrl("/Inventory/ComponentsEndpoints/" + xname)
res, body, err := api.MakeRequest(url, "GET", nil)
if err != nil {
return fmt.Errorf("could not get endpoint: %v", err)
}
fmt.Println(res)
fmt.Println(string(body))
return nil
}
func AddRedfishEndpoint(inventory []byte) error {
if inventory == nil {
return fmt.Errorf("could not add redfish endpoint: no data found")
}
// Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
res, body, _ := api.MakeRequest(url, "POST", inventory)
fmt.Println("res: ", res)
fmt.Println("body: ", string(body))
return nil
}
func UpdateRedfishEndpoint() {
// Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint
}

25
api/util.go Normal file
View file

@ -0,0 +1,25 @@
package api
import (
"bytes"
"fmt"
"io"
"net/http"
)
func MakeRequest(url string, httpMethod string, body []byte) (*http.Response, []byte, error) {
// url := getSmdEndpointUrl(endpoint)
req, _ := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
req.Header.Add("User-Agent", "magellan")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("could not make request: %v", err)
}
b, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, nil, fmt.Errorf("could not read response body: %v", err)
}
return res, b, err
}