Add endpoint for primary DAL connection

This commit is contained in:
Tomaž Jerman
2022-05-20 15:37:33 +02:00
parent b85f16a368
commit bda52ed992
7 changed files with 107 additions and 34 deletions
+4
View File
@@ -835,6 +835,10 @@ endpoints:
name: labels
title: Labels
parser: label.ParseStrings
- name: readPrimary
method: GET
title: Read primary connection details
path: "/primary"
- name: read
method: GET
title: Read connection details
+4
View File
@@ -118,6 +118,10 @@ func (ctrl Connection) Update(ctx context.Context, r *request.ConnectionUpdate)
return ctrl.svc.Update(ctx, connection)
}
func (ctrl Connection) ReadPrimary(ctx context.Context, r *request.ConnectionReadPrimary) (interface{}, error) {
return ctrl.svc.FindByID(ctx, 0)
}
func (ctrl Connection) Read(ctx context.Context, r *request.ConnectionRead) (interface{}, error) {
return ctrl.svc.FindByID(ctx, r.ConnectionID)
}
+25 -6
View File
@@ -22,6 +22,7 @@ type (
List(context.Context, *request.ConnectionList) (interface{}, error)
Create(context.Context, *request.ConnectionCreate) (interface{}, error)
Update(context.Context, *request.ConnectionUpdate) (interface{}, error)
ReadPrimary(context.Context, *request.ConnectionReadPrimary) (interface{}, error)
Read(context.Context, *request.ConnectionRead) (interface{}, error)
Delete(context.Context, *request.ConnectionDelete) (interface{}, error)
Undelete(context.Context, *request.ConnectionUndelete) (interface{}, error)
@@ -29,12 +30,13 @@ type (
// HTTP API interface
Connection struct {
List func(http.ResponseWriter, *http.Request)
Create func(http.ResponseWriter, *http.Request)
Update func(http.ResponseWriter, *http.Request)
Read func(http.ResponseWriter, *http.Request)
Delete func(http.ResponseWriter, *http.Request)
Undelete func(http.ResponseWriter, *http.Request)
List func(http.ResponseWriter, *http.Request)
Create func(http.ResponseWriter, *http.Request)
Update func(http.ResponseWriter, *http.Request)
ReadPrimary func(http.ResponseWriter, *http.Request)
Read func(http.ResponseWriter, *http.Request)
Delete func(http.ResponseWriter, *http.Request)
Undelete func(http.ResponseWriter, *http.Request)
}
)
@@ -88,6 +90,22 @@ func NewConnection(h ConnectionAPI) *Connection {
api.Send(w, r, value)
},
ReadPrimary: func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
params := request.NewConnectionReadPrimary()
if err := params.Fill(r); err != nil {
api.Send(w, r, err)
return
}
value, err := h.ReadPrimary(r.Context(), params)
if err != nil {
api.Send(w, r, err)
return
}
api.Send(w, r, value)
},
Read: func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
params := request.NewConnectionRead()
@@ -145,6 +163,7 @@ func (h Connection) MountRoutes(r chi.Router, middlewares ...func(http.Handler)
r.Get("/connections/", h.List)
r.Post("/connections/", h.Create)
r.Put("/connections/{connectionID}", h.Update)
r.Get("/connections/primary", h.ReadPrimary)
r.Get("/connections/{connectionID}", h.Read)
r.Delete("/connections/{connectionID}", h.Delete)
r.Post("/connections/{connectionID}/undelete", h.Undelete)
+19
View File
@@ -171,6 +171,9 @@ type (
Labels map[string]string
}
ConnectionReadPrimary struct {
}
ConnectionRead struct {
// ConnectionID PATH parameter
//
@@ -822,6 +825,22 @@ func (r *ConnectionUpdate) Fill(req *http.Request) (err error) {
return err
}
// NewConnectionReadPrimary request
func NewConnectionReadPrimary() *ConnectionReadPrimary {
return &ConnectionReadPrimary{}
}
// Auditable returns all auditable/loggable parameters
func (r ConnectionReadPrimary) Auditable() map[string]interface{} {
return map[string]interface{}{}
}
// Fill processes request and fills internal variables
func (r *ConnectionReadPrimary) Fill(req *http.Request) (err error) {
return err
}
// NewConnectionRead request
func NewConnectionRead() *ConnectionRead {
return &ConnectionRead{}
+15 -5
View File
@@ -18,6 +18,8 @@ type (
store store.Storer
ac connectionAccessController
dal dalConnections
primaryConnection types.Connection
}
connectionAccessController interface {
@@ -37,12 +39,13 @@ type (
}
)
func Connection(ctx context.Context, dal dalConnections) (*connection, error) {
func Connection(ctx context.Context, pcOpts types.Connection, dal dalConnections) (*connection, error) {
out := &connection{
ac: DefaultAccessControl,
actionlog: DefaultActionlog,
store: DefaultStore,
dal: dal,
ac: DefaultAccessControl,
actionlog: DefaultActionlog,
store: DefaultStore,
dal: dal,
primaryConnection: pcOpts,
}
return out, out.reloadConnections(ctx)
@@ -54,6 +57,13 @@ func (svc *connection) FindByID(ctx context.Context, ID uint64) (q *types.Connec
)
err = func() error {
if ID == 0 {
// primary; construct it since it doesn't actually exist
aux := svc.primaryConnection
q = &aux
return nil
}
if ID == 0 {
return ConnectionErrInvalidID()
}
+4 -2
View File
@@ -86,6 +86,7 @@ var (
DefaultApigwFilter *apigwFilter
DefaultApigwProfiler *apigwProfiler
DefaultReport *report
primaryConnectionConfig types.Connection
DefaultStatistics *statistics
@@ -101,7 +102,7 @@ var (
}
)
func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, ws websocketSender, c Config) (err error) {
func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, primaryConn types.Connection, ws websocketSender, c Config) (err error) {
var (
hcd = healthcheck.Defaults()
)
@@ -147,7 +148,8 @@ func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, ws websock
DefaultSettings = Settings(ctx, DefaultStore, DefaultLogger, DefaultAccessControl, CurrentSettings)
DefaultConnection, err = Connection(ctx, dal.Service())
primaryConnectionConfig = primaryConn
DefaultConnection, err = Connection(ctx, primaryConnectionConfig, dal.Service())
if err != nil {
return
}