-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge_types_controller.go
227 lines (198 loc) · 6.63 KB
/
bridge_types_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package web
import (
"database/sql"
"fmt"
"net/http"
"strings"
"github.com/jackc/pgconn"
"github.com/O1MaGnUmO1/chainlink-common/pkg/assets"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/bridges"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/logger/audit"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/services/erinaceus"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/store/models"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/web/presenters"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
// ValidateBridgeTypeNotExist checks that a bridge has not already been created
func ValidateBridgeTypeNotExist(bt *bridges.BridgeTypeRequest, orm bridges.ORM) error {
fe := models.NewJSONAPIErrors()
_, err := orm.FindBridge(bt.Name)
if err == nil {
fe.Add(fmt.Sprintf("Bridge Type %v already exists", bt.Name))
}
if err != nil && !errors.Is(err, sql.ErrNoRows) {
fe.Add(fmt.Sprintf("Error determining if bridge type %v already exists", bt.Name))
}
return fe.CoerceEmptyToNil()
}
// ValidateBridgeType checks that the bridge type has the required field with valid values.
func ValidateBridgeType(bt *bridges.BridgeTypeRequest) error {
fe := models.NewJSONAPIErrors()
if len(bt.Name.String()) < 1 {
fe.Add("No name specified")
}
if _, err := bridges.ParseBridgeName(bt.Name.String()); err != nil {
fe.Merge(err)
}
u := bt.URL.String()
if len(strings.TrimSpace(u)) == 0 {
fe.Add("URL must be present")
}
if bt.MinimumContractPayment != nil &&
bt.MinimumContractPayment.Cmp(assets.NewLinkFromJuels(0)) < 0 {
fe.Add("MinimumContractPayment must be positive")
}
return fe.CoerceEmptyToNil()
}
// BridgeTypesController manages BridgeType requests in the node.
type BridgeTypesController struct {
App erinaceus.Application
}
// Create adds the BridgeType to the given context.
func (btc *BridgeTypesController) Create(c *gin.Context) {
btr := &bridges.BridgeTypeRequest{}
if err := c.ShouldBindJSON(btr); err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
bta, bt, err := bridges.NewBridgeType(btr)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
if e := ValidateBridgeType(btr); e != nil {
jsonAPIError(c, http.StatusBadRequest, e)
return
}
orm := btc.App.BridgeORM()
if e := ValidateBridgeTypeNotExist(btr, orm); e != nil {
jsonAPIError(c, http.StatusBadRequest, e)
return
}
if e := orm.CreateBridgeType(bt); e != nil {
jsonAPIError(c, http.StatusInternalServerError, e)
return
}
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
var apiErr error
if pgErr.ConstraintName == "external_initiators_name_key" {
apiErr = fmt.Errorf("bridge Type %v conflict", bt.Name)
} else {
apiErr = err
}
jsonAPIError(c, http.StatusConflict, apiErr)
return
}
resource := presenters.NewBridgeResource(*bt)
resource.IncomingToken = bta.IncomingToken
btc.App.GetAuditLogger().Audit(audit.BridgeCreated, map[string]interface{}{
"bridgeName": bta.Name,
"bridgeConfirmations": bta.Confirmations,
"bridgeMinimumContractPayment": bta.MinimumContractPayment,
"bridgeURL": bta.URL,
})
jsonAPIResponse(c, resource, "bridge")
}
// Index lists Bridges, one page at a time.
func (btc *BridgeTypesController) Index(c *gin.Context, size, page, offset int) {
bridges, count, err := btc.App.BridgeORM().BridgeTypes(offset, size)
var resources []presenters.BridgeResource
for _, bridge := range bridges {
resources = append(resources, *presenters.NewBridgeResource(bridge))
}
paginatedResponse(c, "Bridges", size, page, resources, count, err)
}
// Show returns the details of a specific Bridge.
func (btc *BridgeTypesController) Show(c *gin.Context) {
name := c.Param("BridgeName")
taskType, err := bridges.ParseBridgeName(name)
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
bt, err := btc.App.BridgeORM().FindBridge(taskType)
if errors.Is(err, sql.ErrNoRows) {
jsonAPIError(c, http.StatusNotFound, errors.New("bridge not found"))
return
}
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
jsonAPIResponse(c, presenters.NewBridgeResource(bt), "bridge")
}
// Update can change the restricted attributes for a bridge
func (btc *BridgeTypesController) Update(c *gin.Context) {
name := c.Param("BridgeName")
btr := &bridges.BridgeTypeRequest{}
taskType, err := bridges.ParseBridgeName(name)
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
orm := btc.App.BridgeORM()
bt, err := orm.FindBridge(taskType)
if errors.Is(err, sql.ErrNoRows) {
jsonAPIError(c, http.StatusNotFound, errors.New("bridge not found"))
return
}
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
if err := c.ShouldBindJSON(btr); err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
if err := ValidateBridgeType(btr); err != nil {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
if err := orm.UpdateBridgeType(&bt, btr); err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
btc.App.GetAuditLogger().Audit(audit.BridgeUpdated, map[string]interface{}{
"bridgeName": bt.Name,
"bridgeConfirmations": bt.Confirmations,
"bridgeMinimumContractPayment": bt.MinimumContractPayment,
"bridgeURL": bt.URL,
})
jsonAPIResponse(c, presenters.NewBridgeResource(bt), "bridge")
}
// Destroy removes a specific Bridge.
func (btc *BridgeTypesController) Destroy(c *gin.Context) {
name := c.Param("BridgeName")
taskType, err := bridges.ParseBridgeName(name)
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
orm := btc.App.BridgeORM()
bt, err := orm.FindBridge(taskType)
if errors.Is(err, sql.ErrNoRows) {
jsonAPIError(c, http.StatusNotFound, errors.New("bridge not found"))
return
}
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, fmt.Errorf("error searching for bridge: %+v", err))
return
}
jobsUsingBridge, err := btc.App.JobORM().FindJobIDsWithBridge(name)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, fmt.Errorf("error searching for associated v2 jobs: %+v", err))
return
}
if len(jobsUsingBridge) > 0 {
jsonAPIError(c, http.StatusConflict, fmt.Errorf("can't remove the bridge because jobs %v are associated with it", jobsUsingBridge))
return
}
if err = orm.DeleteBridgeType(&bt); err != nil {
jsonAPIError(c, http.StatusInternalServerError, fmt.Errorf("failed to delete bridge: %+v", err))
return
}
btc.App.GetAuditLogger().Audit(audit.BridgeDeleted, map[string]interface{}{"name": name})
jsonAPIResponse(c, presenters.NewBridgeResource(bt), "bridge")
}