diff --git a/pkg/multicloud/aliyun/dbinstance.go b/pkg/multicloud/aliyun/dbinstance.go index e2aa0d466c..c936495c02 100644 --- a/pkg/multicloud/aliyun/dbinstance.go +++ b/pkg/multicloud/aliyun/dbinstance.go @@ -851,18 +851,6 @@ func (region *SRegion) RenewDBInstance(instanceId string, bc billing.SBillingCyc return err } -func (rds *SDBInstance) GetTags() (map[string]string, error) { - tags, err := rds.region.ListTags(ALIYUN_SERVICE_RDS, "INSTANCE", rds.GetId()) - if err != nil { - return nil, errors.Wrap(err, `rds.region.ListTags`) - } - ret := map[string]string{} - for _, tag := range tags { - ret[tag.TagKey] = tag.TagValue - } - return ret, nil -} - func (rds *SDBInstance) SetTags(tags map[string]string, replace bool) error { return rds.region.SetResourceTags(ALIYUN_SERVICE_RDS, "INSTANCE", rds.GetId(), tags, replace) } diff --git a/pkg/multicloud/aliyun/disk.go b/pkg/multicloud/aliyun/disk.go index 42a31c1f79..a94c096f20 100644 --- a/pkg/multicloud/aliyun/disk.go +++ b/pkg/multicloud/aliyun/disk.go @@ -33,10 +33,6 @@ type SMountInstances struct { MountInstance []string } -type STags struct { - Tag []string -} - type SDisk struct { storage *SStorage multicloud.SDisk @@ -69,7 +65,6 @@ type SDisk struct { Size int SourceSnapshotId string Status string - Tags STags Type string ZoneId string } @@ -317,18 +312,19 @@ func (self *SRegion) resetDisk(diskId, snapshotId string) error { } func (self *SDisk) CreateISnapshot(ctx context.Context, name, desc string) (cloudprovider.ICloudSnapshot, error) { - if snapshotId, err := self.storage.zone.region.CreateSnapshot(self.DiskId, name, desc); err != nil { - log.Errorf("createSnapshot fail %s", err) - return nil, err - } else if snapshot, err := self.getSnapshot(snapshotId); err != nil { - return nil, err - } else { - snapshot.region = self.storage.zone.region - if err := cloudprovider.WaitStatus(snapshot, api.SNAPSHOT_READY, 15*time.Second, 3600*time.Second); err != nil { - return nil, err - } - return snapshot, nil + snapshotId, err := self.storage.zone.region.CreateSnapshot(self.DiskId, name, desc) + if err != nil { + return nil, errors.Wrapf(err, "CreateSnapshot") } + snapshot, err := self.storage.zone.region.GetISnapshotById(snapshotId) + if err != nil { + return nil, errors.Wrapf(err, "getSnapshot(%s)", snapshotId) + } + err = cloudprovider.WaitStatus(snapshot, api.SNAPSHOT_READY, 15*time.Second, 3600*time.Second) + if err != nil { + return nil, errors.Wrapf(err, "cloudprovider.WaitStatus") + } + return snapshot, nil } func (self *SRegion) CreateSnapshot(diskId, name, desc string) (string, error) { @@ -347,41 +343,25 @@ func (self *SRegion) CreateSnapshot(diskId, name, desc string) (string, error) { } func (self *SDisk) GetISnapshot(snapshotId string) (cloudprovider.ICloudSnapshot, error) { - if snapshot, err := self.getSnapshot(snapshotId); err != nil { - return nil, err - } else { - snapshot.region = self.storage.zone.region - return snapshot, nil - } -} - -func (self *SDisk) getSnapshot(snapshotId string) (*SSnapshot, error) { - if snapshots, total, err := self.storage.zone.region.GetSnapshots("", "", "", []string{snapshotId}, 0, 1); err != nil { - return nil, err - } else if total != 1 { - return nil, cloudprovider.ErrNotFound - } else { - return &snapshots[0], nil - } + return self.storage.zone.region.GetISnapshotById(snapshotId) } func (self *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) { snapshots := make([]SSnapshot, 0) for { - if parts, total, err := self.storage.zone.region.GetSnapshots("", self.DiskId, "", []string{}, 0, 20); err != nil { - log.Errorf("GetDisks fail %s", err) - return nil, err - } else { - snapshots = append(snapshots, parts...) - if len(snapshots) >= total { - break - } + parts, total, err := self.storage.zone.region.GetSnapshots("", self.DiskId, "", []string{}, 0, 20) + if err != nil { + return nil, errors.Wrapf(err, "GetSnapshots(%s)", self.DiskId) + } + snapshots = append(snapshots, parts...) + if len(snapshots) >= total { + break } } - isnapshots := make([]cloudprovider.ICloudSnapshot, len(snapshots)) + isnapshots := []cloudprovider.ICloudSnapshot{} for i := 0; i < len(snapshots); i++ { snapshots[i].region = self.storage.zone.region - isnapshots[i] = &snapshots[i] + isnapshots = append(isnapshots, &snapshots[i]) } return isnapshots, nil } diff --git a/pkg/multicloud/aliyun/elasticcache_instance.go b/pkg/multicloud/aliyun/elasticcache_instance.go index b79b84d406..8847f949e9 100644 --- a/pkg/multicloud/aliyun/elasticcache_instance.go +++ b/pkg/multicloud/aliyun/elasticcache_instance.go @@ -61,7 +61,6 @@ type SElasticcache struct { Bandwidth int64 `json:"Bandwidth"` ChargeType TChargeType `json:"ChargeType"` InstanceType string `json:"InstanceType"` - Tags Tags `json:"Tags"` InstanceStatus string `json:"InstanceStatus"` Port int `json:"Port"` InstanceClass string `json:"InstanceClass"` @@ -101,7 +100,6 @@ type SElasticcacheAttribute struct { ReplicationMode string `json:"ReplicationMode"` InstanceType string `json:"InstanceType"` InstanceStatus string `json:"InstanceStatus"` - Tags Tags `json:"Tags"` Port int64 `json:"Port"` InstanceClass string `json:"InstanceClass"` CreateTime time.Time `json:"CreateTime"` @@ -925,18 +923,6 @@ func (self *SElasticcache) GetICloudElasticcacheBackup(backupId string) (cloudpr return nil, cloudprovider.ErrNotFound } -func (instance *SElasticcache) GetTags() (map[string]string, error) { - tags, err := instance.region.ListTags(ALIYUN_SERVICE_KVS, "INSTANCE", instance.GetId()) - if err != nil { - return nil, errors.Wrap(err, "instance.region.ListTags") - } - ret := map[string]string{} - for _, tag := range tags { - ret[tag.TagKey] = tag.TagValue - } - return ret, nil -} - func (instance *SElasticcache) SetTags(tags map[string]string, replace bool) error { return instance.region.SetResourceTags(ALIYUN_SERVICE_KVS, "INSTANCE", instance.GetId(), tags, replace) } diff --git a/pkg/multicloud/aliyun/filesystem.go b/pkg/multicloud/aliyun/filesystem.go index 236862b8aa..ab3f117944 100644 --- a/pkg/multicloud/aliyun/filesystem.go +++ b/pkg/multicloud/aliyun/filesystem.go @@ -70,8 +70,6 @@ type SFileSystem struct { FileSystemType string FileSystemId string RegionId string - - Tags Tags } func (self *SFileSystem) GetId() string { @@ -167,10 +165,6 @@ func (self *SFileSystem) Refresh() error { return jsonutils.Update(self, fs) } -func (self *SFileSystem) GetTags() (map[string]string, error) { - return self.Tags.GetTags() -} - func (self *SRegion) GetFileSystems(id string, pageSize, pageNum int) ([]SFileSystem, int, error) { if pageSize < 1 || pageSize > 100 { pageSize = 50 diff --git a/pkg/multicloud/aliyun/instance.go b/pkg/multicloud/aliyun/instance.go index facbb9345f..a426e302ae 100644 --- a/pkg/multicloud/aliyun/instance.go +++ b/pkg/multicloud/aliyun/instance.go @@ -130,9 +130,6 @@ type SInstance struct { VlanId string VpcAttributes SVpcAttributes ZoneId string - - sys map[string]string - user map[string]string } // {"AutoReleaseTime":"","ClusterId":"","Cpu":1,"CreationTime":"2018-05-23T07:58Z","DedicatedHostAttribute":{"DedicatedHostId":"","DedicatedHostName":""},"Description":"","DeviceAvailable":true,"EipAddress":{"AllocationId":"","InternetChargeType":"","IpAddress":""},"ExpiredTime":"2018-05-30T16:00Z","GPUAmount":0,"GPUSpec":"","HostName":"iZ2ze57isp1ali72tzkjowZ","ImageId":"centos_7_04_64_20G_alibase_201701015.vhd","InnerIpAddress":{"IpAddress":[]},"InstanceChargeType":"PrePaid","InstanceId":"i-2ze57isp1ali72tzkjow","InstanceName":"gaoxianqi-test-7days","InstanceNetworkType":"vpc","InstanceType":"ecs.t5-lc2m1.nano","InstanceTypeFamily":"ecs.t5","InternetChargeType":"PayByBandwidth","InternetMaxBandwidthIn":-1,"InternetMaxBandwidthOut":0,"IoOptimized":true,"Memory":512,"NetworkInterfaces":{"NetworkInterface":[{"MacAddress":"00:16:3e:10:f0:c9","NetworkInterfaceId":"eni-2zecqsagtpztl6x5hu2r","PrimaryIpAddress":"192.168.220.214"}]},"OSName":"CentOS 7.4 64位","OSType":"linux","OperationLocks":{"LockReason":[]},"PublicIpAddress":{"IpAddress":[]},"Recyclable":false,"RegionId":"cn-beijing","ResourceGroupId":"","SaleCycle":"Week","SecurityGroupIds":{"SecurityGroupId":["sg-2zecqsagtpztl6x9zynl"]},"SerialNumber":"df05d9b4-df3d-4400-88d1-5f843f0dd088","SpotPriceLimit":0.000000,"SpotStrategy":"NoSpot","StartTime":"2018-05-23T07:58Z","Status":"Running","StoppedMode":"Not-applicable","VlanId":"","VpcAttributes":{"NatIpAddress":"","PrivateIpAddress":{"IpAddress":["192.168.220.214"]},"VSwitchId":"vsw-2ze9cqwza4upoyujq1thd","VpcId":"vpc-2zer4jy8ix3i8f0coc5uw"},"ZoneId":"cn-beijing-f"} @@ -183,14 +180,6 @@ func (self *SInstance) GetSecurityGroupIds() ([]string, error) { func (self *SInstance) GetSysTags() map[string]string { data := map[string]string{} - // The pricingInfo key structure is 'RegionId::InstanceType::NetworkType::OSType::IoOptimized' - optimized := "optimized" - if !self.IoOptimized { - optimized = "none" - } - priceKey := fmt.Sprintf("%s::%s::%s::%s::%s", self.RegionId, self.InstanceType, self.InstanceNetworkType, self.OSType, optimized) - data["price_key"] = priceKey - data["zone_ext_id"] = self.host.zone.GetGlobalId() if len(self.ImageId) > 0 { if image, err := self.host.zone.region.GetImage(self.ImageId); err != nil { log.Errorf("Failed to find image %s for instance %s", self.ImageId, self.GetName()) @@ -201,33 +190,13 @@ func (self *SInstance) GetSysTags() map[string]string { } } } - sys, _, _ := self.fetchTags() + sys := self.STags.GetSysTags() for k, v := range sys { data[k] = v } return data } -func (self *SInstance) GetTags() (map[string]string, error) { - _, tags, err := self.fetchTags() - if err != nil { - return nil, errors.Wrap(err, "self.fetchTags") - } - return tags, nil -} - -func (self *SInstance) fetchTags() (map[string]string, map[string]string, error) { - if self.sys != nil || self.user != nil { - return self.sys, self.user, nil - } - var err error - self.sys, self.user, err = self.host.zone.region.ListSysAndUserTags(ALIYUN_SERVICE_ECS, "instance", self.InstanceId) - if err != nil { - return nil, nil, errors.Wrapf(err, "ListSysAndUserTags") - } - return self.sys, self.user, nil -} - func (self *SInstance) GetIHost() cloudprovider.ICloudHost { return self.host } @@ -384,12 +353,11 @@ func (self *SInstance) GetStatus() string { } func (self *SInstance) Refresh() error { - self.sys, self.user = nil, nil - new, err := self.host.zone.region.GetInstance(self.InstanceId) + ins, err := self.host.zone.region.GetInstance(self.InstanceId) if err != nil { return err } - return jsonutils.Update(self, new) + return jsonutils.Update(self, ins) } /* diff --git a/pkg/multicloud/aliyun/loadbalancer.go b/pkg/multicloud/aliyun/loadbalancer.go index 5bca4d0939..e5a5d1bf8c 100644 --- a/pkg/multicloud/aliyun/loadbalancer.go +++ b/pkg/multicloud/aliyun/loadbalancer.go @@ -105,18 +105,6 @@ func (lb *SLoadbalancer) GetStatus() string { return api.LB_STATUS_DISABLED } -func (lb *SLoadbalancer) GetTags() (map[string]string, error) { - tags, err := lb.region.ListTags(ALIYUN_SERVICE_SLB, "instance", lb.GetId()) - if err != nil { - return nil, errors.Wrap(err, "lb.region.ListTags") - } - ret := map[string]string{} - for _, tag := range tags { - ret[tag.TagKey] = tag.TagValue - } - return ret, nil -} - func (lb *SLoadbalancer) GetAddress() string { return lb.Address } diff --git a/pkg/multicloud/aliyun/mount_target.go b/pkg/multicloud/aliyun/mount_target.go index 9a1408b78d..98ac893ced 100644 --- a/pkg/multicloud/aliyun/mount_target.go +++ b/pkg/multicloud/aliyun/mount_target.go @@ -44,7 +44,6 @@ type SMountTarget struct { MountTargetDomain string AccessGroup string ClientMasterNodes ClientMasterNodes - Tags Tags } func (self *SMountTarget) GetGlobalId() string { diff --git a/pkg/multicloud/aliyun/region.go b/pkg/multicloud/aliyun/region.go index 5b8f7fc6fe..dcdba34b47 100644 --- a/pkg/multicloud/aliyun/region.go +++ b/pkg/multicloud/aliyun/region.go @@ -451,7 +451,7 @@ func (self *SRegion) GetVpcs(vpcId []string, offset int, limit int) ([]SVpc, int params["VpcId"] = strings.Join(vpcId, ",") } - body, err := self.ecsRequest("DescribeVpcs", params) + body, err := self.vpcRequest("DescribeVpcs", params) if err != nil { log.Errorf("GetVpcs fail %s", err) return nil, 0, err diff --git a/pkg/multicloud/aliyun/securitygroup.go b/pkg/multicloud/aliyun/securitygroup.go index 04789974f6..18a20ce321 100644 --- a/pkg/multicloud/aliyun/securitygroup.go +++ b/pkg/multicloud/aliyun/securitygroup.go @@ -62,23 +62,6 @@ type SPermissions struct { Permission []SPermission } -type Tags struct { - Tag []Tag -} - -func (t Tags) GetTags() (map[string]string, error) { - ret := map[string]string{} - for _, tag := range t.Tag { - ret[tag.TagKey] = tag.TagValue - } - return ret, nil -} - -type Tag struct { - TagKey string - TagValue string -} - type SSecurityGroup struct { multicloud.SSecurityGroup @@ -91,21 +74,12 @@ type SSecurityGroup struct { InnerAccessPolicy string Permissions SPermissions RegionId string - Tags Tags } func (self *SSecurityGroup) GetVpcId() string { return self.VpcId } -func (self *SSecurityGroup) GetTags() (map[string]string, error) { - tags := map[string]string{} - for _, value := range self.Tags.Tag { - tags[value.TagKey] = value.TagValue - } - return tags, nil -} - func (self *SSecurityGroup) GetId() string { return self.SecurityGroupId } diff --git a/pkg/multicloud/azure/disk.go b/pkg/multicloud/azure/disk.go index 26fa9a23a8..7a15bf973b 100644 --- a/pkg/multicloud/azure/disk.go +++ b/pkg/multicloud/azure/disk.go @@ -168,6 +168,10 @@ func (self *SRegion) GetDisks() ([]SDisk, error) { return disks, nil } +func (self *SDisk) GetTags() (map[string]string, error) { + return self.Tags, nil +} + func (self *SDisk) GetStatus() string { status := self.Properties.ProvisioningState switch status { diff --git a/pkg/multicloud/azure/eip.go b/pkg/multicloud/azure/eip.go index 85965b866f..971d107667 100644 --- a/pkg/multicloud/azure/eip.go +++ b/pkg/multicloud/azure/eip.go @@ -64,6 +64,10 @@ type SEipAddress struct { Sku *PublicIPAddressSku } +func (self *SEipAddress) GetTags() (map[string]string, error) { + return self.Tags.GetTags() +} + func (self *SRegion) AllocateEIP(name, projectId string) (*SEipAddress, error) { params := map[string]interface{}{ "Location": self.Name, diff --git a/pkg/multicloud/azure/network.go b/pkg/multicloud/azure/network.go index ff55d988c9..985ff89645 100644 --- a/pkg/multicloud/azure/network.go +++ b/pkg/multicloud/azure/network.go @@ -35,6 +35,11 @@ type SNetwork struct { Name string Properties SubnetPropertiesFormat AddressPrefix string `json:"addressPrefix,omitempty"` + Tags TAzureTags +} + +func (self *SNetwork) GetTags() (map[string]string, error) { + return self.Tags, nil } func (self *SNetwork) GetId() string { diff --git a/pkg/multicloud/azure/storageaccount.go b/pkg/multicloud/azure/storageaccount.go index 0f77b357b4..74db7bb40f 100644 --- a/pkg/multicloud/azure/storageaccount.go +++ b/pkg/multicloud/azure/storageaccount.go @@ -94,9 +94,9 @@ type SStorageAccount struct { ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` Type string `json:"type,omitempty"` - Tags TAzureTags Properties AccountProperties `json:"properties"` + Tags TAzureTags } func (self *SRegion) listStorageAccounts() ([]SStorageAccount, error) { diff --git a/pkg/multicloud/azure/vpc.go b/pkg/multicloud/azure/vpc.go index 0c59cea8d5..86ce1bc182 100644 --- a/pkg/multicloud/azure/vpc.go +++ b/pkg/multicloud/azure/vpc.go @@ -55,6 +55,10 @@ type SVpc struct { Properties VirtualNetworkPropertiesFormat `json:"properties,omitempty"` } +func (self *SVpc) GetTags() (map[string]string, error) { + return self.Tags, nil +} + func (self *SVpc) GetId() string { return self.ID } diff --git a/pkg/multicloud/qcloud/elasticcache_instance.go b/pkg/multicloud/qcloud/elasticcache_instance.go index 923bcd27f7..ca65159467 100644 --- a/pkg/multicloud/qcloud/elasticcache_instance.go +++ b/pkg/multicloud/qcloud/elasticcache_instance.go @@ -35,7 +35,6 @@ type SElasticcache struct { InstanceID string `json:"InstanceId"` InstanceName string `json:"InstanceName"` InstanceNode []interface{} `json:"InstanceNode"` - InstanceTags []InstanceTag `json:"InstanceTags"` InstanceTitle string `json:"InstanceTitle"` OfflineTime string `json:"OfflineTime"` Port int `json:"Port"` @@ -53,7 +52,6 @@ type SElasticcache struct { Status int `json:"Status"` SubStatus int64 `json:"SubStatus"` SubnetID int64 `json:"SubnetId"` - Tags []interface{} `json:"Tags"` Type int `json:"Type"` UniqSubnetID string `json:"UniqSubnetId"` UniqVpcID string `json:"UniqVpcId"` @@ -66,11 +64,6 @@ func (self *SElasticcache) SetTags(tags map[string]string, replace bool) error { return self.region.SetResourceTags("redis", "instance", []string{self.InstanceID}, tags, replace) } -type InstanceTag struct { - TagKey string `json:"TagKey"` - TagValue string `json:"TagValue"` -} - type MaintenanceTime struct { StartTime string `json:"start_time"` EndTime string `json:"end_time"` @@ -241,17 +234,6 @@ func (self *SElasticcache) IsEmulated() bool { return false } -func (self *SElasticcache) GetTags() (map[string]string, error) { - tags, err := self.region.FetchResourceTags("redis", "instance", []string{self.GetId()}) - if err != nil { - return nil, errors.Wrap(err, "self.region.FetchResourceTags") - } - if _, ok := tags[self.GetId()]; !ok { - return map[string]string{}, nil - } - return *tags[self.GetId()], nil -} - func (self *SElasticcache) GetProjectId() string { return strconv.Itoa(self.ProjectID) } diff --git a/pkg/multicloud/resource_base.go b/pkg/multicloud/resource_base.go index d617762efb..6f9ea097f3 100644 --- a/pkg/multicloud/resource_base.go +++ b/pkg/multicloud/resource_base.go @@ -15,12 +15,80 @@ package multicloud import ( - "yunion.io/x/pkg/errors" + "strings" "yunion.io/x/onecloud/pkg/cloudprovider" ) -type SResourceBase struct{} +type STagSet struct { + TagSet []STag + //Redis + InstanceTags []STag +} + +func (self STagSet) GetTags() (map[string]string, error) { + ret := map[string]string{} + for _, tag := range self.TagSet { + ret[tag.Key] = tag.Value + } + for _, tag := range self.InstanceTags { + ret[tag.TagKey] = tag.TagValue + } + return ret, nil +} + +type STag struct { + TagKey string + TagValue string + + Key string + Value string +} + +type STags struct { + Tags struct { + Tag []STag + } +} + +func (self *STags) GetTags() (map[string]string, error) { + ret := map[string]string{} + for _, tag := range self.Tags.Tag { + if strings.HasPrefix(tag.TagKey, "aliyun") || strings.HasPrefix(tag.TagKey, "acs:") || + strings.HasSuffix(tag.Key, "aliyun") || strings.HasPrefix(tag.Key, "acs:") { + continue + } + if len(tag.TagKey) > 0 { + ret[tag.TagKey] = tag.TagValue + } else if len(tag.Key) > 0 { + ret[tag.Key] = tag.Value + } + } + return ret, nil +} + +func (self *STags) GetSysTags() map[string]string { + ret := map[string]string{} + for _, tag := range self.Tags.Tag { + if strings.HasPrefix(tag.TagKey, "aliyun") || strings.HasPrefix(tag.TagKey, "acs:") || + strings.HasPrefix(tag.Key, "aliyun") || strings.HasPrefix(tag.Key, "acs:") { + if len(tag.TagKey) > 0 { + ret[tag.TagKey] = tag.TagValue + } else if len(tag.Key) > 0 { + ret[tag.Key] = tag.Value + } + } + } + return ret +} + +type SResourceBase struct { + // Qcloud + STagSet + + // Aliyun + STags +} func (self *SResourceBase) IsEmulated() bool { return false @@ -35,7 +103,15 @@ func (self *SResourceBase) GetSysTags() map[string]string { } func (self *SResourceBase) GetTags() (map[string]string, error) { - return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "GetTags") + tags, _ := self.STagSet.GetTags() + if len(tags) > 0 { + return tags, nil + } + tags, _ = self.STags.GetTags() + if len(tags) > 0 { + return tags, nil + } + return map[string]string{}, nil } func (self *SResourceBase) SetTags(tags map[string]string, replace bool) error {