✨ Database allocator
This commit is contained in:
20
pkg/nex/cruda/allocator.go
Normal file
20
pkg/nex/cruda/allocator.go
Normal file
@ -0,0 +1,20 @@
|
||||
package cruda
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
func (v *CudaConn) AllocDatabase(name string) (string, error) {
|
||||
conn := v.Conn.GetNexusGrpcConn()
|
||||
ctx := context.Background()
|
||||
ctx = metadata.AppendToOutgoingContext(ctx, "client_id", v.Conn.Info.Id)
|
||||
out, err := proto.NewDatabaseControllerClient(conn).AllocDatabase(ctx, &proto.AllocDatabaseRequest{
|
||||
Name: name,
|
||||
})
|
||||
if err != nil || !out.GetIsSuccess() {
|
||||
return "", err
|
||||
}
|
||||
return out.GetDsn(), nil
|
||||
}
|
33
pkg/nex/cruda/allocator_test.go
Normal file
33
pkg/nex/cruda/allocator_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package cruda_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAllocDatabase(t *testing.T) {
|
||||
conn, err := nex.NewNexusConn("127.0.0.1:7001", &proto.ServiceInfo{
|
||||
Id: "alloc01",
|
||||
Type: "alloc",
|
||||
Label: "Allocator",
|
||||
GrpcAddr: "127.0.0.1:6001",
|
||||
HttpAddr: nil,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(fmt.Errorf("unable to connect nexus: %v", err))
|
||||
}
|
||||
|
||||
if err := conn.RegisterService(); err != nil {
|
||||
t.Fatal(fmt.Errorf("unable to register service: %v", err))
|
||||
}
|
||||
|
||||
cc := cruda.NewCudaConn(conn)
|
||||
dsn, err := cc.AllocDatabase("test")
|
||||
if err != nil {
|
||||
t.Fatal(fmt.Errorf("unable to allocate database: %v", err))
|
||||
}
|
||||
t.Log(fmt.Sprintf("Allocated database: %s", dsn))
|
||||
}
|
13
pkg/nex/cruda/conn.go
Normal file
13
pkg/nex/cruda/conn.go
Normal file
@ -0,0 +1,13 @@
|
||||
package cruda
|
||||
|
||||
import "git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||
|
||||
type CudaConn struct {
|
||||
Conn *nex.Conn
|
||||
}
|
||||
|
||||
func NewCudaConn(conn *nex.Conn) *CudaConn {
|
||||
return &CudaConn{
|
||||
Conn: conn,
|
||||
}
|
||||
}
|
15
pkg/nex/net.go
Normal file
15
pkg/nex/net.go
Normal file
@ -0,0 +1,15 @@
|
||||
package nex
|
||||
|
||||
import "net"
|
||||
|
||||
func GetOutboundIP() (net.IP, error) {
|
||||
conn, err := net.Dial("udp", "1.1.1.1:80")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer conn.Close()
|
||||
}
|
||||
|
||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||
return localAddr.IP, nil
|
||||
}
|
Reference in New Issue
Block a user