class Kashi::DSL::Test::Result

Constants

ATTRIBUTES
PARAMS

Public Class Methods

new(context) click to toggle source
# File lib/kashi/dsl/test.rb, line 23
def initialize(context)
  @context = context
  @options = context.options
  @options[:secret_expander] = SecretExpander.new(@options[:secret_provider]) if @options[:secret_provider]
end

Public Instance Methods

basic_pass() click to toggle source
# File lib/kashi/dsl/test.rb, line 119
def basic_pass
  secret_expander = @options[:secret_expander]
  if secret_expander
    secret_expander.expand(@basic_pass)
  else
    @basic_pass
  end
end
cake(sc_test) click to toggle source
# File lib/kashi/dsl/test.rb, line 64
def cake(sc_test)
  @sc_test = sc_test
  self.test_id = sc_test['TestID']
  self
end
client() click to toggle source
# File lib/kashi/dsl/test.rb, line 128
def client
  @client ||= ClientWrapper.new(@options)
end
create() click to toggle source
# File lib/kashi/dsl/test.rb, line 56
def create
  Kashi.logger.info("Create Test `#{website_name}`".colorize(:green))
  Kashi.logger.debug(create_params)
  return { 'Success' => true } if @options[:dry_run]

  client.tests_update(create_params)
end
create_params() click to toggle source
# File lib/kashi/dsl/test.rb, line 50
def create_params
  hash = modify_params
  hash.delete(:TestID)
  hash
end
dsl_hash() click to toggle source
# File lib/kashi/dsl/test.rb, line 70
def dsl_hash
  Kashi::Utils.normalize_hash(to_h)
end
modify() click to toggle source
# File lib/kashi/dsl/test.rb, line 106
def modify
  return unless updated?
  Kashi.logger.info("Modify Test `#{website_name}` #{test_id}".colorize(:blue))
  masked_dsl_has = dsl_hash.dup.tap do |h|
    h[:basic_pass] = '****' if h[:basic_pass] != ''
  end
  Kashi.logger.info("<diff>\n#{Kashi::Utils.diff(sc_hash, masked_dsl_has, color: @options[:color])}")
  Kashi.logger.debug(modify_params)
  return if @options[:dry_run]

  client.tests_update(modify_params)
end
modify_params() click to toggle source
# File lib/kashi/dsl/test.rb, line 33
def modify_params
  hash = to_h.select { |k, _| ATTRIBUTES.include?(k) }
  ATTRIBUTES.zip(PARAMS).each do |from, to|
    hash[to] = hash.delete(from)
  end
  %i/NodeLocations TestTags StatusCodes/.each do |k|
    hash[k] = Array(hash[k]).join(',')
  end
  contact_group_id_by_group_name = client.contactgroups.each_with_object({}) do |contact_group, h|
    h[contact_group['GroupName']] = contact_group['ContactID']
  end
  hash[:ContactGroup] = contact_group.map { |name|
    contact_group_id_by_group_name[name]
  }.compact.join(',')
  hash
end
sc_hash() click to toggle source
# File lib/kashi/dsl/test.rb, line 74
def sc_hash
  hash = @sc_test.to_h.keys.each_with_object({}) do |k, h|
    next if %w/DownTimes LastTested NextLocation Processing ProcessingOn ProcessingState Sensitive Status Uptime Method ContactGroup ContactID/.include?(k)
    h[k.to_s.to_snake.to_sym] = @sc_test.to_h[k]
  end
  # rename
  { uri: :website_url, dnsip: :dns_ip, tags: :test_tags }.each do |k, v|
    hash[v] = hash.delete(k)
  end
  %i/basic_user basic_pass/.each do |k|
    hash[k] = ''
  end
  %i/port use_jar virus/.each do |k|
    hash[k] = '' unless hash.key?(k)
  end
  %i/paused enable_ssl_warning follow_redirect do_not_find/.each do |k|
    hash[k] = hash[k] ? 1 : 0
  end
  hash[:contact_group] = hash.delete(:contact_groups).map { |contact_group| contact_group['Name'] }
  hash[:test_tags] = Array(hash[:test_tags])
  if hash[:custom_header] == false || hash[:custom_header] == ''
    hash[:custom_header] = ''
  else
    hash[:custom_header] = JSON.parse(hash[:custom_header]).to_json
  end
  Kashi::Utils.normalize_hash(hash)
end
to_h() click to toggle source
# File lib/kashi/dsl/test.rb, line 29
def to_h
  Hash[ATTRIBUTES.sort.map { |name| [name, public_send(name)] }]
end
updated?() click to toggle source
# File lib/kashi/dsl/test.rb, line 102
def updated?
  dsl_hash != sc_hash
end