Class: ChefLicensing::TUIEngine::TUIActions

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-licensing/tui_engine/tui_actions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TUIActions

Returns a new instance of TUIActions.



13
14
15
16
17
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 13

def initialize(opts = {})
  @opts = opts
  @logger = ChefLicensing::Config.logger
  @output = ChefLicensing::Config.output
end

Instance Attribute Details

#error_msgObject

Returns the value of attribute error_msg.



12
13
14
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 12

def error_msg
  @error_msg
end

#invalid_license_msgObject

Returns the value of attribute invalid_license_msg.



12
13
14
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 12

def invalid_license_msg
  @invalid_license_msg
end

#licenseObject

Returns the value of attribute license.



12
13
14
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 12

def license
  @license
end

#license_idObject

Returns the value of attribute license_id.



12
13
14
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 12

def license_id
  @license_id
end

#license_typeObject

Returns the value of attribute license_type.



12
13
14
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 12

def license_type
  @license_type
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 12

def logger
  @logger
end

#outputObject

Returns the value of attribute output.



12
13
14
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 12

def output
  @output
end

#rejection_msgObject

Returns the value of attribute rejection_msg.



12
13
14
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 12

def rejection_msg
  @rejection_msg
end

Instance Method Details

#determine_restriction_type(input) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 105

def determine_restriction_type(input)
  if license_type == :free && LicenseKeyFetcher::File.user_has_active_trial_license?(@opts)
    "active_trial_restriction"
  else
    "#{license_type}_restriction"
  end
end

#display_license_info(inputs) ⇒ Object



96
97
98
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 96

def display_license_info(inputs)
  ChefLicensing::ListLicenseKeys.display_overview({ license_keys: [license_id] })
end

#fetch_invalid_license_msg(input) ⇒ Object



92
93
94
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 92

def fetch_invalid_license_msg(input)
  invalid_license_msg
end

#fetch_license_id(input) ⇒ Object



84
85
86
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 84

def fetch_license_id(input)
  license_id
end

#fetch_license_type_restricted(inputs) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 113

def fetch_license_type_restricted(inputs)
  if license_restricted?(:trial) && license_restricted?(:free)
    "trial_and_free"
  elsif license_restricted?(:trial)
    "trial"
  else
    "free"
  end
end

#filter_license_type_options(inputs) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 123

def filter_license_type_options(inputs)
  if (license_restricted?(:trial) && license_restricted?(:free)) || LicenseKeyFetcher::File.user_has_active_trial_license?(@opts)
    "ask_for_commercial_only"
  elsif license_restricted?(:trial)
    "ask_for_license_except_trial"
  elsif license_restricted?(:free)
    "ask_for_license_except_free"
  else
    "ask_for_all_license_type"
  end
end

#is_commercial_license?(input) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 88

def is_commercial_license?(input)
  input[:is_commercial]
end

#is_license_allowed?(input) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 43

def is_license_allowed?(input)
  client_api_call(license_id)
  self.license_type = get_license_type
  # When user enters the license via TUI, we need to check if the license type is commercial to display warnings.
  input[:is_commercial] = license_type == :commercial ? true : false
  input[:license_type] = license_type
  if license_restricted?(license_type)
    # Existing license keys needs to be fetcher to show details of existing license of license type which is restricted.
    # However, if user is trying to add Free Tier License, and user has active trial license, we fetch the trial license key
    if license_type == :free && LicenseKeyFetcher::File.user_has_active_trial_license?(@opts)
      existing_license_keys_in_file = LicenseKeyFetcher::File.fetch_license_keys_based_on_type(:trial, @opts)
    else
      existing_license_keys_in_file = LicenseKeyFetcher::File.fetch_license_keys_based_on_type(license_type, @opts)
    end
    self.license_id = existing_license_keys_in_file.last
    false
  else
    true
  end
end

#is_license_valid_on_server?(input) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 29

def is_license_valid_on_server?(input)
  license_id = input[:ask_for_license_id]
  spinner = TTY::Spinner.new(":spinner [Running] License validation in progress...", format: :dots, clear: true, output: output)
  spinner.auto_spin # Start the spinner
  is_valid = ChefLicensing::LicenseKeyValidator.validate!(license_id)
  spinner.success # Stop the spinner
  self.license_id = license_id
  is_valid
rescue ChefLicensing::InvalidLicense => e
  spinner.error # Stop the spinner
  self.invalid_license_msg = e.message || "Something went wrong while validating the license"
  false
end

#is_license_with_valid_pattern?(input) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 19

def is_license_with_valid_pattern?(input)
  license_id = input[:ask_for_license_id]
  input[:ask_for_license_id] = ChefLicensing::LicenseKeyFetcher::Base.verify_and_extract_license(license_id)
  true
rescue ChefLicensing::LicenseKeyFetcher::Base::InvalidLicenseKeyFormat => e
  output.puts e.message
  logger.debug e.message
  false
end

#is_run_allowed_on_license_exhausted?(input) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 80

def is_run_allowed_on_license_exhausted?(input)
  input[:license_type].downcase == "commercial"
end

#license_expiration_status?(input) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 64

def license_expiration_status?(input)
  get_license(license_id)
  if license.expired? || license.have_grace?
    ChefLicensing::Context.local_licensing_service? ? "expired_in_local_mode" : "expired"
  elsif license.about_to_expire?
    input[:license_expiration_date] = Date.parse(license.expiration_date).strftime("%a, %d %b %Y")
    input[:number_of_days_in_expiration] = license.number_of_days_in_expiration
    "about_to_expire"
  elsif license.exhausted? && (license.license_type.downcase == "commercial" || license.license_type.downcase == "free")
    input[:license_type] = license.license_type
    "exhausted_license"
  else
    "active"
  end
end

#set_license_info(input) ⇒ Object



100
101
102
103
# File 'lib/chef-licensing/tui_engine/tui_actions.rb', line 100

def set_license_info(input)
  self.license_id = input[:license_id]
  self.license_type = input[:license_type]
end