-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Models Scaffolding for KV Store (#40)
- Loading branch information
Showing
7 changed files
with
603 additions
and
110 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
"github.com/docker/libkv" | ||
"github.com/docker/libkv/store" | ||
"github.com/docker/libkv/store/consul" | ||
"time" | ||
) | ||
|
||
var db store.Store | ||
|
||
func init() { | ||
consul.Register() | ||
} | ||
|
||
// InitBackend creates the default backend capability | ||
func InitBackend() { | ||
client := "localhost:8500" | ||
|
||
var err error | ||
db, err = libkv.NewStore( | ||
store.CONSUL, | ||
[]string{client}, | ||
&store.Config{ | ||
ConnectionTimeout: 10 * time.Second, | ||
}, | ||
) | ||
if err != nil { | ||
fmt.Printf("Failed to init DB!\n") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package models | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"testing" | ||
) | ||
|
||
func TestModels(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Models Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package models | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
const ( | ||
// nodesPrefix is the area under rhd where nodes are stored | ||
nodesPrefix = "nodes/" | ||
) | ||
|
||
// RhdNode stores relevant cache data about a node | ||
type RhdNode struct { | ||
ID string | ||
RhdID string | ||
} | ||
|
||
// NewRhdNode creates a new RhdNode object for storage | ||
func NewRhdNode(rhdID string, nodeID string) (*RhdNode, error) { | ||
return &RhdNode{ | ||
RhdID: rhdID, | ||
ID: nodeID, | ||
}, nil | ||
} | ||
|
||
// CreateNode allows creating a node on the backend | ||
func CreateNode(node *RhdNode) error { | ||
return UpdateNode(node) | ||
} | ||
|
||
// UpdateNode updates a RhdNode on the backend | ||
func UpdateNode(node *RhdNode) error { | ||
nodePath := rhdPrefix + node.RhdID + "/" + nodesPrefix + node.ID | ||
if err := CreateNodeCache(node); err != nil { | ||
return err | ||
} | ||
b, err := json.Marshal(node) | ||
if err != nil { | ||
return err | ||
} | ||
return db.Put(nodePath, b, nil) | ||
} | ||
|
||
// GetAllNodes is currently stubbed out but unsupported | ||
func GetAllNodes() ([]*RhdNode, error) { | ||
var nodes []*RhdNode | ||
return nodes, nil | ||
} | ||
|
||
// GetAllNodesByRhdID is currently stubbed out but unsupported | ||
func GetAllNodesByRhdID(id string) ([]*RhdNode, error) { | ||
var rhds []*RhdNode | ||
return rhds, nil | ||
} | ||
|
||
// GetNodeByRhdIDByNodeID is currently stubbed out but unsupported | ||
func GetNodeByRhdIDByNodeID(rid string, nid string) (*RhdNode, error) { | ||
return &RhdNode{}, nil | ||
} | ||
|
||
// GetNodesByRhdIDByNodeIDs is currently stubbed out but unsupported | ||
func GetNodesByRhdIDByNodeIDs(rid string, ids []string) ([]*RhdNode, error) { | ||
var nodes []*RhdNode | ||
return nodes, nil | ||
} | ||
|
||
// CreateNodeCache stores the cache layer lookup functionality | ||
func CreateNodeCache(node *RhdNode) error { | ||
nodePath := nodesPrefix + node.ID | ||
return db.Put(nodePath, []byte(node.RhdID), nil) | ||
} | ||
|
||
// GetRhdIDByNodeID returns the RHD ID for a node | ||
func GetRhdIDByNodeID(id string) (string, error) { | ||
nodesPath := nodesPrefix + id | ||
pair, err := db.Get(nodesPath) | ||
return string(pair.Value), err | ||
} | ||
|
||
// DeleteNodeCache removes a cache index | ||
func DeleteNodeCache(id string) error { | ||
nodePath := nodesPrefix + id | ||
return db.DeleteTree(nodePath) | ||
} |
Oops, something went wrong.