diff --git a/pkg/util/aws/aws.go b/pkg/util/aws/aws.go index 3bd755e2d9..61ef8c8faf 100644 --- a/pkg/util/aws/aws.go +++ b/pkg/util/aws/aws.go @@ -77,6 +77,8 @@ func (self *SAwsClient) fetchRegions() error { name := *region.RegionName endpoint := *region.Endpoint sregion := SRegion{client: self, RegionId: name, RegionEndpoint: endpoint} + // 初始化region client + sregion.getEc2Client() regions = append(regions, sregion) self.iregions = append(self.iregions, &sregion) } diff --git a/pkg/util/aws/eip.go b/pkg/util/aws/eip.go index f18769ef65..9381e3110f 100644 --- a/pkg/util/aws/eip.go +++ b/pkg/util/aws/eip.go @@ -93,7 +93,8 @@ func (self *SEipAddress) GetBandwidth() int { } func (self *SEipAddress) GetInternetChargeType() string { - panic("implement me") + // todo : implement me + return models.EIP_CHARGE_TYPE_BY_TRAFFIC } func (self *SEipAddress) GetManagerId() string { @@ -126,13 +127,13 @@ func (self *SEipAddress) ChangeBandwidth(bw int) error { return self.region.UpdateEipBandwidth(self.AllocationId, bw) } -func (region *SRegion) GetEips(eipId string) ([]SEipAddress, int, error) { +func (self *SRegion) GetEips(eipId string) ([]SEipAddress, int, error) { params := ec2.DescribeAddressesInput{} if len(eipId) > 0 { - params.AllocationIds = []*string{&eipId} + params.SetAllocationIds([]*string{&eipId}) } - res, err := region.ec2Client.DescribeAddresses(¶ms) + res, err := self.ec2Client.DescribeAddresses(¶ms) if err != nil { log.Errorf("DescribeEipAddresses fail %s", err) return nil, 0, err @@ -140,7 +141,7 @@ func (region *SRegion) GetEips(eipId string) ([]SEipAddress, int, error) { eips := make([]SEipAddress, 0) for _, ip := range res.Addresses { - eips = append(eips, SEipAddress{region: region, AllocationId: *ip.AllocationId, + eips = append(eips, SEipAddress{region: self, AllocationId: *ip.AllocationId, Tags: STags{}, InstanceId: *ip.InstanceId, AssociationId: *ip.AssociationId, @@ -151,7 +152,6 @@ func (region *SRegion) GetEips(eipId string) ([]SEipAddress, int, error) { IpAddress: *ip.PublicIp, }) } - return eips, len(eips), nil } diff --git a/pkg/util/aws/host.go b/pkg/util/aws/host.go index 08c7205914..fd31435e17 100644 --- a/pkg/util/aws/host.go +++ b/pkg/util/aws/host.go @@ -105,7 +105,7 @@ func (self *SHost) GetSysInfo() jsonutils.JSONObject { } func (self *SHost) GetSN() string { - panic("implement me") + return "" } func (self *SHost) GetCpuCount() int8 { diff --git a/pkg/util/aws/image.go b/pkg/util/aws/image.go index 5786f2204e..fe3ec74c6f 100644 --- a/pkg/util/aws/image.go +++ b/pkg/util/aws/image.go @@ -5,6 +5,8 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/compute/models" + "fmt" + "github.com/aws/aws-sdk-go/service/ec2" ) type ImageStatusType string @@ -18,10 +20,10 @@ const ( type ImageOwnerType string const ( - ImageOwnerSystem ImageOwnerType = "system" + ImageOwnerSystem ImageOwnerType = "amazon" ImageOwnerSelf ImageOwnerType = "self" - ImageOwnerOthers ImageOwnerType = "others" - ImageOwnerMarketplace ImageOwnerType = "marketplace" + ImageOwnerOthers ImageOwnerType = "microsoft" + ImageOwnerMarketplace ImageOwnerType = "aws-marketplace" ) type ImageImportTask struct { @@ -118,7 +120,14 @@ func (self *SRegion) ImportImage(name string, osArch string, osType string, osDi } func (self *SRegion) GetImage(imageId string) (*SImage, error) { - return nil, nil + images, _, err := self.GetImages("", ImageOwnerSelf, []string{imageId}, "", 0, 1) + if err != nil { + return nil, err + } + if len(images) == 0 { + return nil, fmt.Errorf("image %s not found", imageId) + } + return &images[0], nil } func (self *SRegion) GetImageByName(name string) (*SImage, error) { @@ -130,7 +139,50 @@ func (self *SRegion) GetImageStatus(imageId string) (ImageStatusType, error) { } func (self *SRegion) GetImages(status ImageStatusType, owner ImageOwnerType, imageId []string, name string, offset int, limit int) ([]SImage, int, error) { - return nil,0, nil + params := &ec2.DescribeImagesInput{} + filters := make([]*ec2.Filter, 0) + if len(status) > 0 { + filters = AppendSingleValueFilter(filters, "state", string(status)) + } + + if len(name) > 0 { + filters = AppendSingleValueFilter(filters, "name", name) + } + + if len(owner) > 0 { + own := string(owner) + params.SetOwners([]*string{&own}) + } + + if len(imageId) > 0 { + params.SetImageIds(ConvertedList(imageId)) + } + + ret, err := self.ec2Client.DescribeImages(params) + if err != nil { + return nil, 0, err + } + + images := make([]SImage, len(ret.Images)) + for _, image := range ret.Images { + images = append(images, SImage{ + storageCache: self.getStoragecache(), + Architecture: *image.Architecture, + Description: *image.Description, + ImageId: *image.ImageId, + ImageName: *image.ImageId, + // OSName: *image.Platform, + OSType: *image.ImageType, + IsSupportIoOptimized: *image.EnaSupport, + // Platform: *image.Platform, + Status: ImageStatusCreating, // *image.State, + // Usage: "", + // Size: ., + // CreationTime: *image.CreationDate, + }) + } + + return images, len(images), nil } func (self *SRegion) DeleteImage(imageId string) error { diff --git a/pkg/util/aws/instance.go b/pkg/util/aws/instance.go index c9730b8e32..164a84f1b6 100644 --- a/pkg/util/aws/instance.go +++ b/pkg/util/aws/instance.go @@ -161,7 +161,8 @@ func (self *SInstance) GetMetadata() *jsonutils.JSONDict { } func (self *SInstance) GetBillingType() string { - panic("implement me") + // todo: implement me + return models.BILLING_TYPE_POSTPAID } func (self *SInstance) GetExpiredAt() time.Time { @@ -372,7 +373,7 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit instances := make([]SInstance, 0) for _, reservation := range res.Reservations { for _, instance := range reservation.Instances { - instances = append(instances, SInstance{ + sinstance := SInstance{ RegionId: self.RegionId, ZoneId: *instance.Placement.AvailabilityZone, InstanceId: *instance.InstanceId, @@ -387,8 +388,6 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit // ExpiredTime: // ProductCodes: *instance.ProductCodes PublicDNSName: *instance.PublicDnsName, - InnerIpAddress: SIpAddress{[]string{*instance.PrivateIpAddress}}, - PublicIpAddress: SIpAddress{[]string{*instance.PublicIpAddress}}, RootDeviceName: *instance.RootDeviceName, Status: *instance.State.Name, // VlanId: @@ -400,7 +399,17 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit // OSName: // OSType: // Description: - }) + } + + if instance.PrivateIpAddress != nil { + sinstance.InnerIpAddress = SIpAddress{[]string{*instance.PrivateIpAddress}} + } + + if instance.PublicIpAddress != nil { + sinstance.PublicIpAddress = SIpAddress{[]string{*instance.PublicIpAddress}} + } + + instances = append(instances, sinstance) } } diff --git a/pkg/util/aws/network.go b/pkg/util/aws/network.go index 8d71678603..cf88091b10 100644 --- a/pkg/util/aws/network.go +++ b/pkg/util/aws/network.go @@ -112,7 +112,14 @@ func (self *SRegion) createNetwork(zoneId string, vpcId string, name string, cid } func (self *SRegion) getNetwork(networkId string) (*SNetwork, error) { - return nil, nil + networks, total, err := self.GetNetwroks([]string{networkId}, "") + if err != nil { + return nil, err + } + if total != 1 { + return nil, cloudprovider.ErrNotFound + } + return &networks[0], nil } func (self *SRegion) deleteNetwork(vswitchId string) error { @@ -138,13 +145,13 @@ func (self *SRegion) GetNetwroks(ids []string, vpcId string) ([]SNetwork, int, e params.SetFilters(filters) } - items, err := self.ec2Client.DescribeSubnets(params) + ret, err := self.ec2Client.DescribeSubnets(params) if err != nil { return nil, 0, err } - subnets := make([]SNetwork, len(items.Subnets)) - for _, item := range items.Subnets { + subnets := []SNetwork{} + for _, item := range ret.Subnets { subnet := SNetwork{} subnet.CidrBlock = *item.CidrBlock subnet.VpcId = *item.VpcId @@ -155,6 +162,5 @@ func (self *SRegion) GetNetwroks(ids []string, vpcId string) ([]SNetwork, int, e subnet.NetworkName = *item.SubnetId subnets = append(subnets, subnet) } - return subnets, len(subnets), nil } diff --git a/pkg/util/aws/region.go b/pkg/util/aws/region.go index 611e1b49eb..0d06e158ff 100644 --- a/pkg/util/aws/region.go +++ b/pkg/util/aws/region.go @@ -92,6 +92,10 @@ func (self *SRegion) fetchIVpcs() error { } func (self *SRegion) fetchInfrastructure() error { + if _, err := self.getEc2Client();err != nil { + return err + } + if err := self.fetchZones(); err != nil { return err } @@ -168,6 +172,11 @@ func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) { } func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) { + _, err := self.getEc2Client() + if err != nil { + return nil, err + } + eips, total, err := self.GetEips("") if err != nil { return nil, err diff --git a/pkg/util/aws/utils.go b/pkg/util/aws/utils.go index c9bc33d712..559db9e2c3 100644 --- a/pkg/util/aws/utils.go +++ b/pkg/util/aws/utils.go @@ -27,5 +27,16 @@ func ConvertedList(list []string) ([]*string) { result = append(result, &item) } + return result +} + +func ConvertedPointList(list []*string) ([]string) { + result := make([]string, len(list)) + for _, item := range list { + if item != nil { + result = append(result, *item) + } + } + return result } \ No newline at end of file diff --git a/pkg/util/aws/vpc.go b/pkg/util/aws/vpc.go index 67deadfeae..48debaa329 100644 --- a/pkg/util/aws/vpc.go +++ b/pkg/util/aws/vpc.go @@ -6,6 +6,7 @@ import ( "yunion.io/x/pkg/util/secrules" "strings" "yunion.io/x/log" + "github.com/aws/aws-sdk-go/service/ec2" ) type SUserCIDRs struct { @@ -167,13 +168,21 @@ func (self *SVpc) SyncSecurityGroup(secgroupId string, name string, rules []secr } func (self *SVpc) getWireByZoneId(zoneId string) *SWire { - for i := 0; i <= len(self.iwires); i += 1 { + for i := 0; i < len(self.iwires); i += 1 { wire := self.iwires[i].(*SWire) if wire.zone.ZoneId == zoneId { return wire } } - return nil + + zone, err := self.region.getZoneById(zoneId) + if err != nil { + return nil + } + return &SWire{ + zone: zone, + vpc: self, + } } func (self *SVpc) fetchNetworks() error { @@ -203,7 +212,15 @@ func (self *SVpc) fetchSecurityGroups() error { } func (self *SRegion) getVpc(vpcId string) (*SVpc, error) { - return nil, nil + vpcs, total, err := self.GetVpcs([]string{vpcId}, 0, 1) + if err != nil { + return nil, err + } + if total != 1 { + return nil, cloudprovider.ErrNotFound + } + vpcs[0].region = self + return &vpcs[0], nil } func (self *SRegion) revokeSecurityGroup(secgroupId, instanceId string, keep bool) error { @@ -220,4 +237,33 @@ func (self *SRegion) deleteSecurityGroup(secGrpId string) error { func (self *SRegion) DeleteVpc(vpcId string) error { return nil +} + +func (self *SRegion) GetVpcs(vpcId []string, offset int, limit int) ([]SVpc, int, error) { + params := &ec2.DescribeVpcsInput{} + if len(vpcId) > 0 { + params.SetVpcIds(ConvertedList(vpcId)) + } + + ret, err := self.ec2Client.DescribeVpcs(params) + if err != nil { + return nil, 0, err + } + + vpcs := make([]SVpc, len(ret.Vpcs)) + for _, item := range ret.Vpcs { + vpcs = append(vpcs, SVpc{ + region: self, + // secgroups: nil, + RegionId: self.RegionId, + VpcId: *item.VpcId, + VpcName: *item.VpcId, + CidrBlock: *item.CidrBlock, + IsDefault: *item.IsDefault, + Status: *item.State, + // Tags: *item.Tags, + }) + } + + return vpcs, len(vpcs), nil } \ No newline at end of file diff --git a/pkg/util/aws/zone.go b/pkg/util/aws/zone.go index 0faf16fc5d..dfc6551c83 100644 --- a/pkg/util/aws/zone.go +++ b/pkg/util/aws/zone.go @@ -121,4 +121,18 @@ func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, erro } } return nil, cloudprovider.ErrNotFound +} + +func (self *SRegion) getZoneById(id string) (*SZone, error) { + izones, err := self.GetIZones() + if err != nil { + return nil, err + } + for i := 0; i < len(izones); i += 1 { + zone := izones[i].(*SZone) + if zone.ZoneId == id { + return zone, nil + } + } + return nil, fmt.Errorf("no such zone %s", id) } \ No newline at end of file