Skip to content
Snippets Groups Projects
Unverified Commit c909abe4 authored by Cam Saul's avatar Cam Saul
Browse files

Allow POST /api/setup to set DB schedules

parent df74910b
No related branches found
No related tags found
No related merge requests found
......@@ -46,12 +46,15 @@
{arg nil})))
(defn- dox-for-schema
"Look up the docstr for annotation."
"Look up the docstring for SCHEMA for use in auto-generated API documentation.
In most cases this is defined by wrapping the schema with `with-api-error-message`."
[schema]
(if-not schema
""
(or (su/api-error-message schema)
(log/warn "We don't have a nice error message for schema:" schema))))
(log/warn "We don't have a nice error message for schema:"
schema
"Consider wrapping it in `su/with-api-error-message`."))))
(defn- format-route-schema-dox [param->schema]
(when (seq param->schema)
......
......@@ -152,7 +152,7 @@
;;; ------------------------------------------------------------ GET /api/database/:id ------------------------------------------------------------
(def ^:private ExpandedSchedulesMap
(def ExpandedSchedulesMap
"Schema for the `:schedules` key we add to the response containing 'expanded' versions of the CRON schedules.
This same key is used in reverse to update the schedules."
(su/with-api-error-message
......@@ -339,7 +339,10 @@
{(s/optional-key :metadata_sync_schedule) cron-util/CronScheduleString
(s/optional-key :cache_field_values_schedule) cron-util/CronScheduleString})
(s/defn ^:private ^:always-validate schedule-map->cron-strings :- CronSchedulesMap
(s/defn ^:always-validate schedule-map->cron-strings :- CronSchedulesMap
"Convert a map of `:schedules` as passed in by the frontend to a map of cron strings with the approriate keys for
Database. This map can then be merged directly inserted into the DB, or merged with a map of other columns to
insert/update."
[{:keys [metadata_sync cache_field_values]} :- ExpandedSchedulesMap]
(cond-> {}
metadata_sync (assoc :metadata_sync_schedule (cron-util/schedule-map->cron-string metadata_sync))
......
(ns metabase.api.setup
(:require [compojure.core :refer [GET POST]]
[medley.core :as m]
[metabase
[driver :as driver]
[email :as email]
......@@ -10,7 +9,7 @@
[util :as u]]
[metabase.api
[common :as api]
[database :refer [DBEngine]]]
[database :as database-api :refer [DBEngine]]]
[metabase.integrations.slack :as slack]
[metabase.models
[database :refer [Database]]
......@@ -29,14 +28,18 @@
(api/defendpoint POST "/"
"Special endpoint for creating the first user during setup.
This endpoint both creates the user AND logs them in and returns a session ID."
[:as {{:keys [token] {:keys [name engine details is_full_sync]} :database, {:keys [first_name last_name email password]} :user, {:keys [allow_tracking site_name]} :prefs} :body}]
[:as {{:keys [token]
{:keys [name engine details is_full_sync schedules]} :database
{:keys [first_name last_name email password]} :user
{:keys [allow_tracking site_name]} :prefs} :body}]
{token SetupToken
site_name su/NonBlankString
first_name su/NonBlankString
last_name su/NonBlankString
email su/Email
password su/ComplexPassword
allow_tracking (s/maybe (s/cond-pre s/Bool su/BooleanString))}
allow_tracking (s/maybe (s/cond-pre s/Bool su/BooleanString))
schedules (s/maybe database-api/ExpandedSchedulesMap)}
;; Now create the user
(let [session-id (str (java.util.UUID/randomUUID))
new-user (db/insert! User
......@@ -54,13 +57,16 @@
allow_tracking)) ; the setting will set itself correctly whether a boolean or boolean string is specified
;; setup database (if needed)
(when (driver/is-engine? engine)
(->> (db/insert! Database
:name name
:engine engine
:details details
:is_full_sync (or (nil? is_full_sync) ; default to `true` is `is_full_sync` isn't specified
is_full_sync))
(events/publish-event! :database-create)))
(let [db (db/insert! Database
(merge
{:name name
:engine engine
:details details
:is_full_sync (or (nil? is_full_sync) ; default to `true` is `is_full_sync` isn't specified
is_full_sync)}
(when schedules
(database-api/schedule-map->cron-strings schedules))))]
(events/publish-event! :database-create db)))
;; clear the setup token now, it's no longer needed
(setup/clear-token!)
;; then we create a session right away because we want our new user logged in to continue the setup process
......
......@@ -27,9 +27,13 @@
(secret-key->hash secret-key))))
;; log a nice message letting people know whether DB details encryption is enabled
(log/info (format "DB details encryption is %s for this Metabase instance. %s"
(if default-secret-key "ENABLED" "DISABLED")
(u/emoji (if default-secret-key "🔐" "🔓"))))
(log/info
(format "DB details encryption is %s for this Metabase instance. %s"
(if default-secret-key "ENABLED" "DISABLED")
(u/emoji (if default-secret-key "🔐" "🔓")))
"\nSee"
"http://www.metabase.com/docs/latest/operations-guide/start.html#encrypting-your-database-connection-details-at-rest"
"for more information.")
(defn encrypt
"Encrypt string S as hex bytes using a SECRET-KEY (a 64-byte byte array), by default the hashed value of `MB_ENCRYPTION_SECRET_KEY`."
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment