simple-route-manager/controller.go

52 lines
1.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
func GetUpdate(controllerURL string, timeoutSec int) (int64, error) {
client := &http.Client{Timeout: time.Duration(timeoutSec) * time.Second}
resp, err := client.Get(controllerURL + "/update")
if err != nil {
return 0, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return 0, fmt.Errorf("unexpected status: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}
var ts int64
if err := json.Unmarshal(body, &ts); err != nil {
return 0, err
}
return ts, nil
}
func GetSubnets(controllerURL string, timeoutSec int) ([]string, error) {
client := &http.Client{Timeout: time.Duration(timeoutSec) * time.Second}
resp, err := client.Get(controllerURL + "/subnets")
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var subnets []string
if err := json.Unmarshal(body, &subnets); err != nil {
return nil, err
}
return subnets, nil
}