mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #2075 from Mjoycarry/feature/nat_api
nat create delete restful api finished
This commit is contained in:
@@ -15,12 +15,16 @@
|
||||
package compute
|
||||
|
||||
const (
|
||||
NAT_STAUTS_AVAILABLE = "available" //可用
|
||||
NAT_STATUS_ALLOCATE = "allocate" //创建中
|
||||
NAT_STATUS_DEPLOYING = "deploying" //配置中
|
||||
NAT_STATUS_UNKNOWN = "unknown"
|
||||
NAT_STAUTS_AVAILABLE = "available" //可用
|
||||
NAT_STATUS_ALLOCATE = "allocate" //创建中
|
||||
NAT_STATUS_DEPLOYING = "deploying" //配置中
|
||||
NAT_STATUS_UNKNOWN = "unknown"
|
||||
NAT_STATUS_FAILED = "failed"
|
||||
NAT_STATUS_DELETED = "deleted"
|
||||
NAT_STATUS_DELETING = "deleting"
|
||||
NAT_STATUS_DELETE_FAILED = "delete_failed"
|
||||
|
||||
QCLOUD_NAT_SPEC_SMALL = "Small"
|
||||
QCLOUD_NAT_SPEC_MIDDLE = "Middle"
|
||||
QCLOUD_NAT_SPEC_LARGE = "Large"
|
||||
QCLOUD_NAT_SPEC_SMALL = "small"
|
||||
QCLOUD_NAT_SPEC_MIDDLE = "middle"
|
||||
QCLOUD_NAT_SPEC_LARGE = "large"
|
||||
)
|
||||
|
||||
@@ -316,9 +316,9 @@ func syncNatDTable(ctx context.Context, userCred mcclient.TokenCredential, provi
|
||||
log.Errorf(msg)
|
||||
return
|
||||
}
|
||||
result := NatDEntryManager.SyncNatDTables(ctx, userCred, provider.GetOwnerId(), provider, localNatGateway, dtable)
|
||||
result := NatDEntryManager.SyncNatDTable(ctx, userCred, provider.GetOwnerId(), provider, localNatGateway, dtable)
|
||||
msg := result.Result()
|
||||
log.Infof("SyncNatDTables for NatGateway %s result: %s", localNatGateway.Name, msg)
|
||||
log.Infof("SyncNatDTable for NatGateway %s result: %s", localNatGateway.Name, msg)
|
||||
if result.IsError() {
|
||||
return
|
||||
}
|
||||
@@ -332,9 +332,9 @@ func syncNatSTable(ctx context.Context, userCred mcclient.TokenCredential, provi
|
||||
log.Errorf(msg)
|
||||
return
|
||||
}
|
||||
result := NatSEntryManager.SyncNatSTables(ctx, userCred, provider.GetOwnerId(), provider, localNatGateway, stable)
|
||||
result := NatSEntryManager.SyncNatSTable(ctx, userCred, provider.GetOwnerId(), provider, localNatGateway, stable)
|
||||
msg := result.Result()
|
||||
log.Infof("SyncNatSTables for NatGateway %s result: %s", localNatGateway.Name, msg)
|
||||
log.Infof("SyncNatSTable for NatGateway %s result: %s", localNatGateway.Name, msg)
|
||||
if result.IsError() {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -19,15 +19,16 @@ import (
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
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"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
@@ -42,8 +43,8 @@ func init() {
|
||||
SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
|
||||
SNatDEntry{},
|
||||
"natdtables_tbl",
|
||||
"natdtable",
|
||||
"natdtables",
|
||||
"natdentry",
|
||||
"natdentries",
|
||||
),
|
||||
}
|
||||
NatDEntryManager.SetVirtualObject(NatDEntryManager)
|
||||
@@ -119,15 +120,18 @@ func (man *SNatDEntryManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQ
|
||||
}
|
||||
|
||||
func (man *SNatDEntryManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
return nil, httperrors.NewNotImplementedError("Not Implemented")
|
||||
if !data.Contains("external_ip_id") {
|
||||
return nil, errors.Error("Request body should contain key 'externalIpId'")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (manager *SNatDEntryManager) SyncNatDTables(ctx context.Context, userCred mcclient.TokenCredential, syncOwnerId mcclient.IIdentityProvider, provider *SCloudprovider, nat *SNatGateway, extDTable []cloudprovider.ICloudNatDEntry) compare.SyncResult {
|
||||
func (manager *SNatDEntryManager) SyncNatDTable(ctx context.Context, userCred mcclient.TokenCredential, syncOwnerId mcclient.IIdentityProvider, provider *SCloudprovider, nat *SNatGateway, extDTable []cloudprovider.ICloudNatDEntry) compare.SyncResult {
|
||||
lockman.LockClass(ctx, manager, db.GetLockClassKey(manager, syncOwnerId))
|
||||
defer lockman.ReleaseClass(ctx, manager, db.GetLockClassKey(manager, syncOwnerId))
|
||||
|
||||
result := compare.SyncResult{}
|
||||
dbNatDTables, err := nat.GetDTables()
|
||||
dbNatDTables, err := nat.GetDTable()
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
@@ -249,3 +253,52 @@ func (self *SNatDEntry) GetCustomizeColumns(ctx context.Context, userCred mcclie
|
||||
extra.Add(jsonutils.NewString(natgateway.Name), "natgateway")
|
||||
return extra
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
if len(self.NatgatewayId) == 0 {
|
||||
return
|
||||
}
|
||||
// ValidateCreateData function make data must contain 'externalIpId' key
|
||||
externalIPID, _ := data.GetString("external_ip_id")
|
||||
taskData := jsonutils.NewDict()
|
||||
taskData.Set("external_ip_id", jsonutils.NewString(externalIPID))
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "SNatDEntryCreateTask", self, userCred, taskData, "", "", nil)
|
||||
if err != nil {
|
||||
log.Errorf("SNatDEntryCreateTask newTask error %s", err)
|
||||
} else {
|
||||
task.ScheduleRun(nil)
|
||||
}
|
||||
}
|
||||
|
||||
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) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
|
||||
if len(self.ExternalId) > 0 {
|
||||
return self.startDeleteVpcTask(ctx, userCred)
|
||||
} else {
|
||||
return self.realDelete(ctx, userCred)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) realDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
db.OpsLog.LogEvent(self, db.ACT_DELOCATE, self.GetShortDesc(ctx), userCred)
|
||||
self.SetStatus(userCred, api.NAT_STATUS_DELETED, "real delete")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SNatDEntry) startDeleteVpcTask(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)
|
||||
return err
|
||||
}
|
||||
task.ScheduleRun(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
@@ -127,7 +128,7 @@ func (manager *SNatGetewayManager) getNatgatewaysByProviderId(providerId string)
|
||||
return nats, nil
|
||||
}
|
||||
|
||||
func (self *SNatGateway) GetDTables() ([]SNatDEntry, error) {
|
||||
func (self *SNatGateway) GetDTable() ([]SNatDEntry, error) {
|
||||
tables := []SNatDEntry{}
|
||||
q := NatDEntryManager.Query().Equals("natgateway_id", self.Id)
|
||||
err := db.FetchModelObjects(NatDEntryManager, q, &tables)
|
||||
@@ -137,7 +138,7 @@ func (self *SNatGateway) GetDTables() ([]SNatDEntry, error) {
|
||||
return tables, nil
|
||||
}
|
||||
|
||||
func (self *SNatGateway) GetSTables() ([]SNatSEntry, error) {
|
||||
func (self *SNatGateway) GetSTable() ([]SNatSEntry, error) {
|
||||
tables := []SNatSEntry{}
|
||||
q := NatSEntryManager.Query().Equals("natgateway_id", self.Id)
|
||||
err := db.FetchModelObjects(NatSEntryManager, q, &tables)
|
||||
@@ -364,3 +365,25 @@ func (self *SNatGateway) SyncNatGatewayEips(ctx context.Context, userCred mcclie
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (self *SNatGateway) GetINatGateway() (cloudprovider.ICloudNatGateway, error) {
|
||||
model, err := VpcManager.FetchById(self.VpcId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Fetch vpc by ID failed")
|
||||
}
|
||||
vpc := model.(*SVpc)
|
||||
cloudVpc, err := vpc.GetIVpc()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Fetch IVpc failed")
|
||||
}
|
||||
cloudNatGateways, err := cloudVpc.GetINatGateways()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "Get INatGateways of vpc %s failed", cloudVpc.GetGlobalId())
|
||||
}
|
||||
for i := range cloudNatGateways {
|
||||
if cloudNatGateways[i].GetGlobalId() == self.ExternalId {
|
||||
return cloudNatGateways[i], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.Error("CloudNatGateway Not Found")
|
||||
}
|
||||
|
||||
@@ -19,15 +19,16 @@ import (
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
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"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
@@ -42,8 +43,8 @@ func init() {
|
||||
SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
|
||||
SNatSEntry{},
|
||||
"natstables_tbl",
|
||||
"natstable",
|
||||
"natstables",
|
||||
"natsentry",
|
||||
"natsentries",
|
||||
),
|
||||
}
|
||||
NatSEntryManager.SetVirtualObject(NatSEntryManager)
|
||||
@@ -125,15 +126,18 @@ func (man *SNatSEntryManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQ
|
||||
}
|
||||
|
||||
func (man *SNatSEntryManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
return nil, httperrors.NewNotImplementedError("Not Implemented")
|
||||
if !data.Contains("external_ip_id") {
|
||||
return nil, errors.Error("Request body should contain key 'externalIpId'")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (manager *SNatSEntryManager) SyncNatSTables(ctx context.Context, userCred mcclient.TokenCredential, syncOwnerId mcclient.IIdentityProvider, provider *SCloudprovider, nat *SNatGateway, extTable []cloudprovider.ICloudNatSEntry) compare.SyncResult {
|
||||
func (manager *SNatSEntryManager) SyncNatSTable(ctx context.Context, userCred mcclient.TokenCredential, syncOwnerId mcclient.IIdentityProvider, provider *SCloudprovider, nat *SNatGateway, extTable []cloudprovider.ICloudNatSEntry) compare.SyncResult {
|
||||
lockman.LockClass(ctx, manager, db.GetLockClassKey(manager, syncOwnerId))
|
||||
defer lockman.ReleaseClass(ctx, manager, db.GetLockClassKey(manager, syncOwnerId))
|
||||
|
||||
result := compare.SyncResult{}
|
||||
dbNatSTables, err := nat.GetSTables()
|
||||
dbNatSTables, err := nat.GetSTable()
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
@@ -270,3 +274,52 @@ func (self *SNatSEntry) GetCustomizeColumns(ctx context.Context, userCred mcclie
|
||||
extra.Add(jsonutils.NewString(network.Name), "network")
|
||||
return extra
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
if len(self.NatgatewayId) == 0 {
|
||||
return
|
||||
}
|
||||
// ValidateCreateData function make data must contain 'externalIpId' key
|
||||
externalIPID, _ := data.GetString("external_ip_id")
|
||||
taskData := jsonutils.NewDict()
|
||||
taskData.Set("external_ip_id", jsonutils.NewString(externalIPID))
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "SNatSEntryCreateTask", self, userCred, taskData, "", "", nil)
|
||||
if err != nil {
|
||||
log.Errorf("SNatSEntryCreateTask newTask error %s", err)
|
||||
} else {
|
||||
task.ScheduleRun(nil)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
} else {
|
||||
return self.realDelete(ctx, userCred)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) realDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
db.OpsLog.LogEvent(self, db.ACT_DELOCATE, self.GetShortDesc(ctx), userCred)
|
||||
self.SetStatus(userCred, api.NAT_STATUS_DELETED, "real delete")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SNatSEntry) startDeleteVpcTask(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)
|
||||
return err
|
||||
}
|
||||
task.ScheduleRun(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -949,7 +949,7 @@ func (table *SNatSEntry) purge(ctx context.Context, userCred mcclient.TokenCrede
|
||||
}
|
||||
|
||||
func (nat *SNatGateway) purgeSTables(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
tables, err := nat.GetSTables()
|
||||
tables, err := nat.GetSTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -976,7 +976,7 @@ func (table *SNatDEntry) purge(ctx context.Context, userCred mcclient.TokenCrede
|
||||
}
|
||||
|
||||
func (nat *SNatGateway) purgeDTables(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
tables, err := nat.GetDTables()
|
||||
tables, err := nat.GetDTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
// 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/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
)
|
||||
|
||||
type SNatDEntryCreateTask struct {
|
||||
taskman.STask
|
||||
}
|
||||
|
||||
func init() {
|
||||
taskman.RegisterTask(SNatDEntryCreateTask{})
|
||||
}
|
||||
|
||||
func (self *SNatDEntryCreateTask) TaskFailed(ctx context.Context, dnatEntry *models.SNatDEntry, 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)
|
||||
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, "")
|
||||
cloudNatGateway, err := dnatEntry.GetINatGateway()
|
||||
if err != nil {
|
||||
self.TaskFailed(ctx, dnatEntry, errors.Wrap(err, "Get NatGateway failed"))
|
||||
return
|
||||
}
|
||||
|
||||
externalIPID, err := body.GetString("external_ip_id")
|
||||
// construct a DNat RUle
|
||||
dnatRule := cloudprovider.SNatDRule{
|
||||
Protocol: dnatEntry.IpProtocol,
|
||||
InternalIP: dnatEntry.InternalIP,
|
||||
InternalPort: dnatEntry.InternalPort,
|
||||
ExternalIP: dnatEntry.ExternalIP,
|
||||
ExternalIPID: externalIPID,
|
||||
ExternalPort: dnatEntry.ExternalPort,
|
||||
}
|
||||
_, err = cloudNatGateway.CreateINatDEntry(dnatRule)
|
||||
if err != nil {
|
||||
self.TaskFailed(ctx, dnatEntry, errors.Wrapf(err, "Create DNat Entry '%s' failed", dnatEntry.ExternalId))
|
||||
return
|
||||
}
|
||||
|
||||
dnatEntry.SetStatus(self.UserCred, api.NAT_STAUTS_AVAILABLE, "")
|
||||
|
||||
logclient.AddActionLogWithStartable(self, dnatEntry, logclient.ACT_ALLOCATE, nil, self.UserCred, true)
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
// 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/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
)
|
||||
|
||||
type SNatDEntryDeleteTask struct {
|
||||
taskman.STask
|
||||
}
|
||||
|
||||
func init() {
|
||||
taskman.RegisterTask(SNatDEntryDeleteTask{})
|
||||
}
|
||||
|
||||
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)
|
||||
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_ALLOCATE, "")
|
||||
cloudNatGateway, err := dnatEntry.GetINatGateway()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, dnatEntry, errors.Wrap(err, "Get NatGateway failed"))
|
||||
return
|
||||
}
|
||||
cloudNatDEntry, err := cloudNatGateway.GetINatDEntryByID(dnatEntry.ExternalId)
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, dnatEntry, errors.Wrapf(err, "Get DNat Entry by ID '%s' failed", dnatEntry.ExternalId))
|
||||
}
|
||||
err = cloudNatDEntry.Delete()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, dnatEntry, errors.Wrapf(err, "Delete DNat Entry '%s' failed", dnatEntry.ExternalId))
|
||||
}
|
||||
dnatEntry.SetStatus(self.UserCred, api.NAT_STATUS_DELETED, "")
|
||||
|
||||
logclient.AddActionLogWithStartable(self, dnatEntry, logclient.ACT_DELETE, nil, self.UserCred, true)
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
// 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/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
)
|
||||
|
||||
type SNatSEntryCreateTask struct {
|
||||
taskman.STask
|
||||
}
|
||||
|
||||
func init() {
|
||||
taskman.RegisterTask(SNatSEntryCreateTask{})
|
||||
}
|
||||
|
||||
func (self *SNatSEntryCreateTask) TaskFailed(ctx context.Context, snatEntry *models.SNatSEntry, 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)
|
||||
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, "")
|
||||
cloudNatGateway, err := snatEntry.GetINatGateway()
|
||||
if err != nil {
|
||||
self.TaskFailed(ctx, snatEntry, errors.Wrap(err, "Get NatGateway failed"))
|
||||
return
|
||||
}
|
||||
|
||||
externalIPID, err := body.GetString("external_ip_id")
|
||||
// construct a DNat RUle
|
||||
snatRule := cloudprovider.SNatSRule{
|
||||
SourceCIDR: snatEntry.SourceCIDR,
|
||||
ExternalIP: snatEntry.IP,
|
||||
ExternalIPID: externalIPID,
|
||||
}
|
||||
_, err = cloudNatGateway.CreateINatSEntry(snatRule)
|
||||
if err != nil {
|
||||
self.TaskFailed(ctx, snatEntry, errors.Wrapf(err, "Create SNat Entry '%s' failed", snatEntry.ExternalId))
|
||||
return
|
||||
}
|
||||
|
||||
snatEntry.SetStatus(self.UserCred, api.NAT_STAUTS_AVAILABLE, "")
|
||||
|
||||
logclient.AddActionLogWithStartable(self, snatEntry, logclient.ACT_ALLOCATE, nil, self.UserCred, true)
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
// 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/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
)
|
||||
|
||||
type SNatSEntryDeleteTask struct {
|
||||
taskman.STask
|
||||
}
|
||||
|
||||
func init() {
|
||||
taskman.RegisterTask(SNatSEntryDeleteTask{})
|
||||
}
|
||||
|
||||
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)
|
||||
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_ALLOCATE, "")
|
||||
cloudNatGateway, err := snatEntry.GetINatGateway()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, snatEntry, errors.Wrap(err, "Get NatGateway failed"))
|
||||
return
|
||||
}
|
||||
cloudNatDEntry, err := cloudNatGateway.GetINatSEntryByID(snatEntry.ExternalId)
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, snatEntry, errors.Wrapf(err, "Get SNat Entry by ID '%s' failed", snatEntry.ExternalId))
|
||||
}
|
||||
err = cloudNatDEntry.Delete()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, snatEntry, errors.Wrapf(err, "Delete SNat Entry '%s' failed", snatEntry.ExternalId))
|
||||
}
|
||||
snatEntry.SetStatus(self.UserCred, api.NAT_STATUS_DELETED, "")
|
||||
|
||||
logclient.AddActionLogWithStartable(self, snatEntry, logclient.ACT_DELETE, nil, self.UserCred, true)
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
@@ -15,13 +15,13 @@
|
||||
package modules
|
||||
|
||||
var (
|
||||
NatDTables ResourceManager
|
||||
NatDTable ResourceManager
|
||||
)
|
||||
|
||||
func init() {
|
||||
NatDTables = NewComputeManager("natdtable", "natdtables",
|
||||
NatDTable = NewComputeManager("natdentry", "natdentries",
|
||||
[]string{"ID", "Name", "Status", "Natgateway_Id", "Natgateway", "External_IP", "External_Port", "Internal_IP", "Internal_Port", "Ip_Protocol"},
|
||||
[]string{})
|
||||
|
||||
registerCompute(&NatDTables)
|
||||
registerCompute(&NatDTable)
|
||||
}
|
||||
|
||||
@@ -15,13 +15,13 @@
|
||||
package modules
|
||||
|
||||
var (
|
||||
NatSTables ResourceManager
|
||||
NatSTable ResourceManager
|
||||
)
|
||||
|
||||
func init() {
|
||||
NatSTables = NewComputeManager("natstable", "natstables",
|
||||
NatSTable = NewComputeManager("natsentry", "natsentries",
|
||||
[]string{"ID", "Name", "Status", "IP", "Natgateway_Id", "Natgateway", "Network", "Network_Id", "Source_CIDR"},
|
||||
[]string{})
|
||||
|
||||
registerCompute(&NatSTables)
|
||||
registerCompute(&NatSTable)
|
||||
}
|
||||
|
||||
@@ -33,3 +33,34 @@ type NatSTableListOptions struct {
|
||||
|
||||
BaseListOptions
|
||||
}
|
||||
|
||||
type NatDDeleteShowOptions struct {
|
||||
ID string `help:"ID of the DNat"`
|
||||
}
|
||||
|
||||
type NatSDeleteShowOptions struct {
|
||||
ID string `help:"ID of the SNat"`
|
||||
}
|
||||
|
||||
type NatGatewayShowOptions struct {
|
||||
ID string `help:"ID of Nat Gateway"`
|
||||
}
|
||||
|
||||
type NatDCreateOptions struct {
|
||||
NAME string `help:"DNAT's name"`
|
||||
NATGATEWAYID string `help:"The nat gateway'id to which DNat belongs"`
|
||||
INTERNALIP string `help:"Internal IP"`
|
||||
INTERNALPORT string `help:"Internal Port"`
|
||||
EXTERNALIP string `help:"External IP"`
|
||||
EXTERNALIPID string `help:"External IP ID, can be empty except huawei Cloud"`
|
||||
EXTERNALPORT string `help:"External Port"`
|
||||
IPPROTOCOL string `help:"Transport Protocol(tcp|udp)"`
|
||||
}
|
||||
|
||||
type NatSCreateOptions struct {
|
||||
NAME string `help:"SNAT's name"`
|
||||
NATGATEWAYID string `help:"The nat gateway'id to which SNat belongs"`
|
||||
IP string `help:"External IP"`
|
||||
EXTERNALIPID string `help:"External IP ID, can be empty except huawei Cloud"`
|
||||
SOURCECIDR string `help:"Source CIDR"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user