api_host.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  27. }
  28. // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
  29. func (h *Host) ConvertNMHostToAPI() *ApiHost {
  30. a := ApiHost{}
  31. a.Debug = h.Debug
  32. a.EndpointIP = h.EndpointIP.String()
  33. a.FirewallInUse = h.FirewallInUse
  34. a.ID = h.ID.String()
  35. a.Interfaces = h.Interfaces
  36. a.InternetGateway = h.InternetGateway.String()
  37. if isEmptyAddr(a.InternetGateway) {
  38. a.InternetGateway = ""
  39. }
  40. a.IsStatic = h.IsStatic
  41. a.ListenPort = h.ListenPort
  42. a.LocalAddress = h.LocalAddress.String()
  43. if isEmptyAddr(a.LocalAddress) {
  44. a.LocalAddress = ""
  45. }
  46. a.LocalListenPort = h.LocalListenPort
  47. a.LocalRange = h.LocalRange.String()
  48. if isEmptyAddr(a.LocalRange) {
  49. a.LocalRange = ""
  50. }
  51. a.MTU = h.MTU
  52. a.MacAddress = h.MacAddress.String()
  53. a.Name = h.Name
  54. a.OS = h.OS
  55. a.Nodes = h.Nodes
  56. a.ProxyEnabled = h.ProxyEnabled
  57. a.ProxyListenPort = h.ProxyListenPort
  58. a.PublicKey = h.PublicKey.String()
  59. a.Verbosity = h.Verbosity
  60. a.Version = h.Version
  61. a.IsDefault = h.IsDefault
  62. return &a
  63. }
  64. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  65. // a Host struct
  66. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  67. h := Host{}
  68. h.ID = currentHost.ID
  69. h.HostPass = currentHost.HostPass
  70. h.DaemonInstalled = currentHost.DaemonInstalled
  71. h.EndpointIP = net.ParseIP(a.EndpointIP)
  72. h.Debug = a.Debug
  73. h.FirewallInUse = a.FirewallInUse
  74. h.IPForwarding = currentHost.IPForwarding
  75. h.Interface = currentHost.Interface
  76. h.Interfaces = currentHost.Interfaces
  77. h.InternetGateway = currentHost.InternetGateway
  78. h.IsDocker = currentHost.IsDocker
  79. h.IsK8S = currentHost.IsK8S
  80. h.IsStatic = a.IsStatic
  81. h.ListenPort = a.ListenPort
  82. h.LocalListenPort = currentHost.ListenPort
  83. h.MTU = a.MTU
  84. h.MacAddress = currentHost.MacAddress
  85. h.PublicKey = currentHost.PublicKey
  86. h.Name = a.Name
  87. h.Version = currentHost.Version
  88. h.Verbosity = a.Verbosity
  89. h.Nodes = currentHost.Nodes
  90. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  91. h.OS = currentHost.OS
  92. if len(a.LocalAddress) > 0 {
  93. _, localAddr, err := net.ParseCIDR(a.LocalAddress)
  94. if err == nil {
  95. h.LocalAddress = *localAddr
  96. }
  97. } else if !isEmptyAddr(currentHost.LocalAddress.String()) {
  98. h.LocalAddress = currentHost.LocalAddress
  99. }
  100. if len(a.LocalRange) > 0 {
  101. _, localRange, err := net.ParseCIDR(a.LocalRange)
  102. if err == nil {
  103. h.LocalRange = *localRange
  104. }
  105. } else if !isEmptyAddr(currentHost.LocalRange.String()) {
  106. h.LocalRange = currentHost.LocalRange
  107. }
  108. h.ProxyEnabled = a.ProxyEnabled
  109. h.IsDefault = a.IsDefault
  110. return &h
  111. }