Skip to content

Instantly share code, notes, and snippets.

@Apurer
Last active May 7, 2024 10:50
Show Gist options
  • Save Apurer/dcea317ce45b63ba1e582ae38ace60bd to your computer and use it in GitHub Desktop.
Save Apurer/dcea317ce45b63ba1e582ae38ace60bd to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"log"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/redfish/v1/Systems/", handleRedfishRequest)
log.Println("Server starting on :8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handleRedfishRequest(w http.ResponseWriter, r *http.Request) {
// Extract FQDN from the path /redfish/v1/Systems/{fqdn}
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 5 {
http.Error(w, "Invalid request path", http.StatusBadRequest)
return
}
fqdn := parts[4]
// Check if the remaining path is valid
expectedPath := "Actions/ComputerSystem.Reset"
if strings.Join(parts[5:], "/") != expectedPath {
http.Error(w, "Invalid action path", http.StatusBadRequest)
return
}
// Read and decode the request body
var data map[string]string
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
return
}
resetType, ok := data["ResetType"]
if !ok || (resetType != "On" && resetType != "GracefulShutdown") {
http.Error(w, "Invalid or missing ResetType", http.StatusBadRequest)
return
}
// Construct the iDRAC URL
idracURL := "https://" + fqdn + "/redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem.Reset"
// Forward the request to iDRAC
// Create the request to iDRAC
payload := `{"ResetType": "` + resetType + `"}`
req, err := http.NewRequest("POST", idracURL, strings.NewReader(payload))
if err != nil {
http.Error(w, "Failed to create request: "+err.Error(), http.StatusInternalServerError)
return
}
// Set appropriate headers including Basic Auth
username := "yourUsername" // Replace with actual username
password := "yourPassword" // Replace with actual password
auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
req.Header.Set("Authorization", "Basic "+auth)
req.Header.Set("Content-Type", "application/json")
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, "Failed to send request: "+err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// Copy the response from iDRAC back to the original client
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"log"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/redfish/v1/Systems/", handleRedfishRequest)
log.Println("Server starting on :8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handleRedfishRequest(w http.ResponseWriter, r *http.Request) {
// Extract FQDN from the path /redfish/v1/Systems/{fqdn}
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 5 {
http.Error(w, "Invalid request path", http.StatusBadRequest)
return
}
fqdn := parts[4]
// Read and decode the request body
var data map[string]string
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
return
}
resetType, ok := data["ResetType"]
if !ok || (resetType != "On" && resetType != "GracefulShutdown") {
http.Error(w, "Invalid or missing ResetType", http.StatusBadRequest)
return
}
// Construct the iDRAC URL
idracURL := "https://" + fqdn + "/redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem.Reset"
// Forward the request to iDRAC
// Create the request to iDRAC
payload := `{"ResetType": "` + resetType + `"}`
req, err := http.NewRequest("POST", idracURL, strings.NewReader(payload))
if err != nil {
http.Error(w, "Failed to create request: "+err.Error(), http.StatusInternalServerError)
return
}
// Set appropriate headers including Basic Auth
username := "yourUsername" // Replace with actual username
password := "yourPassword" // Replace with actual password
auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
req.Header.Set("Authorization", "Basic "+auth)
req.Header.Set("Content-Type", "application/json")
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, "Failed to send request: "+err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// Copy the response from iDRAC back to the original client
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment