Skip to content

Commit bf4c46c

Browse files
committed
NEP-17834 setup config for multi env creds
1 parent c8554f7 commit bf4c46c

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
superset-*.gem
1414

1515
# ignore local .env variables
16-
.env
16+
.env*
1717

1818
# ignore local log file
1919
log/superset-client.log

bin/console

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,35 @@ Dir["./lib/**/*.rb"].each { |f| require f }
1818

1919
require "pry"
2020

21+
# if accessing multiple supeset host environments regularly you can optionally
22+
# setup multiple env files, see ./doc/setting_up_personal_api_credentials.md for more info
23+
if ENV['SUPERSET_ENVIRONMENT']
24+
env_file = ".env-#{ENV['SUPERSET_ENVIRONMENT']}"
25+
if File.exist?(env_file)
26+
Dotenv.overload(env_file)
27+
puts "ENV configuration loaded from from #{env_file}"
28+
else
29+
puts "Environment file #{env_file} not found"
30+
exit
31+
end
32+
end
33+
34+
35+
# Add the SUPERSET_ENVIRONMENT to the PRY prompt if it exists
36+
if ENV['SUPERSET_ENVIRONMENT']
37+
Pry.config.prompt = Pry::Prompt.new('custom', 'Custom Pry prompt with suffix', [proc do |target_self, nest_level, pry|
38+
"[#{pry.input_ring.size}] (#{":#{nest_level}" unless nest_level.zero?}ENV:#{ENV['SUPERSET_ENVIRONMENT'].upcase})> "
39+
end,
40+
proc do |target_self, nest_level, pry|
41+
"[#{pry.input_ring.size}] (#{":#{nest_level}" unless nest_level.zero?}ENV:#{ENV['SUPERSET_ENVIRONMENT'].upcase})* "
42+
end])
43+
end
44+
45+
unless ENV['SUPERSET_HOST'] && ENV['SUPERSET_API_USERNAME'] && ENV['SUPERSET_API_PASSWORD']
46+
puts "Missing environment variables. Check your .env file"
47+
puts "Please set each value for SUPERSET_HOST, SUPERSET_API_USERNAME, and SUPERSET_API_PASSWORD values"
48+
puts "Refer to ./doc/setting_up_personal_api_credentials.md for more info"
49+
exit
50+
end
51+
2152
Pry.start(__FILE__)

doc/setting_up_personal_api_credentials.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ ENV['SUPERSET_API_USERNAME']="your-username"
1414
ENV['SUPERSET_API_PASSWORD']="your-password"
1515
```
1616

17+
If you have multiple superset hosts across various environments you also have the option
18+
to create individual env files per environment. More details below.
19+
1720
## What is my user name?
1821

1922
If your Superset instance is setup to authenicate via SSO then your authenticating agent will most likely have provided a username for you in the form of a UUID value.
@@ -57,6 +60,59 @@ Edit the params to only consist of only the password field and the value of your
5760

5861
And click Execute.
5962

60-
Create your `.env`
61-
Add your username and password to the `.env` file.
63+
Within your `.env` now add your username and password.
64+
65+
# Accessing API across Multiple Environments
66+
67+
Given some local development requirements where you have to access multiple superset hosts across various environments with different credentials you can setup the env creds as follows.
68+
69+
Just set the overall superset environment in `.env`
70+
71+
```
72+
# .env file holding one setting for the overall superset environment
73+
SUPERSET_ENVIRONMENT='staging'
74+
```
75+
76+
Then create a new file called `.env-staging` that holds your superset staging host and credentials.
77+
78+
```
79+
# specific settings for the superset staging host
80+
SUPERSET_HOST="https://your-staging-superset-host.com"
81+
SUPERSET_API_USERNAME="staging-user-here"
82+
SUPERSET_API_PASSWORD="set-password-here"
83+
```
84+
85+
Do the same for production env.
86+
Create a new file called `.env-production` that holds your superset production host and credentials.
87+
88+
```
89+
# specific settings for the superset production host
90+
SUPERSET_HOST="https://your-production-superset-host.com"
91+
SUPERSET_API_USERNAME="production-user-here"
92+
SUPERSET_API_PASSWORD="set-password-here"
93+
```
94+
95+
The command `bin/console` will then load your env file depending on the value in ENV['SUPERSET_ENVIRONMENT'] from the primary `.env`.
96+
97+
When you need to switch envs, exit the console, edit the .env to your desired value, eg `production`, then open a console again.
98+
99+
Bonus is the Pry prompt will now also include the `SUPERSET_ENVIRONMENT` value.
100+
101+
```
102+
bin/console
103+
ENV configuration loaded from from .env-staging
104+
[1] (ENV:STAGING)> Superset::Dashboard::List.new(title_contains: 'video').list
105+
106+
Happi: GET https://your-staging-superset-host.com/api/v1/dashboard/?q=(filters:!((col:dashboard_title,opr:ct,value:'video')),page:0,page_size:100), {}
107+
+----+------------------+-----------+------------------------------------------------------------------+
108+
| Superset::Dashboard::List |
109+
+----+------------------+-----------+------------------------------------------------------------------+
110+
| Id | Dashboard title | Status | Url |
111+
+----+------------------+-----------+------------------------------------------------------------------+
112+
| 6 | Video Game Sales | published | https://staging.ready-superset.jobready.io/superset/dashboard/6/ |
113+
+----+------------------+-----------+------------------------------------------------------------------+
114+
```
115+
116+
117+
62118

0 commit comments

Comments
 (0)