yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/remotefile/host.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 remotefile
    16  
    17  import (
    18  	"yunion.io/x/jsonutils"
    19  	"yunion.io/x/pkg/utils"
    20  
    21  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  	"yunion.io/x/cloudmux/pkg/multicloud"
    24  )
    25  
    26  type SHost struct {
    27  	SResourceBase
    28  	multicloud.SHostBase
    29  	zone *SZone
    30  
    31  	AccessIp      string
    32  	AccessMac     string
    33  	ZoneId        string
    34  	Enabled       bool
    35  	HostStatus    string
    36  	SN            string
    37  	CpuCount      int
    38  	NodeCount     int8
    39  	CpuDesc       string
    40  	CpuMbz        int
    41  	MemSizeMb     int
    42  	StorageSizeMb int
    43  	StorageType   string
    44  
    45  	AttachStorageTypes []string
    46  }
    47  
    48  func (self *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
    49  	return nil, cloudprovider.ErrNotSupported
    50  }
    51  
    52  func (self *SHost) GetEnabled() bool {
    53  	return self.Enabled
    54  }
    55  
    56  func (self *SHost) GetHostStatus() string {
    57  	if len(self.HostStatus) == 0 {
    58  		return api.HOST_ONLINE
    59  	}
    60  	return self.HostStatus
    61  }
    62  
    63  func (self *SHost) GetAccessIp() string {
    64  	return self.AccessIp
    65  }
    66  
    67  func (self *SHost) GetAccessMac() string {
    68  	return self.AccessMac
    69  }
    70  
    71  func (self *SHost) GetSysInfo() jsonutils.JSONObject {
    72  	return jsonutils.NewDict()
    73  }
    74  
    75  func (self *SHost) GetSN() string {
    76  	return self.SN
    77  }
    78  
    79  func (self *SHost) GetCpuCount() int {
    80  	return self.CpuCount
    81  }
    82  
    83  func (self *SHost) GetNodeCount() int8 {
    84  	return self.NodeCount
    85  }
    86  
    87  func (self *SHost) GetCpuDesc() string {
    88  	return self.CpuDesc
    89  }
    90  
    91  func (self *SHost) GetCpuMhz() int {
    92  	return self.CpuMbz
    93  }
    94  
    95  func (self *SHost) GetCpuCmtbound() float32 {
    96  	return 1
    97  }
    98  
    99  func (self *SHost) GetMemSizeMB() int {
   100  	return self.MemSizeMb
   101  }
   102  
   103  func (self *SHost) GetMemCmtbound() float32 {
   104  	return 1
   105  }
   106  
   107  func (self *SHost) GetReservedMemoryMb() int {
   108  	return 0
   109  }
   110  
   111  func (self *SHost) GetStorageSizeMB() int {
   112  	return self.StorageSizeMb
   113  }
   114  
   115  func (self *SHost) GetStorageType() string {
   116  	return self.StorageType
   117  }
   118  
   119  func (self *SHost) GetHostType() string {
   120  	return api.HOST_TYPE_REMOTEFILE
   121  }
   122  
   123  func (self *SHost) GetIsMaintenance() bool {
   124  	return false
   125  }
   126  
   127  func (self *SHost) GetVersion() string {
   128  	return ""
   129  }
   130  
   131  func (self *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
   132  	return nil, cloudprovider.ErrNotImplemented
   133  }
   134  
   135  func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   136  	storages, err := self.zone.GetIStorages()
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  	ret := []cloudprovider.ICloudStorage{}
   141  	for i := range storages {
   142  		if utils.IsInStringArray(storages[i].GetStorageType(), self.AttachStorageTypes) {
   143  			ret = append(ret, storages[i])
   144  		}
   145  	}
   146  	return ret, nil
   147  }
   148  
   149  func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   150  	storages, err := self.GetIStorages()
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  	for i := range storages {
   155  		if storages[i].GetGlobalId() == id {
   156  			return storages[i], nil
   157  		}
   158  	}
   159  	return nil, cloudprovider.ErrNotFound
   160  }
   161  
   162  func (self *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
   163  	vms, err := self.zone.region.client.GetInstances()
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  	ret := []cloudprovider.ICloudVM{}
   168  	for i := range vms {
   169  		if vms[i].HostId != self.GetId() {
   170  			continue
   171  		}
   172  		vms[i].host = self
   173  		ret = append(ret, &vms[i])
   174  	}
   175  	return ret, nil
   176  }
   177  
   178  func (self *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
   179  	vms, err := self.GetIVMs()
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  	for i := range vms {
   184  		if vms[i].GetGlobalId() == id {
   185  			return vms[i], nil
   186  		}
   187  	}
   188  	return nil, cloudprovider.ErrNotFound
   189  }
   190  
   191  func (self *SHost) GetIWires() ([]cloudprovider.ICloudWire, error) {
   192  	return nil, cloudprovider.ErrNotImplemented
   193  }