sport_ngin_aws_auditor 3.10.1 → 3.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5240cf6426681a9d75f9a357c7912adb58b00d81
4
- data.tar.gz: 09c2971243df4a8bb6998d65a9c101bf3eeb7566
3
+ metadata.gz: 371951220694aabe393ce1d64bd750ead6323bbe
4
+ data.tar.gz: fa9cfd373c5ede8e321522359d3227e9df72de56
5
5
  SHA512:
6
- metadata.gz: 82c3810457541106a45e05f874227eff17b6d5ea4b8421aad5c1a0baa40a3c2768ca3eeecd584e4248c126a026f12d5d41187ae137ec7b6fa79f9d1c896bef91
7
- data.tar.gz: 246ff9987a1af7b769ad8df4dcb7e19ad13b020143532872593a11b95620eefcfe21d9ca20fe0a8f829a1525d9bf27f5816b2a9bff0cb4c97a3863cfded4f41c
6
+ metadata.gz: 4332d0f1a87ba742df5a3d17e62622fc286aff652859939cca4e9f0fc77cbd293a08154b8bb7b3e44856f8d577ab19b16bce901a5251fa4bead7f22c3b3b4b34
7
+ data.tar.gz: f8593e6acbf8963b9554471feba300618882ad5c64b9b800760d0cb81b394e5d4d8fd06c91c9f880e58ce259c192b890a6523584e37619a0172470801f42251c
data/CHANGELOG.markdown CHANGED
@@ -1,3 +1,8 @@
1
+ #### v3.11.0
2
+ * Automatically ignore instances based on a regex string
3
+
4
+ > Emma Sax: Unknown User: https://github.com/sportngin/sport_ngin_aws_auditor/pull/26
5
+
1
6
  #### v3.10.1
2
7
  * Caching should not affect RI counts between runs
3
8
 
@@ -3,11 +3,12 @@ require_relative './instance_helper'
3
3
  module SportNginAwsAuditor
4
4
  class AuditData
5
5
 
6
- attr_accessor :data, :retired_tags, :retired_ris, :selected_audit_type, :klass, :tag_name, :region
7
- def initialize(instances, reserved, class_type, tag_name)
6
+ attr_accessor :data, :retired_tags, :retired_ris, :selected_audit_type, :klass, :tag_name, :region, :ignore_instances_regexes
7
+ def initialize(instances, reserved, class_type, tag_name, ignore_instances_regexes)
8
8
  self.selected_audit_type = (!instances && !reserved) ? "all" : (instances ? "instances" : "reserved")
9
9
  self.klass = SportNginAwsAuditor.const_get(class_type)
10
10
  self.tag_name = tag_name
11
+ self.ignore_instances_regexes = ignore_instances_regexes
11
12
  end
12
13
 
13
14
  def instances?
@@ -50,7 +51,7 @@ module SportNginAwsAuditor
50
51
  instances_with_tag = self.klass.filter_instances_with_tags(instances)
51
52
  instances_without_tag = self.klass.filter_instances_without_tags(instances)
52
53
  instance_hash = self.klass.instance_count_hash(instances_without_tag)
53
- self.klass.apply_tagged_instances(instances_with_tag, instance_hash)
54
+ self.klass.add_additional_instances_to_hash(instances_with_tag, instance_hash, " with tag (")
54
55
 
55
56
  return instance_hash, retired_tags
56
57
  end
@@ -59,7 +60,7 @@ module SportNginAwsAuditor
59
60
  instances = self.klass.get_instances(tag_name)
60
61
  gather_region(instances)
61
62
  retired_tags = self.klass.get_retired_tags(instances)
62
- instance_hash = self.klass.compare(instances)
63
+ instance_hash = self.klass.compare(instances, ignore_instances_regexes)
63
64
  retired_ris = self.klass.get_recent_retired_reserved_instances
64
65
 
65
66
  return instance_hash, retired_tags, retired_ris
@@ -11,6 +11,9 @@ command 'audit' do |c|
11
11
  c.switch [:n, :no_tag], :desc => "Ignore all tags during audit"
12
12
  c.switch [:s, :slack], :desc => "Will print condensed version of audit to a Slack channel"
13
13
  c.switch [:z, :zone_output], :desc => "Will print the Missing RIs and Tagged instances with zones"
14
+ c.flag [:g, :ignore_instances_patterns], :default_value => "kitchen, auto", :desc => "Ignore instances if an instance contains
15
+ one of these strings in the name,
16
+ pass in like: string1, string2, string3"
14
17
  c.action do |global_options, options, args|
15
18
  require_relative '../scripts/audit'
16
19
  raise ArgumentError, 'You must specify an AWS account' unless args.first
@@ -7,44 +7,63 @@ module SportNginAwsAuditor
7
7
  attr_accessor :type, :count, :category, :tag_value, :reason, :name, :region_based
8
8
  def initialize(type, data_hash, region)
9
9
  if type.include?(" with tag")
10
- type = type.dup # because type is a frozen string right now
11
- type.slice!(" with tag")
12
- self.type = type
13
- self.category = "tagged"
14
- self.name = data_hash[:name] || nil
15
- self.reason = data_hash[:tag_reason] || nil
16
- self.tag_value = data_hash[:tag_value] || nil
17
- self.region_based = data_hash[:region_based] || nil
10
+ gather_tagged_data(type, data_hash, region)
11
+ elsif type.include?(" ignored")
12
+ gather_ignored_data(type, data_hash, region)
18
13
  else
19
- self.region_based = data_hash[:region_based] || nil
20
-
21
- if data_hash[:count] < 0
22
- self.category = "running"
23
- elsif data_hash[:count] == 0
24
- self.category = "matched"
25
- elsif data_hash[:count] > 0
26
- self.category = "reserved"
27
- end
28
-
29
- if region_based?
30
- # if type = 'Linux VPC t2.small'...
31
- my_match = type.match(/(\w*\s*\w*\s{1})\s*(\s*\S*)/)
32
-
33
- # then platform = 'Linux VPC '...
34
- platform = my_match[1] if my_match
35
-
36
- # and size = 't2.small'
37
- size = my_match[2] if my_match
38
-
39
- self.type = platform << region << ' ' << size
40
- else
41
- self.type = type
42
- end
14
+ gather_normal_data(type, data_hash, region)
43
15
  end
44
16
 
45
17
  self.count = data_hash[:count].abs
46
18
  end
47
19
 
20
+ def gather_tagged_data(type, data_hash, region)
21
+ type = type.dup # because type is a frozen string right now
22
+ type.slice!(" with tag")
23
+ self.type = type
24
+ self.category = "tagged"
25
+ self.name = data_hash[:name] || nil
26
+ self.reason = data_hash[:tag_reason] || nil
27
+ self.tag_value = data_hash[:tag_value] || nil
28
+ self.region_based = data_hash[:region_based] || nil
29
+ end
30
+
31
+ def gather_ignored_data(type, data_hash, region)
32
+ type = type.dup
33
+ type.slice!(" ignored")
34
+ self.type = type
35
+ self.category = "ignored"
36
+ self.name = data_hash[:name] || nil
37
+ self.region_based = data_hash[:region_based] || nil
38
+ end
39
+
40
+ def gather_normal_data(type, data_hash, region)
41
+ self.region_based = data_hash[:region_based] || nil
42
+
43
+ if data_hash[:count] < 0
44
+ self.category = "running"
45
+ elsif data_hash[:count] == 0
46
+ self.category = "matched"
47
+ elsif data_hash[:count] > 0
48
+ self.category = "reserved"
49
+ end
50
+
51
+ if region_based?
52
+ # if type = 'Linux VPC t2.small'...
53
+ my_match = type.match(/(\w*\s*\w*\s{1})\s*(\s*\S*)/)
54
+
55
+ # then platform = 'Linux VPC '...
56
+ platform = my_match[1] if my_match
57
+
58
+ # and size = 't2.small'
59
+ size = my_match[2] if my_match
60
+
61
+ self.type = platform << region << ' ' << size
62
+ else
63
+ self.type = type
64
+ end
65
+ end
66
+
48
67
  def region_based?
49
68
  self.region_based
50
69
  end
@@ -53,6 +72,10 @@ module SportNginAwsAuditor
53
72
  self.category == "tagged"
54
73
  end
55
74
 
75
+ def ignored?
76
+ self.category == "ignored"
77
+ end
78
+
56
79
  def reserved?
57
80
  self.category == "reserved"
58
81
  end
@@ -12,6 +12,8 @@ module SportNginAwsAuditor
12
12
  Hash[get_reserved_instances.map { |instance| instance.nil? ? next : [instance.id, instance]}.compact]
13
13
  end
14
14
 
15
+ #################### ADDING DATA TO HASH ####################
16
+
15
17
  def instance_count_hash(instances)
16
18
  instance_hash = Hash.new()
17
19
  instances.each do |instance|
@@ -25,10 +27,38 @@ module SportNginAwsAuditor
25
27
  instance_hash
26
28
  end
27
29
 
28
- def apply_tagged_instances(instances_to_add, instance_hash)
30
+ def add_region_ris_to_hash(ris_region, differences)
31
+ ris_region.each do |ri|
32
+ differences.each do |key, value|
33
+ # if key = 'Linux VPC us-east-1a t2.medium'...
34
+ my_match = key.match(/(\w*\s*\w*\s*)\w{2}-\w{2,}-\w{2}(\s*\S*)/)
35
+
36
+ # then platform = 'Linux VPC'...
37
+ platform = my_match[1] if my_match
38
+ platform[platform.length - 1] = ''
39
+
40
+ # and size = 't2.medium'
41
+ size = my_match[2] if my_match
42
+ size[0] = ''
43
+
44
+ if (platform == ri.platform) && (size == ri.instance_type) && (value[:count] < 0)
45
+ until (ri.count == 0) || (value[:count] == 0)
46
+ value[:count] = value[:count] + 1
47
+ ri.count = ri.count - 1
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ ris_region.each do |ri|
54
+ differences[ri.to_s] = {:count => ri.count, :region_based => true}
55
+ end
56
+ end
57
+
58
+ def add_additional_instances_to_hash(instances_to_add, instance_hash, extra_string)
29
59
  instances_to_add.each do |instance|
30
60
  next if instance.nil?
31
- key = instance.to_s.dup << " with tag (" << instance.name << ")"
61
+ key = instance.to_s.dup << extra_string << instance.name << ")"
32
62
  instance_result = {}
33
63
 
34
64
  if instance_hash.has_key?(instance.to_s) && instance_hash[instance.to_s][:count] > 0
@@ -44,8 +74,8 @@ module SportNginAwsAuditor
44
74
  instance_result[:count] = instance.count
45
75
  end
46
76
 
47
- instance_result.merge!({:name => instance.name, :tag_reason => instance.tag_reason,
48
- :tag_value => instance.tag_value, :region_based => false})
77
+ merged_hash = gather_hash(extra_string, instance)
78
+ instance_result.merge!(merged_hash)
49
79
 
50
80
  instance_hash[key] = instance_result
51
81
  end if instances_to_add
@@ -53,35 +83,31 @@ module SportNginAwsAuditor
53
83
  instance_hash
54
84
  end
55
85
 
56
- def apply_region_ris(ris_region, differences)
57
- ris_region.each do |ri|
58
- ri.count_remaining = ri.count
59
-
60
- differences.each do |key, value|
61
- # if key = 'Linux VPC us-east-1a t2.medium'...
62
- my_match = key.match(/(\w*\s*\w*\s*)\w{2}-\w{2,}-\w{2}(\s*\S*)/)
63
-
64
- # then platform = 'Linux VPC'...
65
- platform = my_match[1] if my_match
66
- platform[platform.length - 1] = ''
86
+ def gather_hash(extra_string, instance)
87
+ if extra_string.include?("tag")
88
+ {:name => instance.name, :tag_reason => instance.tag_reason,
89
+ :tag_value => instance.tag_value, :region_based => false}
90
+ elsif extra_string.include?("ignore")
91
+ {:name => instance.name, :region_based => false}
92
+ end
93
+ end
67
94
 
68
- # and size = 't2.medium'
69
- size = my_match[2] if my_match
70
- size[0] = ''
95
+ #################### PARSING AND COMPARING DATA ####################
71
96
 
72
- if (platform == ri.platform) && (size == ri.instance_type) && (value[:count] < 0)
73
- until (ri.count_remaining == 0) || (value[:count] == 0)
74
- value[:count] = value[:count] + 1
75
- ri.count_remaining = ri.count_remaining - 1
76
- end
77
- end
78
- end
79
- end
97
+ def sort_through_instances(instances, ignore_instances_regexes)
98
+ ignored_instances, not_ignored_instances = filter_ignored_instances(instances, ignore_instances_regexes)
99
+ instances_with_tag = filter_instances_with_tags(not_ignored_instances)
100
+ instances_without_tag = filter_instances_without_tags(not_ignored_instances)
101
+ instance_hash = instance_count_hash(instances_without_tag)
102
+ return ignored_instances, instances_with_tag, instance_hash
103
+ end
80
104
 
81
- ris_region.each do |ri|
82
- differences[ri.to_s] = {:count => ri.count_remaining, :region_based => true}
83
- ri.count_remaining = nil
84
- end
105
+ def sort_through_RIs
106
+ ris = get_reserved_instances
107
+ ris_availability = filter_ris_availability_zone(ris)
108
+ ris_region = filter_ris_region_based(ris)
109
+ ris_hash = instance_count_hash(ris_availability)
110
+ return ris_region, ris_hash
85
111
  end
86
112
 
87
113
  def measure_differences(instance_hash, ris_hash)
@@ -94,29 +120,22 @@ module SportNginAwsAuditor
94
120
  differences
95
121
  end
96
122
 
97
- def compare(instances)
98
- instances_with_tag = filter_instances_with_tags(instances)
99
- instances_without_tag = filter_instances_without_tags(instances)
100
- instance_hash = instance_count_hash(instances_without_tag)
123
+ def add_additional_data(ris_region, instances_with_tag, ignored_instances, differences)
124
+ add_region_ris_to_hash(ris_region, differences)
125
+ add_additional_instances_to_hash(instances_with_tag, differences, " with tag (")
126
+ add_additional_instances_to_hash(ignored_instances, differences, " ignored (")
127
+ return differences
128
+ end
101
129
 
102
- ris = get_reserved_instances
103
- ris_availability = filter_ris_availability_zone(ris)
104
- ris_region = filter_ris_region_based(ris)
105
- ris_hash = instance_count_hash(ris_availability)
106
-
130
+ def compare(instances, ignore_instances_regexes)
131
+ ignored_instances, instances_with_tag, instance_hash = sort_through_instances(instances, ignore_instances_regexes)
132
+ ris_region, ris_hash = sort_through_RIs
107
133
  differences = measure_differences(instance_hash, ris_hash)
108
- apply_region_ris(ris_region, differences)
109
- apply_tagged_instances(instances_with_tag, differences)
134
+ add_additional_data(ris_region, instances_with_tag, ignored_instances, differences)
110
135
  differences
111
136
  end
112
137
 
113
- # this gets all retired reserved instances and filters out only the ones that have expired
114
- # within the past week
115
- def get_recent_retired_reserved_instances
116
- get_retired_reserved_instances.select do |ri|
117
- ri.expiration_date > (Time.now - 604800)
118
- end
119
- end
138
+ #################### FILTERING ACTIVE DATA ####################
120
139
 
121
140
  # assuming the value of the tag is in the form: 01/01/2000 like a date
122
141
  def filter_instances_with_tags(instances)
@@ -144,6 +163,26 @@ module SportNginAwsAuditor
144
163
  ris.select { |ri| ri.scope == 'Region' }
145
164
  end
146
165
 
166
+ # this breaks up the instances array into instances with any of the strings in the ignore_instances_regexes and
167
+ # instances without
168
+ def filter_ignored_instances(instances, ignore_instances_regexes)
169
+ instances.partition { |instance|
170
+ ignore_instances_regexes.any? { |regex|
171
+ instance.name ? instance.name.match(regex) != nil : false
172
+ }
173
+ }
174
+ end
175
+
176
+ #################### GATHERING RETIRED DATA ####################
177
+
178
+ # this gets all retired reserved instances and filters out only the ones that have expired
179
+ # within the past week
180
+ def get_recent_retired_reserved_instances
181
+ get_retired_reserved_instances.select do |ri|
182
+ ri.expiration_date > (Time.now - 604800)
183
+ end
184
+ end
185
+
147
186
  # this returns a hash of all instances that have retired between 1 week ago and today
148
187
  def get_retired_tags(instances)
149
188
  return_array = []
@@ -25,11 +25,16 @@ module SportNginAwsAuditor
25
25
  tag_name = options[:tag]
26
26
  end
27
27
 
28
+ ignore_instances_patterns = options[:ignore_instances_patterns].split(', ') if options[:ignore_instances_patterns]
29
+ ignore_instances_regexes = []
30
+ ignore_instances_patterns.each do |r|
31
+ ignore_instances_regexes << Regexp.new(r)
32
+ end
28
33
  zone_output = options[:zone_output]
29
34
 
30
35
  cycle = [["EC2Instance", options[:ec2]],
31
- ["RDSInstance", options[:rds]],
32
- ["CacheInstance", options[:cache]]]
36
+ ["RDSInstance", options[:rds]],
37
+ ["CacheInstance", options[:cache]]]
33
38
 
34
39
  if !slack
35
40
  print "Gathering info, please wait..."; print "\r"
@@ -38,7 +43,7 @@ module SportNginAwsAuditor
38
43
  end
39
44
 
40
45
  cycle.each do |c|
41
- audit_results = AuditData.new(options[:instances], options[:reserved], c.first, tag_name)
46
+ audit_results = AuditData.new(options[:instances], options[:reserved], c.first, tag_name, ignore_instances_regexes)
42
47
  audit_results.gather_data
43
48
  output_options = {:slack => slack, :class_type => c.first,
44
49
  :environment => environment, :zone_output => zone_output}
@@ -108,6 +113,8 @@ module SportNginAwsAuditor
108
113
  else
109
114
  say "<%= color('#{prefix} #{name}: (expiring on #{instance.tag_value})', :#{color}) %>"
110
115
  end
116
+ elsif instance.ignored?
117
+ say "<%= color('#{prefix} #{name}', :#{color}) %>"
111
118
  else
112
119
  say "<%= color('#{prefix} #{name}: #{count}', :#{color}) %>"
113
120
  end
@@ -115,10 +122,10 @@ module SportNginAwsAuditor
115
122
 
116
123
  def self.print_to_slack(audit_results, output_options)
117
124
  discrepancy_array = []
118
- tagged_array = []
125
+ tagged_ignored_array = []
119
126
 
120
127
  audit_results.data.each do |instance|
121
- unless instance.matched? || instance.tagged?
128
+ unless instance.matched? || instance.tagged? || instance.ignored?
122
129
  discrepancy_array.push(instance)
123
130
  end
124
131
  end
@@ -128,13 +135,13 @@ module SportNginAwsAuditor
128
135
  end
129
136
 
130
137
  audit_results.data.each do |instance|
131
- if instance.tagged?
132
- tagged_array.push(instance)
138
+ if instance.tagged? || instance.ignored?
139
+ tagged_ignored_array.push(instance)
133
140
  end
134
141
  end
135
142
 
136
- unless tagged_array.empty?
137
- print_tagged(tagged_array, output_options)
143
+ unless tagged_ignored_array.empty?
144
+ print_tagged(tagged_ignored_array, output_options)
138
145
  end
139
146
 
140
147
  print_retired_ris(audit_results, output_options) unless audit_results.retired_ris.empty?
@@ -159,19 +166,23 @@ module SportNginAwsAuditor
159
166
  slack_instances.perform
160
167
  end
161
168
 
162
- def self.print_tagged(tagged_array, output_options)
169
+ def self.print_tagged(tagged_ignored_array, output_options)
163
170
  title = "There are currently some tagged #{output_options[:class_type]}s in #{output_options[:environment]}:\n"
164
171
  slack_instances = NotifySlack.new(title, options[:config_json])
165
172
 
166
- tagged_array.each do |tagged|
167
- type = output_options[:zone_output] ? tagged.type : print_without_zone(tagged.type)
168
- count = tagged.count
169
- color, rgb, prefix = color_chooser(tagged)
173
+ tagged_ignored_array.each do |tagged_or_ignored|
174
+ type = output_options[:zone_output] ? tagged_or_ignored.type : print_without_zone(tagged_or_ignored.type)
175
+ count = tagged_or_ignored.count
176
+ color, rgb, prefix = color_chooser(tagged_or_ignored)
170
177
 
171
- if tagged.reason
172
- text = "#{prefix} #{type}: (expiring on #{tagged.tag_value} because of #{tagged.reason})"
173
- else
174
- text = "#{prefix} #{type}: (expiring on #{tagged.tag_value})"
178
+ if tagged_or_ignored.tagged?
179
+ if tagged_or_ignored.reason
180
+ text = "#{prefix} #{tagged_or_ignored.name}: (expiring on #{tagged_or_ignored.tag_value} because of #{tagged_or_ignored.reason})"
181
+ else
182
+ text = "#{prefix} #{tagged_or_ignored.name}: (expiring on #{tagged_or_ignored.tag_value})"
183
+ end
184
+ elsif tagged_or_ignored.ignored?
185
+ text = "#{prefix} #{tagged_or_ignored.name}"
175
186
  end
176
187
 
177
188
  slack_instances.attachments.push({"color" => rgb, "text" => text, "mrkdwn_in" => ["text"]})
@@ -232,6 +243,8 @@ module SportNginAwsAuditor
232
243
  def self.color_chooser(instance)
233
244
  if instance.tagged?
234
245
  return "blue", "#0000CC", "TAGGED -"
246
+ elsif instance.ignored?
247
+ return "blue", "#0000CC", "IGNORED -"
235
248
  elsif instance.running?
236
249
  return "yellow", "#FFD700", "MISSING RI -"
237
250
  elsif instance.matched?
@@ -1,3 +1,3 @@
1
1
  module SportNginAwsAuditor
2
- VERSION = "3.10.1"
2
+ VERSION = "3.11.0"
3
3
  end
@@ -10,6 +10,7 @@ module SportNginAwsAuditor
10
10
  @instance4 = double('ec2_instance4')
11
11
  @ec2_instances = [@instance1, @instance2]
12
12
  @retired_ris = [@instance3, @instance4]
13
+ @ignore_instances_regexes = ["kitchen", "auto"]
13
14
  allow(SportNginAwsAuditor::EC2Instance).to receive(:get_instances).and_return(@ec2_instances)
14
15
  allow(SportNginAwsAuditor::EC2Instance).to receive(:get_reserved_instances).and_return(@ec2_instances)
15
16
  allow(SportNginAwsAuditor::EC2Instance).to receive(:get_retired_tags).and_return([])
@@ -17,8 +18,8 @@ module SportNginAwsAuditor
17
18
  allow(SportNginAwsAuditor::EC2Instance).to receive(:filter_instances_without_tags).and_return(@ec2_instances)
18
19
  allow(SportNginAwsAuditor::EC2Instance).to receive(:instance_count_hash).and_return({'instance1' => 1,
19
20
  'instance2' => 1})
20
- allow(SportNginAwsAuditor::EC2Instance).to receive(:apply_tagged_instances).and_return({'instance1' => 1,
21
- 'instance2' => 1})
21
+ allow(SportNginAwsAuditor::EC2Instance).to receive(:add_additional_instances_to_hash).and_return({'instance1' => 1,
22
+ 'instance2' => 1})
22
23
  allow(SportNginAwsAuditor::EC2Instance).to receive(:compare).and_return({'instance1' => 1,
23
24
  'instance2' => 1})
24
25
  allow(SportNginAwsAuditor::EC2Instance).to receive(:get_recent_retired_reserved_instances).and_return(@retired_ris)
@@ -27,75 +28,75 @@ module SportNginAwsAuditor
27
28
 
28
29
  context '#initialization' do
29
30
  it 'should gather instance data' do
30
- audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance")
31
+ audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
31
32
  expect(audit_results.selected_audit_type).to eq("instances")
32
33
  end
33
34
 
34
35
  it 'should gather reserved instance data' do
35
- audit_results = AuditData.new(false, true, "EC2Instance", "no-reserved-instance")
36
+ audit_results = AuditData.new(false, true, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
36
37
  expect(audit_results.selected_audit_type).to eq("reserved")
37
38
  end
38
39
 
39
40
  it 'should by default gather instance data' do
40
- audit_results = AuditData.new(true, true, "EC2Instance", "no-reserved-instance")
41
+ audit_results = AuditData.new(true, true, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
41
42
  expect(audit_results.selected_audit_type).to eq("instances")
42
43
  end
43
44
 
44
45
  it 'should gather all data to compare' do
45
- audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance")
46
+ audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
46
47
  expect(audit_results.selected_audit_type).to eq("all")
47
48
  end
48
49
 
49
50
  it 'should use EC2Instance class' do
50
- audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance")
51
+ audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
51
52
  expect(audit_results.klass).to eq(SportNginAwsAuditor::EC2Instance)
52
53
  end
53
54
 
54
55
  it 'should use EC2Instance class' do
55
- audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance")
56
+ audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
56
57
  expect(audit_results.tag_name).to eq("no-reserved-instance")
57
58
  end
58
59
  end
59
60
 
60
61
  context '#instances?' do
61
62
  it 'should return true' do
62
- audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance")
63
+ audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
63
64
  expect(audit_results.instances?).to eq(true)
64
65
  end
65
66
 
66
67
  it 'should return true' do
67
- audit_results = AuditData.new(false, true, "EC2Instance", "no-reserved-instance")
68
+ audit_results = AuditData.new(false, true, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
68
69
  expect(audit_results.instances?).to eq(false)
69
70
  end
70
71
  end
71
72
 
72
73
  context '#reserved?' do
73
74
  it 'should return true' do
74
- audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance")
75
+ audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
75
76
  expect(audit_results.reserved?).to eq(false)
76
77
  end
77
78
 
78
79
  it 'should return true' do
79
- audit_results = AuditData.new(false, true, "EC2Instance", "no-reserved-instance")
80
+ audit_results = AuditData.new(false, true, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
80
81
  expect(audit_results.reserved?).to eq(true)
81
82
  end
82
83
  end
83
84
 
84
85
  context '#all?' do
85
86
  it 'should return true' do
86
- audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance")
87
+ audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
87
88
  expect(audit_results.all?).to eq(false)
88
89
  end
89
90
 
90
91
  it 'should return true' do
91
- audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance")
92
+ audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
92
93
  expect(audit_results.all?).to eq(true)
93
94
  end
94
95
  end
95
96
 
96
97
  context '#gather_data' do
97
98
  it 'should gather some empty results by comparison' do
98
- audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance")
99
+ audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
99
100
  audit_results.gather_data
100
101
  expect(audit_results.data).to eq([@instance, @instance])
101
102
  expect(audit_results.retired_tags).to eq([])
@@ -103,7 +104,7 @@ module SportNginAwsAuditor
103
104
  end
104
105
 
105
106
  it 'should gather some empty results from just instances' do
106
- audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance")
107
+ audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
107
108
  audit_results.gather_data
108
109
  expect(audit_results.data).to eq([@instance, @instance])
109
110
  expect(audit_results.retired_tags).to eq([])
@@ -111,7 +112,7 @@ module SportNginAwsAuditor
111
112
  end
112
113
 
113
114
  it 'should gather some empty results from just reserved' do
114
- audit_results = AuditData.new(false, true, "EC2Instance", "no-reserved-instance")
115
+ audit_results = AuditData.new(false, true, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
115
116
  audit_results.gather_data
116
117
  expect(audit_results.data).to eq([@instance, @instance])
117
118
  expect(audit_results.retired_tags).to eq(nil)
@@ -121,7 +122,7 @@ module SportNginAwsAuditor
121
122
 
122
123
  context '#gather_instances_data' do
123
124
  it 'should gather some instances data but not convert' do
124
- audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance")
125
+ audit_results = AuditData.new(true, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
125
126
  result1, result2 = audit_results.gather_instances_data
126
127
  expect(result1).to eq({'instance1' => 1, 'instance2' => 1})
127
128
  expect(result2).to eq([])
@@ -130,7 +131,7 @@ module SportNginAwsAuditor
130
131
 
131
132
  context '#gather_all_data' do
132
133
  it 'should gather some comparison data but not convert' do
133
- audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance")
134
+ audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
134
135
  result1, result2, result3 = audit_results.gather_all_data
135
136
  expect(result1).to eq({'instance1' => 1, 'instance2' => 1})
136
137
  expect(result2).to eq([])
@@ -140,7 +141,7 @@ module SportNginAwsAuditor
140
141
 
141
142
  context '#gather_region' do
142
143
  it 'should gather the region from an instance' do
143
- audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance")
144
+ audit_results = AuditData.new(false, false, "EC2Instance", "no-reserved-instance", @ignore_instances_regexes)
144
145
  audit_results.gather_region(@ec2_instances)
145
146
  expect(audit_results.region).to eq('us-east')
146
147
  end
@@ -104,16 +104,23 @@ module SportNginAwsAuditor
104
104
  end
105
105
  end
106
106
 
107
- context '#apply_tagged_instances' do
107
+ context '#add_additional_instances_to_hash' do
108
108
  it 'should add the instances to the hash of differences' do
109
109
  klass = SportNginAwsAuditor::EC2Instance
110
- result = klass.apply_tagged_instances(@ec2_instances, {})
110
+ result = klass.add_additional_instances_to_hash(@ec2_instances, {}, " with tag (")
111
111
  expect(result).to eq({'Linux VPC us-east-1b t2.small with tag (Example-instance-01)' => {count: 1, name: @ec2_instance1.key_name, tag_reason: nil, tag_value: nil, region_based: false},
112
112
  'Windows us-east-1b t2.medium with tag (Example-instance-02)' => {count: 1, name: @ec2_instance2.key_name, tag_reason: nil, tag_value: nil, region_based: false}})
113
113
  end
114
+
115
+ it 'should add the ignored instances to the hash of differences' do
116
+ klass = SportNginAwsAuditor::EC2Instance
117
+ result = klass.add_additional_instances_to_hash(@ec2_instances, {}, " ignored (")
118
+ expect(result).to eq({'Linux VPC us-east-1b t2.small ignored (Example-instance-01)' => {count: 1, name: @ec2_instance1.key_name, region_based: false},
119
+ 'Windows us-east-1b t2.medium ignored (Example-instance-02)' => {count: 1, name: @ec2_instance2.key_name, region_based: false}})
120
+ end
114
121
  end
115
122
 
116
- context '#apply_region_ris' do
123
+ context '#add_region_ris_to_hash' do
117
124
  it 'should factor in the region based RIs into the counting when there is a mixture of region based and non region based' do
118
125
  klass = SportNginAwsAuditor::EC2Instance
119
126
  allow(@ec2_instance1).to receive(:count).and_return(5)
@@ -128,9 +135,9 @@ module SportNginAwsAuditor
128
135
  ris_count = ris.has_key?(key) ? ris[key][:count] : 0
129
136
  differences[key] = {count: ris_count - instance_count, region_based: false}
130
137
  end
131
- result = klass.apply_region_ris(@region_reserved_instances, differences)
138
+ result = klass.add_region_ris_to_hash(@region_reserved_instances, differences)
132
139
  expect(differences).to eq({"Linux VPC us-east-1b t2.small"=>{count: 0, region_based: false}, "Windows us-east-1b t2.medium"=>{count: 0, region_based: false},
133
- "Linux VPC t2.small" => {count: 2, region_based: true}, "Windows t2.medium" => {count: 2, region_based: true}})
140
+ "Linux VPC t2.small" => {count: 2, region_based: true}, "Windows t2.medium" => {count: 4, region_based: true}})
134
141
  end
135
142
 
136
143
  it 'should factor in the region based RIs into the counting when there are no zone specific RIs' do
@@ -140,9 +147,9 @@ module SportNginAwsAuditor
140
147
  allow(@region_reserved_ec2_instance1).to receive(:count=)
141
148
  allow(@region_reserved_ec2_instance2).to receive(:count=)
142
149
  instance_hash = klass.instance_count_hash(@ec2_instances)
143
- result = klass.apply_region_ris(@region_reserved_instances, instance_hash)
150
+ result = klass.add_region_ris_to_hash(@region_reserved_instances, instance_hash)
144
151
  expect(instance_hash).to eq({"Linux VPC us-east-1b t2.small"=>{count: 0, region_based: false}, "Windows us-east-1b t2.medium"=>{count: 5, region_based: false},
145
- "Linux VPC t2.small" => {count: 2, region_based: true}, "Windows t2.medium" => {count: 2, region_based: true}})
152
+ "Linux VPC t2.small" => {count: 2, region_based: true}, "Windows t2.medium" => {count: 4, region_based: true}})
146
153
  end
147
154
  end
148
155
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sport_ngin_aws_auditor
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.10.1
4
+ version: 3.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elliot Hursh
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-12-02 00:00:00.000000000 Z
13
+ date: 2016-12-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: aws-sdk