Procházet zdrojové kódy

add status to egress model

abhishek9686 před 4 měsíci
rodič
revize
9c0729e61e
3 změnil soubory, kde provedl 17 přidání a 0 odebrání
  1. 9 0
      controllers/egress.go
  2. 1 0
      models/egress.go
  3. 7 0
      schema/egress.go

+ 9 - 0
controllers/egress.go

@@ -65,6 +65,7 @@ func createEgress(w http.ResponseWriter, r *http.Request) {
 		IsInetGw:    req.IsInetGw,
 		Nodes:       make(datatypes.JSONMap),
 		Tags:        make(datatypes.JSONMap),
+		Status:      true,
 		CreatedBy:   r.Header.Get("user"),
 		CreatedAt:   time.Now().UTC(),
 	}
@@ -156,12 +157,16 @@ func updateEgress(w http.ResponseWriter, r *http.Request) {
 
 	var updateNat bool
 	var updateInetGw bool
+	var updateStatus bool
 	if req.Nat != e.Nat {
 		updateNat = true
 	}
 	if req.IsInetGw != e.IsInetGw {
 		updateInetGw = true
 	}
+	if req.Status != e.Status {
+		updateStatus = true
+	}
 	e.Nodes = make(datatypes.JSONMap)
 	e.Tags = make(datatypes.JSONMap)
 	for nodeID, metric := range req.Nodes {
@@ -192,6 +197,10 @@ func updateEgress(w http.ResponseWriter, r *http.Request) {
 		e.IsInetGw = req.IsInetGw
 		e.UpdateINetGwStatus(db.WithContext(context.TODO()))
 	}
+	if updateStatus {
+		e.Status = req.Status
+		e.UpdateEgressStatus(db.WithContext(context.TODO()))
+	}
 	go mq.PublishPeerUpdate(false)
 	logic.ReturnSuccessResponseWithJson(w, r, e, "updated egress resource")
 }

+ 1 - 0
models/egress.go

@@ -9,5 +9,6 @@ type EgressReq struct {
 	Tags        []string       `json:"tags"`
 	Range       string         `json:"range"`
 	Nat         bool           `json:"nat"`
+	Status      bool           `json:"status"`
 	IsInetGw    bool           `json:"is_internet_gateway"`
 }

+ 7 - 0
schema/egress.go

@@ -20,6 +20,7 @@ type Egress struct {
 	Range       string            `gorm:"range" json:"range"`
 	Nat         bool              `gorm:"nat" json:"nat"`
 	IsInetGw    bool              `gorm:"is_internet_gateway" json:"is_internet_gateway"`
+	Status      bool              `gorm:"status" json:"status"`
 	CreatedBy   string            `gorm:"created_by" json:"created_by"`
 	CreatedAt   time.Time         `gorm:"created_at" json:"created_at"`
 	UpdatedAt   time.Time         `gorm:"updated_at" json:"updated_at"`
@@ -49,6 +50,12 @@ func (e *Egress) UpdateINetGwStatus(ctx context.Context) error {
 	}).Error
 }
 
+func (e *Egress) UpdateEgressStatus(ctx context.Context) error {
+	return db.FromContext(ctx).Table(e.Table()).Where("id = ?", e.ID).Updates(map[string]any{
+		"status": e.Status,
+	}).Error
+}
+
 func (e *Egress) Create(ctx context.Context) error {
 	return db.FromContext(ctx).Table(e.Table()).Create(&e).Error
 }