mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
The V2 Calls api endpoints have been added beneath fns: (#1203)
/fns/{fnID}/calls
/fns/{fnID}/calls/{callID}
The S3 implementation forces our hand as we if we want to list Calls
under a Fn, we have to use the FnID as a prefix on the object names,
which mean we need it to look up any Call. It also makes sense in
terms of resource hierarchy.
These endpoints can optionally be disabled (as other endpoints), if a
service provider needs to provide this functionality via other means.
The 'calls' test has been fully migrated to fn calls. This has been
done to reduce the copy pasta a bit, and on balance is ok as the
routes calls will be removed soon.
This commit is contained in:
@@ -27,6 +27,7 @@ func migrateErr(version int64, up bool, err error) ErrMigration {
|
||||
dir := "up"
|
||||
if !up {
|
||||
dir = "down"
|
||||
version++
|
||||
}
|
||||
return ErrMigration(fmt.Sprintf("error running migration. version: %v direction: %v err: %v", version, dir, err))
|
||||
}
|
||||
@@ -185,7 +186,6 @@ func run(ctx context.Context, tx *sqlx.Tx, m Migration, up bool) error {
|
||||
return fmt.Errorf("non-contiguous migration attempted down: %v != %v", m.Version(), curVersion)
|
||||
}
|
||||
|
||||
// TODO is this robust enough? we could check
|
||||
version := m.Version()
|
||||
if !up {
|
||||
version = m.Version() - 1
|
||||
|
||||
46
api/datastore/sql/migrations/18_add_fnid_calls.go
Normal file
46
api/datastore/sql/migrations/18_add_fnid_calls.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/fnproject/fn/api/datastore/sql/migratex"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
func up18(ctx context.Context, tx *sqlx.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, "ALTER TABLE calls ADD fn_id varchar(256);")
|
||||
|
||||
switch tx.DriverName() {
|
||||
case "mysql":
|
||||
_, err := tx.ExecContext(ctx, "ALTER TABLE calls MODIFY app_id varchar(256) NULL;")
|
||||
return err
|
||||
case "postgres", "pgx":
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE calls ALTER COLUMN app_id DROP NOT NULL;")
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func down18(ctx context.Context, tx *sqlx.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, "ALTER TABLE calls DROP COLUMN fn_id;")
|
||||
|
||||
switch tx.DriverName() {
|
||||
case "mysql":
|
||||
_, err := tx.ExecContext(ctx, "ALTER TABLE calls MODIFY app_id varchar(256) NOT NULL;")
|
||||
return err
|
||||
case "postgres", "pgx":
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE calls ALTER COLUMN app_id SET NOT NULL;")
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func init() {
|
||||
Migrations = append(Migrations, &migratex.MigFields{
|
||||
VersionFunc: vfunc(18),
|
||||
UpFunc: up18,
|
||||
DownFunc: down18,
|
||||
})
|
||||
}
|
||||
@@ -72,7 +72,8 @@ var tables = [...]string{`CREATE TABLE IF NOT EXISTS routes (
|
||||
completed_at varchar(256) NOT NULL,
|
||||
status varchar(256) NOT NULL,
|
||||
id varchar(256) NOT NULL,
|
||||
app_id varchar(256) NOT NULL,
|
||||
app_id varchar(256),
|
||||
fn_id varchar(256),
|
||||
path varchar(256) NOT NULL,
|
||||
stats text,
|
||||
error text,
|
||||
@@ -955,6 +956,7 @@ func (ds *SQLStore) InsertCall(ctx context.Context, call *models.Call) error {
|
||||
completed_at,
|
||||
status,
|
||||
app_id,
|
||||
fn_id,
|
||||
path,
|
||||
stats,
|
||||
error
|
||||
@@ -966,6 +968,7 @@ func (ds *SQLStore) InsertCall(ctx context.Context, call *models.Call) error {
|
||||
:completed_at,
|
||||
:status,
|
||||
:app_id,
|
||||
:fn_id,
|
||||
:path,
|
||||
:stats,
|
||||
:error
|
||||
@@ -975,7 +978,7 @@ func (ds *SQLStore) InsertCall(ctx context.Context, call *models.Call) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (ds *SQLStore) GetCall(ctx context.Context, appID, callID string) (*models.Call, error) {
|
||||
func (ds *SQLStore) GetCall1(ctx context.Context, appID, callID string) (*models.Call, error) {
|
||||
query := fmt.Sprintf(`%s WHERE id=? AND app_id=?`, callSelector)
|
||||
query = ds.db.Rebind(query)
|
||||
row := ds.db.QueryRowxContext(ctx, query, callID, appID)
|
||||
@@ -991,7 +994,47 @@ func (ds *SQLStore) GetCall(ctx context.Context, appID, callID string) (*models.
|
||||
return &call, nil
|
||||
}
|
||||
|
||||
func (ds *SQLStore) GetCalls(ctx context.Context, filter *models.CallFilter) ([]*models.Call, error) {
|
||||
func (ds *SQLStore) GetCall(ctx context.Context, fnID, callID string) (*models.Call, error) {
|
||||
query := fmt.Sprintf(`%s WHERE id=? AND fn_id=?`, callSelector)
|
||||
query = ds.db.Rebind(query)
|
||||
row := ds.db.QueryRowxContext(ctx, query, callID, fnID)
|
||||
|
||||
var call models.Call
|
||||
err := row.StructScan(&call)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, models.ErrCallNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &call, nil
|
||||
}
|
||||
|
||||
func (ds *SQLStore) GetCalls(ctx context.Context, filter *models.CallFilter) (*models.CallList, error) {
|
||||
if filter.Cursor != "" {
|
||||
cursor, err := base64.RawURLEncoding.DecodeString(filter.Cursor)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filter.Cursor = string(cursor)
|
||||
}
|
||||
|
||||
calls, err := ds.GetCalls1(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
callList := &models.CallList{Items: calls}
|
||||
|
||||
if len(calls) > 0 && len(calls) == filter.PerPage {
|
||||
last := []byte(calls[len(calls)-1].ID)
|
||||
callList.NextCursor = base64.RawURLEncoding.EncodeToString(last)
|
||||
}
|
||||
|
||||
return callList, nil
|
||||
}
|
||||
|
||||
func (ds *SQLStore) GetCalls1(ctx context.Context, filter *models.CallFilter) ([]*models.Call, error) {
|
||||
res := []*models.Call{}
|
||||
query, args := buildFilterCallQuery(filter)
|
||||
query = fmt.Sprintf("%s %s", callSelector, query)
|
||||
@@ -1109,8 +1152,15 @@ func buildFilterCallQuery(filter *models.CallFilter) (string, []interface{}) {
|
||||
if !time.Time(filter.FromTime).IsZero() {
|
||||
args = where(&b, args, "created_at>?", filter.FromTime.String())
|
||||
}
|
||||
args = where(&b, args, "app_id=?", filter.AppID)
|
||||
args = where(&b, args, "path=?", filter.Path)
|
||||
if filter.AppID != "" {
|
||||
args = where(&b, args, "app_id=?", filter.AppID)
|
||||
}
|
||||
if filter.FnID != "" {
|
||||
args = where(&b, args, "fn_id=?", filter.FnID)
|
||||
}
|
||||
if filter.Path != "" {
|
||||
args = where(&b, args, "path=?", filter.Path)
|
||||
}
|
||||
|
||||
fmt.Fprintf(&b, ` ORDER BY id DESC`) // TODO assert this is indexed
|
||||
fmt.Fprintf(&b, ` LIMIT ?`)
|
||||
|
||||
Reference in New Issue
Block a user