Mercurial > gemma
changeset 2051:7d627258e045 unify_imports
Imports: Re-enabled /imports/config POST.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 27 Jan 2019 22:40:32 +0100 |
parents | a3ccef8ec304 |
children | 0b203a3b3e8e |
files | pkg/common/attributes.go pkg/controllers/importconfig.go pkg/imports/config.go |
diffstat | 3 files changed, 125 insertions(+), 102 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/common/attributes.go Sun Jan 27 22:03:46 2019 +0100 +++ b/pkg/common/attributes.go Sun Jan 27 22:40:32 2019 +0100 @@ -35,6 +35,17 @@ } ) +func (ca Attributes) Marshal(src interface{}) error { + if ca == nil { + return nil + } + var err error + if m, ok := src.(AttributesMarshaler); ok { + err = m.MarshalAttributes(ca) + } + return err +} + func (ca Attributes) Unmarshal(dst interface{}) error { if ca == nil { return nil
--- a/pkg/controllers/importconfig.go Sun Jan 27 22:03:46 2019 +0100 +++ b/pkg/controllers/importconfig.go Sun Jan 27 22:40:32 2019 +0100 @@ -15,16 +15,20 @@ import ( "database/sql" + "encoding/json" "fmt" "net/http" "strconv" "github.com/gorilla/mux" + "gemma.intevation.de/gemma/pkg/auth" + "gemma.intevation.de/gemma/pkg/common" "gemma.intevation.de/gemma/pkg/imports" "gemma.intevation.de/gemma/pkg/scheduler" ) +/* const ( selectImportConfigurationIDSQL = ` SELECT @@ -33,18 +37,8 @@ kind FROM import.import_configuration WHERE id = $1` - - insertImportConfigurationSQL = ` -INSERT INTO import.import_configuration -(username, kind) -VALUES ($1, $2) -RETURNING id` - - insertImportConfigurationAttributeSQL = ` -INSERT INTO import.import_configuration_attributes -(import_configuration_id, k, v) -VALUES ($1, $2, $3)` ) +*/ func runImportConfig( _ interface{}, @@ -283,115 +277,73 @@ return } -/* - -func storeConfigAttributes( - ctx context.Context, - tx *sql.Tx, - id int64, - attrs common.Attributes, -) error { - if len(attrs) == 0 { - return nil - } - attrStmt, err := tx.PrepareContext(ctx, insertImportConfigurationAttributeSQL) - if err != nil { - return err - } - defer attrStmt.Close() - // Sort to make it deterministic - keys := make([]string, len(attrs)) - i := 0 - for key := range attrs { - keys[i] = key - i++ - } - sort.Strings(keys) - for _, key := range keys { - if _, err := attrStmt.ExecContext(ctx, id, key, attrs[key]); err != nil { - return err - } - } - return nil -} - -*/ - func addImportConfig( input interface{}, req *http.Request, conn *sql.Conn, ) (jr JSONResult, err error) { - /* + cfg := input.(*imports.ImportConfigIn) - importConfig := input.(*imports.Config) + kind := imports.JobKind(cfg.Kind) - session, _ := auth.GetSession(req) + ctor := imports.ImportModelForJobKind(kind) + if ctor == nil { + err = JSONError{ + Code: http.StatusBadRequest, + Message: fmt.Sprintf("No kind %s found", string(cfg.Kind)), + } + return + } + config := ctor() + if err = json.Unmarshal(cfg.Config, config); err != nil { + return + } - var cron, url sql.NullString + session, _ := auth.GetSession(req) - if importConfig.Cron != nil { - cron = sql.NullString{String: string(*importConfig.Cron), Valid: true} - } - if importConfig.URL != nil { - url = sql.NullString{String: *importConfig.URL, Valid: true} - } + pc := imports.PersistentConfig{ + User: session.User, + Kind: string(cfg.Kind), + Attributes: common.Attributes{}, + } + pc.Attributes.Marshal(config) + + ctx := req.Context() - ctx := req.Context() + var tx *sql.Tx + if tx, err = conn.BeginTx(ctx, nil); err != nil { + return + } + defer tx.Rollback() - var tx *sql.Tx + var id int64 + if id, err = pc.StoreContext(ctx, tx); err != nil { + return + } - if tx, err = conn.BeginTx(ctx, nil); err != nil { + // Need to start a scheduler job right away? + if cron, ok := pc.Attributes.Get("cron"); ok { + if err = scheduler.BindAction(string(cfg.Kind), cron, id); err != nil { return } - defer tx.Rollback() + } - var id int64 - if err = tx.QueryRowContext( - ctx, - insertImportConfigurationSQL, - session.User, - string(importConfig.Kind), - cron, - importConfig.SendEMail, - url, - ).Scan(&id); err != nil { - return - } - - // Store extra attributes - if err = storeConfigAttributes(ctx, tx, id, importConfig.Attributes); err != nil { - return - } + if err = tx.Commit(); err != nil { + scheduler.UnbindByID(id) + return + } - // Need to start a scheduler job right away? - if importConfig.Cron != nil { - if err = scheduler.BindAction( - string(importConfig.Kind), - string(*importConfig.Cron), - id, - ); err != nil { - return - } - } + var result = struct { + ID int64 `json:"id"` + }{ + ID: id, + } - if err = tx.Commit(); err != nil { - scheduler.UnbindByID(id) - return - } - - var result = struct { - ID int64 `json:"id"` - }{ - ID: id, - } - - jr = JSONResult{ - Code: http.StatusCreated, - Result: &result, - } - */ + jr = JSONResult{ + Code: http.StatusCreated, + Result: &result, + } return }
--- a/pkg/imports/config.go Sun Jan 27 22:03:46 2019 +0100 +++ b/pkg/imports/config.go Sun Jan 27 22:40:32 2019 +0100 @@ -18,6 +18,7 @@ "database/sql" "encoding/json" "fmt" + "sort" "gemma.intevation.de/gemma/pkg/auth" "gemma.intevation.de/gemma/pkg/common" @@ -109,6 +110,17 @@ import.import_configuration_attributes a ON c.id = a.import_configuration_id ORDER by c.id` + + insertImportConfigurationSQL = ` +INSERT INTO import.import_configuration +(username, kind) +VALUES ($1, $2) +RETURNING id` + + insertImportConfigurationAttributeSQL = ` +INSERT INTO import.import_configuration_attributes +(import_configuration_id, k, v) +VALUES ($1, $2, $3)` ) func (pc *PersistentConfig) Update(ctx context.Context, tx *sql.Tx) error { @@ -279,3 +291,51 @@ ) return err } + +func storeConfigAttributes( + ctx context.Context, + tx *sql.Tx, + id int64, + attrs common.Attributes, +) error { + if len(attrs) == 0 { + return nil + } + attrStmt, err := tx.PrepareContext(ctx, insertImportConfigurationAttributeSQL) + if err != nil { + return err + } + defer attrStmt.Close() + // Sort to make it deterministic + keys := make([]string, len(attrs)) + i := 0 + for key := range attrs { + keys[i] = key + i++ + } + sort.Strings(keys) + for _, key := range keys { + if _, err := attrStmt.ExecContext(ctx, id, key, attrs[key]); err != nil { + return err + } + } + return nil +} + +func (pc *PersistentConfig) StoreContext(ctx context.Context, tx *sql.Tx) (int64, error) { + var id int64 + if err := tx.QueryRowContext( + ctx, + insertImportConfigurationSQL, + pc.User, + pc.Kind, + ).Scan(&id); err != nil { + return 0, err + } + + if err := storeConfigAttributes(ctx, tx, id, pc.Attributes); err != nil { + return 0, err + } + pc.ID = id + return id, nil +}