ip.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package esxi
  15. import (
  16. "github.com/vmware/govmomi/vim25/mo"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/netutils"
  20. "yunion.io/x/pkg/util/regutils"
  21. )
  22. type IPV4Range struct {
  23. iprange *netutils.IPV4AddrRange
  24. }
  25. func (i IPV4Range) Contains(ip string) bool {
  26. ipaddr, err := netutils.NewIPV4Addr(ip)
  27. if err != nil {
  28. log.Errorf("unable to parse ip %q: %v", ip, err)
  29. return false
  30. }
  31. if i.iprange == nil {
  32. return true
  33. }
  34. return i.iprange.Contains(ipaddr)
  35. }
  36. var vmIPV4Filter IPV4Range
  37. func initVMIPV4Filter(cidr string) error {
  38. if len(cidr) == 0 {
  39. return nil
  40. }
  41. prefix, err := netutils.NewIPV4Prefix(cidr)
  42. if err != nil {
  43. return errors.Wrapf(err, "parse cidr %q", cidr)
  44. }
  45. irange := prefix.ToIPRange()
  46. vmIPV4Filter.iprange = &irange
  47. return nil
  48. }
  49. var HOST_PROPS = []string{"name", "config.network", "vm"}
  50. var VM_PROPS = []string{"name", "guest.net", "config.template", "summary.config.uuid", "summary.runtime.powerState"}
  51. func (cli *SESXiClient) AllHostIP() (map[string]string, []mo.HostSystem, error) {
  52. var hosts []mo.HostSystem
  53. err := cli.scanAllMObjects(HOST_PROPS, &hosts)
  54. if err != nil {
  55. return nil, nil, errors.Wrap(err, "scanAllMObjects")
  56. }
  57. ret := make(map[string]string, len(hosts))
  58. for i := range hosts {
  59. // find ip
  60. host := &SHost{SManagedObject: newManagedObject(cli, &hosts[i], nil)}
  61. ip := host.GetAccessIp()
  62. ret[host.GetName()] = ip
  63. }
  64. return ret, hosts, nil
  65. }
  66. func (cli *SESXiClient) VMIP(host mo.HostSystem) (map[string][]string, error) {
  67. var vms []mo.VirtualMachine
  68. err := cli.references2Objects(host.Vm, VM_PROPS, &vms)
  69. if err != nil {
  70. return nil, errors.Wrap(err, "references2Objects")
  71. }
  72. ret := make(map[string][]string, len(vms))
  73. for i := range vms {
  74. vm := vms[i]
  75. if vm.Config == nil || vm.Config.Template {
  76. continue
  77. }
  78. if vm.Guest == nil {
  79. continue
  80. }
  81. guestIps := make([]string, 0)
  82. for _, net := range vm.Guest.Net {
  83. for _, ip := range net.IpAddress {
  84. if regutils.MatchIP4Addr(ip) {
  85. guestIps = append(guestIps, ip)
  86. }
  87. }
  88. }
  89. ret[vm.Name] = guestIps
  90. }
  91. return ret, nil
  92. }
  93. type SVMIPInfo struct {
  94. Moid string
  95. PowerState string
  96. Name string
  97. Uuid string
  98. MacIps []SMacIps
  99. }
  100. type SMacIps struct {
  101. Mac string
  102. IPs []string
  103. }
  104. func (cli *SESXiClient) VMIP2() ([]SVMIPInfo, error) {
  105. var vms []mo.VirtualMachine
  106. err := cli.scanAllMObjects(VM_PROPS, &vms)
  107. if err != nil {
  108. return nil, errors.Wrap(err, "scanAllMObjects")
  109. }
  110. ret := make([]SVMIPInfo, 0, len(vms))
  111. for i := range vms {
  112. vm := vms[i]
  113. if vm.Config == nil || vm.Config.Template {
  114. continue
  115. }
  116. if vm.Guest == nil {
  117. continue
  118. }
  119. info := SVMIPInfo{
  120. Moid: vm.Reference().Value,
  121. PowerState: string(vm.Summary.Runtime.PowerState),
  122. Uuid: vm.Summary.Config.Uuid,
  123. Name: vm.Name,
  124. }
  125. macIps := make([]SMacIps, 0, len(vm.Guest.Net))
  126. for _, net := range vm.Guest.Net {
  127. if len(net.Network) == 0 {
  128. continue
  129. }
  130. macip := SMacIps{
  131. Mac: net.MacAddress,
  132. }
  133. for _, ip := range net.IpAddress {
  134. if regutils.MatchIP4Addr(ip) {
  135. macip.IPs = append(macip.IPs, ip)
  136. }
  137. }
  138. macIps = append(macIps, macip)
  139. }
  140. info.MacIps = macIps
  141. ret = append(ret, info)
  142. }
  143. return ret, nil
  144. }