wire.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "fmt"
  17. "time"
  18. "github.com/vmware/govmomi/vim25/mo"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/netutils"
  21. "yunion.io/x/pkg/util/regutils"
  22. "yunion.io/x/cloudmux/pkg/apis/compute"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. )
  25. type sWire struct {
  26. network IVMNetwork
  27. client *SESXiClient
  28. }
  29. func (wire *sWire) GetId() string {
  30. return wire.network.GetId()
  31. }
  32. func (wire *sWire) GetName() string {
  33. return wire.network.GetName()
  34. }
  35. func (wire *sWire) GetGlobalId() string {
  36. if wire.client.IsVCenter() {
  37. return fmt.Sprintf("%s/%s", wire.client.GetUUID(), wire.network.GetId())
  38. } else {
  39. return wire.network.GetId()
  40. }
  41. }
  42. func (wire *sWire) GetCreatedAt() time.Time {
  43. return time.Time{}
  44. }
  45. func (wire *sWire) GetDescription() string {
  46. return fmt.Sprintf("%s %s %s", wire.network.GetType(), wire.network.GetName(), wire.network.GetId())
  47. }
  48. func (wire *sWire) GetStatus() string {
  49. return compute.WIRE_STATUS_AVAILABLE
  50. }
  51. func (wire *sWire) Refresh() error {
  52. return nil
  53. }
  54. func (wire *sWire) IsEmulated() bool {
  55. return false
  56. }
  57. func (wire *sWire) GetTags() (map[string]string, error) {
  58. return nil, nil
  59. }
  60. func (wire *sWire) SetTags(tags map[string]string, replace bool) error {
  61. return nil
  62. }
  63. func (wire *sWire) GetIVpc() cloudprovider.ICloudVpc {
  64. return wire.client.fakeVpc
  65. }
  66. func (wire *sWire) GetIZone() cloudprovider.ICloudZone {
  67. return nil
  68. }
  69. func (wire *sWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  70. return nil, errors.ErrNotSupported
  71. }
  72. func (wire *sWire) GetBandwidth() int {
  73. return 10000
  74. }
  75. func (wire *sWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  76. return nil, errors.ErrNotFound
  77. }
  78. func (wire *sWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  79. return nil, errors.ErrNotSupported
  80. }
  81. func (wire *sWire) getAvailableIpsMacs() ([]string, []string, error) {
  82. var hosts []mo.HostSystem
  83. err := wire.client.references2Objects(wire.network.GetHosts(), HOST_PROPS, &hosts)
  84. if err != nil {
  85. return nil, nil, errors.Wrap(err, "references2Objects HOST_PROPS")
  86. }
  87. retIps := make([]string, 0)
  88. retMacs := make([]string, 0)
  89. for i := range hosts {
  90. h := hosts[i]
  91. ips, macs, err := wire.getAvailableIpsMacsOnHost(h)
  92. if err != nil {
  93. return nil, nil, errors.Wrapf(err, "getAvailableIpsOnHost %s", h.Name)
  94. }
  95. retIps = append(retIps, ips...)
  96. retMacs = append(retMacs, macs...)
  97. }
  98. return retIps, retMacs, nil
  99. }
  100. func (wire *sWire) getAvailableIpsMacsOnVM(vm mo.VirtualMachine) ([]string, []string) {
  101. ips := make([]string, 0)
  102. macs := make([]string, 0)
  103. for _, net := range vm.Guest.Net {
  104. if net.Network != wire.GetName() {
  105. continue
  106. }
  107. if len(net.MacAddress) > 0 {
  108. mac := netutils.FormatMacAddr(net.MacAddress)
  109. if len(mac) > 0 {
  110. macs = append(macs, mac)
  111. }
  112. }
  113. for _, ip := range net.IpAddress {
  114. if regutils.MatchIP4Addr(ip) {
  115. ips = append(ips, ip)
  116. }
  117. }
  118. }
  119. return ips, macs
  120. }
  121. func (wire *sWire) getAvailableIpsMacsOnHost(host mo.HostSystem) ([]string, []string, error) {
  122. var vms []mo.VirtualMachine
  123. err := wire.client.references2Objects(host.Vm, VM_PROPS, &vms)
  124. if err != nil {
  125. return nil, nil, errors.Wrap(err, "references2Objects VM_PROPS")
  126. }
  127. retIps := make([]string, 0)
  128. retMacs := make([]string, 0)
  129. for _, vm := range vms {
  130. ips, macs := wire.getAvailableIpsMacsOnVM(vm)
  131. retIps = append(retIps, ips...)
  132. retMacs = append(retMacs, macs...)
  133. }
  134. return retIps, retMacs, nil
  135. }