From 9fad17ef6060e459bb9a2bc535502eeda3f4304f Mon Sep 17 00:00:00 2001 From: CrazyBolillo Date: Tue, 30 Jul 2024 21:37:48 -0600 Subject: [PATCH] feat(endpoint): add extensions to new endpoints Extensions may now be assigned to a given endpoint during its creation. They are also returned when listing endpoints. --- .../20240730051335_ery_extension.sql | 12 +++++++ docs/swagger.yaml | 17 +++++++--- internal/handler/endpoint.go | 31 ++++++++++++++++--- internal/sqlc/models.go | 7 +++++ internal/sqlc/queries.sql.go | 26 ++++++++++++++-- queries.sql | 11 +++++-- 6 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 db/migrations/20240730051335_ery_extension.sql diff --git a/db/migrations/20240730051335_ery_extension.sql b/db/migrations/20240730051335_ery_extension.sql new file mode 100644 index 0000000..dcabf2b --- /dev/null +++ b/db/migrations/20240730051335_ery_extension.sql @@ -0,0 +1,12 @@ +-- migrate:up +ALTER TABLE ps_endpoints ADD COLUMN sid SERIAL PRIMARY KEY; + +CREATE TABLE ery_extension ( + id SERIAL PRIMARY KEY, + endpoint_id SERIAL NOT NULL, + extension varchar UNIQUE, + FOREIGN KEY (endpoint_id) REFERENCES ps_endpoints(sid) +) + +-- migrate:down + diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 88898f2..2134417 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -7,6 +7,8 @@ definitions: type: array context: type: string + extension: + type: string id: type: string max_contacts: @@ -18,13 +20,20 @@ definitions: transport: type: string type: object + handler.listEndpointsRequest: + properties: + endpoints: + items: + $ref: '#/definitions/sqlc.ListEndpointsRow' + type: array + type: object sqlc.ListEndpointsRow: properties: context: type: string - id: + extension: type: string - transport: + id: type: string type: object host: localhost:8080 @@ -87,9 +96,7 @@ paths: "200": description: OK schema: - items: - $ref: '#/definitions/sqlc.ListEndpointsRow' - type: array + $ref: '#/definitions/handler.listEndpointsRequest' "400": description: Bad Request "500": diff --git a/internal/handler/endpoint.go b/internal/handler/endpoint.go index a08315c..fb80567 100644 --- a/internal/handler/endpoint.go +++ b/internal/handler/endpoint.go @@ -22,10 +22,15 @@ type createEndpointRequest struct { ID string `json:"id"` Password string `json:"password"` Realm string `json:"realm,omitempty"` - Transport string `json:"transport"` + Transport string `json:"transport,omitempty"` Context string `json:"context"` Codecs []string `json:"codecs"` MaxContacts int32 `json:"max_contacts,omitempty"` + Extension string `json:"extension,omitempty"` +} + +type listEndpointsRequest struct { + Endpoints []sqlc.ListEndpointsRow `json:"endpoints"` } func (e *Endpoint) Router() chi.Router { @@ -40,7 +45,7 @@ func (e *Endpoint) Router() chi.Router { // @Summary List existing endpoints. // @Param limit query int false "Limit the amount of endpoints returned" default(15) // @Produce json -// @Success 200 {object} []sqlc.ListEndpointsRow +// @Success 200 {object} listEndpointsRequest // @Failure 400 // @Failure 500 // @Tags endpoints @@ -60,11 +65,18 @@ func (e *Endpoint) list(w http.ResponseWriter, r *http.Request) { queries := sqlc.New(e.Conn) endpoints, err := queries.ListEndpoints(r.Context(), int32(limit)) if err != nil { + slog.Error("Query execution failed", slog.String("path", r.URL.Path), slog.String("msg", err.Error())) w.WriteHeader(http.StatusInternalServerError) return } + if endpoints == nil { + endpoints = []sqlc.ListEndpointsRow{} + } - content, err := json.Marshal(endpoints) + response := listEndpointsRequest{ + Endpoints: endpoints, + } + content, err := json.Marshal(response) if err != nil { w.WriteHeader(http.StatusInternalServerError) return @@ -120,7 +132,7 @@ func (e *Endpoint) create(w http.ResponseWriter, r *http.Request) { return } - err = queries.NewEndpoint(r.Context(), sqlc.NewEndpointParams{ + sid, err := queries.NewEndpoint(r.Context(), sqlc.NewEndpointParams{ ID: payload.ID, Transport: db.Text(payload.Transport), Context: db.Text(payload.Context), @@ -140,6 +152,17 @@ func (e *Endpoint) create(w http.ResponseWriter, r *http.Request) { return } + if payload.Extension != "" { + err = queries.NewExtension(r.Context(), sqlc.NewExtensionParams{ + EndpointID: sid, + Extension: db.Text(payload.Extension), + }) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + } + err = tx.Commit(r.Context()) if err != nil { w.WriteHeader(http.StatusInternalServerError) diff --git a/internal/sqlc/models.go b/internal/sqlc/models.go index c973113..7f41e32 100644 --- a/internal/sqlc/models.go +++ b/internal/sqlc/models.go @@ -1589,6 +1589,12 @@ type Cdr struct { ID int64 `json:"id"` } +type EryExtension struct { + ID int32 `json:"id"` + EndpointID int32 `json:"endpoint_id"` + Extension pgtype.Text `json:"extension"` +} + type Extension struct { ID int64 `json:"id"` Context string `json:"context"` @@ -1887,6 +1893,7 @@ type PsEndpoint struct { SecurityMechanisms pgtype.Text `json:"security_mechanisms"` SendAoc NullAstBoolValues `json:"send_aoc"` OverlapContext pgtype.Text `json:"overlap_context"` + Sid int32 `json:"sid"` } type PsEndpointIDIp struct { diff --git a/internal/sqlc/queries.sql.go b/internal/sqlc/queries.sql.go index 942645c..a767e92 100644 --- a/internal/sqlc/queries.sql.go +++ b/internal/sqlc/queries.sql.go @@ -89,11 +89,12 @@ func (q *Queries) NewAOR(ctx context.Context, arg NewAORParams) error { return err } -const newEndpoint = `-- name: NewEndpoint :exec +const newEndpoint = `-- name: NewEndpoint :one INSERT INTO ps_endpoints (id, transport, aors, auth, context, disallow, allow) VALUES ($1, $2, $1, $1, $3, 'all', $4) +RETURNING sid ` type NewEndpointParams struct { @@ -103,13 +104,32 @@ type NewEndpointParams struct { Allow pgtype.Text `json:"allow"` } -func (q *Queries) NewEndpoint(ctx context.Context, arg NewEndpointParams) error { - _, err := q.db.Exec(ctx, newEndpoint, +func (q *Queries) NewEndpoint(ctx context.Context, arg NewEndpointParams) (int32, error) { + row := q.db.QueryRow(ctx, newEndpoint, arg.ID, arg.Transport, arg.Context, arg.Allow, ) + var sid int32 + err := row.Scan(&sid) + return sid, err +} + +const newExtension = `-- name: NewExtension :exec +INSERT INTO ery_extension + (endpoint_id, extension) +VALUES + ($1, $2) +` + +type NewExtensionParams struct { + EndpointID int32 `json:"endpoint_id"` + Extension pgtype.Text `json:"extension"` +} + +func (q *Queries) NewExtension(ctx context.Context, arg NewExtensionParams) error { + _, err := q.db.Exec(ctx, newExtension, arg.EndpointID, arg.Extension) return err } diff --git a/queries.sql b/queries.sql index 7e19f53..8825860 100644 --- a/queries.sql +++ b/queries.sql @@ -10,11 +10,12 @@ INSERT INTO ps_aors VALUES ($1, $2); --- name: NewEndpoint :exec +-- name: NewEndpoint :one INSERT INTO ps_endpoints (id, transport, aors, auth, context, disallow, allow) VALUES - ($1, $2, $1, $1, $3, 'all', $4); + ($1, $2, $1, $1, $3, 'all', $4) +RETURNING sid; -- name: DeleteEndpoint :exec DELETE FROM ps_endpoints WHERE id = $1; @@ -31,3 +32,9 @@ SELECT FROM ps_endpoints LIMIT $1; + +-- name: NewExtension :exec +INSERT INTO ery_extension + (endpoint_id, extension) +VALUES + ($1, $2);