Class: Opsicle::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/opsicle/commands/update.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, type) ⇒ Update

Returns a new instance of Update.



8
9
10
11
# File 'lib/opsicle/commands/update.rb', line 8

def initialize(environment, type)
  @client = Client.new(environment)
  @type = type
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/opsicle/commands/update.rb', line 6

def client
  @client
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/opsicle/commands/update.rb', line 6

def type
  @type
end

Instance Method Details

#describeObject



20
21
22
23
24
25
26
# File 'lib/opsicle/commands/update.rb', line 20

def describe
  api_method = "describe_#{@type}s"
  api_opts = {
      :"#{@type}_ids" => [client.config.opsworks_config[:"#{@type}_id"]]
  }
  client.api_call(api_method, api_opts)[:"#{@type}s"][0]
end

#execute(values, options) ⇒ Object



13
14
15
16
17
18
# File 'lib/opsicle/commands/update.rb', line 13

def execute(values, options)
  before = describe
  update(values)
  after = describe
  print(before, after)
end

#format_diff(diff) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/opsicle/commands/update.rb', line 40

def format_diff(diff)
  diff.map { |change|
    case change[0]
      when '-'
        change.insert(3, nil)
        change.map! { |i| Output.format(i, :removal) }
      when '+'
        change.insert(2, nil)
        change.map! { |i| Output.format(i, :addition) }
      when '~'
        change.map! { |i| Output.format(i, :modification) }
    end
    change
  }
end


34
35
36
37
38
# File 'lib/opsicle/commands/update.rb', line 34

def print(before, after)
  diff = HashDiff.diff(before, after)
  Output.say("Changes: #{diff.size}")
  Output.terminal.say(Terminal::Table.new headings: %w[Change Key Before After], rows: format_diff(diff)) if diff.size > 0
end

#update(values) ⇒ Object



28
29
30
31
32
# File 'lib/opsicle/commands/update.rb', line 28

def update(values)
  api_method = "update_#{@type}"
  api_opts = values.merge(:"#{@type}_id" => client.config.opsworks_config[:"#{@type}_id"])
  client.api_call(api_method, api_opts)
end