Class: Kashi::DSL::Test::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/kashi/dsl/test.rb

Constant Summary collapse

PARAMS =
%i/
  TestID Paused WebsiteName WebsiteURL Port NodeLocations Timeout CustomHeader Confirmation CheckRate
  DNSServer DNSIP BasicUser BasicPass LogoImage UseJar WebsiteHost Virus FindString DoNotFind
  TestType ContactGroup TriggerRate TestTags StatusCodes EnableSSLWarning FollowRedirect
  PostRaw FinalEndpoint
/
ATTRIBUTES =

PingURL RealBrowser Public Branding

%i/
  test_id paused website_name website_url port node_locations timeout custom_header confirmation check_rate
  dns_server dns_ip basic_user basic_pass logo_image use_jar website_host virus find_string do_not_find
  test_type contact_group trigger_rate test_tags status_codes enable_ssl_warning follow_redirect
  post_raw final_endpoint
/

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Result

Returns a new instance of Result.



23
24
25
26
27
# 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

Instance Method Details

#basic_passObject



119
120
121
122
123
124
125
126
# 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) ⇒ Object



64
65
66
67
68
# File 'lib/kashi/dsl/test.rb', line 64

def cake(sc_test)
  @sc_test = sc_test
  self.test_id = sc_test['TestID']
  self
end

#clientObject



128
129
130
# File 'lib/kashi/dsl/test.rb', line 128

def client
  @client ||= ClientWrapper.new(@options)
end

#createObject



56
57
58
59
60
61
62
# 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_paramsObject



50
51
52
53
54
# File 'lib/kashi/dsl/test.rb', line 50

def create_params
  hash = modify_params
  hash.delete(:TestID)
  hash
end

#dsl_hashObject



70
71
72
# File 'lib/kashi/dsl/test.rb', line 70

def dsl_hash
  Kashi::Utils.normalize_hash(to_h)
end

#modifyObject



106
107
108
109
110
111
112
113
114
115
116
117
# 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_paramsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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_hashObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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_hObject



29
30
31
# File 'lib/kashi/dsl/test.rb', line 29

def to_h
  Hash[ATTRIBUTES.sort.map { |name| [name, public_send(name)] }]
end

#updated?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/kashi/dsl/test.rb', line 102

def updated?
  dsl_hash != sc_hash
end