yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/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 google
    16  
    17  import (
    18  	"fmt"
    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 SHost struct {
    29  	multicloud.SHostBase
    30  
    31  	zone *SZone
    32  }
    33  
    34  func (host *SHost) GetId() string {
    35  	return host.zone.GetGlobalId()
    36  }
    37  
    38  func (host *SHost) GetGlobalId() string {
    39  	return host.GetId()
    40  }
    41  
    42  func (host *SHost) GetName() string {
    43  	return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Name, host.zone.GetName())
    44  }
    45  
    46  func (host *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
    47  	return host.zone.GetIStorages()
    48  }
    49  
    50  func (host *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
    51  	return host.zone.GetIStorageById(id)
    52  }
    53  
    54  func (host *SHost) IsEmulated() bool {
    55  	return true
    56  }
    57  
    58  func (host *SHost) GetStatus() string {
    59  	return api.HOST_STATUS_RUNNING
    60  }
    61  
    62  func (host *SHost) Refresh() error {
    63  	return nil
    64  }
    65  
    66  func (host *SHost) GetHostStatus() string {
    67  	return api.HOST_ONLINE
    68  }
    69  
    70  func (host *SHost) GetEnabled() bool {
    71  	return true
    72  }
    73  
    74  func (host *SHost) GetAccessIp() string {
    75  	return ""
    76  }
    77  
    78  func (host *SHost) GetAccessMac() string {
    79  	return ""
    80  }
    81  
    82  func (host *SHost) GetSysInfo() jsonutils.JSONObject {
    83  	info := jsonutils.NewDict()
    84  	info.Add(jsonutils.NewString(CLOUD_PROVIDER_GOOGLE), "manufacture")
    85  	return info
    86  }
    87  
    88  func (host *SHost) GetSN() string {
    89  	return ""
    90  }
    91  
    92  func (host *SHost) GetCpuCount() int {
    93  	return 0
    94  }
    95  
    96  func (host *SHost) GetNodeCount() int8 {
    97  	return 0
    98  }
    99  
   100  func (host *SHost) GetCpuDesc() string {
   101  	return ""
   102  }
   103  
   104  func (host *SHost) GetCpuMhz() int {
   105  	return 0
   106  }
   107  
   108  func (host *SHost) GetMemSizeMB() int {
   109  	return 0
   110  }
   111  
   112  func (host *SHost) GetStorageSizeMB() int {
   113  	return 0
   114  }
   115  
   116  func (host *SHost) GetStorageType() string {
   117  	return api.DISK_TYPE_HYBRID
   118  }
   119  
   120  func (host *SHost) GetHostType() string {
   121  	return api.HOST_TYPE_GOOGLE
   122  }
   123  
   124  func (host *SHost) GetWire() *SWire {
   125  	vpc := &SVpc{region: host.zone.region}
   126  	return &SWire{vpc: vpc}
   127  }
   128  
   129  func (host *SHost) GetIWires() ([]cloudprovider.ICloudWire, error) {
   130  	ivpcs, err := host.zone.region.GetIVpcs()
   131  	if err != nil {
   132  		return nil, errors.Wrap(err, "region.GetIVpcs")
   133  	}
   134  	iwires := []cloudprovider.ICloudWire{}
   135  	for i := range ivpcs {
   136  		_iwires, err := ivpcs[i].GetIWires()
   137  		if err != nil {
   138  			return nil, errors.Wrap(err, "ivpcs[i].GetIWires")
   139  		}
   140  		iwires = append(iwires, _iwires...)
   141  	}
   142  	return iwires, nil
   143  }
   144  
   145  func (host *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
   146  	instances, err := host.zone.region.GetInstances(host.zone.Name, 0, "")
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  	iVMs := []cloudprovider.ICloudVM{}
   151  	for i := range instances {
   152  		instances[i].host = host
   153  		iVMs = append(iVMs, &instances[i])
   154  	}
   155  	return iVMs, nil
   156  }
   157  
   158  func (host *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
   159  	instance, err := host.zone.region.GetInstance(id)
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  	if instance.Zone != host.zone.SelfLink {
   164  		return nil, cloudprovider.ErrNotFound
   165  	}
   166  	instance.host = host
   167  	return instance, nil
   168  }
   169  
   170  func (host *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
   171  	instance, err := host.zone.region._createVM(host.zone.Name, desc)
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  	instance.host = host
   176  	return instance, nil
   177  }
   178  
   179  func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
   180  	return nil, cloudprovider.ErrNotSupported
   181  }
   182  
   183  func (host *SHost) GetIsMaintenance() bool {
   184  	return false
   185  }
   186  
   187  func (host *SHost) GetVersion() string {
   188  	return GOOGLE_API_VERSION
   189  }