func AddStorageNodeToDB(hostname string, node_state int, node_status string) error { // Add the node details to the DB var storage_node models.Node storage_node.Hostname = hostname storage_node.State = node_state storage_node.Status = node_status sessionCopy := db.GetDatastore().Copy() defer sessionCopy.Close() coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES) var node models.Node err = coll.Find(bson.M{"hostname": storage_node.Hostname}).One(&node) if err != mgo.ErrNotFound { logger.Get().Critical(fmt.Sprintf("Node with name: %v already exists", storage_node.Hostname)) return errors.New(fmt.Sprintf("Node with name: %v already exists", storage_node.Hostname)) } // Persist the node details if err := coll.Insert(storage_node); err != nil { logger.Get().Critical("Error adding the node: %s. error: %v", storage_node.Hostname, err) return err } return nil }
func updateStorageNodeToDB(storage_node models.Node) error { // Add the node details to the DB sessionCopy := db.GetDatastore().Copy() defer sessionCopy.Close() coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES) storage_node.State = models.NODE_STATE_ACTIVE if err := coll.Update(bson.M{"hostname": storage_node.Hostname}, storage_node); err != nil { logger.Get().Critical("Error Updating the node: %s. error: %v", storage_node.Hostname, err) return err } return nil }