yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/ip.go (about) 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 15 package esxi 16 17 import ( 18 "github.com/vmware/govmomi/vim25/mo" 19 20 "yunion.io/x/log" 21 "yunion.io/x/pkg/errors" 22 "yunion.io/x/pkg/util/netutils" 23 "yunion.io/x/pkg/util/regutils" 24 ) 25 26 type IPV4Range struct { 27 iprange *netutils.IPV4AddrRange 28 } 29 30 func (i IPV4Range) Contains(ip string) bool { 31 ipaddr, err := netutils.NewIPV4Addr(ip) 32 if err != nil { 33 log.Errorf("unable to parse ip %q: %v", ip, err) 34 return false 35 } 36 if i.iprange == nil { 37 return true 38 } 39 return i.iprange.Contains(ipaddr) 40 } 41 42 var vmIPV4Filter IPV4Range 43 44 func initVMIPV4Filter(cidr string) error { 45 if len(cidr) == 0 { 46 return nil 47 } 48 prefix, err := netutils.NewIPV4Prefix(cidr) 49 if err != nil { 50 return errors.Wrapf(err, "parse cidr %q", cidr) 51 } 52 irange := prefix.ToIPRange() 53 vmIPV4Filter.iprange = &irange 54 return nil 55 } 56 57 var HOST_PROPS = []string{"name", "config.network", "vm"} 58 59 var VM_PROPS = []string{"name", "guest.net", "config.template", "summary.config.uuid", "summary.runtime.powerState"} 60 61 func (cli *SESXiClient) AllHostIP() (map[string]string, []mo.HostSystem, error) { 62 var hosts []mo.HostSystem 63 err := cli.scanAllMObjects(HOST_PROPS, &hosts) 64 if err != nil { 65 return nil, nil, errors.Wrap(err, "scanAllMObjects") 66 } 67 ret := make(map[string]string, len(hosts)) 68 for i := range hosts { 69 // find ip 70 host := &SHost{SManagedObject: newManagedObject(cli, &hosts[i], nil)} 71 ip := host.GetAccessIp() 72 ret[host.GetName()] = ip 73 } 74 return ret, hosts, nil 75 } 76 77 func (cli *SESXiClient) VMIP(host mo.HostSystem) (map[string][]string, error) { 78 var vms []mo.VirtualMachine 79 err := cli.references2Objects(host.Vm, VM_PROPS, &vms) 80 if err != nil { 81 return nil, errors.Wrap(err, "references2Objects") 82 } 83 ret := make(map[string][]string, len(vms)) 84 for i := range vms { 85 vm := vms[i] 86 if vm.Config == nil || vm.Config.Template { 87 continue 88 } 89 if vm.Guest == nil { 90 continue 91 } 92 guestIps := make([]string, 0) 93 for _, net := range vm.Guest.Net { 94 for _, ip := range net.IpAddress { 95 if regutils.MatchIP4Addr(ip) { 96 guestIps = append(guestIps, ip) 97 } 98 } 99 } 100 ret[vm.Name] = guestIps 101 } 102 return ret, nil 103 } 104 105 type SVMIPInfo struct { 106 Moid string 107 PowerState string 108 Name string 109 Uuid string 110 MacIps []SMacIps 111 } 112 113 type SMacIps struct { 114 Mac string 115 IPs []string 116 } 117 118 func (cli *SESXiClient) VMIP2() ([]SVMIPInfo, error) { 119 var vms []mo.VirtualMachine 120 err := cli.scanAllMObjects(VM_PROPS, &vms) 121 if err != nil { 122 return nil, errors.Wrap(err, "scanAllMObjects") 123 } 124 ret := make([]SVMIPInfo, 0, len(vms)) 125 for i := range vms { 126 vm := vms[i] 127 if vm.Config == nil || vm.Config.Template { 128 continue 129 } 130 if vm.Guest == nil { 131 continue 132 } 133 info := SVMIPInfo{ 134 Moid: vm.Reference().Value, 135 PowerState: string(vm.Summary.Runtime.PowerState), 136 Uuid: vm.Summary.Config.Uuid, 137 Name: vm.Name, 138 } 139 macIps := make([]SMacIps, 0, len(vm.Guest.Net)) 140 for _, net := range vm.Guest.Net { 141 if len(net.Network) == 0 { 142 continue 143 } 144 macip := SMacIps{ 145 Mac: net.MacAddress, 146 } 147 for _, ip := range net.IpAddress { 148 if regutils.MatchIP4Addr(ip) { 149 macip.IPs = append(macip.IPs, ip) 150 } 151 } 152 macIps = append(macIps, macip) 153 } 154 info.MacIps = macIps 155 ret = append(ret, info) 156 } 157 return ret, nil 158 }