Class: Inferno::CLI::Session::CreateSession

Inherits:
Object
  • Object
show all
Includes:
Connection, Errors
Defined in:
lib/inferno/apps/cli/session/create_session.rb

Constant Summary collapse

COMMAND_OPTIONS =
{
  suite_options: {
    aliases: ['-o'],
    type: :hash,
    desc: 'Suite options used to initialize the session.'
  },
  preset: {
    aliases: ['-p'],
    type: :string,
    desc: 'Preset to apply when creating the session (internal ID or title).'
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Errors

#handle_web_api_error, #not_found_error_message, #parse_error_response, #test_run_not_found_message, #text_error_message

Methods included from Connection

#base_url, #check_session_exists, #connection, #delete, #get, #handle_connection_error, #post

Constructor Details

#initialize(suite, options) ⇒ CreateSession

Returns a new instance of CreateSession.



28
29
30
31
# File 'lib/inferno/apps/cli/session/create_session.rb', line 28

def initialize(suite, options)
  @suite = suite
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



25
26
27
# File 'lib/inferno/apps/cli/session/create_session.rb', line 25

def options
  @options
end

#suiteObject (readonly)

Returns the value of attribute suite.



26
27
28
# File 'lib/inferno/apps/cli/session/create_session.rb', line 26

def suite
  @suite
end

Instance Method Details

#all_suite_definitionsObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/inferno/apps/cli/session/create_session.rb', line 54

def all_suite_definitions
  @all_suite_definitions ||= begin
    response = get('api/test_suites')
    if response.status != 200
      puts JSON.pretty_generate({ errors: "Could not fetch test suites list from '#{base_url}'" })
      exit(3)
    end
    JSON.parse(response.body)
  end
end

#create_sessionObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/inferno/apps/cli/session/create_session.rb', line 38

def create_session
  request_body = { test_suite_id: suite_id }
  request_body[:preset_id] = preset_id if options[:preset].present?
  request_body[:suite_options] = suite_options_list if options[:suite_options].present?

  response = post('api/test_sessions', request_body.to_json, content_type: 'application/json')

  handle_web_api_error(response, :session_create) if response.status != 200

  JSON.parse(response.body)
end

#preset_idObject



79
80
81
# File 'lib/inferno/apps/cli/session/create_session.rb', line 79

def preset_id
  @preset_id ||= resolve_preset_identifier
end

#resolve_preset_identifierObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/inferno/apps/cli/session/create_session.rb', line 88

def resolve_preset_identifier
  return unless options[:preset].present?

  preset = options[:preset]
  return preset if suite_presets.any? { |p| p['id'] == preset }

  matched = suite_presets.find { |p| p['title'] == preset }
  return matched['id'] if matched.present?

  valid_presets = suite_presets.map { |p| "#{p['id']} (#{p['title']})" }.join(', ')
  puts JSON.pretty_generate(
    { errors: "Preset '#{preset}' not found for suite '#{suite_id}'. Valid presets: #{valid_presets}" }
  )
  exit(3)
end

#resolve_suite_identifierObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/inferno/apps/cli/session/create_session.rb', line 65

def resolve_suite_identifier
  matched = all_suite_definitions.find { |s| s['id'] == suite }
  return matched['id'] if matched.present?

  matched = all_suite_definitions.find { |s| s['title'] == suite || s['short_title'] == suite }
  return matched['id'] if matched.present?

  valid_suites = all_suite_definitions.map { |s| "#{s['id']} (#{s['title']})" }.join(', ')
  puts JSON.pretty_generate(
    { errors: "Suite '#{suite}' not found. Valid suites: #{valid_suites}" }
  )
  exit(3)
end

#resolve_suite_option_key(option_key) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/inferno/apps/cli/session/create_session.rb', line 116

def resolve_suite_option_key(option_key)
  return option_key if suite_option_definitions.any? { |d| d['id'] == option_key }

  matched = suite_option_definitions.find { |d| d['title'] == option_key }
  return matched['id'] if matched.present?

  valid_options = suite_option_definitions.map { |d| "#{d['id']} (#{d['title']})" }.join(', ')
  puts JSON.pretty_generate({
                              errors: "Unknown suite option '#{option_key}' for suite '#{suite_id}'. " \
                                      "Valid options: #{valid_options}"
                            })
  exit(3)
end

#resolve_suite_option_value(option_id, provided_value) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/inferno/apps/cli/session/create_session.rb', line 130

def resolve_suite_option_value(option_id, provided_value)
  provided_value = provided_value.to_s
  list_options = suite_option_list_options(option_id)

  return provided_value if list_options.blank?
  return provided_value if list_options.any? { |o| o['value'] == provided_value }

  resolve_suite_option_value_by_label(option_id, provided_value, list_options)
end

#resolve_suite_option_value_by_label(option_id, provided_value, list_options) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/inferno/apps/cli/session/create_session.rb', line 145

def resolve_suite_option_value_by_label(option_id, provided_value, list_options)
  matched = list_options.find { |o| o['label'] == provided_value }
  return matched['value'] if matched

  valid_options = list_options.map { |o| "#{o['value']} (#{o['label']})" }.join(', ')
  puts JSON.pretty_generate({
                              errors: "Invalid value '#{provided_value}' for suite option '#{option_id}'. " \
                                      "Valid values: #{valid_options}"
                            })
  exit(3)
end

#runObject



33
34
35
36
# File 'lib/inferno/apps/cli/session/create_session.rb', line 33

def run
  puts JSON.pretty_generate(create_session)
  exit(0)
end

#suite_idObject



50
51
52
# File 'lib/inferno/apps/cli/session/create_session.rb', line 50

def suite_id
  @suite_id ||= resolve_suite_identifier
end

#suite_option_definitionsObject



104
105
106
107
# File 'lib/inferno/apps/cli/session/create_session.rb', line 104

def suite_option_definitions
  suite_def = all_suite_definitions.find { |s| s['id'] == suite_id }
  suite_def&.fetch('suite_options', []) || []
end

#suite_option_list_options(option_id) ⇒ Object



140
141
142
143
# File 'lib/inferno/apps/cli/session/create_session.rb', line 140

def suite_option_list_options(option_id)
  definition = suite_option_definitions.find { |d| d['id'] == option_id }
  definition&.fetch('list_options', nil)
end

#suite_options_listObject



109
110
111
112
113
114
# File 'lib/inferno/apps/cli/session/create_session.rb', line 109

def suite_options_list
  options[:suite_options].keys.map do |option_key|
    option_id = resolve_suite_option_key(option_key)
    { id: option_id, value: resolve_suite_option_value(option_id, options[:suite_options][option_key]) }
  end
end

#suite_presetsObject



83
84
85
86
# File 'lib/inferno/apps/cli/session/create_session.rb', line 83

def suite_presets
  suite_def = all_suite_definitions.find { |s| s['id'] == suite_id }
  suite_def&.fetch('presets', []) || []
end