1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

added responses

This commit is contained in:
Matthias Hannig
2017-05-18 12:52:10 +02:00
parent 2a6cc841de
commit 4cafd2630d
3 changed files with 162 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
package main
package api
// Alice LG Rest API
//

94
backend/api/response.go Normal file
View File

@@ -0,0 +1,94 @@
package api
import (
"time"
)
// Details, usually the original backend response
type Details map[string]interface{}
// Status
type ApiStatus struct {
Version string `json:"version"`
CacheStatus CacheStatus `json:"cache_status"`
ResultFromCache bool `json:"result_from_cache"`
}
type CacheStatus struct {
CachedAt time.Time `json:"cached_at"`
OrigTtl int `json:"orig_ttl"`
}
type Status struct {
ServerTime time.Time `json:"server_time"`
LastReboot time.Time `json:"last_reboot"`
LastReconfig time.Time `json:"last_reconfig"`
Message string `json:"message"`
RouterId string `json:"router_id"`
Version string `json:"version"`
Backend string `json:"backend"`
}
type StatusResponse struct {
Api ApiStatus `json:"api"`
Ttl time.Time `json:"ttl"`
Status Status `json:"status"`
}
// Routeservers
type Routeserver struct {
Id int `json:"id"`
Name string `json:"name"`
}
type RouteserversResponse struct {
Routeservers []Routeserver `json:"routeservers"`
}
// Neighbours
type Neighbour struct {
Id string `json:"id"`
// Mandatory fields
Address string `json:"address"`
Asn int `json:"asn"`
State string `json:"state"`
Description string `json:"description"`
RoutesReceived int `json:"routes_received"`
RoutesFiltered int `json:"routes_filtered"`
RoutesExported int `json:"routes_exported"`
RoutesPreferred int `json:"routes_preferred"`
Uptime int `json:"uptime"`
// Original response
Details map[string]interface{} `json:"details"`
}
type NeighboursResponse struct {
Api ApiStatus `json:"api"`
Ttl time.Time `json:"ttl"`
Neighbours []Neighbour `json:"neighbours"`
}
// BGP
type Community []int
type BgpInfo struct {
AsPath []int `json:"as_path"`
NextHop string `json:"next_hop"`
Communities []Community `json:"communities"`
LocalPref string `json:"local_pref"`
Med string `json:"med"`
}
// Prefixes
type Prefix struct {
Network string `json:"network"`
Interface string `json:"interface"`
Metric int `json:"metric"`
Bgp BgpInfo `json:"bgp"`
Age time.Time `json:"age"`
Flags []string `json:"flags"` // [BGP, unicast, univ]
Details Details `json:"details"`
}

View File

@@ -0,0 +1,67 @@
package api
import (
"encoding/json"
"testing"
"time"
)
func TestStatusResponseSerialization(t *testing.T) {
// Make status
response := StatusResponse{
Api: ApiStatus{
Version: "2.0.0",
CacheStatus: CacheStatus{},
ResultFromCache: false,
},
Ttl: time.Now(),
Status: Status{
Message: "Server is up and running",
RouterId: "testrouter",
Version: "1.6.3",
Backend: "birdwatcher",
},
}
result, err := json.Marshal(response)
if err != nil {
t.Error(err)
}
_ = result
}
func TestNeighbourSerialization(t *testing.T) {
// Original backend response
payload := `{
"action": "restart",
"bgp_state": "Established",
"bird_protocol": "BGP",
"connection": "Established"
}`
details := make(map[string]interface{})
err := json.Unmarshal([]byte(payload), &details)
if err != nil {
t.Error(err)
}
// Make neighbour
neighbour := Neighbour{
Id: "PROTOCOL_23_42_",
State: "Established",
Description: "Some peer",
Address: "10.10.10.1",
Details: details,
}
result, err := json.Marshal(neighbour)
if err != nil {
t.Error(err)
}
_ = result
}