Mercurial > gemma
comparison pkg/controllers/importconfig.go @ 2052:0b203a3b3e8e unify_imports
Imports: Re-enabled /imports/config/{id} PATCH.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 27 Jan 2019 23:09:42 +0100 |
parents | 7d627258e045 |
children | c64c47ff2ab1 |
comparison
equal
deleted
inserted
replaced
2051:7d627258e045 | 2052:0b203a3b3e8e |
---|---|
26 "gemma.intevation.de/gemma/pkg/common" | 26 "gemma.intevation.de/gemma/pkg/common" |
27 "gemma.intevation.de/gemma/pkg/imports" | 27 "gemma.intevation.de/gemma/pkg/imports" |
28 "gemma.intevation.de/gemma/pkg/scheduler" | 28 "gemma.intevation.de/gemma/pkg/scheduler" |
29 ) | 29 ) |
30 | 30 |
31 /* | |
32 const ( | |
33 selectImportConfigurationIDSQL = ` | |
34 SELECT | |
35 id, | |
36 username, | |
37 kind | |
38 FROM import.import_configuration | |
39 WHERE id = $1` | |
40 ) | |
41 */ | |
42 | |
43 func runImportConfig( | 31 func runImportConfig( |
44 _ interface{}, | 32 _ interface{}, |
45 req *http.Request, | 33 req *http.Request, |
46 conn *sql.Conn, | 34 conn *sql.Conn, |
47 ) (jr JSONResult, err error) { | 35 ) (jr JSONResult, err error) { |
72 input interface{}, | 60 input interface{}, |
73 req *http.Request, | 61 req *http.Request, |
74 conn *sql.Conn, | 62 conn *sql.Conn, |
75 ) (jr JSONResult, err error) { | 63 ) (jr JSONResult, err error) { |
76 | 64 |
77 /* | 65 ctx := req.Context() |
78 | 66 |
79 ctx := req.Context() | 67 raw := input.(*json.RawMessage) |
80 | 68 |
81 importConfig := input.(*imports.Config) | 69 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) |
82 | 70 |
83 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) | 71 var pc *imports.PersistentConfig |
84 | 72 pc, err = imports.LoadPersistentConfigContext(ctx, conn, id) |
85 var tx *sql.Tx | 73 switch { |
86 | 74 case err == sql.ErrNoRows: |
87 if tx, err = conn.BeginTx(ctx, nil); err != nil { | 75 err = JSONError{ |
88 return | 76 Code: http.StatusNotFound, |
89 } | 77 Message: fmt.Sprintf("No configuration %d found", id), |
90 defer tx.Rollback() | 78 } |
91 | 79 return |
92 var ( | 80 case err != nil: |
93 entry imports.IDConfig | 81 return |
94 kind string | 82 } |
95 dummy sql.NullString | 83 |
96 url sql.NullString | 84 kind := imports.JobKind(pc.Kind) |
97 ) | 85 ctor := imports.ImportModelForJobKind(kind) |
98 | 86 if ctor == nil { |
99 err = conn.QueryRowContext(ctx, selectImportConfigurationIDSQL, id).Scan( | 87 err = JSONError{ |
100 &entry.ID, | 88 Code: http.StatusInternalServerError, |
101 &entry.User, | 89 Message: fmt.Sprintf("No constructor for kind '%s' found", pc.Kind), |
102 &kind, | 90 } |
103 &entry.SendEMail, | 91 return |
104 &dummy, | 92 } |
105 &url, | 93 config := ctor() |
106 ) | 94 if err = json.Unmarshal(*raw, config); err != nil { |
107 | 95 return |
108 switch { | 96 } |
109 case err == sql.ErrNoRows: | 97 |
110 err = JSONError{ | 98 _, oldCron := pc.Attributes.Get("cron") |
111 Code: http.StatusNotFound, | 99 |
112 Message: fmt.Sprintf("No schedule %d found", id), | 100 session, _ := auth.GetSession(req) |
113 } | 101 pc.User = session.User |
114 return | 102 pc.Attributes = common.Attributes{} |
115 case err != nil: | 103 pc.Attributes.Marshal(config) |
116 return | 104 |
117 } | 105 cron, newCron := pc.Attributes.Get("cron") |
118 | 106 |
119 session, _ := auth.GetSession(req) | 107 var tx *sql.Tx |
120 | 108 if tx, err = conn.BeginTx(ctx, nil); err != nil { |
121 entry.SendEMail = importConfig.SendEMail | 109 return |
122 | 110 } |
123 // We always take the cron spec from the input. | 111 defer tx.Rollback() |
124 // If there is no spec remove schedule. | 112 |
125 var cron sql.NullString | 113 if err = pc.UpdateContext(ctx, tx); err != nil { |
126 if importConfig.Cron != nil { | 114 return |
127 cron = sql.NullString{String: string(*importConfig.Cron), Valid: true} | 115 } |
128 } | 116 |
129 | 117 if oldCron { |
130 if importConfig.URL != nil { | 118 scheduler.UnbindByID(id) |
131 url = sql.NullString{String: *importConfig.URL, Valid: true} | 119 } |
132 } | 120 |
133 | 121 if newCron { |
134 if _, err = tx.ExecContext(ctx, updateImportConfigurationSQL, | 122 if err = scheduler.BindAction( |
123 string(pc.Kind), | |
124 cron, | |
135 id, | 125 id, |
136 session.User, | |
137 string(importConfig.Kind), | |
138 cron, | |
139 url, | |
140 importConfig.SendEMail, | |
141 ); err != nil { | 126 ); err != nil { |
142 return | 127 return |
143 } | 128 } |
144 | 129 } |
145 if importConfig.Attributes != nil { | 130 |
146 if _, err = tx.ExecContext(ctx, deleteImportConfiguationAttributesSQL, id); err != nil { | 131 if err = tx.Commit(); err != nil { |
147 return | 132 return |
148 } | 133 } |
149 if err = storeConfigAttributes(ctx, tx, id, importConfig.Attributes); err != nil { | 134 |
150 return | 135 var result = struct { |
151 } | 136 ID int64 `json:"id"` |
152 } | 137 }{ |
153 | 138 ID: id, |
154 scheduler.UnbindByID(id) | 139 } |
155 | 140 |
156 if cron.Valid { | 141 jr = JSONResult{Result: &result} |
157 if err = scheduler.BindAction( | |
158 string(importConfig.Kind), | |
159 cron.String, | |
160 id, | |
161 ); err != nil { | |
162 return | |
163 } | |
164 } | |
165 | |
166 if err = tx.Commit(); err != nil { | |
167 return | |
168 } | |
169 | |
170 var result = struct { | |
171 ID int64 `json:"id"` | |
172 }{ | |
173 ID: id, | |
174 } | |
175 | |
176 jr = JSONResult{Result: &result} | |
177 */ | |
178 return | 142 return |
179 } | 143 } |
180 | 144 |
181 func infoImportConfig( | 145 func infoImportConfig( |
182 _ interface{}, | 146 _ interface{}, |
208 if ctor == nil { | 172 if ctor == nil { |
209 err = JSONError{ | 173 err = JSONError{ |
210 Code: http.StatusInternalServerError, | 174 Code: http.StatusInternalServerError, |
211 Message: fmt.Sprintf("No constructor for kind '%s' found", cfg.Kind), | 175 Message: fmt.Sprintf("No constructor for kind '%s' found", cfg.Kind), |
212 } | 176 } |
177 return | |
213 } | 178 } |
214 | 179 |
215 what := ctor() | 180 what := ctor() |
216 | 181 |
217 if err = cfg.Attributes.Unmarshal(what); err != nil { | 182 if err = cfg.Attributes.Unmarshal(what); err != nil { |