Files
cloudpods/pkg/util/openstack/zone.go
万垚奇 28c0452851 Merge pull request #984 in YUNIONIO/onecloud from ~WANYAOQI/onecloud:feature/host-baremetal-v2 to release/2.6.0
* commit '06446f778364e97cd2df66d8ed40f3c0c4d6574e': (135 commits)
  baremetal: ipmitool passowrd EscapeEchoString
  add hostmetrics
  add hostmetrics
  types struct 符合命名规范
  修正:centos 7宿主机格式化的ext4和xfs能够被centos 6虚拟机挂载
  add bsd raw socket code
  fix code
  fix code
  dep add gopsutil/mem
  fix dhcp server
  baremetal: quit signal handle
  baremetal: quit signal handle
  fix dhcp server
  gendoc
  remove jsonutils NewPair
  fix qmp montior cmd
  snapshot recycle, disk recycle, dhcp relay use raw socket
  fix code, test migrate snapshot, fix deploy
  add migrate presend arp
  add ethernet pkg
  ...
2019-01-28 20:04:24 +08:00

196 lines
4.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package openstack
import (
"fmt"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/util/version"
)
type ZoneState struct {
Available bool
}
const (
HYPERVISORS_VERSION = "2.28"
)
type SZone struct {
region *SRegion
iwires []cloudprovider.ICloudWire
istorages []cloudprovider.ICloudStorage
ZoneName string
ZoneState ZoneState
}
func (zone *SZone) GetMetadata() *jsonutils.JSONDict {
return nil
}
func (zone *SZone) GetId() string {
return zone.ZoneName
}
func (zone *SZone) GetName() string {
return fmt.Sprintf("%s %s", CLOUD_PROVIDER_OPENSTACK, zone.ZoneName)
}
func (zone *SZone) GetGlobalId() string {
return fmt.Sprintf("%s/%s", zone.region.GetGlobalId(), zone.ZoneName)
}
func (zone *SZone) IsEmulated() bool {
return false
}
func (zone *SZone) GetStatus() string {
if zone.ZoneState.Available {
return models.ZONE_ENABLE
}
return models.ZONE_SOLDOUT
}
func (zone *SZone) Refresh() error {
// do nothing
return nil
}
func (zone *SZone) GetIRegion() cloudprovider.ICloudRegion {
return zone.region
}
func (zone *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
return zone.iwires, nil
}
func (zone *SZone) getStorageByCategory(category string) (*SStorage, error) {
storages, err := zone.GetIStorages()
if err != nil {
return nil, err
}
for i := 0; i < len(storages); i++ {
storage := storages[i].(*SStorage)
if storage.Name == category {
return storage, nil
}
}
return nil, fmt.Errorf("No such storage %s", category)
}
func (zone *SZone) addWire(wire *SWire) {
if zone.iwires == nil {
zone.iwires = []cloudprovider.ICloudWire{}
}
zone.iwires = append(zone.iwires, wire)
}
func (zone *SZone) fetchStorages() error {
zone.istorages = []cloudprovider.ICloudStorage{}
for _, service := range []string{"volumev3", "volumev2", "volume"} {
_, resp, err := zone.region.Get(service, "/types", "", nil)
if err == nil {
storages := []SStorage{}
if err := resp.Unmarshal(&storages, "volume_types"); err != nil {
return err
}
for i := 0; i < len(storages); i++ {
storages[i].zone = zone
zone.istorages = append(zone.istorages, &storages[i])
}
return nil
}
log.Debugf("failed to get volume types by service %s error: %v, try another", service, err)
}
return fmt.Errorf("failed to find storage types by cinder service")
}
func (zone *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
if zone.istorages == nil {
zone.fetchStorages()
}
return zone.istorages, nil
}
func (zone *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
if zone.istorages == nil {
zone.fetchStorages()
}
for i := 0; i < len(zone.istorages); i++ {
if zone.istorages[i].GetGlobalId() == id {
return zone.istorages[i], nil
}
}
return nil, cloudprovider.ErrNotFound
}
type SOsHost struct {
Zone string
HostName string
Service string
}
func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
// 2.28 Hypervisor CPU字段是字符串会解析失败
ihosts := []cloudprovider.ICloudHost{}
hosts := []SHost{}
_, maxVersion, err := zone.region.GetVersion("compute")
if err == nil && version.GE(maxVersion, HYPERVISORS_VERSION) {
_, resp, err := zone.region.Get("compute", "/os-hypervisors/detail", maxVersion, nil)
if err != nil {
return nil, err
}
if err := resp.Unmarshal(&hosts, "hypervisors"); err != nil {
return nil, err
}
for i := 0; i < len(hosts); i++ {
hosts[i].zone = zone
ihosts = append(ihosts, &hosts[i])
}
return ihosts, nil
}
_, resp, err := zone.region.Get("compute", "/os-hosts", "", nil)
if err != nil {
return nil, err
}
_hosts := []SOsHost{}
if err := resp.Unmarshal(&_hosts, "hosts"); err != nil {
return nil, err
}
for i := 0; i < len(_hosts); i++ {
if _hosts[i].Zone == zone.ZoneName && _hosts[i].Service == "compute" {
host := SHost{HostName: _hosts[i].HostName, Zone: _hosts[i].Zone, zone: zone}
ihosts = append(ihosts, &host)
}
}
return ihosts, nil
}
func (zone *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
_, maxVersion, err := zone.region.GetVersion("compute")
host := &SHost{zone: zone}
if err == nil && version.GE(maxVersion, HYPERVISORS_VERSION) {
_, resp, err := zone.region.Get("compute", "/os-hypervisors/"+id, maxVersion, nil)
if err != nil {
return nil, err
}
return host, resp.Unmarshal(&host, "hypervisor")
}
host.HostName = id
host.Resource = []map[string]SResource{}
_, resp, err := zone.region.Get("compute", "/os-hosts/"+id, maxVersion, nil)
if err != nil {
return nil, err
}
return host, resp.Unmarshal(&(host.Resource), "host")
}