Class: Kontena::Cli::Stacks::YAML::Opto::Resolvers::ServiceLink

Inherits:
Opto::Resolver
  • Object
show all
Includes:
Common
Defined in:
lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb

Instance Method Summary collapse

Methods included from Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning

Instance Method Details

#default_index(services) ⇒ Integer

Parameters:

  • services (Array<Hash>)

Returns:

  • (Integer)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 34

def default_index(services)
  index = services.index {|s| service_link(s) == option.default }

  if index && !option.required?
    index + 2 # extra offset for the initial <none> option
  elsif index
    index + 1 # menu index starts from 1
  else
    0 # XXX: this just explodes?
  end
end

#filter_by_image(services, image) ⇒ Object



64
65
66
67
68
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 64

def filter_by_image(services, image)
  services.select { |s|
    s['image'].include?(image)
  }
end

#filter_by_name(services, name) ⇒ Object



70
71
72
73
74
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 70

def filter_by_name(services, name)
  services.select { |s|
    s['name'].include?(name)
  }
end

#get_servicesArray<Hash>

Returns:

  • (Array<Hash>)


26
27
28
29
30
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 26

def get_services
  client.get("grids/#{current_grid}/services")['services']
rescue
  []
end

#resolveObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 5

def resolve
  return nil unless current_master && current_grid
  message = hint['prompt']
  name_filter = hint['name']
  image_filter = hint['image']
  raise "prompt missing" unless message

  services = get_services
  services = filter_by_image(services, image_filter) if image_filter
  services = filter_by_name(services, name_filter) if name_filter
  return nil if services.size == 0
  prompt.select(message) do |menu|
    menu.default(default_index(services)) if option.default
    menu.choice "<none>", nil unless option.required?
    services.each do |s|
      menu.choice service_name(s), service_link(s)
    end
  end
end

Parameters:

  • service (Hash)

Returns:



48
49
50
51
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 48

def service_link(service)
  grid, stack, service = service['id'].split('/')
  "#{stack}/#{service}"
end

#service_name(service) ⇒ String

Parameters:

  • service (Hash)

Returns:



55
56
57
58
59
60
61
62
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 55

def service_name(service)
  grid, stack, service = service['id'].split('/')
  if stack == 'null'.freeze
    service
  else
    "#{stack}/#{service}"
  end
end

#stackObject



76
77
78
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 76

def stack
  ENV['STACK']
end