yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/instance.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  	"context"
    19  	"fmt"
    20  	"strings"
    21  	"time"
    22  
    23  	"yunion.io/x/cloudmux/pkg/apis"
    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 SInstance struct {
    30  	BingoTags
    31  	multicloud.SInstanceBase
    32  	node *SNode
    33  
    34  	ReservationId string `json:"reservationId"`
    35  	OwnerId       string
    36  	GroupSet      []struct {
    37  		GroupId   string
    38  		GroupName string
    39  	}
    40  	InstancesSet struct {
    41  		InstanceId    string `json:"instanceId"`
    42  		InstanceName  string `json:"instanceName"`
    43  		HostName      string `json:"hostName"`
    44  		ImageId       string `json:"imageId"`
    45  		InstanceState struct {
    46  			Code            int    `json:"code"`
    47  			Name            string `json:"name"`
    48  			PendingProgress string `json:"pendingProgress"`
    49  		} `json:"instanceState"`
    50  		PrivateDNSName     string `json:"privateDnsName"`
    51  		DNSName            string `json:"dnsName"`
    52  		PrivateIPAddress   string `json:"privateIpAddress"`
    53  		PrivateIPAddresses string `json:"privateIpAddresses"`
    54  		IPAddress          string `json:"ipAddress"`
    55  		NifInfo            string `json:"nifInfo"`
    56  		KeyName            string `json:"keyName"`
    57  		AmiLaunchIndex     int    `json:"amiLaunchIndex"`
    58  		ProductCodesSet    []struct {
    59  			ProductCode string `json:"productCode"`
    60  		} `json:"productCodesSet"`
    61  		InstanceType   string    `json:"instanceType"`
    62  		VmtypeCPU      int       `json:"vmtype_cpu"`
    63  		VmtypeMem      int       `json:"vmtype_mem"`
    64  		VmtypeDisk     int       `json:"vmtype_disk"`
    65  		VmtypeGpu      int       `json:"vmtype_gpu"`
    66  		VmtypeSsd      int       `json:"vmtype_ssd"`
    67  		VmtypeHdd      int       `json:"vmtype_hdd"`
    68  		VmtypeHba      int       `json:"vmtype_hba"`
    69  		VmtypeSriov    int       `json:"vmtype_sriov"`
    70  		LaunchTime     time.Time `json:"launchTime"`
    71  		RootDeviceType string    `json:"rootDeviceType"`
    72  		HostAddress    string    `json:"hostAddress"`
    73  		Platform       string    `json:"platform"`
    74  		UseCompactMode bool      `json:"useCompactMode"`
    75  		ExtendDisk     bool      `json:"extendDisk"`
    76  		Placement      struct {
    77  			AvailabilityZone string `json:"availabilityZone"`
    78  		} `json:"placement"`
    79  		Namespace    string `json:"namespace"`
    80  		KernelId     string `json:"kernelId"`
    81  		RamdiskId    string `json:"ramdiskId"`
    82  		OperName     string `json:"operName"`
    83  		OperProgress int    `json:"operProgress"`
    84  		Features     string `json:"features"`
    85  		Monitoring   struct {
    86  			State string `json:"state"`
    87  		} `json:"monitoring"`
    88  		SubnetId              string    `json:"subnetId"`
    89  		VpcId                 string    `json:"vpcId"`
    90  		StorageId             string    `json:"storageId"`
    91  		DisableAPITermination bool      `json:"disableApiTermination"`
    92  		Vncdisabled           bool      `json:"vncdisabled"`
    93  		StartTime             time.Time `json:"startTime"`
    94  		CustomStatus          string    `json:"customStatus"`
    95  		SystemStatus          int       `json:"systemStatus"`
    96  		NetworkStatus         int       `json:"networkStatus"`
    97  		ScheduleTags          string    `json:"scheduleTags"`
    98  		StorageScheduleTags   string    `json:"storageScheduleTags"`
    99  		IsEncrypt             bool      `json:"isEncrypt"`
   100  		IsImported            bool      `json:"isImported"`
   101  		Ec2Version            string    `json:"ec2Version"`
   102  		Passphrase            string    `json:"passphrase"`
   103  		DrsEnabled            bool      `json:"drs_enabled"`
   104  		LaunchPriority        int       `json:"launchPriority"`
   105  		CPUPriority           int       `json:"cpuPriority"`
   106  		MemPriority           int       `json:"memPriority"`
   107  		CPUQuota              int       `json:"cpuQuota"`
   108  		AutoMigrate           bool      `json:"autoMigrate"`
   109  		DrMirrorId            string    `json:"drMirrorId"`
   110  		BlockDeviceMapping    []struct {
   111  			DeviceName string `json:"deviceName"`
   112  			Ebs        struct {
   113  				AttachTime          time.Time `json:"attachTime"`
   114  				DeleteOnTermination bool      `json:"deleteOnTermination"`
   115  				Status              string    `json:"status"`
   116  				VolumeId            string    `json:"volumeId"`
   117  				Size                int       `json:"size"`
   118  			} `json:"ebs"`
   119  		} `json:"blockDeviceMapping"`
   120  		EnableLiveScaleup bool   `json:"enableLiveScaleup"`
   121  		ImageBytes        int64  `json:"imageBytes"`
   122  		StatusReason      string `json:"statusReason"`
   123  		Hypervisor        string `json:"hypervisor"`
   124  		Bootloader        string `json:"bootloader"`
   125  		BmMachineId       string `json:"bmMachineId"`
   126  	}
   127  }
   128  
   129  func (self *SInstance) GetId() string {
   130  	return self.InstancesSet.InstanceId
   131  }
   132  
   133  func (self *SInstance) GetGlobalId() string {
   134  	return self.GetId()
   135  }
   136  
   137  func (self *SInstance) GetName() string {
   138  	if len(self.InstancesSet.InstanceName) > 0 {
   139  		return self.InstancesSet.InstanceName
   140  	}
   141  	return self.GetId()
   142  }
   143  
   144  func (self *SInstance) GetSecurityGroupIds() ([]string, error) {
   145  	ret := []string{}
   146  	for _, sec := range self.GroupSet {
   147  		ret = append(ret, sec.GroupId)
   148  	}
   149  	return ret, nil
   150  }
   151  
   152  func (self *SInstance) AssignSecurityGroup(secgroupId string) error {
   153  	return cloudprovider.ErrNotImplemented
   154  }
   155  
   156  func (self *SInstance) SetSecurityGroups(secgroupIds []string) error {
   157  	return cloudprovider.ErrNotImplemented
   158  }
   159  
   160  func (self *SInstance) AttachDisk(ctx context.Context, diskId string) error {
   161  	return cloudprovider.ErrNotImplemented
   162  }
   163  
   164  func (self *SInstance) DetachDisk(ctx context.Context, diskId string) error {
   165  	return cloudprovider.ErrNotImplemented
   166  }
   167  
   168  func (self *SInstance) ChangeConfig(ctx context.Context, config *cloudprovider.SManagedVMChangeConfig) error {
   169  	return cloudprovider.ErrNotImplemented
   170  }
   171  
   172  func (self *SInstance) DeployVM(ctx context.Context, name string, username string, password string, publicKey string, deleteKeypair bool, description string) error {
   173  	return cloudprovider.ErrNotImplemented
   174  }
   175  
   176  func (self *SInstance) DeleteVM(ctx context.Context) error {
   177  	return cloudprovider.ErrNotImplemented
   178  }
   179  
   180  func (self *SInstance) GetVcpuCount() int {
   181  	return self.InstancesSet.VmtypeCPU
   182  }
   183  
   184  func (self *SInstance) GetVmemSizeMB() int {
   185  	return self.InstancesSet.VmtypeMem
   186  }
   187  
   188  func (self *SInstance) GetBootOrder() string {
   189  	return "cdn"
   190  }
   191  
   192  func (self *SInstance) GetVga() string {
   193  	return ""
   194  }
   195  
   196  func (self *SInstance) GetVdi() string {
   197  	return ""
   198  }
   199  
   200  func (self *SInstance) GetOsArch() string {
   201  	return apis.OS_ARCH_X86_64
   202  }
   203  
   204  func (self *SInstance) GetOsType() cloudprovider.TOsType {
   205  	if self.InstancesSet.Platform == "linux" {
   206  		return cloudprovider.OsTypeLinux
   207  	}
   208  	return cloudprovider.OsTypeWindows
   209  }
   210  
   211  func (self *SInstance) GetFullOsName() string {
   212  	return ""
   213  }
   214  
   215  func (self *SInstance) GetBios() cloudprovider.TBiosType {
   216  	return cloudprovider.ToBiosType(self.InstancesSet.Bootloader)
   217  }
   218  
   219  func (i *SInstance) GetOsDist() string {
   220  	return ""
   221  }
   222  
   223  func (i *SInstance) GetOsVersion() string {
   224  	return ""
   225  }
   226  
   227  func (i *SInstance) GetOsLang() string {
   228  	return ""
   229  }
   230  
   231  func (self *SInstance) GetMachine() string {
   232  	return ""
   233  }
   234  
   235  func (self *SInstance) GetInstanceType() string {
   236  	return self.InstancesSet.InstanceType
   237  }
   238  
   239  func (self *SInstance) GetError() error {
   240  	return nil
   241  }
   242  
   243  func (self *SInstance) GetHostname() string {
   244  	return self.InstancesSet.HostName
   245  }
   246  
   247  func (self *SInstance) GetIHost() cloudprovider.ICloudHost {
   248  	return self.node
   249  }
   250  
   251  func (self *SInstance) GetIHostId() string {
   252  	info := strings.Split(self.InstancesSet.HostAddress, "@")
   253  	if len(info) == 2 {
   254  		return info[1]
   255  	}
   256  	return ""
   257  }
   258  
   259  func (self *SInstance) GetHypervisor() string {
   260  	return api.HYPERVISOR_BINGO_CLOUD
   261  }
   262  
   263  func (self *SInstance) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
   264  	storages, err := self.node.cluster.region.getStorages()
   265  	if err != nil {
   266  		return nil, err
   267  	}
   268  	storageMaps := map[string]SStorage{}
   269  	for i := range storages {
   270  		storageMaps[storages[i].StorageId] = storages[i]
   271  	}
   272  	ret := []cloudprovider.ICloudDisk{}
   273  	for _, _disk := range self.InstancesSet.BlockDeviceMapping {
   274  		disk, err := self.node.cluster.region.GetDisk(_disk.Ebs.VolumeId)
   275  		if err != nil {
   276  			return nil, err
   277  		}
   278  		storage, ok := storageMaps[disk.StorageId]
   279  		if ok {
   280  			storage.cluster = self.node.cluster
   281  			disk.storage = &storage
   282  			ret = append(ret, disk)
   283  		}
   284  	}
   285  	return ret, nil
   286  }
   287  
   288  func (self *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) {
   289  	nics, err := self.node.cluster.region.GetInstanceNics(self.InstancesSet.InstanceId)
   290  	if err != nil {
   291  		return nil, err
   292  	}
   293  	ret := []cloudprovider.ICloudNic{}
   294  	for i := range nics {
   295  		ret = append(ret, &nics[i])
   296  	}
   297  	return ret, nil
   298  }
   299  
   300  func (self *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) {
   301  	eips, _, err := self.node.cluster.region.GetEips("", self.InstancesSet.InstanceId, "")
   302  	if err != nil {
   303  		return nil, err
   304  	}
   305  	for i := range eips {
   306  		eips[i].region = self.node.cluster.region
   307  		return &eips[i], nil
   308  	}
   309  	return nil, nil
   310  }
   311  
   312  func (self *SInstance) GetProjectId() string {
   313  	return ""
   314  }
   315  
   316  func (self *SInstance) GetStatus() string {
   317  	switch self.InstancesSet.InstanceState.Name {
   318  	case "stopped":
   319  		return api.VM_READY
   320  	default:
   321  		return self.InstancesSet.InstanceState.Name
   322  	}
   323  }
   324  
   325  func (self *SInstance) GetVNCInfo(input *cloudprovider.ServerVncInput) (*cloudprovider.ServerVncOutput, error) {
   326  	return nil, cloudprovider.ErrNotImplemented
   327  }
   328  
   329  func (self *SInstance) RebuildRoot(ctx context.Context, config *cloudprovider.SManagedVMRebuildRootConfig) (string, error) {
   330  	return "", cloudprovider.ErrNotImplemented
   331  }
   332  
   333  func (self *SInstance) StartVM(ctx context.Context) error {
   334  	return cloudprovider.ErrNotImplemented
   335  }
   336  
   337  func (self *SInstance) StopVM(ctx context.Context, opts *cloudprovider.ServerStopOptions) error {
   338  	return cloudprovider.ErrNotImplemented
   339  }
   340  
   341  func (self *SInstance) UpdateUserData(userData string) error {
   342  	return cloudprovider.ErrNotImplemented
   343  }
   344  
   345  func (self *SInstance) UpdateVM(ctx context.Context, name string) error {
   346  	return cloudprovider.ErrNotImplemented
   347  }
   348  
   349  func (self *SRegion) GetInstances(id, nodeId string, maxResult int, nextToken string) ([]SInstance, string, error) {
   350  	params := map[string]string{}
   351  	if maxResult > 0 {
   352  		params["maxRecords"] = fmt.Sprintf("%d", maxResult)
   353  	}
   354  	if len(nextToken) > 0 {
   355  		params["nextToken"] = nextToken
   356  	}
   357  
   358  	idx := 1
   359  	if len(nodeId) > 0 {
   360  		params[fmt.Sprintf("Filter.%d.Name", idx)] = "node-id"
   361  		params[fmt.Sprintf("Filter.%d.Value.1", idx)] = nodeId
   362  		idx++
   363  	}
   364  
   365  	if len(id) > 0 {
   366  		params[fmt.Sprintf("Filter.%d.Name", idx)] = "instance-id"
   367  		params[fmt.Sprintf("Filter.%d.Value.1", idx)] = id
   368  		idx++
   369  	}
   370  
   371  	resp, err := self.invoke("DescribeInstances", params)
   372  	if err != nil {
   373  		return nil, "", err
   374  	}
   375  	result := struct {
   376  		NextToken      string
   377  		ReservationSet []SInstance
   378  	}{}
   379  	err = resp.Unmarshal(&result)
   380  	if err != nil {
   381  		return nil, "", err
   382  	}
   383  	return result.ReservationSet, result.NextToken, nil
   384  }