-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheckMailServer.go
More file actions
85 lines (73 loc) · 2.64 KB
/
Copy pathcheckMailServer.go
File metadata and controls
85 lines (73 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package mailcontroller
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"github.com/greenbone/opensight-golang-libraries/pkg/errorResponses"
"github.com/greenbone/opensight-notification-service/pkg/services/notificationchannelservice"
"github.com/greenbone/opensight-notification-service/pkg/web/errmap"
"github.com/greenbone/opensight-notification-service/pkg/web/ginEx"
"github.com/greenbone/opensight-notification-service/pkg/web/iam"
"github.com/greenbone/opensight-notification-service/pkg/web/mailcontroller/maildto"
"github.com/greenbone/opensight-notification-service/pkg/web/middleware"
)
type CheckMailServerController struct {
notificationChannelServicer notificationchannelservice.MailChannelService
}
func AddCheckMailServerController(
router gin.IRouter,
notificationChannelServicer notificationchannelservice.MailChannelService,
auth gin.HandlerFunc,
registry errmap.ErrorRegistry,
) *CheckMailServerController {
ctrl := &CheckMailServerController{
notificationChannelServicer: notificationChannelServicer,
}
group := router.Group("/notification-channel/mail").
Use(middleware.AuthorizeRoles(auth, iam.OsiAdmin, iam.NotificationAdmin)...)
group.POST("/check", ctrl.CheckMailServer)
ctrl.configureMappings(registry)
return ctrl
}
func (mc *CheckMailServerController) configureMappings(r errmap.ErrorRegistry) {
r.Register(
notificationchannelservice.ErrGetMailChannel,
http.StatusInternalServerError,
errorResponses.ErrorInternalResponse,
)
r.Register(
notificationchannelservice.ErrCreateMailClient,
http.StatusUnprocessableEntity,
errorResponses.NewErrorGenericResponse("Unable to create mail client"),
)
r.Register(
notificationchannelservice.ErrMailServerUnreachable,
http.StatusUnprocessableEntity,
errorResponses.NewErrorGenericResponse("Server is unreachable"),
)
}
// CheckMailServer
//
// @Summary Check mail server
// @Description Check if a mail server is reachable
// @Tags mail-channel
// @Accept json
// @Produce json
// @Security KeycloakAuth
// @Param MailServerConfig body maildto.CheckMailServerRequest true "Mail server to check"
// @Success 204 "Mail server reachable"
// @Failure 400 {object} map[string]string
// @Failure 422 "Mail server error"
// @Router /notification-channel/mail/check [post]
func (mc *CheckMailServerController) CheckMailServer(c *gin.Context) {
var mailServer maildto.CheckMailServerRequest
if !ginEx.BindAndValidateBody(c, &mailServer) {
return
}
err := mc.notificationChannelServicer.CheckNotificationChannelConnectivity(context.Background(), mailServer.ToModel())
if err != nil {
ginEx.AddError(c, err)
return
}
c.Status(http.StatusNoContent)
}