Module: Kontena::Plugin::Cloud::Platform::Common
- Included in:
- Node::CreateCommand, Node::ListCommand, Node::RebootCommand, Node::ShellCommand, Node::TerminateCommand, CreateCommand, ListCommand, RemoveCommand, ShowCommand, UpgradeCommand, UseCommand, User::AddCommand, User::ListCommand, User::RemoveCommand, WizardCommand
- Defined in:
- lib/kontena/plugin/cloud/platform/common.rb
Instance Method Summary collapse
- #cached_platforms ⇒ Object
- #current_organization ⇒ String, NilClass
- #current_platform ⇒ Object
- #fetch_platforms ⇒ Object
- #fetch_platforms_for_org(org_id) ⇒ Array<Kontena::Cli::Models::Platform>
- #find_platform_by_name(name, org, cache = true) ⇒ Kontena::Cli::Models::Platform, NilClass
- #login_to_platform(name, url) ⇒ Object
-
#parse_platform_name(name) ⇒ Array<String>
Organization, platform.
- #platform_config_exists?(name) ⇒ Boolean
- #prompt_platform ⇒ Object
- #require_platform(name) ⇒ Object
Instance Method Details
#cached_platforms ⇒ Object
7 8 9 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 7 def cached_platforms @cached_platforms ||= [] end |
#current_organization ⇒ String, NilClass
41 42 43 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 41 def current_organization @current_organization || ENV['KONTENA_ORGANIZATION'] end |
#current_platform ⇒ Object
45 46 47 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 45 def current_platform self.current_grid end |
#fetch_platforms ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 11 def fetch_platforms all = [] organizations = cloud_client.get('/organizations')['data'] organizations.each do |org| all = all + fetch_platforms_for_org(org['id']) end all end |
#fetch_platforms_for_org(org_id) ⇒ Array<Kontena::Cli::Models::Platform>
22 23 24 25 26 27 28 29 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 22 def fetch_platforms_for_org(org_id) platforms = cloud_client.get("/organizations/#{org_id}/platforms")['data'] platforms.map do |p| platform = Kontena::Cli::Models::Platform.new(p) cached_platforms << platform if cached_platforms.none?{|cached| platform.id == cached.id } platform end end |
#find_platform_by_name(name, org, cache = true) ⇒ Kontena::Cli::Models::Platform, NilClass
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 84 def find_platform_by_name(name, org, cache = true) if cache && platform = cached_platforms.find{|p| p.name == name && p.organization == org } platform else data = cloud_client.get("/organizations/#{org}/platforms/#{name}")['data'] if data platform = Kontena::Cli::Models::Platform.new(data) cached_platforms << platform platform end end end |
#login_to_platform(name, url) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 97 def login_to_platform(name, url) organization, platform = name.split('/') platform = find_platform_by_name(platform, organization) = cloud_client.post("/organizations/#{organization}/masters/#{platform.master_id}/authorize", {}) exchanger = Kontena::Cli::MasterCodeExchanger.new(platform.url) code = exchanger.exchange_code(['code']) login = Kontena::Cli::Master::LoginCommand.new('kontena') cmd = [ '--silent', '--no-login-info', '--skip-grid-auto-select', '--name', name, '--code', code, url ] login.run(cmd) rescue => e error e. end |
#parse_platform_name(name) ⇒ Array<String>
Returns organization, platform.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 65 def parse_platform_name(name) unless name.include?('/') name = "#{current_organization}/#{name}" end org, platform = name.split('/') raise ArgumentError, "Organization missing" unless org [org, platform] end |
#platform_config_exists?(name) ⇒ Boolean
76 77 78 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 76 def platform_config_exists?(name) !self.config.find_server_by(name: name).nil? end |
#prompt_platform ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 31 def prompt_platform platforms = fetch_platforms_for_org(current_organization) prompt.select("Choose platform") do || platforms.each do |p| .choice p.name, p end end end |
#require_platform(name) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 50 def require_platform(name) org, platform = parse_platform_name(name) @current_organization = org p = find_platform_by_name(platform, org) exit_with_error("Platform not found") unless p unless platform_config_exists?(p.to_path) login_to_platform("#{current_organization}/#{platform}", p.url) end self.current_master = "#{current_organization}/#{platform}" self.current_grid = p.grid_id p end |