maatma: refactor IPs getter to include some other interesting information

This commit is contained in:
nemunaire 2019-03-25 23:11:30 +01:00
parent 0af3355576
commit bd729c9f54

View File

@ -8,7 +8,9 @@ import (
func init() { func init() {
router.GET("/api/ips", apiHandler(showIPs)) router.GET("/api/ips", apiHandler(showIPs))
router.GET("/api/students/:sid/ips", apiHandler(studentHandler(showStudentIPs))) router.GET("/api/students/:sid/ips", apiHandler(studentHandler(func(student Student, body []byte) (interface{}, error) {
return getStudentIPs(student), nil
})))
} }
func (s Student) IPSuffix() int64 { func (s Student) IPSuffix() int64 {
@ -26,25 +28,26 @@ func showIPs(_ httprouter.Params, body []byte) (interface{}, error) {
for _, std := range students { for _, std := range students {
sid := fmt.Sprintf("%d", std.Id) sid := fmt.Sprintf("%d", std.Id)
r[sid] = make(map[string]string) r[sid] = getStudentIPs(std)
if std.MAC == nil { if std.MAC == nil {
continue continue
} }
r[sid]["mac"] = *std.MAC r[sid]["mac"] = *std.MAC
r[sid]["vlan0"] = fmt.Sprintf("172.23.0.%d", std.IPSuffix())
r[sid]["vlan7"] = fmt.Sprintf("172.23.142.%d", std.IPSuffix())
} }
return r, nil return r, nil
} }
func showStudentIPs(student Student, body []byte) (interface{}, error) { func getStudentIPs(student Student) (r map[string]string) {
r := make(map[string]string) r = make(map[string]string)
r["vlan0"] = fmt.Sprintf("172.23.0.%d", student.IPSuffix()) r["vlan0"] = fmt.Sprintf("172.23.0.%d", student.IPSuffix())
r["vlan7"] = fmt.Sprintf("172.23.142.%d", student.IPSuffix()) r["vlan7"] = fmt.Sprintf("172.23.142.%d", student.IPSuffix())
r["wg"] = studentIP(student.Id)
r["adn"] = student.myAssociatedDomain()
r["ddn"] = student.MyDelegatedDomain()
return r, nil return
} }