Mercurial > gemma
comparison pkg/controllers/importconfig.go @ 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 |
comparison
equal
deleted
inserted
replaced
2050:a3ccef8ec304 | 2051:7d627258e045 |
---|---|
13 | 13 |
14 package controllers | 14 package controllers |
15 | 15 |
16 import ( | 16 import ( |
17 "database/sql" | 17 "database/sql" |
18 "encoding/json" | |
18 "fmt" | 19 "fmt" |
19 "net/http" | 20 "net/http" |
20 "strconv" | 21 "strconv" |
21 | 22 |
22 "github.com/gorilla/mux" | 23 "github.com/gorilla/mux" |
23 | 24 |
25 "gemma.intevation.de/gemma/pkg/auth" | |
26 "gemma.intevation.de/gemma/pkg/common" | |
24 "gemma.intevation.de/gemma/pkg/imports" | 27 "gemma.intevation.de/gemma/pkg/imports" |
25 "gemma.intevation.de/gemma/pkg/scheduler" | 28 "gemma.intevation.de/gemma/pkg/scheduler" |
26 ) | 29 ) |
27 | 30 |
31 /* | |
28 const ( | 32 const ( |
29 selectImportConfigurationIDSQL = ` | 33 selectImportConfigurationIDSQL = ` |
30 SELECT | 34 SELECT |
31 id, | 35 id, |
32 username, | 36 username, |
33 kind | 37 kind |
34 FROM import.import_configuration | 38 FROM import.import_configuration |
35 WHERE id = $1` | 39 WHERE id = $1` |
36 | |
37 insertImportConfigurationSQL = ` | |
38 INSERT INTO import.import_configuration | |
39 (username, kind) | |
40 VALUES ($1, $2) | |
41 RETURNING id` | |
42 | |
43 insertImportConfigurationAttributeSQL = ` | |
44 INSERT INTO import.import_configuration_attributes | |
45 (import_configuration_id, k, v) | |
46 VALUES ($1, $2, $3)` | |
47 ) | 40 ) |
41 */ | |
48 | 42 |
49 func runImportConfig( | 43 func runImportConfig( |
50 _ interface{}, | 44 _ interface{}, |
51 req *http.Request, | 45 req *http.Request, |
52 conn *sql.Conn, | 46 conn *sql.Conn, |
281 jr = JSONResult{Result: &result} | 275 jr = JSONResult{Result: &result} |
282 | 276 |
283 return | 277 return |
284 } | 278 } |
285 | 279 |
286 /* | |
287 | |
288 func storeConfigAttributes( | |
289 ctx context.Context, | |
290 tx *sql.Tx, | |
291 id int64, | |
292 attrs common.Attributes, | |
293 ) error { | |
294 if len(attrs) == 0 { | |
295 return nil | |
296 } | |
297 attrStmt, err := tx.PrepareContext(ctx, insertImportConfigurationAttributeSQL) | |
298 if err != nil { | |
299 return err | |
300 } | |
301 defer attrStmt.Close() | |
302 // Sort to make it deterministic | |
303 keys := make([]string, len(attrs)) | |
304 i := 0 | |
305 for key := range attrs { | |
306 keys[i] = key | |
307 i++ | |
308 } | |
309 sort.Strings(keys) | |
310 for _, key := range keys { | |
311 if _, err := attrStmt.ExecContext(ctx, id, key, attrs[key]); err != nil { | |
312 return err | |
313 } | |
314 } | |
315 return nil | |
316 } | |
317 | |
318 */ | |
319 | |
320 func addImportConfig( | 280 func addImportConfig( |
321 input interface{}, | 281 input interface{}, |
322 req *http.Request, | 282 req *http.Request, |
323 conn *sql.Conn, | 283 conn *sql.Conn, |
324 ) (jr JSONResult, err error) { | 284 ) (jr JSONResult, err error) { |
325 | 285 |
326 /* | 286 cfg := input.(*imports.ImportConfigIn) |
327 | 287 |
328 importConfig := input.(*imports.Config) | 288 kind := imports.JobKind(cfg.Kind) |
329 | 289 |
330 session, _ := auth.GetSession(req) | 290 ctor := imports.ImportModelForJobKind(kind) |
331 | 291 if ctor == nil { |
332 var cron, url sql.NullString | 292 err = JSONError{ |
333 | 293 Code: http.StatusBadRequest, |
334 if importConfig.Cron != nil { | 294 Message: fmt.Sprintf("No kind %s found", string(cfg.Kind)), |
335 cron = sql.NullString{String: string(*importConfig.Cron), Valid: true} | 295 } |
336 } | 296 return |
337 if importConfig.URL != nil { | 297 } |
338 url = sql.NullString{String: *importConfig.URL, Valid: true} | 298 config := ctor() |
339 } | 299 if err = json.Unmarshal(cfg.Config, config); err != nil { |
340 | 300 return |
341 ctx := req.Context() | 301 } |
342 | 302 |
343 var tx *sql.Tx | 303 session, _ := auth.GetSession(req) |
344 | 304 |
345 if tx, err = conn.BeginTx(ctx, nil); err != nil { | 305 pc := imports.PersistentConfig{ |
346 return | 306 User: session.User, |
347 } | 307 Kind: string(cfg.Kind), |
348 defer tx.Rollback() | 308 Attributes: common.Attributes{}, |
349 | 309 } |
350 var id int64 | 310 pc.Attributes.Marshal(config) |
351 if err = tx.QueryRowContext( | 311 |
352 ctx, | 312 ctx := req.Context() |
353 insertImportConfigurationSQL, | 313 |
354 session.User, | 314 var tx *sql.Tx |
355 string(importConfig.Kind), | 315 if tx, err = conn.BeginTx(ctx, nil); err != nil { |
356 cron, | 316 return |
357 importConfig.SendEMail, | 317 } |
358 url, | 318 defer tx.Rollback() |
359 ).Scan(&id); err != nil { | 319 |
360 return | 320 var id int64 |
361 } | 321 if id, err = pc.StoreContext(ctx, tx); err != nil { |
362 | 322 return |
363 // Store extra attributes | 323 } |
364 if err = storeConfigAttributes(ctx, tx, id, importConfig.Attributes); err != nil { | 324 |
365 return | 325 // Need to start a scheduler job right away? |
366 } | 326 if cron, ok := pc.Attributes.Get("cron"); ok { |
367 | 327 if err = scheduler.BindAction(string(cfg.Kind), cron, id); err != nil { |
368 // Need to start a scheduler job right away? | 328 return |
369 if importConfig.Cron != nil { | 329 } |
370 if err = scheduler.BindAction( | 330 } |
371 string(importConfig.Kind), | 331 |
372 string(*importConfig.Cron), | 332 if err = tx.Commit(); err != nil { |
373 id, | 333 scheduler.UnbindByID(id) |
374 ); err != nil { | 334 return |
375 return | 335 } |
376 } | 336 |
377 } | 337 var result = struct { |
378 | 338 ID int64 `json:"id"` |
379 if err = tx.Commit(); err != nil { | 339 }{ |
380 scheduler.UnbindByID(id) | 340 ID: id, |
381 return | 341 } |
382 } | 342 |
383 | 343 jr = JSONResult{ |
384 var result = struct { | 344 Code: http.StatusCreated, |
385 ID int64 `json:"id"` | 345 Result: &result, |
386 }{ | 346 } |
387 ID: id, | |
388 } | |
389 | |
390 jr = JSONResult{ | |
391 Code: http.StatusCreated, | |
392 Result: &result, | |
393 } | |
394 */ | |
395 return | 347 return |
396 } | 348 } |
397 | 349 |
398 func listImportConfigs( | 350 func listImportConfigs( |
399 _ interface{}, | 351 _ interface{}, |