yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/node.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 bingocloud
    16  
    17  import (
    18  	"strings"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SNode struct {
    29  	multicloud.SHostBase
    30  	multicloud.STagBase
    31  
    32  	cluster *SCluster
    33  
    34  	ClusterId    string
    35  	CpuCores     int
    36  	CpuMax       int
    37  	CpuModel     string
    38  	CpuNode      int
    39  	CpuSockets   int
    40  	CpuUsed      int
    41  	DiskMax      int
    42  	DiskNode     int
    43  	DiskUsed     int
    44  	MemNode      int
    45  	MemoryMax    int
    46  	MemoryUsed   int
    47  	NodeId       string
    48  	NodeName     string
    49  	ScheduleTags string
    50  	Status       string
    51  }
    52  
    53  func (self *SRegion) GetNodes(clusterId, nodeId string) ([]SNode, error) {
    54  	params := map[string]string{}
    55  	if len(clusterId) > 0 {
    56  		params["clusterId"] = clusterId
    57  	}
    58  	if len(clusterId) > 0 {
    59  		params["nodeId"] = nodeId
    60  	}
    61  	resp, err := self.invoke("DescribeNodes", params)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	ret := []SNode{}
    66  	return ret, resp.Unmarshal(&ret, "nodeSet")
    67  }
    68  
    69  func (self *SNode) GetId() string {
    70  	return self.NodeId
    71  }
    72  
    73  func (self *SNode) GetGlobalId() string {
    74  	return self.NodeId
    75  }
    76  
    77  func (self *SNode) GetName() string {
    78  	return self.NodeName
    79  }
    80  
    81  func (self *SNode) GetAccessIp() string {
    82  	info := strings.Split(self.NodeId, "@")
    83  	if len(info) == 2 {
    84  		return info[1]
    85  	}
    86  	return ""
    87  }
    88  
    89  func (self *SNode) GetAccessMac() string {
    90  	return ""
    91  }
    92  
    93  func (self *SNode) GetSysInfo() jsonutils.JSONObject {
    94  	return jsonutils.NewDict()
    95  }
    96  
    97  func (self *SNode) GetSN() string {
    98  	return ""
    99  }
   100  
   101  func (self *SNode) GetCpuCount() int {
   102  	return self.CpuNode
   103  }
   104  
   105  func (self *SNode) GetNodeCount() int8 {
   106  	return int8(self.CpuSockets)
   107  }
   108  
   109  func (self *SNode) GetCpuDesc() string {
   110  	return self.CpuModel
   111  }
   112  
   113  func (self *SNode) GetCpuMhz() int {
   114  	return 0
   115  }
   116  
   117  func (self *SNode) GetCpuCmtbound() float32 {
   118  	if self.CpuMax > self.CpuNode {
   119  		return float32(self.CpuMax) / float32(self.CpuNode)
   120  	}
   121  	return 1
   122  }
   123  
   124  func (self *SNode) GetMemSizeMB() int {
   125  	return self.MemNode
   126  }
   127  
   128  func (self *SNode) GetMemCmtbound() float32 {
   129  	if self.MemoryMax > self.MemNode {
   130  		return float32(self.MemoryMax) / float32(self.MemNode)
   131  	}
   132  	return 1
   133  }
   134  
   135  func (self *SNode) GetReservedMemoryMb() int {
   136  	return 0
   137  }
   138  
   139  func (self *SNode) GetStorageSizeMB() int {
   140  	if self.DiskMax > 0 {
   141  		return self.DiskMax * 1024
   142  	}
   143  	return self.DiskNode * 1024
   144  }
   145  
   146  func (self *SNode) GetStorageType() string {
   147  	return api.STORAGE_LOCAL_SSD
   148  }
   149  
   150  func (self *SNode) GetNodeType() string {
   151  	return api.HOST_TYPE_BINGO_CLOUD
   152  }
   153  
   154  func (self *SNode) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
   155  	return nil, cloudprovider.ErrNotImplemented
   156  }
   157  
   158  func (self *SNode) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   159  	return self.cluster.GetIStorages()
   160  }
   161  
   162  func (self *SNode) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   163  	return self.cluster.GetIStorageById(id)
   164  }
   165  
   166  func (self *SNode) GetEnabled() bool {
   167  	return true
   168  }
   169  
   170  func (self *SNode) GetNodeStatus() string {
   171  	if self.Status == "available" {
   172  		return api.HOST_ONLINE
   173  	}
   174  	return api.HOST_OFFLINE
   175  }
   176  
   177  func (self *SNode) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
   178  	return nil, cloudprovider.ErrNotImplemented
   179  }
   180  
   181  func (self *SNode) GetIVMs() ([]cloudprovider.ICloudVM, error) {
   182  	vms := []SInstance{}
   183  	part, nextToken, err := self.cluster.region.GetInstances("", self.NodeId, MAX_RESULT, "")
   184  	vms = append(vms, part...)
   185  	for len(nextToken) > 0 {
   186  		part, nextToken, err = self.cluster.region.GetInstances("", self.NodeId, MAX_RESULT, nextToken)
   187  		if err != nil {
   188  			return nil, err
   189  		}
   190  		vms = append(vms, part...)
   191  	}
   192  	ret := []cloudprovider.ICloudVM{}
   193  	for i := range vms {
   194  		vms[i].node = self
   195  		ret = append(ret, &vms[i])
   196  	}
   197  	return ret, nil
   198  }
   199  
   200  func (self *SNode) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
   201  	vms, _, err := self.cluster.region.GetInstances(id, self.NodeId, 1, "")
   202  	if err != nil {
   203  		return nil, err
   204  	}
   205  	for i := range vms {
   206  		if vms[i].GetGlobalId() == id {
   207  			vms[i].node = self
   208  			return &vms[i], nil
   209  		}
   210  	}
   211  	return nil, cloudprovider.ErrNotFound
   212  }
   213  
   214  func (self *SNode) GetIWires() ([]cloudprovider.ICloudWire, error) {
   215  	return nil, cloudprovider.ErrNotImplemented
   216  }
   217  
   218  func (self *SNode) GetSchedtags() ([]string, error) {
   219  	return []string{}, nil
   220  }
   221  
   222  func (self *SNode) GetIsMaintenance() bool {
   223  	return self.Status == "maintain"
   224  }
   225  
   226  func (self *SNode) GetVersion() string {
   227  	return ""
   228  }
   229  
   230  func (self *SNode) GetStatus() string {
   231  	return api.HOST_STATUS_RUNNING
   232  }
   233  
   234  func (self *SNode) GetHostStatus() string {
   235  	if self.Status != "available" {
   236  		return api.HOST_OFFLINE
   237  	}
   238  	return api.HOST_ONLINE
   239  }
   240  
   241  func (self *SNode) GetHostType() string {
   242  	return api.HOST_TYPE_BINGO_CLOUD
   243  }
   244  
   245  func (self *SCluster) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
   246  	nodes, err := self.region.GetNodes(self.ClusterId, id)
   247  	if err != nil {
   248  		return nil, err
   249  	}
   250  	for i := range nodes {
   251  		if nodes[i].GetGlobalId() == id {
   252  			nodes[i].cluster = self
   253  			return &nodes[i], nil
   254  		}
   255  	}
   256  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   257  }
   258  
   259  func (self *SCluster) GetIHosts() ([]cloudprovider.ICloudHost, error) {
   260  	nodes, err := self.region.GetNodes(self.ClusterId, "")
   261  	if err != nil {
   262  		return nil, err
   263  	}
   264  	ret := []cloudprovider.ICloudHost{}
   265  	for i := range nodes {
   266  		nodes[i].cluster = self
   267  		ret = append(ret, &nodes[i])
   268  	}
   269  	return ret, nil
   270  }