mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-28 19:45:07 +08:00
Bugfix: add the operation log of natgateway; display correctly multiple
eip of snat rule in huawei cloud; bind ip to natgateway automatically when creating nat rule in aliyun cloud and try to unbind when deleting; fill or unfill the fields AssociateType, AssociateId when creating or deleting nat rule in huawei cloud; add SNatEntry and SNatEntryManager which has common field and function of SNat and DNat.
This commit is contained in:
@@ -154,8 +154,8 @@ DNatEntry:
|
||||
example: tcp
|
||||
name:
|
||||
type: string
|
||||
description: DNAT名称
|
||||
example: 111.230.80.43/udp/32
|
||||
description: DNAT名称(处理过的)
|
||||
example: hello-ea27c0ce-5870-49f3-8d57-f53e63f40361
|
||||
natgateway:
|
||||
type: string
|
||||
description: 所属NAT网关名称
|
||||
@@ -168,6 +168,10 @@ DNatEntry:
|
||||
type: string
|
||||
description: DNAT状态
|
||||
example: available
|
||||
real_name:
|
||||
type: string
|
||||
description: DNAT真实的名称
|
||||
example: hello
|
||||
|
||||
DNatEntryListResponse:
|
||||
type: object
|
||||
@@ -215,7 +219,8 @@ SNatEntry:
|
||||
description: 公网IP
|
||||
name:
|
||||
type: string
|
||||
example: snat-2zeqqfcow4t67ahduei7x
|
||||
description: SNAT名称(经过处理的)
|
||||
example: hello-ea27c0ce-5870-49f3-8d57-f53e63f40361
|
||||
natgateway:
|
||||
type: string
|
||||
example: test-nat
|
||||
@@ -238,6 +243,10 @@ SNatEntry:
|
||||
type: string
|
||||
example: available
|
||||
description: SNAT状态
|
||||
real_name:
|
||||
type: string
|
||||
example: hello
|
||||
description: SNAT真实的名称
|
||||
|
||||
SNatEntryListResponse:
|
||||
type: object
|
||||
|
||||
@@ -36,14 +36,14 @@ import (
|
||||
)
|
||||
|
||||
type SNatDEntryManager struct {
|
||||
db.SStatusStandaloneResourceBaseManager
|
||||
SNatEntryManager
|
||||
}
|
||||
|
||||
var NatDEntryManager *SNatDEntryManager
|
||||
|
||||
func init() {
|
||||
NatDEntryManager = &SNatDEntryManager{
|
||||
SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
|
||||
SNatEntryManager: NewNatEntryManager(
|
||||
SNatDEntry{},
|
||||
"natdtables_tbl",
|
||||
"natdentry",
|
||||
@@ -54,8 +54,7 @@ func init() {
|
||||
}
|
||||
|
||||
type SNatDEntry struct {
|
||||
db.SStatusStandaloneResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
SNatEntry
|
||||
|
||||
ExternalIP string `width:"17" charset:"ascii" list:"user" create:"required"`
|
||||
ExternalPort int `list:"user" create:"required"`
|
||||
@@ -63,63 +62,17 @@ type SNatDEntry struct {
|
||||
InternalIP string `width:"17" charset:"ascii" list:"user" create:"required"`
|
||||
InternalPort int `list:"user" create:"required"`
|
||||
IpProtocol string `width:"8" charset:"ascii" list:"user" create:"required"`
|
||||
|
||||
NatgatewayId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required"`
|
||||
}
|
||||
|
||||
func (manager *SNatDEntryManager) GetContextManagers() [][]db.IModelManager {
|
||||
return [][]db.IModelManager{
|
||||
{NatGatewayManager},
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SNatDEntryManager) AllowListItems(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowList(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatDEntryManager) AllowCreateItem(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowCreate(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) AllowGetDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowGet(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) AllowUpdateItem(ctx context.Context, userCred mcclient.TokenCredential) bool {
|
||||
return db.IsAdminAllowUpdate(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) AllowDeleteItem(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowDelete(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) GetNatgateway() (*SNatGateway, error) {
|
||||
_natgateway, err := NatGatewayManager.FetchById(self.NatgatewayId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return _natgateway.(*SNatGateway), nil
|
||||
}
|
||||
|
||||
func (man *SNatDEntryManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*sqlchemy.SQuery, error) {
|
||||
q, err := man.SStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, query)
|
||||
q, err := man.SNatEntryManager.ListItemFilter(ctx, q, userCred, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data := query.(*jsonutils.JSONDict)
|
||||
q, err = validators.ApplyModelFilters(q, data, []*validators.ModelFilterOptions{
|
||||
return validators.ApplyModelFilters(q, data, []*validators.ModelFilterOptions{
|
||||
{Key: "natgateway", ModelKeyword: "natgateway", OwnerId: userCred},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q, err = managedResourceFilterByAccount(q, query, "natgateway_id", func() *sqlchemy.SQuery {
|
||||
natgateways := NatGatewayManager.Query().SubQuery()
|
||||
return natgateways.Query(natgateways.Field("id"))
|
||||
})
|
||||
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (man *SNatDEntryManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
@@ -158,7 +111,9 @@ func (man *SNatDEntryManager) ValidateCreateData(ctx context.Context, userCred m
|
||||
data.Add(jsonutils.NewBool(true), "need_bind")
|
||||
}
|
||||
data.Remove("external_ip_id")
|
||||
data.Add(jsonutils.NewString(eip.ExternalId), "external_ip_id")
|
||||
data.Add(jsonutils.NewString(eip.Id), "eip_id")
|
||||
data.Add(jsonutils.NewString(eip.ExternalId), "eip_external_id")
|
||||
data.Set("name", jsonutils.NewString(NatGatewayManager.NatNameFromReal(input.Name, input.NatgatewayId)))
|
||||
return data, nil
|
||||
}
|
||||
|
||||
@@ -268,11 +223,7 @@ func (manager *SNatDEntryManager) newFromCloudNatDTable(ctx context.Context, use
|
||||
table := SNatDEntry{}
|
||||
table.SetModelManager(manager, &table)
|
||||
|
||||
newName, err := db.GenerateName(manager, ownerId, extEntry.GetName())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
table.Name = newName
|
||||
table.Name = NatGatewayManager.NatNameFromReal(extEntry.GetName(), nat.Id)
|
||||
table.Status = extEntry.GetStatus()
|
||||
table.ExternalId = extEntry.GetGlobalId()
|
||||
table.IsEmulated = extEntry.IsEmulated()
|
||||
@@ -283,7 +234,7 @@ func (manager *SNatDEntryManager) newFromCloudNatDTable(ctx context.Context, use
|
||||
table.InternalPort = extEntry.GetInternalPort()
|
||||
table.IpProtocol = extEntry.GetIpProtocol()
|
||||
|
||||
err = manager.TableSpec().Insert(&table)
|
||||
err := manager.TableSpec().Insert(&table)
|
||||
if err != nil {
|
||||
log.Errorf("newFromCloudNatDTable fail %s", err)
|
||||
return nil, err
|
||||
@@ -299,18 +250,26 @@ func (self *SNatDEntry) GetExtraDetails(ctx context.Context, userCred mcclient.T
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return extra, nil
|
||||
|
||||
return self.getMoreDetails(ctx, userCred, extra), nil
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) GetCustomizeColumns(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) *jsonutils.JSONDict {
|
||||
extra := self.SStatusStandaloneResourceBase.GetCustomizeColumns(ctx, userCred, query)
|
||||
return self.getMoreDetails(ctx, userCred, extra)
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) getMoreDetails(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query *jsonutils.JSONDict) *jsonutils.JSONDict {
|
||||
|
||||
natgateway, err := self.GetNatgateway()
|
||||
if err != nil {
|
||||
log.Errorf("failed to get naggateway %s for dtable %s(%s) error: %v", self.NatgatewayId, self.Name, self.Id, err)
|
||||
return extra
|
||||
return query
|
||||
}
|
||||
extra.Add(jsonutils.NewString(natgateway.Name), "natgateway")
|
||||
return extra
|
||||
query.Add(jsonutils.NewString(natgateway.Name), "natgateway")
|
||||
query.Add(jsonutils.NewString(NatGatewayManager.NatNameToReal(self.Name, natgateway.GetId())))
|
||||
return query
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
@@ -327,39 +286,15 @@ func (self *SNatDEntry) PostCreate(ctx context.Context, userCred mcclient.TokenC
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) GetINatGateway() (cloudprovider.ICloudNatGateway, error) {
|
||||
model, err := NatGatewayManager.FetchById(self.NatgatewayId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "Fetch NatGateway whose id is %s failed", self.NatgatewayId)
|
||||
}
|
||||
natgateway := model.(*SNatGateway)
|
||||
return natgateway.GetINatGateway()
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
log.Infof("DNAT delete do nothing")
|
||||
self.SetStatus(userCred, api.NAT_STATUS_DELETING, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
|
||||
if len(self.ExternalId) > 0 {
|
||||
return self.StartDeleteVpcTask(ctx, userCred)
|
||||
return self.StartDeleteDNatTask(ctx, userCred)
|
||||
} else {
|
||||
return self.RealDelete(ctx, userCred)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
err := db.DeleteModel(ctx, userCred, self)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
self.SetStatus(userCred, api.NAT_STATUS_DELETED, "real delete")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) StartDeleteVpcTask(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
func (self *SNatDEntry) StartDeleteDNatTask(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "SNatDEntryDeleteTask", self, userCred, nil, "", "", nil)
|
||||
if err != nil {
|
||||
log.Errorf("Start dnatEntry deleteTask fail %s", err)
|
||||
@@ -377,3 +312,8 @@ func (self *SNatDEntryManager) canBindIP(ipAddr string) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) CountByEIP() (int, error) {
|
||||
q := NatDEntryManager.Query().Equals("external_ip", self.ExternalIP)
|
||||
return q.CountWithError()
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package models
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
@@ -514,3 +515,123 @@ func (self *SNatGateway) GetIRegion() (cloudprovider.ICloudRegion, error) {
|
||||
}
|
||||
return provider.GetIRegionById(region.ExternalId)
|
||||
}
|
||||
|
||||
func (nm *SNatGetewayManager) NatNameToReal(name string, natgatewayId string) string {
|
||||
index := strings.Index(name, natgatewayId)
|
||||
if index < 0 {
|
||||
return name
|
||||
}
|
||||
return name[:index-1]
|
||||
}
|
||||
|
||||
func (nm *SNatGetewayManager) NatNameFromReal(name string, natgatewayId string) string {
|
||||
return fmt.Sprintf("%s-%s", name, natgatewayId)
|
||||
}
|
||||
|
||||
type INatHelper interface {
|
||||
db.IModel
|
||||
CountByEIP() (int, error)
|
||||
GetNatgateway() (*SNatGateway, error)
|
||||
SetStatus(userCred mcclient.TokenCredential, status string, reason string) error
|
||||
}
|
||||
|
||||
type SNatEntryManager struct {
|
||||
db.SStatusStandaloneResourceBaseManager
|
||||
}
|
||||
|
||||
func NewNatEntryManager(dt interface{}, tableName string, keyword string, keywordPlural string) SNatEntryManager {
|
||||
return SNatEntryManager{db.NewStatusStandaloneResourceBaseManager(dt, tableName, keyword, keywordPlural)}
|
||||
}
|
||||
|
||||
type SNatEntry struct {
|
||||
db.SStatusStandaloneResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
|
||||
NatgatewayId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required"`
|
||||
}
|
||||
|
||||
func (manager *SNatEntryManager) GetContextManagers() [][]db.IModelManager {
|
||||
return [][]db.IModelManager{
|
||||
{NatGatewayManager},
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SNatEntryManager) AllowListItems(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowList(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatEntryManager) AllowCreateItem(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowCreate(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatEntry) AllowGetDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowGet(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatEntry) AllowUpdateItem(ctx context.Context, userCred mcclient.TokenCredential) bool {
|
||||
return db.IsAdminAllowUpdate(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatEntry) AllowDeleteItem(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowDelete(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatEntry) GetNatgateway() (*SNatGateway, error) {
|
||||
model, err := NatGatewayManager.FetchById(self.NatgatewayId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model.(*SNatGateway), nil
|
||||
}
|
||||
|
||||
func (man *SNatEntryManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*sqlchemy.SQuery, error) {
|
||||
q, err := man.SStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q, err = managedResourceFilterByAccount(q, query, "natgateway_id", func() *sqlchemy.SQuery {
|
||||
natgateways := NatGatewayManager.Query().SubQuery()
|
||||
return natgateways.Query(natgateways.Field("id"))
|
||||
})
|
||||
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (self *SNatEntry) GetINatGateway() (cloudprovider.ICloudNatGateway, error) {
|
||||
model, err := NatGatewayManager.FetchById(self.NatgatewayId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "Fetch NatGateway whose id is %s failed", self.NatgatewayId)
|
||||
}
|
||||
natgateway := model.(*SNatGateway)
|
||||
return natgateway.GetINatGateway()
|
||||
}
|
||||
|
||||
func (self *SNatEntry) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
log.Infof("NAT Entry delete do nothing")
|
||||
self.SetStatus(userCred, api.NAT_STATUS_DELETING, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SNatEntry) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
err := db.DeleteModel(ctx, userCred, self)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
self.SetStatus(userCred, api.NAT_STATUS_DELETED, "real delete")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SNatEntry) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
|
||||
if data.Contains("name") {
|
||||
name, _ := data.GetString("name")
|
||||
natgateway, err := self.GetNatgateway()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data.Set("name", jsonutils.NewString(NatGatewayManager.NatNameFromReal(name, natgateway.GetId())))
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
+34
-100
@@ -37,14 +37,14 @@ import (
|
||||
)
|
||||
|
||||
type SNatSEntryManager struct {
|
||||
db.SStatusStandaloneResourceBaseManager
|
||||
SNatEntryManager
|
||||
}
|
||||
|
||||
var NatSEntryManager *SNatSEntryManager
|
||||
|
||||
func init() {
|
||||
NatSEntryManager = &SNatSEntryManager{
|
||||
SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
|
||||
SNatEntryManager: NewNatEntryManager(
|
||||
SNatSEntry{},
|
||||
"natstables_tbl",
|
||||
"natsentry",
|
||||
@@ -55,48 +55,12 @@ func init() {
|
||||
}
|
||||
|
||||
type SNatSEntry struct {
|
||||
db.SStatusStandaloneResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
SNatEntry
|
||||
|
||||
IP string `width:"17" charset:"ascii" list:"user" create:"required"`
|
||||
IP string `charset:"ascii" list:"user" create:"required"`
|
||||
SourceCIDR string `width:"22" charset:"ascii" list:"user" create:"required"`
|
||||
|
||||
NetworkId string `width:"36" charset:"ascii" list:"user" create:"optional"`
|
||||
NatgatewayId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required"`
|
||||
}
|
||||
|
||||
func (manager *SNatSEntryManager) GetContextManagers() [][]db.IModelManager {
|
||||
return [][]db.IModelManager{
|
||||
{NatGatewayManager},
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SNatSEntryManager) AllowListItems(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowList(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatSEntryManager) AllowCreateItem(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowCreate(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) AllowGetDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowGet(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) AllowUpdateItem(ctx context.Context, userCred mcclient.TokenCredential) bool {
|
||||
return db.IsAdminAllowUpdate(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) AllowDeleteItem(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowDelete(userCred, self)
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) GetNatgateway() (*SNatGateway, error) {
|
||||
_natgateway, err := NatGatewayManager.FetchById(self.NatgatewayId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return _natgateway.(*SNatGateway), nil
|
||||
NetworkId string `width:"36" charset:"ascii" list:"user" create:"optional"`
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) GetNetwork() (*SNetwork, error) {
|
||||
@@ -111,28 +75,19 @@ func (self *SNatSEntry) GetNetwork() (*SNetwork, error) {
|
||||
}
|
||||
|
||||
func (man *SNatSEntryManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*sqlchemy.SQuery, error) {
|
||||
q, err := man.SStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, query)
|
||||
q, err := man.SNatEntryManager.ListItemFilter(ctx, q, userCred, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data := query.(*jsonutils.JSONDict)
|
||||
q, err = validators.ApplyModelFilters(q, data, []*validators.ModelFilterOptions{
|
||||
return validators.ApplyModelFilters(q, data, []*validators.ModelFilterOptions{
|
||||
{Key: "network", ModelKeyword: "network", OwnerId: userCred},
|
||||
{Key: "natgateway", ModelKeyword: "natgateway", OwnerId: userCred},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q, err = managedResourceFilterByAccount(q, query, "natgateway_id", func() *sqlchemy.SQuery {
|
||||
natgateways := NatGatewayManager.Query().SubQuery()
|
||||
return natgateways.Query(natgateways.Field("id"))
|
||||
})
|
||||
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (man *SNatSEntryManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
func (man *SNatSEntryManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
input := &api.SNatSCreateInput{}
|
||||
err := data.Unmarshal(input)
|
||||
if err != nil {
|
||||
@@ -206,7 +161,9 @@ func (man *SNatSEntryManager) ValidateCreateData(ctx context.Context, userCred m
|
||||
}
|
||||
|
||||
data.Remove("external_ip_id")
|
||||
data.Add(jsonutils.NewString(eip.ExternalId), "external_ip_id")
|
||||
data.Set("name", jsonutils.NewString(NatGatewayManager.NatNameFromReal(input.Name, input.NatgatewayId)))
|
||||
data.Add(jsonutils.NewString(eip.Id), "eip_id")
|
||||
data.Add(jsonutils.NewString(eip.ExternalId), "eip_external_id")
|
||||
return data, nil
|
||||
}
|
||||
|
||||
@@ -297,11 +254,7 @@ func (manager *SNatSEntryManager) newFromCloudNatSTable(ctx context.Context, use
|
||||
table := SNatSEntry{}
|
||||
table.SetModelManager(manager, &table)
|
||||
|
||||
newName, err := db.GenerateName(manager, ownerId, extEntry.GetName())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
table.Name = newName
|
||||
table.Name = NatGatewayManager.NatNameFromReal(extEntry.GetName(), nat.Id)
|
||||
table.Status = extEntry.GetStatus()
|
||||
table.ExternalId = extEntry.GetGlobalId()
|
||||
table.IsEmulated = extEntry.IsEmulated()
|
||||
@@ -317,7 +270,7 @@ func (manager *SNatSEntryManager) newFromCloudNatSTable(ctx context.Context, use
|
||||
table.NetworkId = network.GetId()
|
||||
}
|
||||
|
||||
err = manager.TableSpec().Insert(&table)
|
||||
err := manager.TableSpec().Insert(&table)
|
||||
if err != nil {
|
||||
log.Errorf("newFromCloudNatSTable fail %s", err)
|
||||
return nil, err
|
||||
@@ -353,34 +306,34 @@ func (self *SNatSEntry) GetExtraDetails(ctx context.Context, userCred mcclient.T
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return self.getMoreDetails(ctx, userCred, extra)
|
||||
return self.getMoreDetails(ctx, userCred, extra), nil
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) GetCustomizeColumns(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) *jsonutils.JSONDict {
|
||||
extra := self.SStatusStandaloneResourceBase.GetCustomizeColumns(ctx, userCred, query)
|
||||
natgateway, err := self.GetNatgateway()
|
||||
if err != nil {
|
||||
log.Errorf("failed to get naggateway %s for stable %s(%s) error: %v", self.NatgatewayId, self.Name, self.Id, err)
|
||||
return extra
|
||||
}
|
||||
extra.Add(jsonutils.NewString(natgateway.Name), "natgateway")
|
||||
|
||||
extra, _ = self.getMoreDetails(ctx, userCred, extra)
|
||||
return extra
|
||||
return self.getMoreDetails(ctx, userCred, extra)
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) getMoreDetails(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
query *jsonutils.JSONDict) *jsonutils.JSONDict {
|
||||
|
||||
network, err := self.GetNetwork()
|
||||
if err != nil {
|
||||
return query, nil
|
||||
return query
|
||||
}
|
||||
if network == nil {
|
||||
return query, nil
|
||||
return query
|
||||
}
|
||||
query.Add(jsonutils.Marshal(network), "network")
|
||||
return query, nil
|
||||
natgateway, err := self.GetNatgateway()
|
||||
if err != nil {
|
||||
log.Errorf("failed to get naggateway %s for stable %s(%s) error: %v", self.NatgatewayId, self.Name, self.Id, err)
|
||||
return query
|
||||
}
|
||||
query.Add(jsonutils.NewString(natgateway.Name), "natgateway")
|
||||
query.Add(jsonutils.NewString(NatGatewayManager.NatNameToReal(self.Name, natgateway.GetId())))
|
||||
return query
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
@@ -397,39 +350,15 @@ func (self *SNatSEntry) PostCreate(ctx context.Context, userCred mcclient.TokenC
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) GetINatGateway() (cloudprovider.ICloudNatGateway, error) {
|
||||
model, err := NatGatewayManager.FetchById(self.NatgatewayId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "Fetch NatGateway whose id is %s failed", self.NatgatewayId)
|
||||
}
|
||||
natgateway := model.(*SNatGateway)
|
||||
return natgateway.GetINatGateway()
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
|
||||
if len(self.ExternalId) > 0 {
|
||||
return self.StartDeleteVpcTask(ctx, userCred)
|
||||
return self.StartDeleteSNatTask(ctx, userCred)
|
||||
} else {
|
||||
return self.RealDelete(ctx, userCred)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
log.Infof("SNAT delete do nothing")
|
||||
self.SetStatus(userCred, api.NAT_STATUS_DELETING, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
err := db.DeleteModel(ctx, userCred, self)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
self.SetStatus(userCred, api.NAT_STATUS_DELETED, "real delete")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) StartDeleteVpcTask(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
func (self *SNatSEntry) StartDeleteSNatTask(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "SNatSEntryDeleteTask", self, userCred, nil, "", "", nil)
|
||||
if err != nil {
|
||||
log.Errorf("Start snatEntry deleteTask fail %s", err)
|
||||
@@ -448,6 +377,11 @@ func (self *SNatSEntryManager) canBindIP(ipAddr string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) CountByEIP() (int, error) {
|
||||
q := NatSEntryManager.Query().Equals("ip", self.IP)
|
||||
return q.CountWithError()
|
||||
}
|
||||
|
||||
func newIPv4RangeFromCIDR(cidr string) (netutils.IPV4AddrRange, error) {
|
||||
_, ipNet, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
|
||||
@@ -99,7 +99,9 @@ type IRegionDriver interface {
|
||||
|
||||
//Nat gateway
|
||||
DealNatGatewaySpec(spec string) string
|
||||
RequestBindIPToNatgateway(ctx context.Context, task taskman.ITask, natgateway *SNatGateway, needBind bool, eipID string) error
|
||||
RequestBindIPToNatgateway(ctx context.Context, task taskman.ITask, natgateway *SNatGateway, eipId string) error
|
||||
RequestUnBindIPFromNatgateway(ctx context.Context, task taskman.ITask, nat INatHelper, natgateway *SNatGateway) error
|
||||
BindIPToNatgatewayRollback(ctx context.Context, eipId string) error
|
||||
}
|
||||
|
||||
var regionDrivers map[string]IRegionDriver
|
||||
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/validators"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
@@ -863,30 +864,99 @@ func (self *SAliyunRegionDriver) DealNatGatewaySpec(spec string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SAliyunRegionDriver) RequestBindIPToNatgateway(ctx context.Context, task taskman.ITask,
|
||||
natgateway *models.SNatGateway, needBind bool, eipID string) error {
|
||||
// RequestBindIPToNatgateway in aliyun don't need to check eip again which is different from SManagerResongDriver.
|
||||
// RequestBindIPToNatgateway because func ieip.Associate will fail if eip has been associate
|
||||
func (self *SAliyunRegionDriver) RequestBindIPToNatgateway(ctx context.Context, task taskman.ITask, natgateway *models.SNatGateway,
|
||||
eipId string) error {
|
||||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||||
if !needBind {
|
||||
return nil, nil
|
||||
model, err := models.ElasticipManager.FetchById(eipId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lockman.LockObject(ctx, model)
|
||||
defer lockman.ReleaseObject(ctx, model)
|
||||
eip := model.(*models.SElasticip)
|
||||
iregion, err := natgateway.GetIRegion()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ieip, err := iregion.GetIEipById(eipID)
|
||||
ieip, err := iregion.GetIEipById(eip.GetExternalId())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetch eip failed")
|
||||
}
|
||||
err = ieip.Associate(natgateway.GetExternalId())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "bind eip to natgateway")
|
||||
return nil, errors.Wrap(err, "fail to bind eip to natgateway")
|
||||
}
|
||||
|
||||
err = cloudprovider.WaitStatus(ieip, api.EIP_STATUS_ASSOCIATE, 10*time.Second, 300*time.Second)
|
||||
err = cloudprovider.WaitStatus(ieip, api.EIP_STATUS_READY, 5*time.Second, 100*time.Second)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// database
|
||||
_, err = db.Update(eip, func() error {
|
||||
eip.AssociateType = api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
|
||||
eip.AssociateId = natgateway.GetId()
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "fail to update eip '%s' in database", eip.Id)
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SAliyunRegionDriver) RequestUnBindIPFromNatgateway(ctx context.Context, task taskman.ITask,
|
||||
nat models.INatHelper, natgateway *models.SNatGateway) error {
|
||||
|
||||
count, err := nat.CountByEIP()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "fail to count by eip")
|
||||
}
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
eip := &models.SElasticip{}
|
||||
err = models.ElasticipManager.Query().Equals("associate_id", natgateway.Id).First(eip)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "fail to fetch eip associate with natgateway %s", natgateway.Id)
|
||||
}
|
||||
eip.SetModelManager(models.ElasticipManager, eip)
|
||||
lockman.LockObject(ctx, eip)
|
||||
defer lockman.ReleaseObject(ctx, eip)
|
||||
iregion, err := eip.GetIRegion()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "fail to fetch iregion")
|
||||
}
|
||||
ieip, err := iregion.GetIEipById(eip.GetExternalId())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetch eip failed")
|
||||
}
|
||||
err = ieip.Dissociate()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fail to unbind eip from natgateway")
|
||||
}
|
||||
|
||||
err = cloudprovider.WaitStatus(ieip, api.EIP_STATUS_READY, 5*time.Second, 100*time.Second)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// database
|
||||
_, err = db.Update(eip, func() error {
|
||||
eip.AssociateType = ""
|
||||
eip.AssociateId = ""
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "fail to update eip '%s' in database", eip.Id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SAliyunRegionDriver) BindIPToNatgatewayRollback(ctx context.Context, eipId string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -832,8 +832,14 @@ func (self *SKVMRegionDriver) OnSnapshotDelete(ctx context.Context, snapshot *mo
|
||||
func (self *SKVMRegionDriver) DealNatGatewaySpec(spec string) string {
|
||||
return spec
|
||||
}
|
||||
func (self *SKVMRegionDriver) RequestBindIPToNatgateway(ctx context.Context, task taskman.ITask,
|
||||
natgateway *models.SNatGateway, needBind bool, eipID string) error {
|
||||
func (self *SKVMRegionDriver) RequestBindIPToNatgateway(ctx context.Context, task taskman.ITask, natgateway *models.SNatGateway,
|
||||
eipId string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SKVMRegionDriver) RequestUnBindIPFromNatgateway(ctx context.Context, task taskman.ITask,
|
||||
nat models.INatHelper, natgateway *models.SNatGateway) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -847,3 +853,7 @@ func (self *SKVMRegionDriver) RequestPreSnapshotPolicyApply(ctx context.Context,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SKVMRegionDriver) BindIPToNatgatewayRollback(ctx context.Context, eipId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1297,9 +1297,56 @@ func (self *SManagedVirtualizationRegionDriver) DealNatGatewaySpec(spec string)
|
||||
}
|
||||
|
||||
func (self *SManagedVirtualizationRegionDriver) RequestBindIPToNatgateway(ctx context.Context, task taskman.ITask,
|
||||
natgateway *models.SNatGateway, needBind bool, eipID string) error {
|
||||
natgateway *models.SNatGateway, eipId string) error {
|
||||
|
||||
task.ScheduleRun(nil)
|
||||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||||
model, err := models.ElasticipManager.FetchById(eipId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lockman.LockObject(ctx, model)
|
||||
defer lockman.ReleaseObject(ctx, model)
|
||||
eip := model.(*models.SElasticip)
|
||||
// check again
|
||||
if len(eip.AssociateId) > 0 {
|
||||
return nil, fmt.Errorf("eip %s has been associated with resource %s", eip.Id, eip.AssociateId)
|
||||
}
|
||||
_, err = db.Update(eip, func() error {
|
||||
eip.AssociateType = api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
|
||||
eip.AssociateId = natgateway.GetId()
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "fail to update eip '%s' in database", eip.Id)
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SManagedVirtualizationRegionDriver) RequestUnBindIPFromNatgateway(ctx context.Context, task taskman.ITask,
|
||||
nat models.INatHelper, natgateway *models.SNatGateway) error {
|
||||
|
||||
eip := &models.SElasticip{}
|
||||
err := models.ElasticipManager.Query().Equals("associate_id", natgateway.Id).First(eip)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "fail to fetch eip associate with natgateway %s", natgateway.Id)
|
||||
}
|
||||
eip.SetModelManager(models.ElasticipManager, eip)
|
||||
lockman.LockObject(ctx, eip)
|
||||
defer lockman.ReleaseObject(ctx, eip)
|
||||
iregion, err := eip.GetIRegion()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "fail to fetch iregion of eip %s", eip.Id)
|
||||
}
|
||||
ieip, err := iregion.GetIEipById(eip.GetExternalId())
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "fail to fetch cloudeip of eip %s", eip.Id)
|
||||
}
|
||||
err = eip.SyncInstanceWithCloudEip(ctx, task.GetUserCred(), ieip)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "fail to sync eip %s from cloud", eip.Id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1312,3 +1359,25 @@ func (self *SManagedVirtualizationRegionDriver) RequestPreSnapshotPolicyApply(ct
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SManagedVirtualizationRegionDriver) BindIPToNatgatewayRollback(ctx context.Context, eipId string) error {
|
||||
model, err := models.ElasticipManager.FetchById(eipId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
lockman.LockObject(ctx, model)
|
||||
defer lockman.ReleaseObject(ctx, model)
|
||||
eip := model.(*models.SElasticip)
|
||||
if eip.AssociateType != api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY {
|
||||
return nil
|
||||
}
|
||||
_, err = db.Update(eip, func() error {
|
||||
eip.AssociateId = ""
|
||||
eip.AssociateType = ""
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "rollback about binding eip %s failed", eip.Id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
)
|
||||
|
||||
type iTask interface {
|
||||
taskman.ITask
|
||||
TaskFailed(ctx context.Context, nat models.INatHelper, err error)
|
||||
}
|
||||
|
||||
func NatToBindIPStage(ctx context.Context, task iTask, nat models.INatHelper) {
|
||||
nat.SetStatus(task.GetUserCred(), api.NAT_STATUS_ALLOCATE, "")
|
||||
natgateway, err := nat.GetNatgateway()
|
||||
if err != nil {
|
||||
task.TaskFailed(ctx, nat, errors.Wrap(err, "fetch natgateway failed"))
|
||||
return
|
||||
}
|
||||
task.SetStage("OnBindIPComplete", nil)
|
||||
if !task.GetParams().Contains("need_bind") {
|
||||
task.ScheduleRun(nil)
|
||||
return
|
||||
}
|
||||
|
||||
eipId, _ := task.GetParams().GetString("eip_id")
|
||||
if err := natgateway.GetRegion().GetDriver().RequestBindIPToNatgateway(ctx, task, natgateway, eipId); err != nil {
|
||||
task.TaskFailed(ctx, nat, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func CreateINatFailedRollback(ctx context.Context, task iTask, nat models.INatHelper) error {
|
||||
natgateway, err := nat.GetNatgateway()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetch natgateway failed")
|
||||
}
|
||||
eipId, _ := task.GetParams().GetString("eip_id")
|
||||
err = natgateway.GetRegion().GetDriver().BindIPToNatgatewayRollback(ctx, eipId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
@@ -38,35 +39,21 @@ func init() {
|
||||
taskman.RegisterTask(SNatDEntryCreateTask{})
|
||||
}
|
||||
|
||||
func (self *SNatDEntryCreateTask) TaskFailed(ctx context.Context, dnatEntry *models.SNatDEntry, err error) {
|
||||
func (self *SNatDEntryCreateTask) TaskFailed(ctx context.Context, dnatEntry models.INatHelper, err error) {
|
||||
dnatEntry.SetStatus(self.UserCred, api.NAT_STATUS_FAILED, err.Error())
|
||||
db.OpsLog.LogEvent(dnatEntry, db.ACT_ALLOCATE_FAIL, err.Error(), self.UserCred)
|
||||
logclient.AddActionLogWithStartable(self, dnatEntry, logclient.ACT_ALLOCATE, err.Error(), self.UserCred, false)
|
||||
natgateway, err := dnatEntry.GetNatgateway()
|
||||
if err == nil {
|
||||
logclient.AddActionLogWithStartable(self, natgateway, logclient.ACT_NAT_CREATE_DNAT, nil, self.UserCred, false)
|
||||
} else {
|
||||
logclient.AddActionLogWithStartable(self, dnatEntry, logclient.ACT_ALLOCATE, nil, self.UserCred, false)
|
||||
}
|
||||
self.SetStageFailed(ctx, err.Error())
|
||||
}
|
||||
|
||||
func (self *SNatDEntryCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
dnatEntry := obj.(*models.SNatDEntry)
|
||||
dnatEntry.SetStatus(self.UserCred, api.NAT_STATUS_ALLOCATE, "")
|
||||
|
||||
natgateway, err := dnatEntry.GetNatgateway()
|
||||
if err != nil {
|
||||
self.TaskFailed(ctx, dnatEntry, errors.Wrap(err, "fetch natgateway failed"))
|
||||
return
|
||||
}
|
||||
var needBind bool
|
||||
if self.Params.Contains("need_bind") {
|
||||
needBind = true
|
||||
}
|
||||
|
||||
self.SetStage("OnBindIPComplete", nil)
|
||||
externalIPID, _ := self.Params.GetString("external_ip_id")
|
||||
if err := natgateway.GetRegion().GetDriver().RequestBindIPToNatgateway(ctx, self, natgateway, needBind,
|
||||
externalIPID); err != nil {
|
||||
self.TaskFailed(ctx, dnatEntry, err)
|
||||
return
|
||||
}
|
||||
|
||||
NatToBindIPStage(ctx, self, dnatEntry)
|
||||
}
|
||||
|
||||
func (self *SNatDEntryCreateTask) OnBindIPCompleteFailed(ctx context.Context, dnatEntry *models.SNatDEntry,
|
||||
@@ -84,7 +71,7 @@ func (self *SNatDEntryCreateTask) OnBindIPComplete(ctx context.Context, dnatEntr
|
||||
return
|
||||
}
|
||||
|
||||
externalIPID, err := self.Params.GetString("external_ip_id")
|
||||
externalIPID, err := self.Params.GetString("eip_external_id")
|
||||
// construct a DNat RUle
|
||||
dnatRule := cloudprovider.SNatDRule{
|
||||
Protocol: dnatEntry.IpProtocol,
|
||||
@@ -96,6 +83,13 @@ func (self *SNatDEntryCreateTask) OnBindIPComplete(ctx context.Context, dnatEntr
|
||||
}
|
||||
extDnat, err := cloudNatGateway.CreateINatDEntry(dnatRule)
|
||||
if err != nil {
|
||||
if self.Params.Contains("need_bind") {
|
||||
err1 := CreateINatFailedRollback(ctx, self, dnatEntry)
|
||||
if err1 != nil {
|
||||
eip_id, _ := self.Params.GetString("eip_id")
|
||||
log.Errorf("roll back after failing to create dnat in cloud so that eip %s need to sync with cloud", eip_id)
|
||||
}
|
||||
}
|
||||
self.TaskFailed(ctx, dnatEntry, errors.Wrapf(err, "Create DNat Entry '%s' failed", dnatEntry.ExternalId))
|
||||
return
|
||||
}
|
||||
@@ -114,6 +108,11 @@ func (self *SNatDEntryCreateTask) OnBindIPComplete(ctx context.Context, dnatEntr
|
||||
|
||||
dnatEntry.SetStatus(self.UserCred, api.NAT_STAUTS_AVAILABLE, "")
|
||||
db.OpsLog.LogEvent(dnatEntry, db.ACT_ALLOCATE, dnatEntry.GetShortDesc(ctx), self.UserCred)
|
||||
logclient.AddActionLogWithStartable(self, dnatEntry, logclient.ACT_ALLOCATE, nil, self.UserCred, true)
|
||||
natgateway, err := dnatEntry.GetNatgateway()
|
||||
if err == nil {
|
||||
logclient.AddActionLogWithStartable(self, natgateway, logclient.ACT_NAT_CREATE_DNAT, nil, self.UserCred, true)
|
||||
} else {
|
||||
logclient.AddActionLogWithStartable(self, dnatEntry, logclient.ACT_ALLOCATE, nil, self.UserCred, true)
|
||||
}
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
@@ -37,19 +38,26 @@ func init() {
|
||||
taskman.RegisterTask(SNatDEntryDeleteTask{})
|
||||
}
|
||||
|
||||
func (self *SNatDEntryDeleteTask) taskFailed(ctx context.Context, dnatEntry *models.SNatDEntry, err error) {
|
||||
func (self *SNatDEntryDeleteTask) TaskFailed(ctx context.Context, dnatEntry *models.SNatDEntry, err error) {
|
||||
dnatEntry.SetStatus(self.UserCred, api.NAT_STATUS_DELETE_FAILED, err.Error())
|
||||
db.OpsLog.LogEvent(dnatEntry, db.ACT_DELOCATE_FAIL, err.Error(), self.UserCred)
|
||||
logclient.AddActionLogWithStartable(self, dnatEntry, logclient.ACT_DELETE, err.Error(), self.UserCred, false)
|
||||
natgateway, err := dnatEntry.GetNatgateway()
|
||||
if err != nil {
|
||||
logclient.AddActionLogWithStartable(self, natgateway, logclient.ACT_NAT_DELETE_DNAT, nil, self.UserCred, false)
|
||||
}
|
||||
self.SetStageFailed(ctx, err.Error())
|
||||
}
|
||||
|
||||
func (self *SNatDEntryDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
dnatEntry := obj.(*models.SNatDEntry)
|
||||
dnatEntry.SetStatus(self.UserCred, api.NAT_STATUS_DELETING, "")
|
||||
natgateway, err := dnatEntry.GetNatgateway()
|
||||
if err != nil {
|
||||
self.TaskFailed(ctx, dnatEntry, err)
|
||||
}
|
||||
cloudNatGateway, err := dnatEntry.GetINatGateway()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, dnatEntry, errors.Wrap(err, "Get NatGateway failed"))
|
||||
self.TaskFailed(ctx, dnatEntry, errors.Wrap(err, "Get NatGateway failed"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -57,29 +65,36 @@ func (self *SNatDEntryDeleteTask) OnInit(ctx context.Context, obj db.IStandalone
|
||||
if err == cloudprovider.ErrNotFound {
|
||||
// already delete
|
||||
} else if err != nil {
|
||||
self.taskFailed(ctx, dnatEntry, errors.Wrapf(err, "Get DNat Entry by ID '%s' failed", dnatEntry.ExternalId))
|
||||
self.TaskFailed(ctx, dnatEntry, errors.Wrapf(err, "Get DNat Entry by ID '%s' failed", dnatEntry.ExternalId))
|
||||
return
|
||||
} else if cloudNatDEntry != nil {
|
||||
err = cloudNatDEntry.Delete()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, dnatEntry, errors.Wrapf(err, "Delete DNat Entry '%s' failed", dnatEntry.ExternalId))
|
||||
self.TaskFailed(ctx, dnatEntry, errors.Wrapf(err, "Delete DNat Entry '%s' failed", dnatEntry.ExternalId))
|
||||
return
|
||||
}
|
||||
|
||||
err = cloudprovider.WaitDeleted(cloudNatDEntry, 10*time.Second, 300*time.Second)
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, dnatEntry, err)
|
||||
self.TaskFailed(ctx, dnatEntry, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = dnatEntry.Purge(ctx, self.UserCred)
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, dnatEntry, err)
|
||||
self.TaskFailed(ctx, dnatEntry, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Try to dissociate eip with natgateway if there is no nat rule using this eip and task is set ok even if
|
||||
// dissociate failed.
|
||||
err = natgateway.GetRegion().GetDriver().RequestUnBindIPFromNatgateway(ctx, self, dnatEntry, natgateway)
|
||||
if err != nil {
|
||||
log.Debugf("fail to try to dissociate eip with natgateway %s", natgateway.GetId())
|
||||
}
|
||||
|
||||
db.OpsLog.LogEvent(dnatEntry, db.ACT_DELETE, dnatEntry.GetShortDesc(ctx), self.UserCred)
|
||||
logclient.AddActionLogWithStartable(self, dnatEntry, logclient.ACT_DELETE, nil, self.UserCred, true)
|
||||
logclient.AddActionLogWithStartable(self, natgateway, logclient.ACT_NAT_DELETE_DNAT, nil, self.UserCred, true)
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
@@ -38,35 +39,21 @@ func init() {
|
||||
taskman.RegisterTask(SNatSEntryCreateTask{})
|
||||
}
|
||||
|
||||
func (self *SNatSEntryCreateTask) TaskFailed(ctx context.Context, snatEntry *models.SNatSEntry, err error) {
|
||||
func (self *SNatSEntryCreateTask) TaskFailed(ctx context.Context, snatEntry models.INatHelper, err error) {
|
||||
snatEntry.SetStatus(self.UserCred, api.NAT_STATUS_FAILED, err.Error())
|
||||
db.OpsLog.LogEvent(snatEntry, db.ACT_ALLOCATE_FAIL, err.Error(), self.UserCred)
|
||||
logclient.AddActionLogWithStartable(self, snatEntry, logclient.ACT_ALLOCATE, err.Error(), self.UserCred, false)
|
||||
natgateway, err := snatEntry.GetNatgateway()
|
||||
if err == nil {
|
||||
logclient.AddActionLogWithStartable(self, natgateway, logclient.ACT_NAT_CREATE_SNAT, nil, self.UserCred, false)
|
||||
} else {
|
||||
logclient.AddActionLogWithStartable(self, snatEntry, logclient.ACT_ALLOCATE, nil, self.UserCred, false)
|
||||
}
|
||||
self.SetStageFailed(ctx, err.Error())
|
||||
}
|
||||
|
||||
func (self *SNatSEntryCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
snatEntry := obj.(*models.SNatSEntry)
|
||||
snatEntry.SetStatus(self.UserCred, api.NAT_STATUS_ALLOCATE, "")
|
||||
|
||||
natgateway, err := snatEntry.GetNatgateway()
|
||||
if err != nil {
|
||||
self.TaskFailed(ctx, snatEntry, errors.Wrap(err, "fetch natgateway failed"))
|
||||
return
|
||||
}
|
||||
var needBind bool
|
||||
if self.Params.Contains("need_bind") {
|
||||
needBind = true
|
||||
}
|
||||
|
||||
self.SetStage("OnBindIPComplete", nil)
|
||||
externalIPID, _ := self.Params.GetString("external_ip_id")
|
||||
if err := natgateway.GetRegion().GetDriver().RequestBindIPToNatgateway(ctx, self, natgateway, needBind,
|
||||
externalIPID); err != nil {
|
||||
|
||||
self.TaskFailed(ctx, snatEntry, err)
|
||||
return
|
||||
}
|
||||
NatToBindIPStage(ctx, self, snatEntry)
|
||||
}
|
||||
|
||||
func (self *SNatSEntryCreateTask) OnBindIPCompleteFailed(ctx context.Context, snatEntry *models.SNatSEntry,
|
||||
@@ -84,7 +71,7 @@ func (self *SNatSEntryCreateTask) OnBindIPComplete(ctx context.Context, snatEntr
|
||||
return
|
||||
}
|
||||
|
||||
externalIPID, err := self.Params.GetString("external_ip_id")
|
||||
externalIPID, err := self.Params.GetString("eip_external_id")
|
||||
// construct a DNat RUle
|
||||
snatRule := cloudprovider.SNatSRule{
|
||||
ExternalIP: snatEntry.IP,
|
||||
@@ -98,6 +85,14 @@ func (self *SNatSEntryCreateTask) OnBindIPComplete(ctx context.Context, snatEntr
|
||||
}
|
||||
extSnat, err := cloudNatGateway.CreateINatSEntry(snatRule)
|
||||
if err != nil {
|
||||
// create nat failed in cloud
|
||||
if self.Params.Contains("need_bind") {
|
||||
err1 := CreateINatFailedRollback(ctx, self, snatEntry)
|
||||
if err1 != nil {
|
||||
eip_id, _ := self.Params.GetString("eip_id")
|
||||
log.Errorf("roll back after failing to create snat in cloud so that eip %s need to sync with cloud", eip_id)
|
||||
}
|
||||
}
|
||||
self.TaskFailed(ctx, snatEntry, errors.Wrapf(err, "Create SNat Entry '%s' failed", snatEntry.ExternalId))
|
||||
return
|
||||
}
|
||||
@@ -116,6 +111,11 @@ func (self *SNatSEntryCreateTask) OnBindIPComplete(ctx context.Context, snatEntr
|
||||
|
||||
snatEntry.SetStatus(self.UserCred, api.NAT_STAUTS_AVAILABLE, "")
|
||||
db.OpsLog.LogEvent(snatEntry, db.ACT_ALLOCATE, snatEntry.GetShortDesc(ctx), self.UserCred)
|
||||
logclient.AddActionLogWithStartable(self, snatEntry, logclient.ACT_ALLOCATE, nil, self.UserCred, true)
|
||||
natgateway, err := snatEntry.GetNatgateway()
|
||||
if err == nil {
|
||||
logclient.AddActionLogWithStartable(self, natgateway, logclient.ACT_NAT_CREATE_SNAT, nil, self.UserCred, true)
|
||||
} else {
|
||||
logclient.AddActionLogWithStartable(self, snatEntry, logclient.ACT_ALLOCATE, nil, self.UserCred, true)
|
||||
}
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
@@ -37,48 +38,65 @@ func init() {
|
||||
taskman.RegisterTask(SNatSEntryDeleteTask{})
|
||||
}
|
||||
|
||||
func (self *SNatSEntryDeleteTask) taskFailed(ctx context.Context, snatEntry *models.SNatSEntry, err error) {
|
||||
func (self *SNatSEntryDeleteTask) TaskFailed(ctx context.Context, snatEntry *models.SNatSEntry, err error) {
|
||||
snatEntry.SetStatus(self.UserCred, api.NAT_STATUS_DELETE_FAILED, err.Error())
|
||||
db.OpsLog.LogEvent(snatEntry, db.ACT_DELOCATE_FAIL, err.Error(), self.UserCred)
|
||||
logclient.AddActionLogWithStartable(self, snatEntry, logclient.ACT_DELETE, err.Error(), self.UserCred, false)
|
||||
natgateway, err := snatEntry.GetNatgateway()
|
||||
if err == nil {
|
||||
logclient.AddActionLogWithStartable(self, natgateway, logclient.ACT_NAT_DELETE_SNAT, nil, self.UserCred, false)
|
||||
} else {
|
||||
logclient.AddActionLogWithStartable(self, snatEntry, logclient.ACT_DELETE, nil, self.UserCred, false)
|
||||
}
|
||||
self.SetStageFailed(ctx, err.Error())
|
||||
}
|
||||
|
||||
func (self *SNatSEntryDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
snatEntry := obj.(*models.SNatSEntry)
|
||||
snatEntry.SetStatus(self.UserCred, api.NAT_STATUS_DELETING, "")
|
||||
natgateway, err := snatEntry.GetNatgateway()
|
||||
if err != nil {
|
||||
self.TaskFailed(ctx, snatEntry, err)
|
||||
}
|
||||
cloudNatGateway, err := snatEntry.GetINatGateway()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, snatEntry, errors.Wrap(err, "Get NatGateway failed"))
|
||||
self.TaskFailed(ctx, snatEntry, errors.Wrap(err, "Get NatGateway failed"))
|
||||
return
|
||||
}
|
||||
cloudNatSEntry, err := cloudNatGateway.GetINatSEntryByID(snatEntry.ExternalId)
|
||||
if err == cloudprovider.ErrNotFound {
|
||||
//already delete
|
||||
} else if err != nil {
|
||||
self.taskFailed(ctx, snatEntry, errors.Wrapf(err, "Get SNat Entry by ID '%s' failed", snatEntry.ExternalId))
|
||||
self.TaskFailed(ctx, snatEntry, errors.Wrapf(err, "Get SNat Entry by ID '%s' failed", snatEntry.ExternalId))
|
||||
return
|
||||
} else if cloudNatSEntry != nil {
|
||||
err = cloudNatSEntry.Delete()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, snatEntry, errors.Wrapf(err, "Delete SNat Entry '%s' failed", snatEntry.ExternalId))
|
||||
self.TaskFailed(ctx, snatEntry, errors.Wrapf(err, "Delete SNat Entry '%s' failed", snatEntry.ExternalId))
|
||||
return
|
||||
}
|
||||
|
||||
err = cloudprovider.WaitDeleted(cloudNatSEntry, 10*time.Second, 300*time.Second)
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, snatEntry, err)
|
||||
self.TaskFailed(ctx, snatEntry, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = snatEntry.Purge(ctx, self.UserCred)
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, snatEntry, err)
|
||||
self.TaskFailed(ctx, snatEntry, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Try to dissociate eip with natgateway if there is no nat rule using this eip and task is set ok even if
|
||||
// dissociate failed.
|
||||
err = natgateway.GetRegion().GetDriver().RequestUnBindIPFromNatgateway(ctx, self, snatEntry, natgateway)
|
||||
if err != nil {
|
||||
log.Debugf("fail to try to dissociate eip with natgateway %s", natgateway.GetId())
|
||||
}
|
||||
|
||||
logclient.AddActionLogWithStartable(self, natgateway, logclient.ACT_NAT_DELETE_SNAT, nil, self.UserCred, true)
|
||||
|
||||
db.OpsLog.LogEvent(snatEntry, db.ACT_DELETE, snatEntry.GetShortDesc(ctx), self.UserCred)
|
||||
logclient.AddActionLogWithStartable(self, snatEntry, logclient.ACT_DELETE, nil, self.UserCred, true)
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
|
||||
@@ -147,4 +147,9 @@ const (
|
||||
ACT_MKDIR = "创建目录"
|
||||
ACT_DELETE_OBJECT = "删除对象"
|
||||
ACT_UPLOAD_OBJECT = "上传对象"
|
||||
|
||||
ACT_NAT_CREATE_SNAT = "创建SNAT规则"
|
||||
ACT_NAT_CREATE_DNAT = "创建DNAT规则"
|
||||
ACT_NAT_DELETE_SNAT = "删除SNAT规则"
|
||||
ACT_NAT_DELETE_DNAT = "删除DNAT规则"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user