func (self *TDynamoDBStore) InitTable() { glog.Info("Initializing tables") newTableDesc := DynamoDBDemoTableDescription() tableExists := self.findTableByName(newTableDesc.TableName) if tableExists { glog.Infof("Table %s exists, skipping init", newTableDesc.TableName) glog.Infof("Waiting until table %s becomes active", newTableDesc.TableName) self.waitUntilTableIsActive(newTableDesc.TableName) glog.Infof("Table %s is active", newTableDesc.TableName) return } else { glog.Infof("Creating table %s", newTableDesc.TableName) status, err := self.dynamoServer.CreateTable(newTableDesc) contract.RequireNoError(err) if status == TableStatusCreating { glog.Infof("Waiting until table %s becomes active", newTableDesc.TableName) self.waitUntilTableIsActive(newTableDesc.TableName) glog.Infof("Table %s is active", newTableDesc.TableName) return } if status == TableStatusActive { glog.Infof("Table %s is active", newTableDesc.TableName) return } glog.Fatal("Unexpected status:", status) } }
func (self *TDynamoDBStore) DestroyTable() { glog.Info("Destroying tables") newTableDesc := DynamoDBDemoTableDescription() tableExists := self.findTableByName(newTableDesc.TableName) if !tableExists { glog.Infof("Table %s doesn't exists, skipping deletion", newTableDesc.TableName) return } else { _, err := self.dynamoServer.DeleteTable(newTableDesc) if err != nil { glog.Fatal(err) } glog.Infof("Table %s deleted successfully", newTableDesc.TableName) } }
func main() { var cfg Config err := gcfg.ReadFileInto(&cfg, "dynamodb.gcfg.local") if err != nil { glog.Fatal(err) } store := MakeDynamoDBStore(cfg.DynamoDemos_AWS.Key, cfg.DynamoDemos_AWS.Secret) store.InitTable() item := MakeItem("some-unique-id", StringAttr("color", "red")) // insert row if ok, err := store.PutItem(item); !ok { glog.Fatalf("Failed to save item %v because of: %v", item, err) } glog.Infof("Item %v saved ", item) // find row savedItem, err := store.GetItem(item.PrimaryKey) if err != nil { glog.Fatalf("Failed to get saved item with pk %s", item.PrimaryKey) } glog.Infof("Got item %#v", savedItem) // find non-existing row _, err = store.GetItem("unknown") if err == nil { glog.Fatalf("Shouldnt get an item with pk %s", "unknown") } else if err.Error() == "Item not found" { glog.Infof("Failed to get non-existent item with err %s", err.Error()) } else { glog.Fatalf("Failed to get non-existent item with unexpected err %s", err.Error()) } // update row updItem := MakeItem("some-unique-id", StringAttr("color", "violet")) if ok, err := store.UpdateItem(updItem); !ok { glog.Fatalf("Failed to update item with unexpected err: %v", err) } // delete row if ok, err := store.DeleteItem(item); !ok { glog.Fatalf("Failed to delete item with unexpected err: %v", err) } store.DestroyTable() }