Module: Miasma::Models::Orchestration::Terraform::Local

Defined in:
lib/miasma/contrib/terraform/orchestration.rb

Instance Method Summary collapse

Instance Method Details

#event_all(stack, marker = nil) ⇒ Array<Models::Orchestration::Stack::Event>

Return all events for stack

Parameters:

  • stack (Models::Orchestration::Stack)

Returns:

  • (Array<Models::Orchestration::Stack::Event>)


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 190

def event_all(stack, marker = nil)
  params = marker ? {:marker => marker} : {}
  terraform_stack(stack) do |tf_stack|
    tf_stack.events.map do |event|
      Stack::Event.new(
        stack,
        :id => event[:id],
        :resource_id => event[:physical_resource_id],
        :resource_name => event[:resource_name],
        :resource_logical_id => event[:resource_name],
        :resource_state => event[:resource_status].downcase.to_sym,
        :resource_status => event[:resource_status],
        :resource_status_reason => event[:resource_status_reason],
        :time => Time.at(event[:timestamp] / 1000.0)
      ).valid_state
    end
  end
end

#event_all_new(events) ⇒ Array<Models::Orchestration::Stack::Event>

Return all new events for event collection

Parameters:

  • events (Models::Orchestration::Stack::Events)

Returns:

  • (Array<Models::Orchestration::Stack::Event>)


213
214
215
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 213

def event_all_new(events)
  event_all(events.stack, events.all.first.id)
end

#event_reload(event) ⇒ Models::Orchestration::Event

Reload the stack event data from the API

Parameters:

  • resource (Models::Orchestration::Stack::Event)

Returns:

  • (Models::Orchestration::Event)


221
222
223
224
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 221

def event_reload(event)
  event.stack.events.reload
  event.stack.events.get(event.id)
end

#resource_all(stack) ⇒ Array<Models::Orchestration::Stack::Resource>

Return all resources for stack

Parameters:

  • stack (Models::Orchestration::Stack)

Returns:

  • (Array<Models::Orchestration::Stack::Resource>)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 159

def resource_all(stack)
  terraform_stack(stack) do |tf_stack|
    tf_stack.resources.map do |resource|
      Stack::Resource.new(
        stack,
        :id => resource[:physical_id],
        :name => resource[:name],
        :type => resource[:type],
        :logical_id => resource[:name],
        :state => resource[:status].downcase.to_sym,
        :status => resource[:status],
        :status_reason => resource[:resource_status_reason],
        :updated => resource[:updated_time].to_s.empty? ? Time.now : Time.parse(resource[:updated_time])
      ).valid_state
    end
  end
end

#resource_reload(resource) ⇒ Models::Orchestration::Resource

Reload the stack resource data from the API

Parameters:

  • resource (Models::Orchestration::Stack::Resource)

Returns:

  • (Models::Orchestration::Resource)


181
182
183
184
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 181

def resource_reload(resource)
  resource.stack.resources.reload
  resource.stack.resources.get(resource.id)
end

#stack_all(options = {}) ⇒ Array<Models::Orchestration::Stack>

TODO:

check if we need any mappings on state set

Return all stacks

Parameters:

  • options (Hash) (defaults to: {})

    filter

Returns:

  • (Array<Models::Orchestration::Stack>)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 138

def stack_all(options={})
  MiasmaTerraform::Stack.list(terraform_local_directory).map do |stack_name|
    s = terraform_stack(Stack.new(self, :name => stack_name)).info
    Stack.new(
      self,
      :id => s[:id],
      :created => s[:creation_time].to_s.empty? ? nil : Time.at(s[:creation_time].to_i / 1000.0),
      :description => s[:description],
      :name => s[:name],
      :state => s[:status].downcase.to_sym,
      :status => s[:status],
      :status_reason => s[:stack_status_reason],
      :updated => s[:updated_time].to_s.empty? ? nil : Time.at(s[:updated_time].to_i / 1000.0)
    ).valid_state
  end
end

#stack_destroy(stack) ⇒ TrueClass, FalseClass

Delete the stack

Parameters:

  • stack (Models::Orchestration::Stack)

Returns:

  • (TrueClass, FalseClass)


99
100
101
102
103
104
105
106
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 99

def stack_destroy(stack)
  if(stack.persisted?)
    terraform_stack(stack).destroy!
    true
  else
    false
  end
end

#stack_reload(stack) ⇒ Models::Orchestration::Stack

Reload the stack data from the API

Parameters:

  • stack (Models::Orchestration::Stack)

Returns:

  • (Models::Orchestration::Stack)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 75

def stack_reload(stack)
  if(stack.persisted?)
    terraform_stack(stack) do |tf_stack|
      s = tf_stack.info
      stack.load_data(
        :id => s[:id],
        :created => s[:creation_time].to_s.empty? ? nil : Time.at(s[:creation_time].to_i / 1000.0),
        :description => s[:description],
        :name => s[:name],
        :state => s[:status].downcase.to_sym,
        :status => s[:status],
        :status_reason => s[:stack_status_reason],
        :updated => s[:updated_time].to_s.empty? ? nil : Time.at(s[:updated_time].to_i / 1000.0),
        :outputs => s[:outputs].map{|k,v| {:key => k, :value => v}}
      ).valid_state
    end
  end
  stack
end

#stack_save(stack) ⇒ Models::Orchestration::Stack

Save the stack

Parameters:

  • stack (Models::Orchestration::Stack)

Returns:

  • (Models::Orchestration::Stack)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 42

def stack_save(stack)
  tf_stack = nil
  begin
    tf_stack = terraform_stack(stack) do |tf|
      tf.info
      tf
    end
  rescue Miasma::Error::ApiError::RequestError
    if(stack.persisted?)
      raise
    else
      tf_stack = nil
    end
  end
  if(!stack.persisted? && tf_stack)
    raise Miasma::Error::ApiError::RequestError.new(
      "Stack already exists `#{stack.name}`",
      :response => OpenStruct.new(:code => 405)
    )
  end
  tf_stack = terraform_stack(stack) unless tf_stack
  tf_stack.save(
    :template => stack.template,
    :parameters => stack.parameters || {}
  )
  stack.id = stack.name
  stack.valid_state
end

#stack_template_load(stack) ⇒ Smash

Fetch stack template

Parameters:

  • stack (Stack)

Returns:

  • (Smash)

    stack template



112
113
114
115
116
117
118
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 112

def stack_template_load(stack)
  if(stack.persisted?)
    JSON.load(terraform_stack(stack).template).to_smash
  else
    Smash.new
  end
end

#stack_template_validate(stack) ⇒ NilClass, String

Validate stack template

Parameters:

  • stack (Stack)

Returns:

  • (NilClass, String)

    nil if valid, string error message if invalid



124
125
126
127
128
129
130
131
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 124

def stack_template_validate(stack)
  begin
    terraform_stack(stack).validate(stack.template)
    nil
  rescue MiasmaTerraform::Error::Validation => e
    MultiJson.load(e.response.body.to_s).to_smash.get(:error, :message)
  end
end

#terraform_stack(stack) ⇒ Object

Generate wrapper stack



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/miasma/contrib/terraform/orchestration.rb', line 13

def terraform_stack(stack)
  if(terraform_local_directory.to_s.empty?)
    raise ArgumentError.new 'Attribute `terraform_local_directory` must be set for local mode usage'
  end
  tf_stack = memoize(stack.name, :direct) do
    MiasmaTerraform::Stack.new(
      :name => stack.name,
      :container => terraform_local_directory,
      :scrub_destroyed => terraform_local_scrub_destroyed
    )
  end
  if(block_given?)
    begin
      yield(tf_stack)
    rescue MiasmaTerraform::Stack::Error::NotFound
      raise Miasma::Error::ApiError::RequestError.new(
        "Failed to locate stack `#{stack.name}`",
        :response => OpenStruct.new(:code => 404)
      )
    end
  else
    tf_stack
  end
end