// SetID recalculates the id of the Stack based on its // Database name and its own name. func (s *Stack) SetID() { if s.Database != nil { s.ID = uuid.New(s.Database.Name + s.Name) return } s.ID = uuid.New(s.Name) }
// Get gets a config value from a key. func (c *Config) Get(key string) interface{} { s, ok := c.Values.Stacks[uuid.New(CONFIG+key)] if !ok { return nil } return s.Peek() }
// NewDatabase creates a new Database given a name, // without any link to the piladb instance. func NewDatabase(name string) *Database { stacks := make(map[fmt.Stringer]*Stack) return &Database{ ID: uuid.New(name), Name: name, Stacks: stacks, } }
// Set sets a config value having a key and the value. func (c *Config) Set(key string, value interface{}) { s, ok := c.Values.Stacks[uuid.New(CONFIG+key)] if !ok { sID := c.Values.CreateStack(key, time.Now()) s, _ = c.Values.Stacks[sID] } s.Push(value) }
// ResourceStack will return the right Stack resource // given a Database and a Stack ID or Name. func ResourceStack(db *pila.Database, stackInput string) (*pila.Stack, bool) { stack, ok := db.Stacks[uuid.UUID(stackInput)] if !ok { // Fallback to find by stack name stack, ok = db.Stacks[uuid.New(db.Name+stackInput)] } return stack, ok }
// ResourceDatabase will return the right Database resource // given a Conn and a database ID or Name. func ResourceDatabase(conn *Conn, databaseInput string) (*pila.Database, bool) { db, ok := conn.Pila.Database(uuid.UUID(databaseInput)) if !ok { // Fallback to find by database name db, ok = conn.Pila.Database(uuid.New(databaseInput)) } return db, ok }
func TestConfigSet(t *testing.T) { config := NewConfig() expectedValues := []string{"bar", "baz", "bam"} for _, expectedValue := range expectedValues { config.Set("foo", expectedValue) s, _ := config.Values.Stacks[uuid.New(CONFIG+"foo")] if value := s.Peek(); value != expectedValue { t.Errorf("Values is %s, expected %s", value, expectedValue) } } }
func TestResourceStack_False(t *testing.T) { dbName := "db" stackName := "stack" inputs := []string{stackName, uuid.New(dbName + stackName).String()} for _, input := range inputs { db := pila.NewDatabase(dbName) _, ok := ResourceStack(db, input) if ok { t.Errorf("ok is %v, expected false", ok) } } }
func TestResourceDatabase_False(t *testing.T) { dbName := "db" inputs := []string{dbName, uuid.New(dbName).String()} for _, input := range inputs { p := pila.NewPila() conn := NewConn() conn.Pila = p _, ok := ResourceDatabase(conn, input) if ok { t.Errorf("ok is %v, expected false", ok) } } }
func TestNewConfig(t *testing.T) { config := NewConfig() if config.Values == nil { t.Fatal(errors.New("Config is nil")) } inputOutput := []struct { input, output interface{} }{ {config.Values.Name, CONFIG}, {config.Values.ID, uuid.New(CONFIG)}, {len(config.Values.Stacks), 0}, } for _, io := range inputOutput { if io.input != io.output { t.Errorf("got %v, expected %v", io.input, io.output) } } }
func TestResourceStack(t *testing.T) { dbName := "db" stackName := "stack" inputs := []string{stackName, uuid.New(dbName + stackName).String()} for _, input := range inputs { expectedStack := pila.NewStack(stackName, time.Now()) db := pila.NewDatabase(dbName) _ = db.AddStack(expectedStack) stack, ok := ResourceStack(db, input) if !ok { t.Errorf("ok is %v, expected true", ok) } if !reflect.DeepEqual(expectedStack, stack) { t.Errorf("stack is %v, expected stack id %v", stack, expectedStack) } } }
func TestResourceDatabase(t *testing.T) { dbName := "db" inputs := []string{dbName, uuid.New(dbName).String()} for _, input := range inputs { expectedDB := pila.NewDatabase(dbName) p := pila.NewPila() _ = p.AddDatabase(expectedDB) conn := NewConn() conn.Pila = p db, ok := ResourceDatabase(conn, input) if !ok { t.Errorf("ok is %v, expected true", ok) } if !reflect.DeepEqual(expectedDB, db) { t.Errorf("db is %v, expected db id %v", db, expectedDB) } } }