2023-11-01 14:17:58 +01:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/jmespath/go-jmespath"
|
2023-11-21 17:42:49 +01:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/conf"
|
2023-11-01 14:17:58 +01:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
|
|
|
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Querier queries a data store for the given data
|
|
|
|
// and returns data in the corresponding encoding
|
|
|
|
type Querier interface {
|
|
|
|
Query(meshId string, queryParams string) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type JmesQuerier struct {
|
2023-11-05 19:03:58 +01:00
|
|
|
manager mesh.MeshManager
|
2023-11-01 14:17:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type QueryError struct {
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
type QueryRoute struct {
|
|
|
|
Destination string `json:"destination"`
|
|
|
|
HopCount int `json:"hopCount"`
|
|
|
|
}
|
|
|
|
|
2023-11-01 14:17:58 +01:00
|
|
|
type QueryNode struct {
|
2023-11-17 23:13:51 +01:00
|
|
|
HostEndpoint string `json:"hostEndpoint"`
|
|
|
|
PublicKey string `json:"publicKey"`
|
|
|
|
WgEndpoint string `json:"wgEndpoint"`
|
|
|
|
WgHost string `json:"wgHost"`
|
2023-11-21 17:42:49 +01:00
|
|
|
Timestamp int64 `json:"timestamp"`
|
2023-11-17 23:13:51 +01:00
|
|
|
Description string `json:"description"`
|
2023-11-25 04:15:58 +01:00
|
|
|
Routes []QueryRoute `json:"routes"`
|
2023-11-17 23:13:51 +01:00
|
|
|
Alias string `json:"alias"`
|
|
|
|
Services map[string]string `json:"services"`
|
2023-11-21 17:42:49 +01:00
|
|
|
Type conf.NodeType `json:"type"`
|
2023-11-01 14:17:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *QueryError) Error() string {
|
|
|
|
return m.msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query: queries the data
|
|
|
|
func (j *JmesQuerier) Query(meshId, queryParams string) ([]byte, error) {
|
2023-11-05 19:03:58 +01:00
|
|
|
mesh, ok := j.manager.GetMeshes()[meshId]
|
2023-11-01 14:17:58 +01:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, &QueryError{msg: fmt.Sprintf("%s does not exist", meshId)}
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot, err := mesh.GetMesh()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
nodes := lib.Map(lib.MapValues(snapshot.GetNodes()), MeshNodeToQueryNode)
|
2023-11-01 14:17:58 +01:00
|
|
|
|
|
|
|
result, err := jmespath.Search(queryParams, nodes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := json.Marshal(result)
|
|
|
|
return bytes, err
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
func MeshNodeToQueryNode(node mesh.MeshNode) *QueryNode {
|
2023-11-01 14:17:58 +01:00
|
|
|
queryNode := new(QueryNode)
|
|
|
|
queryNode.HostEndpoint = node.GetHostEndpoint()
|
|
|
|
pubKey, _ := node.GetPublicKey()
|
|
|
|
|
|
|
|
queryNode.PublicKey = pubKey.String()
|
|
|
|
|
|
|
|
queryNode.WgEndpoint = node.GetWgEndpoint()
|
|
|
|
queryNode.WgHost = node.GetWgHost().String()
|
|
|
|
|
|
|
|
queryNode.Timestamp = node.GetTimeStamp()
|
2023-11-25 04:15:58 +01:00
|
|
|
queryNode.Routes = lib.Map(node.GetRoutes(), func(r mesh.Route) QueryRoute {
|
|
|
|
return QueryRoute{
|
|
|
|
Destination: r.GetDestination().String(),
|
|
|
|
HopCount: r.GetHopCount(),
|
|
|
|
}
|
|
|
|
})
|
2023-11-01 14:17:58 +01:00
|
|
|
queryNode.Description = node.GetDescription()
|
2023-11-17 20:05:21 +01:00
|
|
|
queryNode.Alias = node.GetAlias()
|
2023-11-17 23:13:51 +01:00
|
|
|
queryNode.Services = node.GetServices()
|
2023-11-21 17:42:49 +01:00
|
|
|
queryNode.Type = node.GetType()
|
2023-11-17 20:05:21 +01:00
|
|
|
|
2023-11-01 14:17:58 +01:00
|
|
|
return queryNode
|
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
func NewJmesQuerier(manager mesh.MeshManager) Querier {
|
2023-11-01 14:17:58 +01:00
|
|
|
return &JmesQuerier{manager: manager}
|
|
|
|
}
|