You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ReST API provides lots of information, but sometimes its limitations prevents to implement certain use cases. For this reason it would be useful to have access to the database on a easy manner.
In the world of GO, the following is one of the most used ORMs: https://gorm.io
Here is an example:
package main
import (
"fmt""encoding/json""github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
// OnmsNode an OpenNMS node objecttypeOnmsNodestruct {
IDuint`gorm:"column:nodeid;primary_key"`Labelstring`gorm:"column:nodelabel"`LabelSourcestring`gorm:"column:nodelabelsource"`ForeignSourcestring`gorm:"column:foreignsource"`ForeignIDstring`gorm:"column:foreignid"`Locationstring`gorm:"column:location"`SysObjIDstring`gorm:"column:nodesysoid"`SysNamestring`gorm:"column:nodesysname"`SysDescrstring`gorm:"column:nodesysdescription"`SysLocationstring`gorm:"column:nodesyslocation"`SysContactstring`gorm:"column:nodesyscontact"`Interfaces []OnmsIPInterface`gorm:"foreignKey:NodeID;AssociationForeignKey:nodeid" json:",omitempty"`
}
// TableName the name of the OpenNMS Node Tablefunc (OnmsNode) TableName() string {
return"node"
}
// OnmsIPInterface the IP Interface ObjecttypeOnmsIPInterfacestruct {
IDuint`gorm:"column:id;primary_key"`IPAddressstring`gorm:"column:ipaddr"`Hostnamestring`gorm:"column:iphostname"`IsManagedstring`gorm:"column:ismanaged"`SnmpPrimarystring`gorm:"column:issnmpprimary"`NodeIDuint`gorm:"column:nodeid"`
}
// TableName the name of the OpenNMS IP Interface Tablefunc (OnmsIPInterface) TableName() string {
return"ipinterface"
}
funcmain() {
db, err:=gorm.Open("postgres", "host=localhost port=5432 sslmode=disable dbname=opennms user=opennms password=opennms")
iferr!=nil {
panic(err.Error())
}
deferdb.Close()
// Get Onenode:=&OnmsNode{}
db.Preload("Interfaces").First(node, 10)
bytesArray, err:=json.MarshalIndent(node, "", " ")
fmt.Println(string(bytesArray))
// Get all Nodesnodes:= []OnmsNode{}
db.Find(&nodes)
bytesArray, err=json.MarshalIndent(nodes, "", " ")
fmt.Println(string(bytesArray))
// Get a sub-set of NodeslinuxNodes:= []OnmsNode{}
db.Where("nodeSysOID like ?", ".1.3.6.1.4.1.8072.%").Find(&linuxNodes)
bytesArray, err=json.MarshalIndent(linuxNodes, "", " ")
fmt.Println(string(bytesArray))
}
The text was updated successfully, but these errors were encountered:
The ReST API provides lots of information, but sometimes its limitations prevents to implement certain use cases. For this reason it would be useful to have access to the database on a easy manner.
In the world of GO, the following is one of the most used ORMs: https://gorm.io
Here is an example:
The text was updated successfully, but these errors were encountered: