diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 38c125c7c8..9fb67d2e25 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -36,6 +36,7 @@ const ( HOST_TYPE_HYPERV = "hyperv" // # Microsoft Hyper-V HOST_TYPE_XEN = "xen" // # XenServer HOST_TYPE_ALIYUN = "aliyun" + HOST_TYPE_AWS = "aws" HOST_TYPE_AZURE = "azure" HOST_TYPE_DEFAULT = HOST_TYPE_HYPERVISOR diff --git a/pkg/util/aws/host.go b/pkg/util/aws/host.go index 13de075346..667e44c92e 100644 --- a/pkg/util/aws/host.go +++ b/pkg/util/aws/host.go @@ -3,6 +3,9 @@ package aws import ( "yunion.io/x/jsonutils" "yunion.io/x/onecloud/pkg/cloudprovider" + "fmt" + "yunion.io/x/onecloud/pkg/compute/models" + "github.com/aws/aws-sdk-go/service/ec2" ) type SHost struct { @@ -10,71 +13,95 @@ type SHost struct { } func (self *SHost) GetId() string { - panic("implement me") + return fmt.Sprintf("%s-%s", self.zone.region.client.providerId, self.zone.GetId()) } func (self *SHost) GetName() string { - panic("implement me") + return fmt.Sprintf("%s-%s", self.zone.region.client.providerName, self.zone.GetId()) } func (self *SHost) GetGlobalId() string { - panic("implement me") + return fmt.Sprintf("%s-%s", self.zone.region.client.providerId, self.zone.GetId()) } func (self *SHost) GetStatus() string { - panic("implement me") + return models.HOST_STATUS_RUNNING } func (self *SHost) Refresh() error { - panic("implement me") + return nil } func (self *SHost) IsEmulated() bool { - panic("implement me") + return true } func (self *SHost) GetMetadata() *jsonutils.JSONDict { - panic("implement me") + return nil } func (self *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) { - panic("implement me") + vms := make([]SInstance, 0) + vms, _, err := self.zone.region.GetInstances(self.zone.ZoneId, nil, len(vms), 50) + if err != nil { + return nil, err + } + + ivms := make([]cloudprovider.ICloudVM, len(vms)) + for i := 0; i < len(vms); i += 1 { + vms[i].host = self + ivms[i] = &vms[i] + } + return ivms, nil } -func (self *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) { - panic("implement me") +func (self *SHost) GetIVMById(gid string) (cloudprovider.ICloudVM, error) { + ivms, _, err := self.zone.region.GetInstances(self.zone.ZoneId, []string{gid}, 0, 1) + if err != nil { + return nil, err + } + if len(ivms) == 0 { + return nil, cloudprovider.ErrNotFound + } + if len(ivms) > 1 { + return nil, cloudprovider.ErrDuplicateId + } + ivms[0].host = self + return &ivms[0], nil } func (self *SHost) GetIWires() ([]cloudprovider.ICloudWire, error) { - panic("implement me") + return self.zone.GetIWires() } func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) { - panic("implement me") + return self.zone.GetIStorages() } func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) { - panic("implement me") + return self.zone.GetIStorageById(id) } func (self *SHost) GetEnabled() bool { - panic("implement me") + return true } func (self *SHost) GetHostStatus() string { - panic("implement me") + return models.HOST_ONLINE } func (self *SHost) GetAccessIp() string { - panic("implement me") + return "" } func (self *SHost) GetAccessMac() string { - panic("implement me") + return "" } func (self *SHost) GetSysInfo() jsonutils.JSONObject { - panic("implement me") + info := jsonutils.NewDict() + info.Add(jsonutils.NewString(CLOUD_PROVIDER_AWS), "manufacture") + return info } func (self *SHost) GetSN() string { @@ -82,42 +109,75 @@ func (self *SHost) GetSN() string { } func (self *SHost) GetCpuCount() int8 { - panic("implement me") + return 0 } func (self *SHost) GetNodeCount() int8 { - panic("implement me") + return 0 } func (self *SHost) GetCpuDesc() string { - panic("implement me") + return "" } func (self *SHost) GetCpuMhz() int { - panic("implement me") + return 0 } func (self *SHost) GetMemSizeMB() int { - panic("implement me") + return 0 } func (self *SHost) GetStorageSizeMB() int { - panic("implement me") + return 0 } func (self *SHost) GetStorageType() string { - panic("implement me") + return models.DISK_TYPE_HYBRID } func (self *SHost) GetHostType() string { - panic("implement me") + return models.HOST_TYPE_AWS } func (self *SHost) GetManagerId() string { - panic("implement me") + return self.zone.region.client.providerId +} + +func (self *SHost) GetInstanceById(instanceId string) (*SInstance, error) { + inst, err := self.zone.region.GetInstance(instanceId) + if err != nil { + return nil, err + } + inst.host = self + return inst, nil } func (self *SHost) CreateVM(name string, imgId string, sysDiskSize int, cpu int, memMB int, vswitchId string, ipAddr string, desc string, passwd string, storageType string, diskSizes []int, publicKey string, extSecGrpId string) (cloudprovider.ICloudVM, error) { - panic("implement me") + vmId, err := self._createVM(name, imgId, sysDiskSize, cpu, memMB, vswitchId, ipAddr, desc, passwd, storageType, diskSizes, publicKey, secgroupId) + if err != nil { + return nil, err + } + vm, err := self.GetInstanceById(vmId) + if err != nil { + return nil, err + } + + return vm, err } + +func (self *SHost) _createVM(name string, imgId string, sysDiskSize int, cpu int, memMB int, + vswitchId string, ipAddr string, desc string, passwd string, + storageType string, diskSizes []int, publicKey string, secgroupId string) (string, error) { + // 网络配置及安全组绑定 + + // 同步keypair + + // 镜像及硬盘配置 + + // 匹配实例类型 + + // 创建实例 + return "", fmt.Errorf("Failed to create, specification not supported") +} \ No newline at end of file diff --git a/pkg/util/aws/instance.go b/pkg/util/aws/instance.go index a1f9c0e3be..596bc57556 100644 --- a/pkg/util/aws/instance.go +++ b/pkg/util/aws/instance.go @@ -1 +1,302 @@ package aws + +import ( + "time" + "yunion.io/x/jsonutils" + "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/pkg/util/secrules" + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/coredns/coredns/plugin/pkg/log" +) + +const ( + // Running:运行中 + //Starting:启动中 + //Stopping:停止中 + //Stopped:已停止 + + InstanceStatusStopped = "Stopped" + InstanceStatusRunning = "Running" + InstanceStatusStopping = "Stopping" + InstanceStatusStarting = "Starting" +) + +type InstanceChargeType string + +type SDedicatedHostAttribute struct { + DedicatedHostId string + DedicatedHostName string +} + +type SIpAddress struct { + IpAddress []string +} + +type SNetworkInterfaces struct { + NetworkInterface []SNetworkInterface +} + +type SNetworkInterface struct { + MacAddress string + NetworkInterfaceId string + PrimaryIpAddress string +} + +type SOperationLocks struct { + LockReason []string +} + +type SSecurityGroupIds struct { + SecurityGroupId []string +} + +// {"NatIpAddress":"","PrivateIpAddress":{"IpAddress":["192.168.220.214"]},"VSwitchId":"vsw-2ze9cqwza4upoyujq1thd","VpcId":"vpc-2zer4jy8ix3i8f0coc5uw"} + +type SVpcAttributes struct { + NatIpAddress string + PrivateIpAddress SIpAddress + VSwitchId string + VpcId string +} + +type SInstance struct { + host *SHost + + // idisks []cloudprovider.ICloudDisk + + AutoReleaseTime string + ClusterId string + Cpu int8 + CreationTime time.Time + DedicatedHostAttribute SDedicatedHostAttribute + Description string + DeviceAvailable bool + EipAddress SEipAddress + ExpiredTime time.Time + GPUAmount int + GPUSpec string + HostName string + ImageId string + InnerIpAddress SIpAddress + InstanceChargeType InstanceChargeType + InstanceId string + InstanceName string + InstanceNetworkType string + InstanceType string + InstanceTypeFamily string + InternetChargeType string + InternetMaxBandwidthIn int + InternetMaxBandwidthOut int + IoOptimized bool + KeyPairName string + Memory int + NetworkInterfaces SNetworkInterfaces + OSName string + OSType string + OperationLocks SOperationLocks + PublicIpAddress SIpAddress + Recyclable bool + RegionId string + ResourceGroupId string + SaleCycle string + SecurityGroupIds SSecurityGroupIds + SerialNumber string + SpotPriceLimit string + SpotStrategy string + StartTime time.Time + Status string + StoppedMode string + VlanId string + VpcAttributes SVpcAttributes + ZoneId string +} + +func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit int) ([]SInstance, int, error) { + params := &ec2.DescribeInstancesInput{} + filters := make([]*ec2.Filter, 0) + if len(zoneId) > 0 { + name := "availability-zone" + filters = append(filters, &ec2.Filter{Name: &name, Values: []*string{&zoneId}}) + } + + if len(ids) > 0 { + _ids := make([]*string, len(ids)) + for _, id := range ids { + _ids = append(_ids, &id) + } + params = params.SetInstanceIds(_ids) + } + + params = params.SetFilters(filters) + res, err := self.ec2Client.DescribeInstances(params) + if err != nil { + log.Errorf("GetInstances fail %s", err) + return nil, 0, err + } + + instances := make([]SInstance, 0) + for _, reservation := range res.Reservations { + for _, instance := range reservation.Instances { + // todo :implement me later + instances = append(instances, SInstance{ + InstanceId: *instance.InstanceId, + ImageId: *instance.ImageId, + InnerIpAddress: SIpAddress{[]string{*instance.PrivateIpAddress}}, + PublicIpAddress: SIpAddress{[]string{*instance.PublicIpAddress}}, + }) + } + } + + return instances, len(instances), nil +} + +func (self *SRegion) GetInstance(instanceId string) (*SInstance, error) { + instances, _, err := self.GetInstances("", []string{instanceId}, 0, 1) + if err != nil { + return nil, err + } + if len(instances) == 0 { + return nil, cloudprovider.ErrNotFound + } + return &instances[0], nil +} + +func (self *SInstance) GetId() string { + panic("implement me") +} + +func (self *SInstance) GetName() string { + panic("implement me") +} + +func (self *SInstance) GetGlobalId() string { + panic("implement me") +} + +func (self *SInstance) GetStatus() string { + panic("implement me") +} + +func (self *SInstance) Refresh() error { + panic("implement me") +} + +func (self *SInstance) IsEmulated() bool { + panic("implement me") +} + +func (self *SInstance) GetMetadata() *jsonutils.JSONDict { + panic("implement me") +} + +func (self *SInstance) GetBillingType() string { + panic("implement me") +} + +func (self *SInstance) GetExpiredAt() time.Time { + panic("implement me") +} + +func (self *SInstance) GetCreateTime() time.Time { + panic("implement me") +} + +func (self *SInstance) GetIHost() cloudprovider.ICloudHost { + panic("implement me") +} + +func (self *SInstance) GetIDisks() ([]cloudprovider.ICloudDisk, error) { + panic("implement me") +} + +func (self *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) { + panic("implement me") +} + +func (self *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) { + panic("implement me") +} + +func (self *SInstance) GetVcpuCount() int8 { + panic("implement me") +} + +func (self *SInstance) GetVmemSizeMB() int { + panic("implement me") +} + +func (self *SInstance) GetBootOrder() string { + panic("implement me") +} + +func (self *SInstance) GetVga() string { + panic("implement me") +} + +func (self *SInstance) GetVdi() string { + panic("implement me") +} + +func (self *SInstance) GetOSType() string { + panic("implement me") +} + +func (self *SInstance) GetOSName() string { + panic("implement me") +} + +func (self *SInstance) GetBios() string { + panic("implement me") +} + +func (self *SInstance) GetMachine() string { + panic("implement me") +} + +func (self *SInstance) SyncSecurityGroup(secgroupId string, name string, rules []secrules.SecurityRule) error { + panic("implement me") +} + +func (self *SInstance) GetHypervisor() string { + panic("implement me") +} + +func (self *SInstance) StartVM() error { + panic("implement me") +} + +func (self *SInstance) StopVM(isForce bool) error { + panic("implement me") +} + +func (self *SInstance) DeleteVM() error { + panic("implement me") +} + +func (self *SInstance) UpdateVM(name string) error { + panic("implement me") +} + +func (self *SInstance) RebuildRoot(imageId string, passwd string, publicKey string, sysSizeGB int) (string, error) { + panic("implement me") +} + +func (self *SInstance) DeployVM(name string, password string, publicKey string, deleteKeypair bool, description string) error { + panic("implement me") +} + +func (self *SInstance) ChangeConfig(instanceId string, ncpu int, vmem int) error { + panic("implement me") +} + +func (self *SInstance) GetVNCInfo() (jsonutils.JSONObject, error) { + panic("implement me") +} + +func (self *SInstance) AttachDisk(diskId string) error { + panic("implement me") +} + +func (self *SInstance) DetachDisk(diskId string) error { + panic("implement me") +} diff --git a/pkg/util/aws/zone.go b/pkg/util/aws/zone.go index b805910cc7..a6da974304 100644 --- a/pkg/util/aws/zone.go +++ b/pkg/util/aws/zone.go @@ -38,6 +38,10 @@ func (self *SZone) fetchStorages() error { return nil } +func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) { + return self.iwires, nil +} + func (self *SZone) GetId() string { return self.ZoneId }