IP4Config (new API) methods implementation
This commit is contained in:
parent
e5777e2fd1
commit
053b71438e
2 changed files with 63 additions and 8 deletions
63
IP4Config.go
63
IP4Config.go
|
@ -2,7 +2,6 @@ package gonetworkmanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/godbus/dbus"
|
"github.com/godbus/dbus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -50,7 +49,7 @@ type IP4RouteData struct {
|
||||||
Prefix uint8
|
Prefix uint8
|
||||||
NextHop string
|
NextHop string
|
||||||
Metric uint8
|
Metric uint8
|
||||||
AdditionalAttributes []string
|
AdditionalAttributes map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type IP4NameserverData struct {
|
type IP4NameserverData struct {
|
||||||
|
@ -126,7 +125,18 @@ func (c *ip4Config) GetAddresses() []IP4Address {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ip4Config) GetAddressData() []IP4AddressData {
|
func (c *ip4Config) GetAddressData() []IP4AddressData {
|
||||||
return []IP4AddressData{}
|
addresses := c.getSliceMapStringVariantProperty(IP4ConfigPropertyAddressData)
|
||||||
|
ret := make([]IP4AddressData, len(addresses))
|
||||||
|
|
||||||
|
for i, address := range addresses {
|
||||||
|
prefix := address["prefix"].Value().(uint32)
|
||||||
|
|
||||||
|
ret[i] = IP4AddressData{
|
||||||
|
Address: address["address"].Value().(string),
|
||||||
|
Prefix: uint8(prefix),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ip4Config) GetGateway() string {
|
func (c *ip4Config) GetGateway() string {
|
||||||
|
@ -151,7 +161,33 @@ func (c *ip4Config) GetRoutes() []IP4Route {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ip4Config) GetRouteData() []IP4RouteData {
|
func (c *ip4Config) GetRouteData() []IP4RouteData {
|
||||||
return []IP4RouteData{}
|
routesData := c.getSliceMapStringVariantProperty(IP4ConfigPropertyRouteData)
|
||||||
|
routes := make([]IP4RouteData, len(routesData))
|
||||||
|
|
||||||
|
for _, routeData := range routesData {
|
||||||
|
|
||||||
|
route := IP4RouteData{}
|
||||||
|
|
||||||
|
for routeDataAttributeName, routeDataAttribute := range routeData {
|
||||||
|
switch routeDataAttributeName {
|
||||||
|
case "dest":
|
||||||
|
route.Destination = routeDataAttribute.Value().(string)
|
||||||
|
case "prefix":
|
||||||
|
prefix, _ := routeDataAttribute.Value().(uint32)
|
||||||
|
route.Prefix = uint8(prefix)
|
||||||
|
case "next-hop":
|
||||||
|
route.NextHop = routeDataAttribute.Value().(string)
|
||||||
|
case "metric":
|
||||||
|
metric := routeDataAttribute.Value().(uint32)
|
||||||
|
route.Metric = uint8(metric)
|
||||||
|
default:
|
||||||
|
route.AdditionalAttributes[routeDataAttributeName] = routeDataAttribute.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
routes = append(routes, route)
|
||||||
|
}
|
||||||
|
return routes
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deprecated: use GetNameserverData
|
// Deprecated: use GetNameserverData
|
||||||
|
@ -167,7 +203,18 @@ func (c *ip4Config) GetNameservers() []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ip4Config) GetNameserverData() []IP4NameserverData {
|
func (c *ip4Config) GetNameserverData() []IP4NameserverData {
|
||||||
return []IP4NameserverData{}
|
nameserversData := c.getSliceMapStringVariantProperty(IP4ConfigPropertyNameserverData)
|
||||||
|
nameservers := make([]IP4NameserverData, len(nameserversData))
|
||||||
|
|
||||||
|
for _, nameserverData := range nameserversData {
|
||||||
|
|
||||||
|
nameserver := IP4NameserverData{
|
||||||
|
Address: nameserverData["address"].Value().(string),
|
||||||
|
}
|
||||||
|
|
||||||
|
nameservers = append(nameservers, nameserver)
|
||||||
|
}
|
||||||
|
return nameservers
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ip4Config) GetDomains() []string {
|
func (c *ip4Config) GetDomains() []string {
|
||||||
|
@ -192,9 +239,9 @@ func (c *ip4Config) GetWinsServerData() []string {
|
||||||
|
|
||||||
func (c *ip4Config) MarshalJSON() ([]byte, error) {
|
func (c *ip4Config) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(map[string]interface{}{
|
return json.Marshal(map[string]interface{}{
|
||||||
"Addresses": c.GetAddresses(),
|
"Addresses": c.GetAddressData(),
|
||||||
"Routes": c.GetRoutes(),
|
"Routes": c.GetRouteData(),
|
||||||
"Nameservers": c.GetNameservers(),
|
"Nameservers": c.GetNameserverData(),
|
||||||
"Domains": c.GetDomains(),
|
"Domains": c.GetDomains(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
8
utils.go
8
utils.go
|
@ -148,6 +148,14 @@ func (d *dbusBase) getSliceSliceUint32Property(iface string) [][]uint32 {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *dbusBase) getSliceMapStringVariantProperty(iface string) []map[string]dbus.Variant {
|
||||||
|
value, ok := d.getProperty(iface).([]map[string]dbus.Variant)
|
||||||
|
if !ok {
|
||||||
|
panic(makeErrVariantType(iface))
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
func (d *dbusBase) getSliceByteProperty(iface string) []byte {
|
func (d *dbusBase) getSliceByteProperty(iface string) []byte {
|
||||||
value, ok := d.getProperty(iface).([]byte)
|
value, ok := d.getProperty(iface).([]byte)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
Reference in a new issue