Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[397] added check for controller installation and a dynamic installat… #404

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/uffizzi/cli/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def handle_succeed_set_default_response(response)
account = response[:body][:account]
account_id = account[:id]
account_name = account[:name]
ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(account_id, account_name))
ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(id: account_id, name: account_name))
Uffizzi.ui.say("The account with name '#{account_name}' was set as default.")

projects = account[:projects]
Expand Down
41 changes: 37 additions & 4 deletions lib/uffizzi/cli/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def handle_token_success(response)
Uffizzi.ui.say('Login successful')

set_current_account_and_project
Uffizzi.ui.say(installation_message)
end

def open_browser(url)
Expand All @@ -88,7 +89,7 @@ def handle_succeed_response(response, username)

if ENV.fetch('CI_PIPELINE_RUN', false)
account = response[:body][:user][:default_account]
return ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(account[:id]))
return ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(id: account[:id]))
end

set_current_account_and_project
Expand Down Expand Up @@ -123,17 +124,17 @@ def set_account
accounts = accounts_response[:body][:accounts]
if accounts.length == 1
current_account = accounts.first
ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(current_account[:id], current_account[:name]))
ConfigFile.write_option(:account, account_config(current_account))
return current_account[:id]
end
question = 'Select an account:'
choices = accounts.map do |account|
{ name: account[:name], value: account[:id] }
end
account_id = Uffizzi.prompt.select(question, choices)
account_name = accounts.detect { |account| account[:id] == account_id }[:name]
selected_account = accounts.detect { |account| account[:id] == account_id }

ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(account_id, account_name))
ConfigFile.write_option(:account, account_config(selected_account))

account_id
end
Expand Down Expand Up @@ -211,5 +212,37 @@ def handle_create_project_succeess(response)

Uffizzi.ui.say("Project #{project[:name]} was successfully created")
end

def installation_message
account_config = ConfigHelper.read_account_config
if account_config[:has_installation]
"\r\n\r\n"\
'####################################################################' \
"\r\n\r\n"\
"Your CLI is configured to use '#{account_config[:vclusters_controller_url]}'." \
"\r\n\r\n"\
'Run `uffizzi config -h` to see CLI configuration options.'\
"\r\n\r\n"
else
"\r\n\r\n"\
'####################################################################'\
"\r\n\r\n"\
'Your CLI is configured to use https://app.uffizzi.com (Uffizzi Cloud).'\
"\r\n\r\n"\
'Run `uffizzi config -h` to see CLI configuration options.'\
"\r\n"\
'Run `uffizzi install -h` to see self-hosted installation options.'\
"\r\n\r\n"
end
end

def account_config(account_data)
Uffizzi::ConfigHelper.account_config(
id: account_data[:id],
name: account_data[:name],
has_installation: account_data[:has_installation],
vclusters_controller_url: account_data[:vclusters_controller_url],
)
end
end
end
2 changes: 1 addition & 1 deletion lib/uffizzi/cli/login_by_identity_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def prepare_request_params(oidc_token, github_access_token)
def handle_succeed_response(response, server, oidc_token)
ConfigFile.write_option(:server, server)
ConfigFile.write_option(:cookie, response[:headers])
ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(response[:body][:account_id]))
ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(id: response[:body][:account_id]))
ConfigFile.write_option(:project, response[:body][:project_slug])
ConfigFile.write_option(:oidc_token, oidc_token)

Expand Down
5 changes: 3 additions & 2 deletions lib/uffizzi/cli/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ def print_projects(projects)
def set_default_project(project)
ConfigFile.write_option(:project, project[:slug])
account = project[:account]
return ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(account[:id], account[:name])) if account
account_config = Uffizzi::ConfigHelper.account_config(id: account[:id], name: account[:name])
return ConfigFile.write_option(:account, account_config) if account

# For core versions < core_v2.2.3
ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(project[:account_id]))
ConfigFile.write_option(:account, Uffizzi::ConfigHelper.account_config(id: project[:account_id]))
end
end
end
8 changes: 6 additions & 2 deletions lib/uffizzi/helpers/config_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def read_option_from_config(option)
ConfigFile.option_has_value?(option) ? ConfigFile.read_option(option) : nil
end

def account_config(id, name = nil)
{ id: id, name: name }
def account_config(id:, name: nil, has_installation: false, vclusters_controller_url: nil)
{ id: id, name: name, has_installation: has_installation, vclusters_controller_url: vclusters_controller_url }
end

def update_clusters_config_by_id(id, params)
Expand Down Expand Up @@ -60,6 +60,10 @@ def dev_environment
read_option_from_config(:dev_environment) || {}
end

def read_account_config
read_option_from_config(:account)
end

private

def clusters
Expand Down
7 changes: 5 additions & 2 deletions test/fixtures/files/uffizzi/uffizzi_accounts_success.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"accounts": [
{
"id": 1,
"name": "uffizzi"
"name": "uffizzi",
"has_installation": false,
"vclusters_controller_url": "https://controller.uclusters.app.uffizzi.com"
},
{
"id": 2,
"name": "hello-world"
"name": "hello-world",
"vclusters_controller_url": "https://controller.uclusters.app.uffizzi.com"
}
]
}
1 change: 0 additions & 1 deletion test/uffizzi/cli/login_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ def test_browser_login_with_new_project_creation_success
assert_requested(stubbed_uffizzi_projects)
assert_requested(stubbed_uffizzi_project)
assert_requested(stubbed_uffizzi_accounts)
assert_match('was successfully created', Uffizzi.ui.last_message)
end

def test_browser_login_with_new_project_creation_when_project_already_exists_and_abort_repeat
Expand Down
Loading