Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Proxy apis for store to get plan and addons (#65423)

Ngoc Khuatbe8790142ccc
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index b7cf45820c4..5f024c623f3 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -1648,6 +1648,7 @@
:uses #{api
events
premium-features
+ store-api
util
enterprise/harbormaster}}
@@ -1905,7 +1906,7 @@
query-processor
util}}
- enterprise/remote-sync
+ enterprise/remote-sync
{:team "UX West"
:api #{metabase-enterprise.remote-sync.api
metabase-enterprise.remote-sync.core
diff --git a/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj b/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj
index cf045c9d6b7..068b08dc38c 100644
--- a/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj
@@ -1,13 +1,15 @@
(ns metabase-enterprise.cloud-add-ons.api
"/api/ee/cloud-add-ons endpoints. "
(:require
+ [clj-http.client :as http]
[metabase-enterprise.harbormaster.client :as hm.client]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.api.routes.common :refer [+auth]]
[metabase.events.core :as events]
[metabase.premium-features.core :as premium-features]
- [metabase.util.i18n :refer [deferred-tru]]
+ [metabase.store-api.core :as store-api]
+ [metabase.util.i18n :refer [deferred-tru tru]]
[metabase.util.log :as log]))
(def ^:private error-no-connection
@@ -16,28 +18,101 @@
(deferred-tru "Could not purchase this add-on."))
(def ^:private error-unexpected
(deferred-tru "Unexpected error"))
+(def ^:private error-not-hosted
+ (deferred-tru "Can only access Store API for Metabase Cloud instances."))
+(def ^:private error-not-eligible
+ (deferred-tru "Can only purchase add-ons for eligible subscriptions."))
+(def ^:private error-not-store-user
+ (deferred-tru "Only Metabase Store users can purchase add-ons."))
+(def ^:private error-terms-not-accepted
+ (deferred-tru "Need to accept terms of service."))
+
+(def ^:private response-not-hosted
+ {:status 400 :body error-not-hosted})
+(def ^:private response-not-eligible
+ {:status 400 :body error-not-eligible})
+(def ^:private response-not-store-user
+ {:status 403 :body error-not-store-user})
+(def ^:private response-terms-not-accepted
+ {:status 400 :body {:errors {:terms_of_service error-terms-not-accepted}}})
+(def ^:private response-success-empty
+ {:status 200 :body {}})
+
+(defn- handle-store-api-error
+ "Handle exceptions from Store API calls and return appropriate error response."
+ [exception & [extra-status-mappings]]
+ (let [status-code (-> exception ex-data :status)
+ default-mappings {404 error-no-connection
+ 403 error-no-connection
+ 401 error-no-connection}
+ status-mappings (merge default-mappings extra-status-mappings)
+ error-body (get status-mappings status-code error-unexpected)]
+ {:status (or status-code 500) :body error-body}))
+
+(defn- make-public-store-request!
+ "Make a GET request to fetch publicly available info from Metabase Store API endpoints."
+ [endpoint]
+ (if-let [url (store-api/store-api-url)]
+ (:body (http/get (str url "/api/v2" endpoint) {:as :json}))
+ (throw (ex-info (tru "Please configure store-api-url") {:status-code 400}))))
+
+(api.macros/defendpoint :get "/plans"
+ "Get plans information from the Metabase Store API."
+ []
+ (api/check-superuser)
+ (cond
+ (not (premium-features/is-hosted?))
+ response-not-hosted
+
+ :else
+ (try
+ {:status 200 :body (make-public-store-request! "/plan")}
+ (catch Exception e
+ (log/warn e "Error fetching plans information")
+ (handle-store-api-error e)))))
+
+(api.macros/defendpoint :get "/addons"
+ "Get addons information from the Metabase Store API."
+ []
+ (api/check-superuser)
+ (cond
+ (not (premium-features/is-hosted?))
+ response-not-hosted
+
+ :else
+ (try
+ {:status 200 :body (make-public-store-request! "/addons")}
+ (catch Exception e
+ (log/warn e "Error fetching addons information")
+ (handle-store-api-error e)))))
(api.macros/defendpoint :post "/:product-type"
"Purchase an add-on."
[{:keys [product-type]} :- [:map
- [:product-type [:enum "metabase-ai"]]]
+ [:product-type [:enum "metabase-ai" "python-execution"]]]
_query-params
{:keys [terms_of_service]} :- [:map
- [:terms_of_service :boolean]]]
+ [:terms_of_service {:optional true} [:maybe :boolean]]]]
(api/check-superuser)
(cond
- (not terms_of_service)
- {:status 400 :body {:errors {:terms_of_service "Need to accept terms of service."}}}
-
(not (premium-features/is-hosted?))
- {:status 400 :body "Can only purchase add-ons for Metabase Cloud instances."}
+ response-not-hosted
+
+ (and (= product-type "metabase-ai")
+ (not terms_of_service))
+ response-terms-not-accepted
+
+ (and (= product-type "metabase-ai")
+ (not (premium-features/offer-metabase-ai?)))
+ response-not-eligible
- (not (premium-features/offer-metabase-ai?))
- {:status 400 :body "Can only purchase add-ons for eligible subscriptions."}
+ (and (= product-type "python-execution")
+ (not (premium-features/enable-python-transforms?)))
+ response-not-eligible
(not (contains? (set (map :email (:store-users (premium-features/token-status))))
(:email @api/*current-user*)))
- {:status 403 :body "Only Metabase Store users can purchase add-ons."}
+ response-not-store-user
:else
(try
@@ -45,15 +120,10 @@
(events/publish-event! :event/cloud-add-on-purchase {:details {:add-on add-on}, :user-id api/*current-user-id*})
(hm.client/call :change-add-ons :upsert-add-ons [add-on]))
(premium-features/clear-cache!)
- {:status 200 :body {}}
+ response-success-empty
(catch Exception e
(log/warnf e "Error adding purchasing add-on '%s'" product-type)
- (case (-> e ex-data :status)
- 404 {:status 404 :body error-no-connection}
- 403 {:status 403 :body error-no-connection}
- 401 {:status 401 :body error-no-connection}
- 400 {:status 400 :body error-cannot-purchase}
- {:status 500 :body error-unexpected})))))
+ (handle-store-api-error e {400 error-cannot-purchase})))))
(def ^{:arglists '([request respond raise])} routes
"`/api/ee/cloud-add-ons` routes."
diff --git a/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj b/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj
index 30f9c508467..21ff1a94a48 100644
--- a/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj
@@ -1,5 +1,6 @@
(ns metabase-enterprise.cloud-add-ons.api-test
(:require
+ [clj-http.client :as http]
[clojure.test :refer :all]
[metabase-enterprise.harbormaster.client :as hm.client]
[metabase-enterprise.semantic-search.test-util :as semantic.tu]
@@ -14,13 +15,13 @@
(mt/user-http-request :rasta :post 403 "ee/cloud-add-ons/metabase-ai"
{:terms_of_service true})))))
(testing "requires accepting terms of service"
- (mt/with-premium-features #{}
+ (mt/with-premium-features #{:hosting}
(is (=? {:errors {:terms_of_service "Need to accept terms of service."}}
(mt/user-http-request :crowberto :post 400 "ee/cloud-add-ons/metabase-ai"
{:terms_of_service false})))))
(testing "requires token feature 'hosting'"
(mt/with-premium-features #{}
- (is (=? "Can only purchase add-ons for Metabase Cloud instances."
+ (is (=? "Can only access Store API for Metabase Cloud instances."
(mt/user-http-request :crowberto :post 400 "ee/cloud-add-ons/metabase-ai"
{:terms_of_service true})))))
(testing "requires token feature 'offer-metabase-ai'"
@@ -69,3 +70,99 @@
(let [{:keys [details user_id]} (mt/latest-audit-log-entry "cloud-add-on-purchase")]
(is (= (:id user) user_id))
(is (= {:add-on {:product-type "metabase-ai"}} details)))))))))))))
+
+(deftest ^:sequential post-python-execution-test
+ (testing "POST /api/ee/cloud-add-ons/python-execution"
+ (testing "requires superuser"
+ (mt/with-premium-features #{}
+ (is (=? "You don't have permissions to do that."
+ (mt/user-http-request :rasta :post 403 "ee/cloud-add-ons/python-execution" {})))))
+ (testing "does not require terms of service"
+ (mt/with-temp [:model/User user {:is_superuser true}]
+ (mt/with-premium-features #{:hosting :transforms-python}
+ (with-redefs [premium-features/token-status (constantly {:store-users [{:email (:email user)}]})
+ hm.client/call (constantly nil)]
+ (testing "succeeds without terms_of_service"
+ (is (=? {}
+ (mt/user-http-request user :post 200 "ee/cloud-add-ons/python-execution" {}))))
+ (testing "succeeds with terms_of_service false"
+ (is (=? {}
+ (mt/user-http-request user :post 200 "ee/cloud-add-ons/python-execution"
+ {:terms_of_service false}))))
+ (testing "succeeds with terms_of_service true"
+ (is (=? {}
+ (mt/user-http-request user :post 200 "ee/cloud-add-ons/python-execution"
+ {:terms_of_service true}))))))))
+ (testing "requires token feature 'hosting'"
+ (mt/with-premium-features #{}
+ (is (=? "Can only access Store API for Metabase Cloud instances."
+ (mt/user-http-request :crowberto :post 400 "ee/cloud-add-ons/python-execution" {})))))
+ (testing "requires token feature 'transforms-python'"
+ (mt/with-premium-features #{:hosting}
+ (is (=? "Can only purchase add-ons for eligible subscriptions."
+ (mt/user-http-request :crowberto :post 400 "ee/cloud-add-ons/python-execution" {})))))
+ (testing "requires current user being a store user"
+ (mt/with-temp [:model/User user {:is_superuser true}]
+ (mt/with-premium-features #{:hosting :transforms-python}
+ (is (=? "Only Metabase Store users can purchase add-ons."
+ (mt/user-http-request user :post 403 "ee/cloud-add-ons/python-execution" {}))))))))
+
+(deftest ^:sequential get-plans-test
+ (testing "GET /api/ee/cloud-add-ons/plans"
+ (testing "requires superuser"
+ (mt/with-premium-features #{}
+ (is (=? "You don't have permissions to do that."
+ (mt/user-http-request :rasta :get 403 "ee/cloud-add-ons/plans")))))
+ (testing "requires token feature 'hosting'"
+ (mt/with-premium-features #{}
+ (is (=? "Can only access Store API for Metabase Cloud instances."
+ (mt/user-http-request :crowberto :get 400 "ee/cloud-add-ons/plans")))))
+ (testing "when conditions are met"
+ (mt/with-premium-features #{:hosting}
+ (testing "passes through HTTP status from Store API"
+ (doseq [[status-code error-message] {404 "Could not establish a connection to Metabase Cloud."
+ 403 "Could not establish a connection to Metabase Cloud."
+ 401 "Could not establish a connection to Metabase Cloud."}]
+ (testing (format "error status %d" status-code)
+ (with-redefs [http/get (fn [& _] (throw (ex-info "TEST" {:status status-code})))]
+ (is (=? error-message
+ (mt/user-http-request :crowberto :get status-code "ee/cloud-add-ons/plans")))))))
+ (testing "responds with HTTP status 500 for other errors"
+ (with-redefs [http/get (fn [& _] (throw (ex-info "TEST" {:status 500})))]
+ (is (=? "Unexpected error"
+ (mt/user-http-request :crowberto :get 500 "ee/cloud-add-ons/plans")))))
+ (testing "succeeds"
+ (let [plan-data {:body {:plan-name "Pro" :tier "premium"}}]
+ (with-redefs [http/get (fn [& _] plan-data)]
+ (is (=? (:body plan-data)
+ (mt/user-http-request :crowberto :get 200 "ee/cloud-add-ons/plans"))))))))))
+
+(deftest ^:sequential get-addons-test
+ (testing "GET /api/ee/cloud-add-ons/addons"
+ (testing "requires superuser"
+ (mt/with-premium-features #{}
+ (is (=? "You don't have permissions to do that."
+ (mt/user-http-request :rasta :get 403 "ee/cloud-add-ons/addons")))))
+ (testing "requires token feature 'hosting'"
+ (mt/with-premium-features #{}
+ (is (=? "Can only access Store API for Metabase Cloud instances."
+ (mt/user-http-request :crowberto :get 400 "ee/cloud-add-ons/addons")))))
+ (testing "when conditions are met"
+ (mt/with-premium-features #{:hosting}
+ (testing "passes through HTTP status from Store API"
+ (doseq [[status-code error-message] {404 "Could not establish a connection to Metabase Cloud."
+ 403 "Could not establish a connection to Metabase Cloud."
+ 401 "Could not establish a connection to Metabase Cloud."}]
+ (testing (format "error status %d" status-code)
+ (with-redefs [http/get (fn [& _] (throw (ex-info "TEST" {:status status-code})))]
+ (is (=? error-message
+ (mt/user-http-request :crowberto :get status-code "ee/cloud-add-ons/addons")))))))
+ (testing "responds with HTTP status 500 for other errors"
+ (with-redefs [http/get (fn [& _] (throw (ex-info "TEST" {:status 500})))]
+ (is (=? "Unexpected error"
+ (mt/user-http-request :crowberto :get 500 "ee/cloud-add-ons/addons")))))
+ (testing "succeeds"
+ (let [addons-data {:body [{:addon-id "metabase-ai" :status "active"}]}]
+ (with-redefs [http/get (fn [& _] addons-data)]
+ (is (=? (:body addons-data)
+ (mt/user-http-request :crowberto :get 200 "ee/cloud-add-ons/addons"))))))))))