api_host.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. LocalRange string `json:"localrange"`
  15. LocalListenPort int `json:"locallistenport"`
  16. ProxyListenPort int `json:"proxy_listen_port"`
  17. MTU int `json:"mtu" yaml:"mtu"`
  18. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  19. EndpointIP string `json:"endpointip" yaml:"endpointip"`
  20. PublicKey string `json:"publickey"`
  21. MacAddress string `json:"macaddress"`
  22. InternetGateway string `json:"internetgateway"`
  23. Nodes []string `json:"nodes"`
  24. ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
  25. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  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.LocalListenPort = h.LocalListenPort
  42. a.LocalRange = h.LocalRange.String()
  43. if isEmptyAddr(a.LocalRange) {
  44. a.LocalRange = ""
  45. }
  46. a.MTU = h.MTU
  47. a.MacAddress = h.MacAddress.String()
  48. a.Name = h.Name
  49. a.OS = h.OS
  50. a.Nodes = h.Nodes
  51. a.ProxyEnabled = h.ProxyEnabled
  52. a.ProxyListenPort = h.ProxyListenPort
  53. a.PublicKey = h.PublicKey.String()
  54. a.Verbosity = h.Verbosity
  55. a.Version = h.Version
  56. a.IsDefault = h.IsDefault
  57. return &a
  58. }
  59. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  60. // a Host struct
  61. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  62. h := Host{}
  63. h.ID = currentHost.ID
  64. h.HostPass = currentHost.HostPass
  65. h.DaemonInstalled = currentHost.DaemonInstalled
  66. h.EndpointIP = net.ParseIP(a.EndpointIP)
  67. h.Debug = a.Debug
  68. h.FirewallInUse = a.FirewallInUse
  69. h.IPForwarding = currentHost.IPForwarding
  70. h.Interface = currentHost.Interface
  71. h.Interfaces = currentHost.Interfaces
  72. h.InternetGateway = currentHost.InternetGateway
  73. h.IsDocker = currentHost.IsDocker
  74. h.IsK8S = currentHost.IsK8S
  75. h.IsStatic = a.IsStatic
  76. h.ListenPort = a.ListenPort
  77. h.LocalListenPort = currentHost.ListenPort
  78. h.MTU = a.MTU
  79. h.MacAddress = currentHost.MacAddress
  80. h.PublicKey = currentHost.PublicKey
  81. h.Name = a.Name
  82. h.Version = currentHost.Version
  83. h.Verbosity = a.Verbosity
  84. h.Nodes = currentHost.Nodes
  85. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  86. h.OS = currentHost.OS
  87. if len(a.LocalRange) > 0 {
  88. _, localRange, err := net.ParseCIDR(a.LocalRange)
  89. if err == nil {
  90. h.LocalRange = *localRange
  91. }
  92. } else if !isEmptyAddr(currentHost.LocalRange.String()) {
  93. h.LocalRange = currentHost.LocalRange
  94. }
  95. h.ProxyEnabled = a.ProxyEnabled
  96. h.IsDefault = a.IsDefault
  97. return &h
  98. }