diff --git a/pkg/util/openstack/host.go b/pkg/util/openstack/host.go index df79baff87..b725e888d3 100644 --- a/pkg/util/openstack/host.go +++ b/pkg/util/openstack/host.go @@ -101,7 +101,7 @@ func (host *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, erro } func (host *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) { - instances, err := host.zone.region.GetInstances(host.zone.ZoneName, host.GetName()) + instances, err := host.zone.region.GetInstances(host.GetName()) if err != nil { return nil, err } diff --git a/pkg/util/openstack/instance.go b/pkg/util/openstack/instance.go index 77ae1ae31b..2ed3213075 100644 --- a/pkg/util/openstack/instance.go +++ b/pkg/util/openstack/instance.go @@ -125,9 +125,9 @@ func (region *SRegion) GetSecurityGroupsByInstance(instanceId string) ([]Securit return secgroups, resp.Unmarshal(&secgroups, "security_groups") } -func (region *SRegion) GetInstances(zoneName string, hostName string) ([]SInstance, error) { +func (region *SRegion) GetInstances(hostName string) ([]SInstance, error) { _, maxVersion, _ := region.GetVersion("compute") - _, resp, err := region.List("compute", "/servers/detail", maxVersion, nil) + _, resp, err := region.List("compute", "/servers/detail?all_tenants=True", maxVersion, nil) if err != nil { return nil, err } @@ -136,10 +136,8 @@ func (region *SRegion) GetInstances(zoneName string, hostName string) ([]SInstan return nil, err } for i := 0; i < len(instances); i++ { - if len(zoneName) == 0 || instances[i].AvailabilityZone == zoneName { - if len(hostName) == 0 || hostName == instances[i].Host { - result = append(result, instances[i]) - } + if len(hostName) == 0 || hostName == instances[i].Host { + result = append(result, instances[i]) } } return result, nil diff --git a/pkg/util/openstack/region.go b/pkg/util/openstack/region.go index cc194037aa..71aa555326 100644 --- a/pkg/util/openstack/region.go +++ b/pkg/util/openstack/region.go @@ -193,7 +193,8 @@ func (region *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) } func (region *SRegion) fetchZones() error { - _, resp, err := region.List("compute", "/os-availability-zone", "", jsonutils.NewDict()) + zone := &SZone{region: region, ZoneName: region.Name, cachedHosts: map[string][]string{}} + _, resp, err := region.List("compute", "/os-availability-zone/detail", "", jsonutils.NewDict()) if err != nil { return err } @@ -201,14 +202,20 @@ func (region *SRegion) fetchZones() error { if err := resp.Unmarshal(&zones, "availabilityZoneInfo"); err != nil { return err } - region.izones = []cloudprovider.ICloudZone{} for i := 0; i < len(zones); i++ { if zones[i].ZoneName == "internal" { continue } - zones[i].region = region - region.izones = append(region.izones, &zones[i]) + zone.cachedHosts[zones[i].ZoneName] = []string{} + for hostname, hostInfo := range zones[i].Hosts { + for k := range hostInfo { + if k == "nova-compute" { + zone.cachedHosts[zones[i].ZoneName] = append(zone.cachedHosts[zones[i].ZoneName], hostname) + } + } + } } + region.izones = []cloudprovider.ICloudZone{zone} return nil } diff --git a/pkg/util/openstack/shell/instance.go b/pkg/util/openstack/shell/instance.go index 3e5289cd35..bec9f2bf2d 100644 --- a/pkg/util/openstack/shell/instance.go +++ b/pkg/util/openstack/shell/instance.go @@ -9,11 +9,10 @@ import ( func init() { type InstanceListOptions struct { - ZoneID string `help:"Zone ID for filter instance list"` - Host string `help:"Host name for filter instance list"` + Host string `help:"Host name for filter instance list"` } shellutils.R(&InstanceListOptions{}, "instance-list", "List instances", func(cli *openstack.SRegion, args *InstanceListOptions) error { - instances, err := cli.GetInstances(args.ZoneID, args.Host) + instances, err := cli.GetInstances(args.Host) if err != nil { return err } diff --git a/pkg/util/openstack/zone.go b/pkg/util/openstack/zone.go index 07c2353887..8638c9616b 100644 --- a/pkg/util/openstack/zone.go +++ b/pkg/util/openstack/zone.go @@ -3,6 +3,7 @@ package openstack import ( "fmt" "strings" + "time" "yunion.io/x/jsonutils" "yunion.io/x/log" @@ -20,14 +21,23 @@ const ( HYPERVISORS_VERSION = "2.28" ) +type HostState struct { + Available bool + Active bool + UpdatedAt time.Time +} + type SZone struct { region *SRegion iwires []cloudprovider.ICloudWire istorages []cloudprovider.ICloudStorage - ZoneName string - ZoneState ZoneState + ZoneName string + + cachedHosts map[string][]string + + Hosts map[string]map[string]HostState } func (zone *SZone) GetMetadata() *jsonutils.JSONDict { @@ -47,14 +57,11 @@ func (zone *SZone) GetGlobalId() string { } func (zone *SZone) IsEmulated() bool { - return false + return true } func (zone *SZone) GetStatus() string { - if zone.ZoneState.Available { - return models.ZONE_ENABLE - } - return models.ZONE_SOLDOUT + return models.ZONE_ENABLE } func (zone *SZone) Refresh() error { @@ -151,6 +158,11 @@ func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) { return nil, err } for i := 0; i < len(hosts); i++ { + // 过滤vmware的机器 + hypervisor := strings.ToLower(hosts[i].HypervisorType) + if strings.Index(hypervisor, "vmware") != -1 { + continue + } hosts[i].zone = zone ihosts = append(ihosts, &hosts[i]) } @@ -168,7 +180,7 @@ func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) { return nil, err } for i := 0; i < len(_hosts); i++ { - if _hosts[i].Zone == zone.ZoneName && _hosts[i].Service == "compute" { + if _hosts[i].Service == "compute" { host := SHost{HostName: _hosts[i].HostName, Zone: _hosts[i].Zone, zone: zone} ihosts = append(ihosts, &host) }