fix(region): aws vpc network eip disk tags sync

This commit is contained in:
Qu Xuan
2021-05-25 17:43:11 +08:00
parent 0333d41fb8
commit 920369021d
14 changed files with 88 additions and 60 deletions
+2
View File
@@ -502,6 +502,7 @@ func (self *SElasticip) SyncWithCloudEip(ctx context.Context, userCred mcclient.
if err != nil {
return errors.Wrap(err, "fail to sync associated instance of EIP")
}
syncVirtualResourceMetadata(ctx, userCred, self, ext)
SyncCloudProject(userCred, self, syncOwnerId, ext, self.ManagerId)
return nil
@@ -555,6 +556,7 @@ func (manager *SElasticipManager) newFromCloudEip(ctx context.Context, userCred
return nil, errors.Wrapf(err, "newFromCloudEip")
}
syncVirtualResourceMetadata(ctx, userCred, &eip, extEip)
SyncCloudProject(userCred, &eip, syncOwnerId, extEip, eip.ManagerId)
err = eip.SyncInstanceWithCloudEip(ctx, userCred, extEip)
+2
View File
@@ -763,6 +763,7 @@ func (self *SNetwork) SyncWithCloudNetwork(ctx context.Context, userCred mcclien
}
db.OpsLog.LogSyncUpdate(self, diff, userCred)
syncVirtualResourceMetadata(ctx, userCred, self, extNet)
SyncCloudProject(userCred, self, syncOwnerId, extNet, vpc.ManagerId)
if provider != nil {
@@ -817,6 +818,7 @@ func (manager *SNetworkManager) newFromCloudNetwork(ctx context.Context, userCre
}
vpc := wire.GetVpc()
syncVirtualResourceMetadata(ctx, userCred, &net, extNet)
SyncCloudProject(userCred, &net, syncOwnerId, extNet, vpc.ManagerId)
if provider != nil {
+4
View File
@@ -890,6 +890,8 @@ func (self *SSnapshot) SyncWithCloudSnapshot(ctx context.Context, userCred mccli
}
db.OpsLog.LogSyncUpdate(self, diff, userCred)
syncVirtualResourceMetadata(ctx, userCred, self, ext)
// bugfix for now:
disk, err := self.GetDisk()
if err != nil && err != sql.ErrNoRows {
@@ -943,6 +945,8 @@ func (manager *SSnapshotManager) newFromCloudSnapshot(ctx context.Context, userC
return nil, errors.Wrapf(err, "Insert")
}
syncVirtualResourceMetadata(ctx, userCred, &snapshot, extSnapshot)
// bugfix for now:
if localDisk != nil {
snapshot.SyncCloudProjectId(userCred, localDisk.GetOwnerId())
+3
View File
@@ -616,6 +616,8 @@ func (self *SVpc) SyncWithCloudVpc(ctx context.Context, userCred mcclient.TokenC
return err
}
syncMetadata(ctx, userCred, self, extVPC)
if provider != nil {
SyncCloudDomain(userCred, self, provider.GetOwnerId())
self.SyncShareState(ctx, userCred, provider.getAccountShareInfo())
@@ -656,6 +658,7 @@ func (manager *SVpcManager) newFromCloudVpc(ctx context.Context, userCred mcclie
return nil, errors.Wrapf(err, "Insert")
}
syncMetadata(ctx, userCred, &vpc, extVPC)
SyncCloudDomain(userCred, &vpc, provider.GetOwnerId())
if provider != nil {
+4
View File
@@ -87,6 +87,10 @@ func (self *SDisk) GetId() string {
return self.DiskId
}
func (self *SDisk) GetTags() (map[string]string, error) {
return self.Tags.GetTags()
}
func (self *SDisk) GetName() string {
if len(self.DiskName) > 0 {
return self.DiskName
+4
View File
@@ -65,6 +65,10 @@ func (self *SEipAddress) GetName() string {
return self.Name
}
func (self *SEipAddress) GetTags() (map[string]string, error) {
return self.Tags.GetTags()
}
func (self *SEipAddress) GetGlobalId() string {
return self.AllocationId
}
+5 -16
View File
@@ -98,6 +98,8 @@ type SInstance struct {
OSType string
Description string
TagSpec TagSpec
// 这些貌似都没啥用
// AutoReleaseTime string
// DeviceAvailable bool
@@ -236,22 +238,7 @@ func (self *SInstance) GetSysTags() map[string]string {
}
func (self *SInstance) GetTags() (map[string]string, error) {
ec2Client, err := self.host.zone.region.getEc2Client()
if err != nil {
return nil, errors.Wrap(err, "getEc2Client")
}
tags, err := FetchTags(ec2Client, self.InstanceId)
if err != nil {
return nil, errors.Wrap(err, "FetchTags()")
}
data := map[string]string{}
err = tags.Unmarshal(&data)
if err != nil {
return nil, errors.Wrap(err, "tags.Unmarshal")
}
delete(data, "Name")
delete(data, "Description")
return data, nil
return self.TagSpec.GetTags()
}
func (self *SInstance) GetBillingType() string {
@@ -726,6 +713,8 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit
ProductCodes: productCodes,
OSName: osType, // todo: 这里在model层回写OSName信息
OSType: osType,
TagSpec: tagspec,
// ExpiredTime:
// VlanId:
// OSType:
+13 -8
View File
@@ -47,6 +47,7 @@ type SNetwork struct {
NetworkName string
VpcId string
ZoneId string
TagSpec TagSpec
}
func (self *SNetwork) GetId() string {
@@ -61,6 +62,10 @@ func (self *SNetwork) GetName() string {
return self.NetworkName
}
func (self *SNetwork) GetTags() (map[string]string, error) {
return self.TagSpec.GetTags()
}
func (self *SNetwork) GetGlobalId() string {
return self.NetworkId
}
@@ -193,12 +198,11 @@ func (self *SRegion) getNetwork(networkId string) (*SNetwork, error) {
if len(networkId) == 0 {
return nil, fmt.Errorf("GetNetwork networkId should not be empty.")
}
networks, total, err := self.GetNetwroks([]string{networkId}, "", 0, 0)
networks, err := self.GetNetwroks([]string{networkId}, "")
if err != nil {
log.Errorf("GetNetwroks %s: %s", networkId, err)
return nil, errors.Wrap(err, "GetNetwroks")
}
if total != 1 {
if len(networks) != 1 {
return nil, errors.Wrap(cloudprovider.ErrNotFound, "getNetwork")
}
return &networks[0], nil
@@ -215,7 +219,7 @@ func (self *SRegion) deleteNetwork(networkId string) error {
return errors.Wrap(err, "DeleteSubnet")
}
func (self *SRegion) GetNetwroks(ids []string, vpcId string, limit int, offset int) ([]SNetwork, int, error) {
func (self *SRegion) GetNetwroks(ids []string, vpcId string) ([]SNetwork, error) {
params := &ec2.DescribeSubnetsInput{}
if len(ids) > 0 {
_ids := make([]*string, len(ids))
@@ -236,20 +240,20 @@ func (self *SRegion) GetNetwroks(ids []string, vpcId string, limit int, offset i
ec2Client, err := self.getEc2Client()
if err != nil {
return nil, 0, errors.Wrap(err, "getEc2Client")
return nil, errors.Wrap(err, "getEc2Client")
}
ret, err := ec2Client.DescribeSubnets(params)
err = parseNotFoundError(err)
if err != nil {
return nil, 0, err
return nil, err
}
subnets := []SNetwork{}
for i := range ret.Subnets {
item := ret.Subnets[i]
if err := FillZero(item); err != nil {
return nil, 0, err
return nil, err
}
tagspec := TagSpec{ResourceType: "subnet"}
@@ -263,9 +267,10 @@ func (self *SRegion) GetNetwroks(ids []string, vpcId string, limit int, offset i
subnet.IsDefault = *item.DefaultForAz
subnet.NetworkId = *item.SubnetId
subnet.NetworkName = tagspec.GetNameTag()
subnet.TagSpec = tagspec
subnets = append(subnets, subnet)
}
return subnets, len(subnets), nil
return subnets, nil
}
func (self *SNetwork) GetProjectId() string {
+4 -6
View File
@@ -419,20 +419,18 @@ func (self *SRegion) fetchIVpcs() error {
}
}
tags := make(map[string]string, 0)
for _, tag := range vpc.Tags {
tags[*tag.Key] = *tag.Value
}
tagspec := TagSpec{ResourceType: "vpc"}
tagspec.LoadingEc2Tags(vpc.Tags)
self.ivpcs = append(self.ivpcs, &SVpc{region: self,
CidrBlock: *vpc.CidrBlock,
CidrBlockAssociationSet: cidrBlockAssociationSet,
Tags: tags,
TagSpec: tagspec,
IsDefault: *vpc.IsDefault,
RegionId: self.RegionId,
Status: *vpc.State,
VpcId: *vpc.VpcId,
VpcName: tags["Name"],
VpcName: tagspec.GetNameTag(),
InstanceTenancy: *vpc.InstanceTenancy,
})
}
+7 -7
View File
@@ -21,15 +21,15 @@ import (
func init() {
type VSwitchListOptions struct {
Limit int `help:"page size"`
Offset int `help:"page offset"`
Ids []string
VpcId string
}
shellutils.R(&VSwitchListOptions{}, "vswitch-list", "List vswitches", func(cli *aws.SRegion, args *VSwitchListOptions) error {
vswitches, total, e := cli.GetNetwroks(nil, "", args.Offset, args.Limit)
if e != nil {
return e
shellutils.R(&VSwitchListOptions{}, "network-list", "List vswitches", func(cli *aws.SRegion, args *VSwitchListOptions) error {
networks, err := cli.GetNetwroks(args.Ids, args.VpcId)
if err != nil {
return err
}
printList(vswitches, total, args.Offset, args.Limit, []string{})
printList(networks, 0, 0, 0, []string{})
return nil
})
}
+5 -6
View File
@@ -22,15 +22,14 @@ import (
func init() {
type VpcListOptions struct {
Limit int `help:"page size"`
Offset int `help:"page offset"`
VpcIds []string
}
shellutils.R(&VpcListOptions{}, "vpc-list", "List vpcs", func(cli *aws.SRegion, args *VpcListOptions) error {
vpcs, total, e := cli.GetVpcs(nil, args.Offset, args.Limit)
if e != nil {
return e
vpcs, err := cli.GetVpcs(args.VpcIds)
if err != nil {
return err
}
printList(vpcs, total, args.Offset, args.Limit, []string{})
printList(vpcs, 0, 0, 0, []string{})
return nil
})
+6
View File
@@ -49,6 +49,7 @@ type SSnapshot struct {
SourceDiskType string
Status SnapshotStatusType
Usage string
TagSpec TagSpec
}
func (self *SSnapshot) GetDiskType() string {
@@ -60,6 +61,10 @@ func (self *SSnapshot) GetId() string {
return self.SnapshotId
}
func (self *SSnapshot) GetTags() (map[string]string, error) {
return self.TagSpec.GetTags()
}
func (self *SSnapshot) GetName() string {
if len(self.SnapshotName) == 0 {
return self.SnapshotId
@@ -169,6 +174,7 @@ func (self *SRegion) GetSnapshots(instanceId string, diskId string, snapshotName
snapshot.SourceDiskSize = int32(*item.VolumeSize)
// snapshot.SourceDiskType
snapshot.SnapshotName = tagspec.GetNameTag()
snapshot.TagSpec = tagspec
snapshots = append(snapshots, snapshot)
}
+11
View File
@@ -78,6 +78,17 @@ func (self *TagSpec) SetTag(k, v string) {
self.Tags[k] = v
}
func (self *TagSpec) GetTags() (map[string]string, error) {
ret := map[string]string{}
for k, v := range self.Tags {
if k == "Name" || k == "Description" {
continue
}
ret[k] = v
}
return ret, nil
}
func (self *TagSpec) SetNameTag(v string) {
self.SetTag("Name", v)
}
+18 -17
View File
@@ -50,7 +50,7 @@ type SVpc struct {
IsDefault bool
Status string
InstanceTenancy string
Tags map[string]string // 名称、描述等
TagSpec TagSpec
}
func (self *SVpc) addWire(wire *SWire) {
@@ -64,6 +64,10 @@ func (self *SVpc) GetId() string {
return self.VpcId
}
func (self *SVpc) GetTags() (map[string]string, error) {
return self.TagSpec.GetTags()
}
func (self *SVpc) GetName() string {
if len(self.VpcName) > 0 {
return self.VpcName
@@ -222,9 +226,9 @@ func (self *SVpc) getWireByZoneId(zoneId string) *SWire {
}
func (self *SVpc) fetchNetworks() error {
networks, _, err := self.region.GetNetwroks(nil, self.VpcId, 0, 0)
networks, err := self.region.GetNetwroks(nil, self.VpcId)
if err != nil {
return err
return errors.Wrapf(err, "GetNetwroks(%s)", self.VpcId)
}
for i := 0; i < len(networks); i += 1 {
@@ -567,12 +571,11 @@ func (self *SRegion) getVpc(vpcId string) (*SVpc, error) {
return nil, fmt.Errorf("GetVpc vpc id should not be empty.")
}
vpcs, total, err := self.GetVpcs([]string{vpcId}, 0, 1)
vpcs, err := self.GetVpcs([]string{vpcId})
if err != nil {
log.Errorf("GetVpcs %s: %s", vpcId, err)
return nil, errors.Wrap(err, "GetVpcs")
}
if total != 1 {
if len(vpcs) != 1 {
return nil, errors.Wrap(cloudprovider.ErrNotFound, "getVpc")
}
vpcs[0].region = self
@@ -639,10 +642,10 @@ func (self *SRegion) DeleteVpc(vpcId string) error {
return errors.Wrap(err, "DeleteVpc")
}
func (self *SRegion) GetVpcs(vpcId []string, offset int, limit int) ([]SVpc, int, error) {
func (self *SRegion) GetVpcs(vpcId []string) ([]SVpc, error) {
ec2Client, err := self.getEc2Client()
if err != nil {
return nil, 0, errors.Wrap(err, "getEc2Client")
return nil, errors.Wrap(err, "getEc2Client")
}
params := &ec2.DescribeVpcsInput{}
@@ -653,13 +656,13 @@ func (self *SRegion) GetVpcs(vpcId []string, offset int, limit int) ([]SVpc, int
ret, err := ec2Client.DescribeVpcs(params)
err = parseNotFoundError(err)
if err != nil {
return nil, 0, err
return nil, err
}
vpcs := []SVpc{}
for _, item := range ret.Vpcs {
if err := FillZero(item); err != nil {
return nil, 0, err
return nil, err
}
cidrBlockAssociationSet := []string{}
for i := range item.CidrBlockAssociationSet {
@@ -669,26 +672,24 @@ func (self *SRegion) GetVpcs(vpcId []string, offset int, limit int) ([]SVpc, int
}
}
tags := make(map[string]string, 0)
for _, tag := range item.Tags {
tags[*tag.Key] = *tag.Value
}
tagspec := TagSpec{ResourceType: "vpc"}
tagspec.LoadingEc2Tags(item.Tags)
vpcs = append(vpcs, SVpc{
region: self,
RegionId: self.RegionId,
VpcId: *item.VpcId,
VpcName: tags["Name"],
VpcName: tagspec.GetNameTag(),
CidrBlock: *item.CidrBlock,
CidrBlockAssociationSet: cidrBlockAssociationSet,
IsDefault: *item.IsDefault,
Status: *item.State,
InstanceTenancy: *item.InstanceTenancy,
Tags: tags,
TagSpec: tagspec,
})
}
return vpcs, len(vpcs), nil
return vpcs, nil
}
func (self *SRegion) GetInternetGateways(vpcId string) ([]SInternetGateway, error) {