comparison pkg/imports/config.go @ 2042:d29ac997eb34 unify_imports

This breaks this branch!!!! Starting to remove the old persistent layer for configured imports.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 25 Jan 2019 16:07:09 +0100
parents 25967829cf00
children 10a1e139d2e8
comparison
equal deleted inserted replaced
2041:d61ca2b3fc12 2042:d29ac997eb34
19 "encoding/json" 19 "encoding/json"
20 "fmt" 20 "fmt"
21 21
22 "gemma.intevation.de/gemma/pkg/auth" 22 "gemma.intevation.de/gemma/pkg/auth"
23 "gemma.intevation.de/gemma/pkg/common" 23 "gemma.intevation.de/gemma/pkg/common"
24 "gemma.intevation.de/gemma/pkg/models"
25 ) 24 )
26 25
27 type ( 26 type (
28 // ImportKind is a string which has to be one 27 // ImportKind is a string which has to be one
29 // of the registered import types. 28 // of the registered import types.
30 ImportKind string 29 ImportKind string
31 30
32 // Config is JSON serialized form of a import configuration. 31 ImportConfigIn struct {
33 Config struct { 32 Kind ImportKind `json:"kind"`
34 // Kind is the import type. 33 Config json.RawMessage `json:"config"`
35 Kind ImportKind `json:"kind"`
36 // SendEMail indicates if a mail should be be send
37 // when the import was changed to states
38 // 'pending' or 'failed'.
39 SendEMail bool `json:"send-email"`
40 // Cron is the cron schedule
41 // of this configuration if this value is not
42 // nil. If nil the import is not scheduled.
43 Cron *models.CronSpec `json:"cron"`
44 // URL is an optional URL used by the import.
45 URL *string `json:"url"`
46 // Attributes are optional key/value pairs for a configuration.
47 Attributes common.Attributes `json:"attributes,omitempty"`
48 } 34 }
49 35
50 // IDConfig is the same as Config with an ID. 36 ImportConfigOut struct {
51 // Mainly used for server delivered configurations. 37 ID int64 `json:"id"`
52 IDConfig struct { 38 Kind ImportKind `json:"kind"`
53 ID int64 `json:"id"` 39 Config interface{} `json:"config"`
54 User string `json:"user"` 40 }
55 Kind ImportKind `json:"kind"` 41
56 SendEMail bool `json:"send-email"` 42 PersistentConfig struct {
57 Cron *models.CronSpec `json:"cron,omitempty"` 43 ID int64
58 URL *string `json:"url,omitempty"` 44 User string
59 Attributes common.Attributes `json:"attributes,omitempty"` 45 Kind string
46 Attributes common.Attributes
60 } 47 }
61 ) 48 )
62 49
63 // UnmarshalJSON checks if the incoming string 50 // UnmarshalJSON checks if the incoming string
64 // is a registered import type. 51 // is a registered import type.
78 } 65 }
79 66
80 const ( 67 const (
81 configUser = "sys_admin" 68 configUser = "sys_admin"
82 69
83 loadConfigSQL = ` 70 loadPersistentConfigSQL = `
84 SELECT 71 SELECT
85 username, 72 username,
86 kind, 73 kind
87 send_email,
88 cron,
89 url
90 FROM import.import_configuration 74 FROM import.import_configuration
91 WHERE id = $1` 75 WHERE id = $1`
92 76
93 loadConfigAttributesSQL = ` 77 loadPersistentConfigAttributesSQL = `
94 SELECT k, v 78 SELECT k, v
95 FROM import.import_configuration_attributes 79 FROM import.import_configuration_attributes
96 WHERE import_configuration_id = $1` 80 WHERE import_configuration_id = $1`
97 ) 81 )
98 82
99 // LoadIDConfigContext loads an import configuration from database. 83 func LoadPersistentConfigContext(ctx context.Context, conn *sql.Conn, id int64) (*PersistentConfig, error) {
100 func LoadIDConfigContext(ctx context.Context, conn *sql.Conn, id int64) (*IDConfig, error) {
101 84
102 cfg := &IDConfig{ID: id} 85 cfg := &PersistentConfig{ID: id}
103 var kind ImportKind 86
104 var cron, url sql.NullString 87 err := conn.QueryRowContext(ctx, loadPersistentConfigSQL, id).Scan(
105 err := conn.QueryRowContext(ctx, loadConfigSQL, id).Scan(
106 &cfg.User, 88 &cfg.User,
107 &kind, 89 &cfg.Kind,
108 &cfg.SendEMail,
109 &cron,
110 &url,
111 ) 90 )
112 91
113 switch { 92 switch {
114 case err == sql.ErrNoRows: 93 case err == sql.ErrNoRows:
115 return nil, nil 94 return nil, nil
116 case err != nil: 95 case err != nil:
117 return nil, err 96 return nil, err
118 } 97 }
119 98
120 cfg.Kind = ImportKind(kind)
121 if cron.Valid {
122 c := models.CronSpec(cron.String)
123 cfg.Cron = &c
124 }
125 if url.Valid {
126 cfg.URL = &url.String
127 }
128 // load the extra attributes. 99 // load the extra attributes.
129 rows, err := conn.QueryContext(ctx, loadConfigAttributesSQL, id) 100 rows, err := conn.QueryContext(ctx, loadPersistentConfigAttributesSQL, id)
130 if err != nil { 101 if err != nil {
131 return nil, err 102 return nil, err
132 } 103 }
133 defer rows.Close() 104 defer rows.Close()
134 var attributes common.Attributes 105 var attributes common.Attributes
149 cfg.Attributes = attributes 120 cfg.Attributes = attributes
150 } 121 }
151 return cfg, nil 122 return cfg, nil
152 } 123 }
153 124
154 func loadIDConfig(id int64) (*IDConfig, error) { 125 func loadPersistentConfig(id int64) (*PersistentConfig, error) {
155 return loadIDConfigContext(context.Background(), id) 126 return loadPersistentConfigContext(context.Background(), id)
156 } 127 }
157 128
158 func loadIDConfigContext(ctx context.Context, id int64) (*IDConfig, error) { 129 func loadPersistentConfigContext(ctx context.Context, id int64) (*PersistentConfig, error) {
159 var cfg *IDConfig 130 var cfg *PersistentConfig
160 err := auth.RunAs(ctx, configUser, func(conn *sql.Conn) error { 131 err := auth.RunAs(ctx, configUser, func(conn *sql.Conn) error {
161 var err error 132 var err error
162 cfg, err = LoadIDConfigContext(ctx, conn, id) 133 cfg, err = LoadPersistentConfigContext(ctx, conn, id)
163 return err 134 return err
164 }) 135 })
165 return cfg, err 136 return cfg, err
166 } 137 }