-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsetup_script.sql
103 lines (90 loc) · 3.64 KB
/
setup_script.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- 1. Create application roles
CREATE APPLICATION ROLE IF NOT EXISTS app_public;
-- 2. Create a versioned schema to hold those UDFs/Stored Procedures
CREATE OR ALTER VERSIONED SCHEMA core;
CREATE SCHEMA IF NOT EXISTS public;
GRANT USAGE ON SCHEMA core TO APPLICATION ROLE app_public;
GRANT USAGE ON SCHEMA public TO APPLICATION ROLE app_public;
-- 3. Create callbacks called in the manifest.yml
CREATE OR REPLACE PROCEDURE core.register_single_callback(ref_name STRING, operation STRING, ref_or_alias STRING)
RETURNS STRING
LANGUAGE SQL
AS
$$
BEGIN
CASE (operation)
WHEN 'ADD' THEN
SELECT SYSTEM$SET_REFERENCE(:ref_name, :ref_or_alias);
WHEN 'REMOVE' THEN
SELECT SYSTEM$REMOVE_REFERENCE(:ref_name);
WHEN 'CLEAR' THEN
SELECT SYSTEM$REMOVE_REFERENCE(:ref_name);
ELSE
RETURN 'unknown operation: ' || operation;
END CASE;
END;
$$;
GRANT USAGE ON PROCEDURE core.register_single_callback(STRING, STRING, STRING) TO APPLICATION ROLE app_public;
-- Configuration callback for the `EXTERNAL_ACCESS_REFERENCE` defined in the manifest.yml
-- The procedure returns a json format object containing information about the EAI to be created, that is
-- and show the same information in a popup-window in the UI.
-- There are no allowed_secrets since the API doesn't require authentication.
CREATE OR REPLACE PROCEDURE core.get_configuration(ref_name STRING)
RETURNS STRING
LANGUAGE SQL
AS
$$
BEGIN
CASE (UPPER(ref_name))
WHEN 'EXTERNAL_ACCESS_REFERENCE' THEN
RETURN OBJECT_CONSTRUCT(
'type', 'CONFIGURATION',
'payload', OBJECT_CONSTRUCT(
'host_ports', ARRAY_CONSTRUCT('api.coincap.io'),
'allowed_secrets', 'NONE')
)::STRING;
ELSE
RETURN '';
END CASE;
END;
$$;
GRANT USAGE ON PROCEDURE core.get_configuration(STRING) TO APPLICATION ROLE app_public;
-- 4. Create stored procedures using the external access reference from the manifest.yml
-- The Stored Procedures needs to be created in runtime because EAI reference needs to be set
-- after installing the application.
CREATE OR REPLACE PROCEDURE core.create_eai_objects()
RETURNS STRING
LANGUAGE SQL
AS
$$
BEGIN
CREATE PROCEDURE IF NOT EXISTS core.get_coin_story(coin VARCHAR, _start INT, _end INT)
RETURNS VARIANT
LANGUAGE PYTHON
RUNTIME_VERSION = 3.9
IMPORTS=('/module-api/coincap.py')
EXTERNAL_ACCESS_INTEGRATIONS = (reference('external_access_reference'))
PACKAGES = ('snowflake-snowpark-python', 'requests')
HANDLER = 'coincap.get_coin_story';
CREATE PROCEDURE IF NOT EXISTS core.get_crypto_coins()
RETURNS VARIANT
LANGUAGE PYTHON
RUNTIME_VERSION = 3.9
IMPORTS=('/module-api/coincap.py')
EXTERNAL_ACCESS_INTEGRATIONS = (reference('external_access_reference'))
PACKAGES = ('snowflake-snowpark-python', 'requests')
HANDLER = 'coincap.get_crypto_coins';
GRANT USAGE ON PROCEDURE core.get_coin_story(VARCHAR, INT, INT) TO APPLICATION ROLE app_public;
GRANT USAGE ON PROCEDURE core.get_crypto_coins() TO APPLICATION ROLE app_public;
RETURN 'SUCCESS';
END;
$$;
GRANT USAGE ON PROCEDURE core.create_eai_objects() TO APPLICATION ROLE app_public;
-- 5. Create a streamlit object using the code you wrote in you wrote in src/module-ui, as shown below.
-- The `from` value is derived from the stage path described in snowflake.yml
CREATE STREAMLIT core.ui
FROM '/streamlit/'
MAIN_FILE = 'ui.py';
-- 6. Grant appropriate privileges over these objects to your application roles.
GRANT USAGE ON STREAMLIT core.ui TO APPLICATION ROLE app_public;
-- A detailed explanation can be found at https://docs.snowflake.com/en/developer-guide/native-apps/adding-streamlit