|
|
|
@@ -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).
|
|
|
|
@@ -112,7 +127,7 @@ func (self *SNetworkIpMac) ValidateUpdateData(
|
|
|
|
|
return input, httperrors.NewInputParameterError("missing update field")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if input.IpAddr != "" {
|
|
|
|
|
if input.IpAddr != "" && input.IpAddr != self.IpAddr {
|
|
|
|
|
iNetwork, err := NetworkManager.FetchByIdOrName(userCred, self.NetworkId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return input, errors.Wrap(err, "fetch network")
|
|
|
|
@@ -124,21 +139,21 @@ func (self *SNetworkIpMac) ValidateUpdateData(
|
|
|
|
|
if ipInUse, err := NetworkIpMacManager.NetworkIpAddrInUse(self.NetworkId, input.IpAddr); err != nil {
|
|
|
|
|
return input, errors.Wrap(err, "check ip addr in use")
|
|
|
|
|
} else if ipInUse {
|
|
|
|
|
return input, errors.Errorf("ip addr %s is in use", input.IpAddr)
|
|
|
|
|
return input, httperrors.NewBadRequestError("ip addr %s is in use", input.IpAddr)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
input.IpAddr = self.IpAddr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if input.MacAddr != "" {
|
|
|
|
|
input.MacAddr = strings.ToLower(input.MacAddr)
|
|
|
|
|
if input.MacAddr != "" && input.MacAddr != self.MacAddr {
|
|
|
|
|
if !utils.IsMatchMacAddr(input.MacAddr) {
|
|
|
|
|
return input, errors.Errorf("mac address %s is not valid", input.MacAddr)
|
|
|
|
|
}
|
|
|
|
|
input.MacAddr = strings.ToLower(input.MacAddr)
|
|
|
|
|
if macInUse, err := NetworkIpMacManager.NetworkMacAddrInUse(self.NetworkId, input.MacAddr); err != nil {
|
|
|
|
|
return input, errors.Wrap(err, "check mac addr in use")
|
|
|
|
|
} else if macInUse {
|
|
|
|
|
return input, errors.Errorf("mac addr %s is in use", input.MacAddr)
|
|
|
|
|
return input, httperrors.NewBadRequestError("mac addr %s is in use", input.MacAddr)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
input.MacAddr = self.MacAddr
|
|
|
|
@@ -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
|
|
|
|
|
}
|
|
|
|
|