Skip to content

Commit

Permalink
[improvement] #55
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Mar 15, 2021
1 parent 41f8212 commit b243a94
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 20 deletions.
8 changes: 8 additions & 0 deletions pkg/api/core/group/connection/v0/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ func UpdateAdmin(c *gin.Context) {
return
}

tmp := dbConnection.Get(connection.ID, &core.Connection{Model: gorm.Model{ID: uint(id)}})
if tmp.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: tmp.Err.Error()})
return
}

noticeSlackAdmin(tmp.Connection[0], input)

input.ID = uint(id)

if err = dbConnection.Update(connection.UpdateAll, input); err != nil {
Expand Down
143 changes: 143 additions & 0 deletions pkg/api/core/group/connection/v0/slack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package v0

import (
"github.com/ashwanthkumar/slack-go-webhook"
"github.com/homenoc/dsbd-backend/pkg/api/core"
"github.com/homenoc/dsbd-backend/pkg/api/core/noc"
"github.com/homenoc/dsbd-backend/pkg/api/core/noc/bgpRouter"
"github.com/homenoc/dsbd-backend/pkg/api/core/noc/tunnelEndPointRouterIP"
connectionTemplate "github.com/homenoc/dsbd-backend/pkg/api/core/template/connection"
ntt "github.com/homenoc/dsbd-backend/pkg/api/core/template/ntt"
"github.com/homenoc/dsbd-backend/pkg/api/core/tool/notification"
dbBGPRouter "github.com/homenoc/dsbd-backend/pkg/api/store/noc/bgpRouter/v0"
dbTunnelEndPointRouterIP "github.com/homenoc/dsbd-backend/pkg/api/store/noc/tunnelEndPointRouterIP/v0"
dbNOC "github.com/homenoc/dsbd-backend/pkg/api/store/noc/v0"
dbConnectionTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/connection/v0"
dbNTTTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/ntt/v0"
"github.com/jinzhu/gorm"
"strconv"
)

func noticeSlackAdmin(before, after core.Connection) {
// 審査ステータスのSlack通知
attachment := slack.Attachment{}

attachment.AddField(slack.Field{Title: "Title", Value: "Connection情報の更新"}).
AddField(slack.Field{Title: "申請者", Value: "管理者"}).
AddField(slack.Field{Title: "Group", Value: strconv.Itoa(int(before.ID)) + "-" + before.Service.Group.Org}).
AddField(slack.Field{Title: "更新状況", Value: changeText(before, after)})
notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true})
}

func changeText(before, after core.Connection) string {
data := ""
if after.Open != nil {
if *before.Open != *after.Open {
if *after.Open {
data += "開通: 未開通 => 開通済み\n"
} else {
data += "開通: 開通 => 未開通\n"
}
}
}

if after.Lock != nil {
if *before.Lock != *after.Lock {
if *after.Lock {
data += "ユーザ変更: 禁止 => 許可\n"
} else {
data += "ユーザ変更: 許可 => 禁止\n"
}
}
}

if after.ConnectionTemplateID != nil {
if *before.ConnectionTemplateID != *after.ConnectionTemplateID {
data += "接続ID: " + before.ConnectionTemplate.Type + " => " +
connectionTemplateText(*after.ConnectionTemplateID) + "\n"
}
}

if after.BGPRouterID != nil {
if *before.BGPRouterID != *after.BGPRouterID {
data += "BGPルータ: " + before.BGPRouter.HostName + " => " + bgpRouterText(*after.BGPRouterID) + "\n"
}
}

if after.TunnelEndPointRouterIPID != nil {
if *before.TunnelEndPointRouterIPID != *after.TunnelEndPointRouterIPID {
data += "トンネルエンドポイントルータ: " + before.TunnelEndPointRouterIP.TunnelEndPointRouter.HostName + " " +
before.TunnelEndPointRouterIP.IP + " => " +
tunnelEndPointRouterIPText(*after.TunnelEndPointRouterIPID) + "\n"
}
}

if after.NTTTemplateID != nil {
if *before.NTTTemplateID != *after.NTTTemplateID {
data += "インターネット接続: " + before.NTTTemplate.Name + " => " + nttTemplateText(*after.NTTTemplateID) + "\n"
}
}

if after.NOCID != nil {
if *before.NOCID != *after.NOCID {
data += "希望NOC: " + before.NOC.Name + " => " + nocText(*after.NOCID) + "\n"
}
}

if after.TermIP != "" {
data += "終端アドレス: " + before.TermIP + "=>" + after.TermIP + "\n"
}

if after.LinkV4Our != "" {
data += "v4(HomeNOC側): " + before.LinkV4Our + "=>" + after.LinkV4Our + "\n"
}

if after.LinkV4Your != "" {
data += "v4(相手団体側): " + before.LinkV4Your + "=>" + after.LinkV4Your + "\n"
}

if after.LinkV6Our != "" {
data += "v6(HomeNOC側): " + before.LinkV6Our + "=>" + after.LinkV6Our + "\n"
}

if after.LinkV6Your != "" {
data += "v6(相手団体側): " + before.LinkV6Your + "=>" + after.LinkV6Your + "\n"
}

return data
}

func connectionTemplateText(status uint) string {
result := dbConnectionTemplate.Get(connectionTemplate.ID, &core.ConnectionTemplate{Model: gorm.Model{ID: status}})
return result.Connections[0].Type
}

func bgpRouterText(status uint) string {
if status != 0 {
result := dbBGPRouter.Get(bgpRouter.ID, &core.BGPRouter{Model: gorm.Model{ID: status}})
return result.BGPRouter[0].HostName
} else {
return "なし"
}
}

func nttTemplateText(status uint) string {
result := dbNTTTemplate.Get(ntt.ID, &core.NTTTemplate{Model: gorm.Model{ID: status}})
return result.NTTs[0].Name
}

func tunnelEndPointRouterIPText(status uint) string {
if status != 0 {
result := dbTunnelEndPointRouterIP.Get(tunnelEndPointRouterIP.ID,
&core.TunnelEndPointRouterIP{Model: gorm.Model{ID: status}})
return result.TunnelEndPointRouterIP[0].TunnelEndPointRouter.HostName + " " +
result.TunnelEndPointRouterIP[0].IP
} else {
return "なし"
}
}

func nocText(status uint) string {
result := dbNOC.Get(noc.ID, &core.NOC{Model: gorm.Model{ID: status}})
return result.NOC[0].Name
}
10 changes: 6 additions & 4 deletions pkg/api/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Service struct {
Open *bool `json:"open"`
Lock *bool `json:"lock"`
AddAllow *bool `json:"add_allow"`
Group Group `json:"group"`
}

type Connection struct {
Expand Down Expand Up @@ -144,10 +145,11 @@ type TunnelEndPointRouter struct {

type TunnelEndPointRouterIP struct {
gorm.Model
TunnelEndPointRouterID uint `json:"tunnel_endpoint_router_id"`
IP string `json:"ip"`
Enable *bool `json:"enable"`
Comment string `json:"comment"`
TunnelEndPointRouter TunnelEndPointRouter `json:"tunnel_endpoint_router"`
TunnelEndPointRouterID uint `json:"tunnel_endpoint_router_id"`
IP string `json:"ip"`
Enable *bool `json:"enable"`
Comment string `json:"comment"`
}

type IP struct {
Expand Down
17 changes: 2 additions & 15 deletions pkg/api/store/group/connection/v0/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,7 @@ func Update(base int, c core.Connection) error {
} else if connection.UpdateServiceID == base {
result = db.Model(&core.Connection{Model: gorm.Model{ID: c.ID}}).Update(core.Connection{ServiceID: c.ServiceID})
} else if base == connection.UpdateAll {
result = db.Model(&core.Connection{Model: gorm.Model{ID: c.ID}}).Update(core.Connection{
ServiceID: c.ServiceID,
BGPRouterID: c.BGPRouterID,
TunnelEndPointRouterIPID: c.TunnelEndPointRouterIPID,
NTTTemplateID: c.NTTTemplateID,
NOC: c.NOC,
TermIP: c.TermIP,
Monitor: c.Monitor,
LinkV4Our: c.LinkV4Our,
LinkV4Your: c.LinkV4Your,
LinkV6Our: c.LinkV6Our,
LinkV6Your: c.LinkV6Your,
Open: c.Open,
Lock: c.Lock,
})
result = db.Model(&core.Connection{Model: gorm.Model{ID: c.ID}}).Update(c)
} else {
log.Println("base select error")
return fmt.Errorf("(%s)error: base select\n", time.Now())
Expand All @@ -93,6 +79,7 @@ func Get(base int, data *core.Connection) connection.ResultDatabase {
Preload("NTTTemplate").
Preload("Service").
Preload("Service.ServiceTemplate").
Preload("Service.Group").
First(&connectionStruct, data.ID).Error
} else if base == connection.ServiceID {
err = db.Where("service_id = ?", data.ServiceID).Find(&connectionStruct).Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func Get(base int, data *core.TunnelEndPointRouterIP) tunnelEndPointRouterIP.Res
var routerStruct []core.TunnelEndPointRouterIP

if base == tunnelEndPointRouterIP.ID { //ID
err = db.First(&routerStruct, data.ID).Error
err = db.Preload("TunnelEndPointRouter").
First(&routerStruct, data.ID).Error
} else if base == tunnelEndPointRouterIP.Enable { //GroupID
err = db.Where("enable = ?", data.Enable).Find(&routerStruct).Error
} else {
Expand Down

0 comments on commit b243a94

Please sign in to comment.