Ocean/ICCC/RegisterHost2Database.go

40 lines
1.5 KiB
Go
Raw Normal View History

package ICCC
import (
"github.com/SommerEngineering/Ocean/ICCC/Scheme"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"github.com/SommerEngineering/Ocean/Tools"
"gopkg.in/mgo.v2/bson"
)
2015-06-17 15:44:52 +00:00
// Function to register this server to the ICCC.
func registerHost2Database() {
2015-06-17 15:44:52 +00:00
// Create the host entry:
host := Scheme.Host{}
host.Hostname = Tools.ThisHostname()
host.IPAddressPort = correctAddressWithPort
2015-06-17 15:44:52 +00:00
// The query to find already existing entries:
selection := bson.D{{`Hostname`, host.Hostname}, {`IPAddressPort`, host.IPAddressPort}}
2015-06-17 15:44:52 +00:00
// Count the already existing entries:
count, _ := collectionHosts.Find(selection).Count()
2015-06-17 15:44:52 +00:00
// Already exist?
if count == 1 {
2015-06-17 15:44:52 +00:00
// Case: Exists!
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameCONFIGURATION, `This host is already registered!`, `host=`+host.Hostname, `address=`+host.IPAddressPort)
} else {
2015-06-17 15:44:52 +00:00
// Case: Not exist.
if errInsert := collectionHosts.Insert(host); errInsert != nil {
2015-06-17 15:44:52 +00:00
// Case: Was not able insert in the database
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to register this host.`, errInsert.Error(), `host=`+host.Hostname, `address=`+host.IPAddressPort)
} else {
2015-06-17 15:44:52 +00:00
// Case: Everything was fine.
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `This host is now registered.`, `host=`+host.Hostname, `address=`+host.IPAddressPort)
}
}
}