/
external_initiators_controller.go
75 lines (63 loc) · 2.37 KB
/
external_initiators_controller.go
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
package web
import (
"net/http"
"github.com/SeerLink/seerlink/core/auth"
"github.com/SeerLink/seerlink/core/services"
"github.com/SeerLink/seerlink/core/services/seerlink"
"github.com/SeerLink/seerlink/core/store/models"
"github.com/SeerLink/seerlink/core/store/orm"
"github.com/SeerLink/seerlink/core/store/presenters"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
// ExternalInitiatorsController manages external initiators
type ExternalInitiatorsController struct {
App seerlink.Application
}
// Create builds and saves a new service agreement record.
func (eic *ExternalInitiatorsController) Create(c *gin.Context) {
eir := &models.ExternalInitiatorRequest{}
if !eic.App.GetStore().Config.Dev() && !eic.App.GetStore().Config.FeatureExternalInitiators() {
err := errors.New("The External Initiator feature is disabled by configuration")
jsonAPIError(c, http.StatusMethodNotAllowed, err)
return
}
eia := auth.NewToken()
if err := c.ShouldBindJSON(eir); err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
ei, err := models.NewExternalInitiator(eia, eir)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
if err := services.ValidateExternalInitiator(eir, eic.App.GetStore()); err != nil {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
if err := eic.App.GetStore().CreateExternalInitiator(ei); err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
resp := presenters.NewExternalInitiatorAuthentication(*ei, *eia)
jsonAPIResponseWithStatus(c, resp, "external initiator authentication", http.StatusCreated)
}
// Destroy deletes an ExternalInitiator
func (eic *ExternalInitiatorsController) Destroy(c *gin.Context) {
if !eic.App.GetStore().Config.Dev() {
jsonAPIError(c, http.StatusMethodNotAllowed, errors.New("External Initiators are currently under development and not yet usable outside of development mode"))
return
}
name := c.Param("Name")
exi, err := eic.App.GetStore().FindExternalInitiatorByName(name)
if errors.Cause(err) == orm.ErrorNotFound {
jsonAPIError(c, http.StatusNotFound, errors.New("external initiator not found"))
return
}
if err := eic.App.GetStore().DeleteExternalInitiator(exi.Name); err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
jsonAPIResponseWithStatus(c, nil, "external initiator", http.StatusNoContent)
}