diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 71f4d0a897..24856ada02 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -352,13 +352,9 @@ func (self *SElasticip) GetShortDesc(ctx context.Context) *jsonutils.JSONDict { } func (manager *SElasticipManager) SyncEips(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, region *SCloudregion, eips []cloudprovider.ICloudEIP, syncOwnerId mcclient.IIdentityProvider) compare.SyncResult { - // ownerProjId := projectId + lockman.LockRawObject(ctx, manager.KeywordPlural(), region.Id) + defer lockman.ReleaseRawObject(ctx, manager.KeywordPlural(), region.Id) - lockman.LockRawObject(ctx, "elasticip", region.Id) - defer lockman.ReleaseRawObject(ctx, "elasticip", region.Id) - - // localEips := make([]SElasticip, 0) - // remoteEips := make([]cloudprovider.ICloudEIP, 0) syncResult := compare.SyncResult{} dbEips, err := region.GetElasticIps(provider.Id, api.EIP_MODE_STANDALONE_EIP) @@ -490,8 +486,6 @@ func (self *SElasticip) SyncInstanceWithCloudEip(ctx context.Context, userCred m func (self *SElasticip) SyncWithCloudEip(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudEIP, syncOwnerId mcclient.IIdentityProvider) error { diff, err := db.UpdateWithLock(ctx, self, func() error { - - // self.Name = ext.GetName() if bandwidth := ext.GetBandwidth(); bandwidth != 0 { self.Bandwidth = bandwidth } @@ -500,6 +494,7 @@ func (self *SElasticip) SyncWithCloudEip(ctx context.Context, userCred mcclient. self.Status = ext.GetStatus() self.ExternalId = ext.GetGlobalId() self.IsEmulated = ext.IsEmulated() + self.AssociateType = ext.GetAssociationType() if chargeType := ext.GetInternetChargeType(); len(chargeType) > 0 { self.ChargeType = chargeType @@ -532,10 +527,10 @@ func (self *SElasticip) SyncWithCloudEip(ctx context.Context, userCred mcclient. }) } - err = self.SyncInstanceWithCloudEip(ctx, userCred, ext) - if err != nil { - return errors.Wrap(err, "fail to sync associated instance of EIP") - } + //err = self.SyncInstanceWithCloudEip(ctx, userCred, ext) + //if err != nil { + // return errors.Wrap(err, "fail to sync associated instance of EIP") + //} syncVirtualResourceMetadata(ctx, userCred, self, ext) // eip有绑定资源,并且绑定资源是项目资源,eip项目信息跟随绑定资源 @@ -560,6 +555,7 @@ func (manager *SElasticipManager) newFromCloudEip(ctx context.Context, userCred eip.ManagerId = provider.Id eip.CloudregionId = region.Id eip.ChargeType = extEip.GetInternetChargeType() + eip.AssociateType = extEip.GetAssociationType() if len(eip.ChargeType) == 0 { eip.ChargeType = api.EIP_CHARGE_TYPE_BY_TRAFFIC } @@ -596,10 +592,10 @@ func (manager *SElasticipManager) newFromCloudEip(ctx context.Context, userCred return nil, errors.Wrapf(err, "newFromCloudEip") } - err = eip.SyncInstanceWithCloudEip(ctx, userCred, extEip) - if err != nil { - return nil, errors.Wrap(err, "fail to sync associated instance of EIP") - } + //err = eip.SyncInstanceWithCloudEip(ctx, userCred, extEip) + //if err != nil { + // return nil, errors.Wrap(err, "fail to sync associated instance of EIP") + //} syncVirtualResourceMetadata(ctx, userCred, &eip, extEip) @@ -1738,7 +1734,7 @@ func (manager *SElasticipManager) TotalCount(scope rbacutils.TRbacScope, ownerId q3sq := manager.Query().SubQuery() q3 := q3sq.Query( sqlchemy.COUNT("eip_used_count", q3sq.Field("id")), - ).Equals("mode", api.EIP_MODE_STANDALONE_EIP).IsNotEmpty("associate_id") + ).Equals("mode", api.EIP_MODE_STANDALONE_EIP).IsNotEmpty("associate_type") q3 = manager.usageQ(scope, ownerId, q3, rangeObjs, providers, brands, cloudEnv, policyResult) err := q1.First(&usage) diff --git a/pkg/compute/tasks/eip_syncstatus_task.go b/pkg/compute/tasks/eip_syncstatus_task.go index 382508db66..4586ae1caf 100644 --- a/pkg/compute/tasks/eip_syncstatus_task.go +++ b/pkg/compute/tasks/eip_syncstatus_task.go @@ -16,9 +16,9 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -35,11 +35,11 @@ func init() { taskman.RegisterTask(EipSyncstatusTask{}) } -func (self *EipSyncstatusTask) taskFail(ctx context.Context, eip *models.SElasticip, msg jsonutils.JSONObject) { - eip.SetStatus(self.UserCred, api.EIP_STATUS_UNKNOWN, msg.String()) - db.OpsLog.LogEvent(eip, db.ACT_SYNC_STATUS, msg, self.GetUserCred()) - logclient.AddActionLogWithStartable(self, eip, logclient.ACT_SYNC_STATUS, msg, self.UserCred, false) - self.SetStageFailed(ctx, msg) +func (self *EipSyncstatusTask) taskFail(ctx context.Context, eip *models.SElasticip, err error) { + eip.SetStatus(self.UserCred, api.EIP_STATUS_UNKNOWN, err.Error()) + db.OpsLog.LogEvent(eip, db.ACT_SYNC_STATUS, err, self.GetUserCred()) + logclient.AddActionLogWithStartable(self, eip, logclient.ACT_SYNC_STATUS, err, self.UserCred, false) + self.SetStageFailed(ctx, jsonutils.NewString(err.Error())) } func (self *EipSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { @@ -47,29 +47,25 @@ func (self *EipSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneMod extEip, err := eip.GetIEip(ctx) if err != nil { - msg := fmt.Sprintf("fail to find ieip for eip %s", err) - self.taskFail(ctx, eip, jsonutils.NewString(msg)) + self.taskFail(ctx, eip, errors.Wrapf(err, "eip.GetIEip")) return } err = extEip.Refresh() if err != nil { - msg := fmt.Sprintf("fail to refresh eip status %s", err) - self.taskFail(ctx, eip, jsonutils.NewString(msg)) + self.taskFail(ctx, eip, errors.Wrapf(err, "extEip.Refresh")) return } err = eip.SyncWithCloudEip(ctx, self.UserCred, eip.GetCloudprovider(), extEip, nil) if err != nil { - msg := fmt.Sprintf("fail to sync eip status %s", err) - self.taskFail(ctx, eip, jsonutils.NewString(msg)) + self.taskFail(ctx, eip, errors.Wrapf(err, "SyncWithCloudEip")) return } err = eip.SyncInstanceWithCloudEip(ctx, self.UserCred, extEip) if err != nil { - msg := fmt.Sprintf("fail to sync eip status %s", err) - self.taskFail(ctx, eip, jsonutils.NewString(msg)) + self.taskFail(ctx, eip, errors.Wrapf(err, "SyncInstanceWithCloudEip")) return } diff --git a/pkg/multicloud/aliyun/eip.go b/pkg/multicloud/aliyun/eip.go index d61f456f2f..c4d533a7a3 100644 --- a/pkg/multicloud/aliyun/eip.go +++ b/pkg/multicloud/aliyun/eip.go @@ -162,14 +162,14 @@ func (self *SEipAddress) GetMode() string { func (self *SEipAddress) GetAssociationType() string { switch self.InstanceType { - case EIP_INSTANCE_TYPE_ECS: + case EIP_INSTANCE_TYPE_ECS, "NetworkInterface": return api.EIP_ASSOCIATE_TYPE_SERVER case EIP_INSTANCE_TYPE_NAT: return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY case EIP_INTANNCE_TYPE_SLB: return api.EIP_ASSOCIATE_TYPE_LOADBALANCER default: - return "unsupported" + return self.InstanceType } } diff --git a/pkg/multicloud/apsara/eip.go b/pkg/multicloud/apsara/eip.go index faddc5f449..726102242e 100644 --- a/pkg/multicloud/apsara/eip.go +++ b/pkg/multicloud/apsara/eip.go @@ -152,15 +152,14 @@ func (self *SEipAddress) GetMode() string { func (self *SEipAddress) GetAssociationType() string { switch self.InstanceType { - case EIP_INSTANCE_TYPE_ECS: + case EIP_INSTANCE_TYPE_ECS, "NetworkInterface": return api.EIP_ASSOCIATE_TYPE_SERVER case EIP_INSTANCE_TYPE_NAT: return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY case EIP_INTANNCE_TYPE_SLB: return api.EIP_ASSOCIATE_TYPE_LOADBALANCER default: - //log.Fatalf("unsupported type: %s", self.InstanceType) - return "unsupported" + return self.InstanceType } } diff --git a/pkg/multicloud/aws/eip.go b/pkg/multicloud/aws/eip.go index ea6be254ee..9ca58ddf3b 100644 --- a/pkg/multicloud/aws/eip.go +++ b/pkg/multicloud/aws/eip.go @@ -113,9 +113,11 @@ func (self *SEipAddress) GetAssociationType() string { switch net.InterfaceType { case "nat_gateway": return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY + default: + return net.InterfaceType } } - return api.EIP_ASSOCIATE_TYPE_SERVER + return "" } func (self *SEipAddress) GetAssociationExternalId() string { diff --git a/pkg/multicloud/azure/classic_eip.go b/pkg/multicloud/azure/classic_eip.go index 700f69b70b..dc93bc8a5c 100644 --- a/pkg/multicloud/azure/classic_eip.go +++ b/pkg/multicloud/azure/classic_eip.go @@ -74,7 +74,10 @@ func (self *SClassicEipAddress) GetAssociationExternalId() string { } func (self *SClassicEipAddress) GetAssociationType() string { - return api.EIP_ASSOCIATE_TYPE_SERVER + if len(self.instanceId) > 0 { + return api.EIP_ASSOCIATE_TYPE_SERVER + } + return "" } func (self *SClassicEipAddress) GetBandwidth() int { diff --git a/pkg/multicloud/azure/eip.go b/pkg/multicloud/azure/eip.go index cfa17f4d9e..10dbad8606 100644 --- a/pkg/multicloud/azure/eip.go +++ b/pkg/multicloud/azure/eip.go @@ -23,6 +23,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/utils" billing_api "yunion.io/x/onecloud/pkg/apis/billing" api "yunion.io/x/onecloud/pkg/apis/compute" @@ -169,21 +170,28 @@ func (region *SRegion) DissociateEip(eipId string) error { } func (self *SEipAddress) GetAssociationExternalId() string { - interfaceId := self.Properties.IPConfiguration.ID - if len(interfaceId) > 0 && strings.Index(interfaceId, "/ipConfigurations/") > 0 { - interfaceId = strings.Split(interfaceId, "/ipConfigurations/")[0] - nic, err := self.region.GetNetworkInterface(interfaceId) - if err != nil { - log.Errorf("Failt to find NetworkInterface for eip %s nic %s", self.Name, interfaceId) - return "" + if self.GetAssociationType() == api.EIP_ASSOCIATE_TYPE_SERVER { + info := strings.Split(self.Properties.IPConfiguration.ID, "/") + nic, _ := self.region.GetNetworkInterface(strings.Join(info[:len(info)-2], "/")) + if nic != nil { + return strings.ToLower(nic.Properties.VirtualMachine.ID) } - return strings.ToLower(nic.Properties.VirtualMachine.ID) } return "" } func (self *SEipAddress) GetAssociationType() string { - return api.EIP_ASSOCIATE_TYPE_SERVER + if len(self.Properties.IPConfiguration.ID) == 0 { + return "" + } + if info := strings.Split(self.Properties.IPConfiguration.ID, "/"); len(info) > 7 { + resType := strings.ToLower(info[6]) + if utils.IsInStringArray(resType, []string{"networkinterfaces"}) { + return api.EIP_ASSOCIATE_TYPE_SERVER + } + return resType + } + return "" } func (self *SEipAddress) GetBandwidth() int { @@ -214,10 +222,6 @@ func (self *SEipAddress) GetMode() string { if self.IsEmulated() { return api.EIP_MODE_INSTANCE_PUBLICIP } - nic, err := self.region.GetNetworkInterface(self.Properties.IPConfiguration.ID) - if err == nil && len(nic.Properties.VirtualMachine.ID) > 0 { - return api.EIP_MODE_INSTANCE_PUBLICIP - } return api.EIP_MODE_STANDALONE_EIP } diff --git a/pkg/multicloud/azure/region.go b/pkg/multicloud/azure/region.go index f0021a5f3f..e0642eccee 100644 --- a/pkg/multicloud/azure/region.go +++ b/pkg/multicloud/azure/region.go @@ -23,6 +23,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/util/netutils" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" @@ -393,6 +394,10 @@ func (region *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) { if len(eips[i].GetIpAddr()) == 0 { continue } + _, err := netutils.NewIPV4Addr(eips[i].GetIpAddr()) + if err != nil { + continue + } eips[i].region = region ieips = append(ieips, &eips[i]) } @@ -400,6 +405,10 @@ func (region *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) { if len(classicEips[i].GetIpAddr()) == 0 { continue } + _, err := netutils.NewIPV4Addr(eips[i].GetIpAddr()) + if err != nil { + continue + } classicEips[i].region = region ieips = append(ieips, &classicEips[i]) } diff --git a/pkg/multicloud/bingocloud/eip.go b/pkg/multicloud/bingocloud/eip.go index 9952820413..ac2e925de8 100644 --- a/pkg/multicloud/bingocloud/eip.go +++ b/pkg/multicloud/bingocloud/eip.go @@ -67,7 +67,10 @@ func (self *SEip) GetINetworkId() string { } func (self *SEip) GetAssociationType() string { - return api.EIP_ASSOCIATE_TYPE_SERVER + if len(self.InstanceId) > 0 { + return api.EIP_ASSOCIATE_TYPE_SERVER + } + return "" } func (self *SEip) GetAssociationExternalId() string { diff --git a/pkg/multicloud/ecloud/eip.go b/pkg/multicloud/ecloud/eip.go index d7315d83bb..53ef87f429 100644 --- a/pkg/multicloud/ecloud/eip.go +++ b/pkg/multicloud/ecloud/eip.go @@ -100,7 +100,7 @@ func (e *SEip) GetAssociationType() string { case "elb": return api.EIP_ASSOCIATE_TYPE_LOADBALANCER default: - return "unsupported" + return e.BindType } } diff --git a/pkg/multicloud/google/eip.go b/pkg/multicloud/google/eip.go index c775b2b151..c5ee48d6bf 100644 --- a/pkg/multicloud/google/eip.go +++ b/pkg/multicloud/google/eip.go @@ -122,7 +122,10 @@ func (addr *SAddress) GetINetworkId() string { } func (addr *SAddress) GetAssociationType() string { - return api.EIP_ASSOCIATE_TYPE_SERVER + if len(addr.GetAssociationExternalId()) > 0 { + return api.EIP_ASSOCIATE_TYPE_SERVER + } + return "" } func (addr *SAddress) GetAssociationExternalId() string { diff --git a/pkg/multicloud/hcso/eip.go b/pkg/multicloud/hcso/eip.go index b63e802c06..b989fed049 100644 --- a/pkg/multicloud/hcso/eip.go +++ b/pkg/multicloud/hcso/eip.go @@ -179,8 +179,7 @@ func (self *SEipAddress) GetAssociationType() string { case "network:nat_gateway": return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY default: - log.Infof("eip %s associate type: %s", self.ID, port.DeviceOwner) - return api.EIP_ASSOCIATE_TYPE_SERVER + return port.DeviceOwner } } diff --git a/pkg/multicloud/huawei/eip.go b/pkg/multicloud/huawei/eip.go index 980014bd85..f6b2112253 100644 --- a/pkg/multicloud/huawei/eip.go +++ b/pkg/multicloud/huawei/eip.go @@ -179,8 +179,7 @@ func (self *SEipAddress) GetAssociationType() string { case "network:nat_gateway": return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY default: - log.Infof("eip %s associate type: %s", self.ID, port.DeviceOwner) - return api.EIP_ASSOCIATE_TYPE_SERVER + return port.DeviceOwner } } diff --git a/pkg/multicloud/jdcloud/eip.go b/pkg/multicloud/jdcloud/eip.go index 3490f10f36..0f5533284d 100644 --- a/pkg/multicloud/jdcloud/eip.go +++ b/pkg/multicloud/jdcloud/eip.go @@ -74,7 +74,7 @@ func (e *SEip) GetAssociationType() string { case "lb": return api.EIP_ASSOCIATE_TYPE_LOADBALANCER default: - return api.EIP_ASSOCIATE_TYPE_UNKNOWN + return e.InstanceType } } diff --git a/pkg/multicloud/openstack/eip.go b/pkg/multicloud/openstack/eip.go index dd22e6ae52..a5ea69c37d 100644 --- a/pkg/multicloud/openstack/eip.go +++ b/pkg/multicloud/openstack/eip.go @@ -167,7 +167,10 @@ func (eip *SEipAddress) GetMode() string { } func (eip *SEipAddress) GetAssociationType() string { - return api.EIP_ASSOCIATE_TYPE_SERVER + if len(eip.GetAssociationExternalId()) > 0 { + return api.EIP_ASSOCIATE_TYPE_SERVER + } + return "" } func (eip *SEipAddress) GetAssociationExternalId() string { diff --git a/pkg/multicloud/qcloud/eip.go b/pkg/multicloud/qcloud/eip.go index 162c6fb442..5a863abbe0 100644 --- a/pkg/multicloud/qcloud/eip.go +++ b/pkg/multicloud/qcloud/eip.go @@ -142,7 +142,10 @@ func (self *SEipAddress) GetAssociationType() string { return instanceType } } - return api.EIP_ASSOCIATE_TYPE_UNKNOWN + if info := strings.Split(self.InstanceId, "-"); len(info) > 0 { + return info[0] + } + return self.InstanceId } return "" } diff --git a/pkg/multicloud/ucloud/eip.go b/pkg/multicloud/ucloud/eip.go index b65bfea813..ba7cc80ce5 100644 --- a/pkg/multicloud/ucloud/eip.go +++ b/pkg/multicloud/ucloud/eip.go @@ -157,18 +157,21 @@ func (self *SEip) GetMode() string { } func (self *SEip) GetAssociationType() string { - return api.EIP_ASSOCIATE_TYPE_SERVER + switch self.Resource.ResourceType { + case "uhost": + return api.EIP_ASSOCIATE_TYPE_SERVER + case "natgw": + return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY + case "ulb": + return api.EIP_ASSOCIATE_TYPE_LOADBALANCER + default: + return self.Resource.ResourceType + } } // 已绑定的资源类型, 枚举值为: uhost, 云主机;natgw:NAT网关;ulb:负载均衡器;upm: 物理机; hadoophost: 大数据集群;fortresshost:堡垒机;udockhost:容器;udhost:私有专区主机;vpngw:IPSec VPN;ucdr:云灾备;dbaudit:数据库审计。 func (self *SEip) GetAssociationExternalId() string { - if self.Resource.ResourceType == "uhost" { - return self.Resource.ResourceID - } else if self.Resource.ResourceType != "" { - log.Warningf("GetAssociationExternalId bind with %s %s.expect uhost", self.Resource.ResourceType, self.Resource.ResourceID) - } - - return "" + return self.Resource.ResourceID } func (self *SEip) GetBandwidth() int { diff --git a/pkg/multicloud/zstack/eip.go b/pkg/multicloud/zstack/eip.go index f1e5dc9e67..2732fb0569 100644 --- a/pkg/multicloud/zstack/eip.go +++ b/pkg/multicloud/zstack/eip.go @@ -101,7 +101,10 @@ func (eip *SEipAddress) GetMode() string { } func (eip *SEipAddress) GetAssociationType() string { - return api.EIP_ASSOCIATE_TYPE_SERVER + if len(eip.GetAssociationExternalId()) > 0 { + return api.EIP_ASSOCIATE_TYPE_SERVER + } + return "" } func (eip *SEipAddress) GetAssociationExternalId() string {