yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/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 jdcloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  	"yunion.io/x/pkg/util/sets"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type SHost struct {
    30  	multicloud.SHostBase
    31  	zone *SZone
    32  }
    33  
    34  func (h *SHost) GetId() string {
    35  	return fmt.Sprintf("%s-%s", h.zone.region.cpcfg.Id, h.zone.GetGlobalId())
    36  }
    37  
    38  func (h *SHost) GetName() string {
    39  	return fmt.Sprintf("%s-%s", h.zone.region.cpcfg.Name, h.zone.GetName())
    40  }
    41  
    42  func (h *SHost) GetGlobalId() string {
    43  	return h.GetId()
    44  }
    45  
    46  func (h *SHost) GetStatus() string {
    47  	return api.HOST_STATUS_RUNNING
    48  }
    49  
    50  func (h *SHost) Refresh() error {
    51  	return nil
    52  }
    53  
    54  func (h *SHost) IsEmulated() bool {
    55  	return true
    56  }
    57  
    58  func (h *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
    59  	vms := make([]SInstance, 0)
    60  	n := 1
    61  	for {
    62  		parts, total, err := h.zone.region.GetInstances(h.zone.ID, nil, n, 100)
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  		vms = append(vms, parts...)
    67  		if len(vms) >= total {
    68  			break
    69  		}
    70  		n++
    71  	}
    72  	// fill instanceType
    73  	instanceTypes := sets.NewString()
    74  	for i := range vms {
    75  		instanceTypes.Insert(vms[i].InstanceType)
    76  	}
    77  	its, err := h.zone.region.InstanceTypes(instanceTypes.UnsortedList()...)
    78  	if err != nil {
    79  		return nil, errors.Wrap(err, "unable to fetch instanceTypes")
    80  	}
    81  	itMap := make(map[string]*SInstanceType, len(its))
    82  	for i := range its {
    83  		itMap[its[i].InstanceType.InstanceType] = &its[i]
    84  	}
    85  	ivms := make([]cloudprovider.ICloudVM, len(vms))
    86  	for i := range vms {
    87  		vms[i].host = h
    88  		vms[i].instanceType = itMap[vms[i].InstanceType]
    89  		ivms[i] = &vms[i]
    90  	}
    91  	return ivms, nil
    92  }
    93  
    94  func (h *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
    95  	in, err := h.zone.region.GetInstanceById(id)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  	in.host = h
   100  	return in, nil
   101  }
   102  
   103  func (h *SHost) GetIWires() ([]cloudprovider.ICloudWire, error) {
   104  	return nil, nil
   105  }
   106  
   107  func (h *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   108  	return h.zone.GetIStorages()
   109  }
   110  
   111  func (h *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   112  	return h.zone.GetIStorageById(id)
   113  }
   114  
   115  func (h *SHost) GetEnabled() bool {
   116  	return true
   117  }
   118  
   119  func (h *SHost) GetHostStatus() string {
   120  	return api.HOST_ONLINE
   121  }
   122  
   123  func (h *SHost) GetAccessIp() string {
   124  	return ""
   125  }
   126  
   127  func (h *SHost) GetAccessMac() string {
   128  	return ""
   129  }
   130  
   131  func (h *SHost) GetSysInfo() jsonutils.JSONObject {
   132  	info := jsonutils.NewDict()
   133  	info.Add(jsonutils.NewString(api.CLOUD_PROVIDER_JDCLOUD), "manufacture")
   134  	return info
   135  }
   136  
   137  func (h *SHost) GetSN() string {
   138  	return ""
   139  }
   140  
   141  func (h *SHost) GetCpuCount() int {
   142  	return 0
   143  }
   144  
   145  func (h *SHost) GetNodeCount() int8 {
   146  	return 0
   147  }
   148  
   149  func (h *SHost) GetCpuDesc() string {
   150  	return ""
   151  }
   152  
   153  func (h *SHost) GetCpuMhz() int {
   154  	return 0
   155  }
   156  
   157  func (h *SHost) GetMemSizeMB() int {
   158  	return 0
   159  }
   160  
   161  func (h *SHost) GetStorageSizeMB() int {
   162  	return 0
   163  }
   164  
   165  func (h *SHost) GetStorageType() string {
   166  	return api.DISK_TYPE_HYBRID
   167  }
   168  
   169  func (h *SHost) GetHostType() string {
   170  	return api.HOST_TYPE_JDCLOUD
   171  }
   172  
   173  func (h *SHost) GetIsMaintenance() bool {
   174  	return false
   175  }
   176  
   177  func (h *SHost) GetVersion() string {
   178  	return ""
   179  }
   180  
   181  func (h *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
   182  	return nil, cloudprovider.ErrNotImplemented
   183  }
   184  
   185  func (h *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
   186  	return nil, cloudprovider.ErrNotSupported
   187  }