Remove dependency on postgres' jsonb

This commit is contained in:
Juan Font Alonso 2021-05-15 00:05:41 +02:00
parent e7a626d3cc
commit e0d916bb5a
4 changed files with 17 additions and 17 deletions

10
api.go
View File

@ -11,8 +11,8 @@ import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/dialects/postgres"
"github.com/klauspost/compress/zstd"
"gorm.io/datatypes"
"inet.af/netaddr"
"tailscale.com/tailcfg"
"tailscale.com/wgengine/wgcfg"
@ -40,7 +40,7 @@ func (h *Headscale) RegisterWebAPI(c *gin.Context) {
<body>
<h1>headscale</h1>
<p>
Run the command below on the headscale server to add this machine to your network:
Run the command below in the headscale server to add this machine to your network:
</p>
<p>
@ -174,13 +174,13 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
defer db.Close()
var m Machine
if db.First(&m, "machine_key = ?", mKey.HexString()).RecordNotFound() {
log.Printf("Cannot find machine: %s", err)
log.Printf("Cannot fingitd machine: %s", err)
return
}
hostinfo, _ := json.Marshal(req.Hostinfo)
m.Name = req.Hostinfo.Hostname
m.HostInfo = postgres.Jsonb{RawMessage: json.RawMessage(hostinfo)}
m.HostInfo = datatypes.JSON(hostinfo)
m.DiscoKey = wgcfg.Key(req.DiscoKey).HexString()
now := time.Now().UTC()
@ -194,7 +194,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
// before their first real endpoint update.
if !req.ReadOnly {
endpoints, _ := json.Marshal(req.Endpoints)
m.Endpoints = postgres.Jsonb{RawMessage: json.RawMessage(endpoints)}
m.Endpoints = datatypes.JSON(endpoints)
m.LastSeen = &now
}
db.Save(&m)

View File

@ -8,7 +8,7 @@ import (
"strconv"
"time"
"github.com/jinzhu/gorm/dialects/postgres"
"gorm.io/datatypes"
"inet.af/netaddr"
"tailscale.com/tailcfg"
"tailscale.com/wgengine/wgcfg"
@ -33,9 +33,9 @@ type Machine struct {
LastSeen *time.Time
Expiry *time.Time
HostInfo postgres.Jsonb
Endpoints postgres.Jsonb
EnabledRoutes postgres.Jsonb
HostInfo datatypes.JSON
Endpoints datatypes.JSON
EnabledRoutes datatypes.JSON
CreatedAt time.Time
UpdatedAt time.Time
@ -79,7 +79,7 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
allowedIPs = append(allowedIPs, ip) // we append the node own IP, as it is required by the clients
routesStr := []string{}
if len(m.EnabledRoutes.RawMessage) != 0 {
if len(m.EnabledRoutes) != 0 {
allwIps, err := m.EnabledRoutes.MarshalJSON()
if err != nil {
return nil, err
@ -99,7 +99,7 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
}
endpoints := []string{}
if len(m.Endpoints.RawMessage) != 0 {
if len(m.Endpoints) != 0 {
be, err := m.Endpoints.MarshalJSON()
if err != nil {
return nil, err
@ -111,7 +111,7 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
}
hostinfo := tailcfg.Hostinfo{}
if len(m.HostInfo.RawMessage) != 0 {
if len(m.HostInfo) != 0 {
hi, err := m.HostInfo.MarshalJSON()
if err != nil {
return nil, err
@ -198,7 +198,7 @@ func (h *Headscale) GetMachine(namespace string, name string) (*Machine, error)
// GetHostInfo returns a Hostinfo struct for the machine
func (m *Machine) GetHostInfo() (*tailcfg.Hostinfo, error) {
hostinfo := tailcfg.Hostinfo{}
if len(m.HostInfo.RawMessage) != 0 {
if len(m.HostInfo) != 0 {
hi, err := m.HostInfo.MarshalJSON()
if err != nil {
return nil, err

View File

@ -5,7 +5,7 @@ import (
"errors"
"log"
"github.com/jinzhu/gorm/dialects/postgres"
"gorm.io/datatypes"
"inet.af/netaddr"
)
@ -49,7 +49,7 @@ func (h *Headscale) EnableNodeRoute(namespace string, nodeName string, routeStr
}
routes, _ := json.Marshal([]string{routeStr}) // TODO: only one for the time being, so overwriting the rest
m.EnabledRoutes = postgres.Jsonb{RawMessage: json.RawMessage(routes)}
m.EnabledRoutes = datatypes.JSON(routes)
db.Save(&m)
db.Close()

View File

@ -3,8 +3,8 @@ package headscale
import (
"encoding/json"
"github.com/jinzhu/gorm/dialects/postgres"
"gopkg.in/check.v1"
"gorm.io/datatypes"
"inet.af/netaddr"
"tailscale.com/tailcfg"
)
@ -46,7 +46,7 @@ func (s *Suite) TestGetRoutes(c *check.C) {
Registered: true,
RegisterMethod: "authKey",
AuthKeyID: uint(pak.ID),
HostInfo: postgres.Jsonb{RawMessage: json.RawMessage(hostinfo)},
HostInfo: datatypes.JSON(hostinfo),
}
db.Save(&m)