Browse Source

update README, add GetDNSRecordId function

volixer 5 months ago
parent
commit
aa5cbb0678
4 changed files with 76 additions and 0 deletions
  1. 25 0
      README.md
  2. 45 0
      handlers/dns_handler.go
  3. 5 0
      routes/dns.go
  4. 1 0
      routes/routes.go

+ 25 - 0
README.md

@@ -51,6 +51,31 @@ curl -X POST http://localhost:8080/sshkey \
   -d '{"keyName": "mykey", "key": "ssh-ed25519 AAAA..."}'
   -d '{"keyName": "mykey", "key": "ssh-ed25519 AAAA..."}'
 ```
 ```
 
 
+### DNS Records
+| Method | Endpoint | Description |
+|--------|----------|-------------|
+| GET    | /dns | List all A records |
+| GET    | /dns/{subdomain} | Get record ID for subdomain |
+| POST   | /dns | Create new A record with random subdomain |
+| DELETE | /dns/{recordId} | Delete DNS record |
+
+### Request Examples
+```bash
+# List DNS A records
+curl http://localhost:8080/dns
+
+# Create DNS A record
+curl -X POST http://localhost:8080/dns \
+  -H "Content-Type: application/json" \
+  -d '{"fieldType": "A", "subDomain": "test", "target": "1.2.3.4", "ttl": 3600}'
+  
+# Get DNS record ID for subdomain
+curl http://localhost:8080/dns/mysubdomain
+
+# Delete DNS record
+curl -X DELETE http://localhost:8080/dns/1234567
+```
+
 ## Project Structure
 ## Project Structure
 ```
 ```
 byom-infra-api/
 byom-infra-api/

+ 45 - 0
handlers/dns_handler.go

@@ -191,3 +191,48 @@ func (h *DNSHandler) ListDNSRecords(w http.ResponseWriter, r *http.Request) {
 		return
 		return
 	}
 	}
 }
 }
+
+// GetDNSRecordID retrieves the record ID for a specific subdomain
+func (h *DNSHandler) GetDNSRecordID(w http.ResponseWriter, r *http.Request) {
+	vars := mux.Vars(r)
+	subdomain := vars["subdomain"]
+	log.Printf("Handling GET request for DNS record ID with subdomain: %s from %s", subdomain, r.RemoteAddr)
+
+	// Get all record IDs
+	var recordIDs []int
+	err := config.OVHClient.Get("/domain/zone/"+h.Zone+"/record", &recordIDs)
+	if err != nil {
+		log.Printf("Error getting DNS record IDs: %v", err)
+		http.Error(w, "Failed to get DNS records: "+err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	// Search through records to find matching subdomain
+	for _, id := range recordIDs {
+		var record DNSRecord
+		err := config.OVHClient.Get(fmt.Sprintf("/domain/zone/%s/record/%d", h.Zone, id), &record)
+		if err != nil {
+			log.Printf("Error getting DNS record %d: %v", id, err)
+			continue
+		}
+
+		if record.FieldType == "A" && record.SubDomain == subdomain {
+			response := struct {
+				ID int `json:"id"`
+			}{
+				ID: id,
+			}
+
+			w.Header().Set("Content-Type", "application/json")
+			if err := json.NewEncoder(w).Encode(response); err != nil {
+				log.Printf("Error encoding response: %v", err)
+				http.Error(w, "Failed to encode response", http.StatusInternalServerError)
+				return
+			}
+			return
+		}
+	}
+
+	// If we get here, no matching record was found
+	http.Error(w, "Record not found", http.StatusNotFound)
+}

+ 5 - 0
routes/dns.go

@@ -21,3 +21,8 @@ func DeleteDNSRecord(w http.ResponseWriter, r *http.Request) {
 func ListDNSRecords(w http.ResponseWriter, r *http.Request) {
 func ListDNSRecords(w http.ResponseWriter, r *http.Request) {
 	dnsHandler.ListDNSRecords(w, r)
 	dnsHandler.ListDNSRecords(w, r)
 }
 }
+
+// GetDNSRecordID forwards the request to the DNSHandler
+func GetDNSRecordID(w http.ResponseWriter, r *http.Request) {
+	dnsHandler.GetDNSRecordID(w, r)
+}

+ 1 - 0
routes/routes.go

@@ -19,6 +19,7 @@ func SetupRoutes(r *mux.Router) {
 
 
 	// DNS routes
 	// DNS routes
 	r.HandleFunc("/dns", ListDNSRecords).Methods("GET")
 	r.HandleFunc("/dns", ListDNSRecords).Methods("GET")
+	r.HandleFunc("/dns/{subdomain}", GetDNSRecordID).Methods("GET")
 	r.HandleFunc("/dns", CreateDNSRecord).Methods("POST")
 	r.HandleFunc("/dns", CreateDNSRecord).Methods("POST")
 	r.HandleFunc("/dns/{id}", DeleteDNSRecord).Methods("DELETE")
 	r.HandleFunc("/dns/{id}", DeleteDNSRecord).Methods("DELETE")