Skip to content

Commit

Permalink
Began converting to rqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiszli committed Jul 20, 2021
1 parent 0775520 commit d22ad13
Show file tree
Hide file tree
Showing 9 changed files with 629 additions and 917 deletions.
109 changes: 44 additions & 65 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package controller

import (
"context"
"encoding/json"
"fmt"
"log"
"time"

"github.com/go-playground/validator/v10"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/functions"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/mongoconn"
Expand Down Expand Up @@ -59,64 +61,41 @@ func GetPeersList(networkName string) ([]models.PeersResponse, error) {
return peers, err
}


func GetExtPeersList(networkName string, macaddress string) ([]models.ExtPeersResponse, error) {

var peers []models.ExtPeersResponse

//Connection mongoDB with mongoconn class
collection := mongoconn.Client.Database("netmaker").Collection("extclients")

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

//Get all nodes in the relevant network which are NOT in pending state
filter := bson.M{"network": networkName, "ingressgatewayid": macaddress}
cur, err := collection.Find(ctx, filter)

if err != nil {
return peers, err
}

// Close the cursor once finished and cancel if it takes too long
defer cancel()

for cur.Next(context.TODO()) {

var peer models.ExtPeersResponse
err := cur.Decode(&peer)
if err != nil {
log.Fatal(err)
}
var peers []models.ExtPeersResponse
records, err := database.FetchRecords(database.EXT_CLIENT_TABLE_NAME)

// add the node to our node array
//maybe better to just return this? But then that's just GetNodes...
peers = append(peers, peer)
}

//Uh oh, fatal error! This needs some better error handling
//TODO: needs appropriate error handling so the server doesnt shut down.
if err := cur.Err(); err != nil {
log.Fatal(err)
}
if err != nil {
return peers, err
}

return peers, err
for _, value := range records {
var peer models.ExtPeersResponse
err = json.Unmarshal([]byte(value), &peer)
if err != nil {
functions.PrintUserLog("netmaker", "failed to unmarshal ext client", 2)
continue
}
peers = append(peers, peer)
}
return peers, err
}


func ValidateNodeCreate(networkName string, node models.Node) error {
v := validator.New()
_ = v.RegisterValidation("macaddress_unique", func(fl validator.FieldLevel) bool {
var isFieldUnique bool = functions.IsFieldUnique(networkName, "macaddress", node.MacAddress)
isFieldUnique, _ := functions.IsMacAddressUnique(node.MacAddress, networkName)
return isFieldUnique
})
_ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
_, err := node.GetNetwork()
return err == nil
})
_ = v.RegisterValidation("in_charset", func(fl validator.FieldLevel) bool {
isgood := functions.NameInNodeCharSet(node.Name)
return isgood
})
_ = v.RegisterValidation("in_charset", func(fl validator.FieldLevel) bool {
isgood := functions.NameInNodeCharSet(node.Name)
return isgood
})
err := v.Struct(node)

if err != nil {
Expand All @@ -133,10 +112,10 @@ func ValidateNodeUpdate(networkName string, node models.NodeUpdate) error {
_, err := node.GetNetwork()
return err == nil
})
_ = v.RegisterValidation("in_charset", func(fl validator.FieldLevel) bool {
isgood := functions.NameInNodeCharSet(node.Name)
return isgood
})
_ = v.RegisterValidation("in_charset", func(fl validator.FieldLevel) bool {
isgood := functions.NameInNodeCharSet(node.Name)
return isgood
})
err := v.Struct(node)
if err != nil {
for _, e := range err.(validator.ValidationErrors) {
Expand Down Expand Up @@ -302,27 +281,27 @@ func DeleteNode(macaddress string, network string) (bool, error) {

func DeleteIntClient(clientid string) (bool, error) {

deleted := false
deleted := false

collection := mongoconn.Client.Database("netmaker").Collection("intclients")
collection := mongoconn.Client.Database("netmaker").Collection("intclients")

filter := bson.M{"clientid": clientid}
filter := bson.M{"clientid": clientid}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

result, err := collection.DeleteOne(ctx, filter)
result, err := collection.DeleteOne(ctx, filter)

deletecount := result.DeletedCount
deletecount := result.DeletedCount

if deletecount > 0 {
deleted = true
}
if deletecount > 0 {
deleted = true
}

defer cancel()
defer cancel()

err = serverctl.ReconfigureServerWireGuard()

return deleted, err
return deleted, err
}

func GetNode(macaddress string, network string) (models.Node, error) {
Expand All @@ -343,18 +322,18 @@ func GetNode(macaddress string, network string) (models.Node, error) {

func GetIntClient(clientid string) (models.IntClient, error) {

var client models.IntClient
var client models.IntClient

collection := mongoconn.Client.Database("netmaker").Collection("intclients")
collection := mongoconn.Client.Database("netmaker").Collection("intclients")

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

filter := bson.M{"clientid": clientid}
err := collection.FindOne(ctx, filter, options.FindOne().SetProjection(bson.M{"_id": 0})).Decode(&clientid)
filter := bson.M{"clientid": clientid}
err := collection.FindOne(ctx, filter, options.FindOne().SetProjection(bson.M{"_id": 0})).Decode(&clientid)

defer cancel()
defer cancel()

return client, err
return client, err
}

func CreateNode(node models.Node, networkName string) (models.Node, error) {
Expand Down
18 changes: 9 additions & 9 deletions controllers/networkHttpController.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ func UpdateNetwork(networkChange models.NetworkUpdate, network models.Network) (
}
}
if haslocalrangeupdate {
err = functions.UpdateNetworkPrivateAddresses(network.NetID)
err = functions.UpdateNetworkLocalAddresses(network.NetID)
if err != nil {
return models.Network{}, err
}
Expand Down Expand Up @@ -734,14 +734,14 @@ func CreateAccessKey(accesskey models.AccessKey, network models.Network) (models
s := servercfg.GetServerConfig()
w := servercfg.GetWGConfig()
servervals := models.ServerConfig{
CoreDNSAddr: s.CoreDNSAddr,
APIConnString: s.APIConnString,
APIHost: s.APIHost,
APIPort: s.APIPort,
GRPCConnString: s.GRPCConnString,
GRPCHost: s.GRPCHost,
GRPCPort: s.GRPCPort,
GRPCSSL: s.GRPCSSL,
CoreDNSAddr: s.CoreDNSAddr,
APIConnString: s.APIConnString,
APIHost: s.APIHost,
APIPort: s.APIPort,
GRPCConnString: s.GRPCConnString,
GRPCHost: s.GRPCHost,
GRPCPort: s.GRPCPort,
GRPCSSL: s.GRPCSSL,
}
wgvals := models.WG{
GRPCWireGuard: w.GRPCWireGuard,
Expand Down
100 changes: 100 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package database

import (
"log"

"github.com/rqlite/gorqlite"
)

const NETWORKS_TABLE_NAME = "networks"
const NODES_TABLE_NAME = "nodes"
const USERS_TABLE_NAME = "users"
const DNS_TABLE_NAME = "dns"
const EXT_CLIENT_TABLE_NAME = "extclients"
const INT_CLIENTS_TABLE_NAME = "intclients"
const DATABASE_FILENAME = "netmaker.db"

var Database gorqlite.Connection

func InitializeDatabase() error {

conn, err := gorqlite.Open("http://")
if err != nil {
return err
}

// sqliteDatabase, _ := sql.Open("sqlite3", "./database/"+dbFilename)
Database = conn
Database.SetConsistencyLevel("strong")
createTables()
return nil
}

func createTables() {
createTable(NETWORKS_TABLE_NAME)
createTable(NODES_TABLE_NAME)
createTable(USERS_TABLE_NAME)
createTable(DNS_TABLE_NAME)
createTable(EXT_CLIENT_TABLE_NAME)
createTable(INT_CLIENTS_TABLE_NAME)
}

func createTable(tableName string) error {
_, err := Database.WriteOne("CREATE TABLE IF NOT EXISTS " + tableName + " (key TEXT NOT NULL UNIQUE PRIMARY KEY, value TEXT)")
if err != nil {
return err
}
return nil
}

func Insert(key string, value string, tableName string) error {
_, err := Database.WriteOne("INSERT OR REPLACE INTO " + tableName + " (key, value) VALUES ('" + key + "', '" + value + "')")
if err != nil {
return err
}
return nil
}

func DeleteRecord(tableName string, key string) error {
_, err := Database.WriteOne("DELETE FROM " + tableName + " WHERE key = \"" + key + "\"")
if err != nil {
return err
}
return nil
}

func DeleteAllRecords(tableName string) error {
_, err := Database.WriteOne("DELETE TABLE " + tableName)
if err != nil {
return err
}
err = createTable(tableName)
if err != nil {
return err
}
return nil
}

func FetchRecord(tableName string, key string) (string, error) {
results, err := FetchRecords(tableName)
if err != nil {
return "", err
}
return results[key], nil
}

func FetchRecords(tableName string) (map[string]string, error) {
row, err := Database.QueryOne("SELECT * FROM " + tableName + " ORDER BY key")
if err != nil {
return nil, err
}
records := make(map[string]string)
for row.Next() { // Iterate and fetch the records from result cursor
var key string
var value string
row.Scan(&key, &value)
records[key] = value
}
log.Println(tableName, records)
return records, nil
}
Loading

0 comments on commit d22ad13

Please sign in to comment.