Class: Terraspace::All::Summary

Inherits:
Object
  • Object
show all
Includes:
CLI::Logs::Concern, Util::Logging
Defined in:
lib/terraspace/all/summary.rb

Constant Summary collapse

@@header_shown =
false

Instance Method Summary collapse

Methods included from Util::Logging

#logger

Methods included from CLI::Logs::Concern

#pid, #readlines

Constructor Details

#initialize(data = {}) ⇒ Summary

Returns a new instance of Summary.



6
7
8
9
10
11
# File 'lib/terraspace/all/summary.rb', line 6

def initialize(data={})
  @data = data
  @command = data[:command]
  @log_path = data[:log_path]
  @terraspace_command = data[:terraspace_command]
end

Instance Method Details

#count_outputs(lines) ⇒ Object

[2020-09-07T14:53:36 #31106 terraspace show b1]: Outputs: [2020-09-07T14:53:36 #31106 terraspace show b1]: [2020-09-07T14:53:36 #31106 terraspace show b1]: length = 1 [2020-09-07T14:53:36 #31106 terraspace show b1]: length2 = 2 [2020-09-07T14:53:36 #31106 terraspace show b1]: random_pet_id = “corgi”



102
103
104
105
106
107
108
109
110
# File 'lib/terraspace/all/summary.rb', line 102

def count_outputs(lines)
  count = 0
  counting = false
  lines.each do |line|
    counting ||= line.include?(": Outputs:")
    count += 1 if counting && line.include?(' = ')
  end
  count
end

#defaultObject

Examples of “complete” line:

[2020-09-06T21:58:25 #11313 terraspace up b1]: Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
[2020-09-07T08:28:15 #26093 terraspace down a1]: Destroy complete! Resources: 2 destroyed.

handles: up and down



29
30
31
32
33
34
35
36
37
# File 'lib/terraspace/all/summary.rb', line 29

def default
  @lines.select! do |line|
    line.include?("complete! Resources:") || # success: handles both apply and destroy output
    line.include?("Changes to Outputs") ||
    line.include?("No changes") ||
    line.include?("Terraspace Cloud ") ||
    line.include?("Error: ")  # error
  end
end

#initObject



39
40
41
42
43
44
# File 'lib/terraspace/all/summary.rb', line 39

def init
  @lines.select! do |line|
    line.include?("successfully initialized") || # success
    line.include?("Error: ")  # error
  end
end

#outputObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/terraspace/all/summary.rb', line 62

def output
  if @lines.grep(/No outputs found/).empty?
    @lines.select! do |line|
      line.include?(" = ") # looks like output
    end
  else
    @lines.select! do |line|
      line.include?("No outputs found")
    end
  end
end

#planObject

Example 1:

[2020-09-07T14:45:14 #23340 terraspace plan b1]: No changes. Infrastructure is up-to-date.

Example 2:

[2020-09-07T14:46:58 #31974 terraspace plan b1]: Changes to Outputs:
[2020-09-07T14:46:58 #31974 terraspace plan b1]:   ~ length = 1 -> 2


51
52
53
54
55
56
57
58
59
60
# File 'lib/terraspace/all/summary.rb', line 51

def plan
  @lines.select! do |line|
    line.include?("No changes. Infrastructure") ||
    line.include?("Changes to Outputs") ||
    line.include?("Plan:") ||
    line.include?("Changes to ") ||
    line.include?("Terraspace Cloud ") ||
    line.include?("Error: ")  # error
  end
end

#providersObject

[2020-09-19T19:36:33 #14387 terraspace providers c1]: => terraform providers [2020-09-19T19:36:33 #14387 terraspace providers c1]: [2020-09-19T19:36:33 #14387 terraspace providers c1]: Providers required by configuration: [2020-09-19T19:36:33 #14387 terraspace providers c1]: .

2020-09-19T19:36:33 #14387 terraspace providers c1]: └── provider[registry.terraform.io/hashicorp/random

[2020-09-19T19:36:33 #14387 terraspace providers c1]:



91
92
93
94
95
# File 'lib/terraspace/all/summary.rb', line 91

def providers
  @lines.select! do |line|
    line.include?("provider[")
  end
end

#runObject



14
15
16
17
18
19
20
21
22
# File 'lib/terraspace/all/summary.rb', line 14

def run
  @lines = readlines(@log_path)
  if respond_to?(@command.to_sym)
    send(@command)
  else
    default
  end
  summarize
end

#showObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/terraspace/all/summary.rb', line 74

def show
  resources = @lines.grep(/: resource "/).count
  outputs = count_outputs(@lines)
  summary = "Resources: #{resources} Outputs: #{outputs}"
  # get summary line before running select! which will remove lines
  @lines.select! do |line|
    line.include?("Error: ")  # error
  end
  @lines.unshift(summary) # add to top
end

#validateObject

[2020-09-07T13:51:45 #21323 terraspace validate a1]: Success! The configuration is valid.



113
114
115
116
117
118
# File 'lib/terraspace/all/summary.rb', line 113

def validate
  @lines.select! do |line|
    line.include?("The configuration is") || # success
    line.include?("Error: ")  # error
  end
end