|
@@ -0,0 +1,80 @@
|
|
|
+package handlers
|
|
|
+
|
|
|
+import (
|
|
|
+ "byom-infra-api/config"
|
|
|
+ "encoding/json"
|
|
|
+ "log"
|
|
|
+ "net/http"
|
|
|
+
|
|
|
+ "github.com/gorilla/mux"
|
|
|
+)
|
|
|
+
|
|
|
+type SSHKey struct {
|
|
|
+ KeyName string `json:"keyName"`
|
|
|
+ Key string `json:"key"`
|
|
|
+}
|
|
|
+
|
|
|
+type SSHKeyHandler struct{}
|
|
|
+
|
|
|
+func NewSSHKeyHandler() *SSHKeyHandler {
|
|
|
+ return &SSHKeyHandler{}
|
|
|
+}
|
|
|
+
|
|
|
+// GetSSHKeys handles the retrieval of all SSH keys
|
|
|
+func (h *SSHKeyHandler) GetSSHKeys(w http.ResponseWriter, r *http.Request) {
|
|
|
+ log.Printf("Handling GET request for all SSH keys from %s", r.RemoteAddr)
|
|
|
+
|
|
|
+ // First, get the list of SSH key IDs
|
|
|
+ var keyIDs []string
|
|
|
+ err := config.OVHClient.Get("/me/sshKey", &keyIDs)
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("Error fetching SSH key IDs: %v", err)
|
|
|
+ http.Error(w, "Failed to fetch SSH keys: "+err.Error(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Then fetch details for each key
|
|
|
+ var sshKeys []SSHKey
|
|
|
+ for _, id := range keyIDs {
|
|
|
+ var key SSHKey
|
|
|
+ err := config.OVHClient.Get("/me/sshKey/"+id, &key)
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("Error fetching details for SSH key %s: %v", id, err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ sshKeys = append(sshKeys, key)
|
|
|
+ }
|
|
|
+
|
|
|
+ log.Printf("Successfully retrieved %d SSH keys", len(sshKeys))
|
|
|
+
|
|
|
+ w.Header().Set("Content-Type", "application/json")
|
|
|
+ if err := json.NewEncoder(w).Encode(sshKeys); err != nil {
|
|
|
+ log.Printf("Error encoding SSH keys response: %v", err)
|
|
|
+ http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// GetSSHKeyByID handles the retrieval of a specific SSH key by ID
|
|
|
+func (h *SSHKeyHandler) GetSSHKeyByID(w http.ResponseWriter, r *http.Request) {
|
|
|
+ vars := mux.Vars(r)
|
|
|
+ keyID := vars["id"]
|
|
|
+ log.Printf("Handling GET request for SSH key ID: %s from %s", keyID, r.RemoteAddr)
|
|
|
+
|
|
|
+ var sshKey SSHKey
|
|
|
+ err := config.OVHClient.Get("/me/sshKey/"+keyID, &sshKey)
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("Error fetching SSH key %s: %v", keyID, err)
|
|
|
+ http.Error(w, "Failed to fetch SSH key: "+err.Error(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ log.Printf("Successfully retrieved SSH key: %s", keyID)
|
|
|
+
|
|
|
+ w.Header().Set("Content-Type", "application/json")
|
|
|
+ if err := json.NewEncoder(w).Encode(sshKey); err != nil {
|
|
|
+ log.Printf("Error encoding SSH key response: %v", err)
|
|
|
+ http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|