mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix(region): network_ip_macs rbac and guestnetwork assign
Signed-off-by: wanyaoqi <d3lx.yq@gmail.com>
This commit is contained in:
@@ -75,8 +75,9 @@ const (
|
||||
ACT_VM_CONVERTING = "vm_converting"
|
||||
ACT_VM_CONVERT_FAIL = "vm_convert_fail"
|
||||
|
||||
ACT_SPLIT = "net_split"
|
||||
ACT_MERGE = "net_merge"
|
||||
ACT_SPLIT = "net_split"
|
||||
ACT_MERGE = "net_merge"
|
||||
ACT_IP_MAC_BIND = "ip_mac_bind"
|
||||
|
||||
ACT_SAVING = "saving"
|
||||
ACT_SAVE = "save"
|
||||
|
||||
@@ -287,16 +287,6 @@ func (manager *SGuestnetworkManager) newGuestNetwork(
|
||||
}
|
||||
|
||||
provider := vpc.GetProviderName()
|
||||
|
||||
macAddr, err := manager.GenerateMac(mac)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(macAddr) == 0 {
|
||||
log.Errorf("Mac address generate fails")
|
||||
return nil, fmt.Errorf("mac address generate fails")
|
||||
}
|
||||
gn.MacAddr = macAddr
|
||||
if !virtual {
|
||||
if len(address) > 0 && reUseAddr {
|
||||
ipAddr, err := netutils.NewIPV4Addr(address)
|
||||
@@ -332,6 +322,21 @@ func (manager *SGuestnetworkManager) newGuestNetwork(
|
||||
}
|
||||
}
|
||||
}
|
||||
var err error
|
||||
if ipBindMac := NetworkIpMacManager.GetMacFromIp(network.Id, gn.IpAddr); ipBindMac != "" {
|
||||
gn.MacAddr = ipBindMac
|
||||
} else {
|
||||
gn.MacAddr, err = manager.GenerateMac(mac)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if len(gn.MacAddr) == 0 {
|
||||
log.Errorf("Mac address generate fails")
|
||||
return nil, fmt.Errorf("mac address generate fails")
|
||||
}
|
||||
|
||||
ifname, err = gn.checkOrAllocateIfname(network, ifname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -3194,9 +3194,6 @@ func (args *Attach2NetworkArgs) onceArgs(i int) attach2NetworkOnceArgs {
|
||||
|
||||
pendingUsage: args.PendingUsage,
|
||||
}
|
||||
if ipBindMac := NetworkIpMacManager.GetMacFromIp(r.network.Id, r.ipAddr); ipBindMac != "" {
|
||||
r.nicConf.Mac = ipBindMac
|
||||
}
|
||||
if i > 0 {
|
||||
r.ipAddr = ""
|
||||
r.bwLimit = 0
|
||||
|
||||
@@ -3,11 +3,13 @@ package models
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/rbacscope"
|
||||
"yunion.io/x/pkg/utils"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
@@ -15,6 +17,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
)
|
||||
|
||||
@@ -54,6 +57,18 @@ type SNetworkIpMac struct {
|
||||
IpAddr string `width:"16" charset:"ascii" nullable:"false" list:"user" index:"true" create:"required"`
|
||||
}
|
||||
|
||||
func (manager *SNetworkIpMacManager) ResourceScope() rbacscope.TRbacScope {
|
||||
return rbacscope.ScopeProject
|
||||
}
|
||||
|
||||
func (self *SNetworkIpMac) GetOwnerId() mcclient.IIdentityProvider {
|
||||
iNetwork, _ := NetworkManager.FetchById(self.NetworkId)
|
||||
if iNetwork != nil {
|
||||
return iNetwork.GetOwnerId()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (manager *SNetworkIpMacManager) NetworkMacAddrInUse(networkId, macAddr string) (bool, error) {
|
||||
count, err := manager.Query().
|
||||
Equals("network_id", networkId).
|
||||
@@ -221,6 +236,27 @@ func (manager *SNetworkIpMacManager) validateIpMac(ip, mac string, network *SNet
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SNetworkIpMac) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
iNetwork, _ := NetworkManager.FetchByIdOrName(userCred, self.NetworkId)
|
||||
note := fmt.Sprintf("create ip %s mac %s bind", self.IpAddr, self.MacAddr)
|
||||
db.OpsLog.LogEvent(iNetwork, db.ACT_IP_MAC_BIND, note, userCred)
|
||||
logclient.AddActionLogWithContext(ctx, iNetwork, logclient.ACT_IP_MAC_BIND, note, userCred, true)
|
||||
}
|
||||
|
||||
func (self *SNetworkIpMac) PostUpdate(ctx context.Context, userCred mcclient.TokenCredential, query, data jsonutils.JSONObject) {
|
||||
iNetwork, _ := NetworkManager.FetchByIdOrName(userCred, self.NetworkId)
|
||||
note := fmt.Sprintf("update ip %s mac %s bind", self.IpAddr, self.MacAddr)
|
||||
db.OpsLog.LogEvent(iNetwork, db.ACT_IP_MAC_BIND, note, userCred)
|
||||
logclient.AddActionLogWithContext(ctx, iNetwork, logclient.ACT_IP_MAC_BIND, note, userCred, true)
|
||||
}
|
||||
|
||||
func (self *SNetworkIpMac) PostDelete(ctx context.Context, userCred mcclient.TokenCredential) {
|
||||
iNetwork, _ := NetworkManager.FetchByIdOrName(userCred, self.NetworkId)
|
||||
note := fmt.Sprintf("delete ip %s mac %s bind", self.IpAddr, self.MacAddr)
|
||||
db.OpsLog.LogEvent(iNetwork, db.ACT_IP_MAC_BIND, note, userCred)
|
||||
logclient.AddActionLogWithContext(ctx, iNetwork, logclient.ACT_IP_MAC_BIND, note, userCred, true)
|
||||
}
|
||||
|
||||
func (manager *SNetworkIpMacManager) PerformBatchCreate(
|
||||
ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input *api.NetworkIpMacBatchCreateInput,
|
||||
) (jsonutils.JSONObject, error) {
|
||||
@@ -235,6 +271,7 @@ func (manager *SNetworkIpMacManager) PerformBatchCreate(
|
||||
input.NetworkId = network.Id
|
||||
|
||||
var errs = []error{}
|
||||
var insertedIpMacs = map[string]string{}
|
||||
for ip, mac := range input.IpMac {
|
||||
if err := manager.validateIpMac(ip, mac, network); err != nil {
|
||||
errs = append(errs, err)
|
||||
@@ -249,7 +286,12 @@ func (manager *SNetworkIpMacManager) PerformBatchCreate(
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
insertedIpMacs[ip] = mac
|
||||
}
|
||||
note := fmt.Sprintf("create ip macs bind %v", insertedIpMacs)
|
||||
db.OpsLog.LogEvent(iNetwork, db.ACT_IP_MAC_BIND, note, userCred)
|
||||
logclient.AddActionLogWithContext(ctx, iNetwork, logclient.ACT_IP_MAC_BIND, note, userCred, true)
|
||||
|
||||
if len(errs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -238,4 +238,6 @@ const (
|
||||
ACT_SET_USER_PASSWORD = "set_user_password"
|
||||
|
||||
ACT_PANIC = "panic"
|
||||
|
||||
ACT_IP_MAC_BIND = "ip_mac_bind"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user