Skip to content

Commit

Permalink
Initial Models Scaffolding for KV Store (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
king-jam committed Nov 4, 2016
1 parent cbb74f1 commit ba1f545
Show file tree
Hide file tree
Showing 7 changed files with 603 additions and 110 deletions.
110 changes: 0 additions & 110 deletions glide.lock

This file was deleted.

6 changes: 6 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package: github.com/RackHD/neighborhood-manager
import:
- package: github.com/docker/libkv
version: ~0.2.1
subpackages:
- store
- store/consul
- package: github.com/hashicorp/consul
version: ~0.7.0
subpackages:
- api
- package: github.com/hashicorp/go-cleanhttp
- package: github.com/king-jam/gossdp
- package: github.com/spf13/viper
- package: github.com/streadway/amqp
testImport:
- package: github.com/onsi/ginkgo
version: ~1.2.0
Expand Down
32 changes: 32 additions & 0 deletions rackhd/models/db.go
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")
}
}
13 changes: 13 additions & 0 deletions rackhd/models/models_suite_test.go
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")
}
84 changes: 84 additions & 0 deletions rackhd/models/nodes.go
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)
}
Loading

0 comments on commit ba1f545

Please sign in to comment.