api_host.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package models
  2. import "net"
  3. // ApiHost - the host struct for API usage
  4. type ApiHost struct {
  5. ID string `json:"id"`
  6. Verbosity int `json:"verbosity"`
  7. FirewallInUse string `json:"firewallinuse"`
  8. Version string `json:"version"`
  9. Name string `json:"name"`
  10. OS string `json:"os"`
  11. Debug bool `json:"debug"`
  12. IsStatic bool `json:"isstatic"`
  13. ListenPort int `json:"listenport"`
  14. LocalAddress string `json:"localaddress"`
  15. LocalRange string `json:"localrange"`
  16. LocalListenPort int `json:"locallistenport"`
  17. ProxyListenPort int `json:"proxy_listen_port"`
  18. MTU int `json:"mtu" yaml:"mtu"`
  19. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  20. EndpointIP string `json:"endpointip" yaml:"endpointip"`
  21. PublicKey string `json:"publickey"`
  22. MacAddress string `json:"macaddress"`
  23. InternetGateway string `json:"internetgateway"`
  24. Nodes []string `json:"nodes"`
  25. ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
  26. }
  27. // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
  28. func (h *Host) ConvertNMHostToAPI() *ApiHost {
  29. a := ApiHost{}
  30. a.Debug = h.Debug
  31. a.EndpointIP = h.EndpointIP.String()
  32. a.FirewallInUse = h.FirewallInUse
  33. a.ID = h.ID.String()
  34. a.Interfaces = h.Interfaces
  35. a.InternetGateway = h.InternetGateway.String()
  36. if isEmptyAddr(a.InternetGateway) {
  37. a.InternetGateway = ""
  38. }
  39. a.IsStatic = h.IsStatic
  40. a.ListenPort = h.ListenPort
  41. a.LocalAddress = h.LocalAddress.String()
  42. if isEmptyAddr(a.LocalAddress) {
  43. a.LocalAddress = ""
  44. }
  45. a.LocalListenPort = h.LocalListenPort
  46. a.LocalRange = h.LocalRange.String()
  47. if isEmptyAddr(a.LocalRange) {
  48. a.LocalRange = ""
  49. }
  50. a.MTU = h.MTU
  51. a.MacAddress = h.MacAddress.String()
  52. a.Name = h.Name
  53. a.OS = h.OS
  54. a.Nodes = h.Nodes
  55. a.ProxyEnabled = h.ProxyEnabled
  56. a.ProxyListenPort = h.ProxyListenPort
  57. a.PublicKey = h.PublicKey.String()
  58. a.Verbosity = h.Verbosity
  59. a.Version = h.Version
  60. return &a
  61. }
  62. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  63. // a Host struct
  64. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  65. h := Host{}
  66. h.ID = currentHost.ID
  67. h.HostPass = currentHost.HostPass
  68. h.DaemonInstalled = currentHost.DaemonInstalled
  69. h.EndpointIP = net.ParseIP(a.EndpointIP)
  70. h.Debug = a.Debug
  71. h.FirewallInUse = a.FirewallInUse
  72. h.IPForwarding = currentHost.IPForwarding
  73. h.Interface = currentHost.Interface
  74. h.Interfaces = currentHost.Interfaces
  75. h.InternetGateway = currentHost.InternetGateway
  76. h.IsDocker = currentHost.IsDocker
  77. h.IsK8S = currentHost.IsK8S
  78. h.IsStatic = a.IsStatic
  79. h.ListenPort = a.ListenPort
  80. h.LocalListenPort = currentHost.ListenPort
  81. h.MTU = a.MTU
  82. h.MacAddress = currentHost.MacAddress
  83. h.PublicKey = currentHost.PublicKey
  84. h.Name = a.Name
  85. h.Version = currentHost.Version
  86. h.Verbosity = a.Verbosity
  87. h.Nodes = currentHost.Nodes
  88. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  89. h.OS = currentHost.OS
  90. if len(a.LocalAddress) > 0 {
  91. _, localAddr, err := net.ParseCIDR(a.LocalAddress)
  92. if err == nil {
  93. h.LocalAddress = *localAddr
  94. }
  95. } else if !isEmptyAddr(currentHost.LocalAddress.String()) {
  96. h.LocalAddress = currentHost.LocalAddress
  97. }
  98. if len(a.LocalRange) > 0 {
  99. _, localRange, err := net.ParseCIDR(a.LocalRange)
  100. if err == nil {
  101. h.LocalRange = *localRange
  102. }
  103. } else if !isEmptyAddr(currentHost.LocalRange.String()) {
  104. h.LocalRange = currentHost.LocalRange
  105. }
  106. h.ProxyEnabled = a.ProxyEnabled
  107. return &h
  108. }