module Miasma::Models::Orchestration::Terraform::Local

Public Instance Methods

event_all(stack, marker = nil) click to toggle source

Return all events for stack

@param stack [Models::Orchestration::Stack] @return [Array<Models::Orchestration::Stack::Event>]

# 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) click to toggle source

Return all new events for event collection

@param events [Models::Orchestration::Stack::Events] @return [Array<Models::Orchestration::Stack::Event>]

# 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) click to toggle source

Reload the stack event data from the API

@param resource [Models::Orchestration::Stack::Event] @return [Models::Orchestration::Event]

# 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) click to toggle source

Return all resources for stack

@param stack [Models::Orchestration::Stack] @return [Array<Models::Orchestration::Stack::Resource>]

# 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) click to toggle source

Reload the stack resource data from the API

@param resource [Models::Orchestration::Stack::Resource] @return [Models::Orchestration::Resource]

# 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={}) click to toggle source

Return all stacks

@param options [Hash] filter @return [Array<Models::Orchestration::Stack>] @todo check if we need any mappings on state set

# 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) click to toggle source

Delete the stack

@param stack [Models::Orchestration::Stack] @return [TrueClass, FalseClass]

# 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) click to toggle source

Reload the stack data from the API

@param stack [Models::Orchestration::Stack] @return [Models::Orchestration::Stack]

# 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) click to toggle source

Save the stack

@param stack [Models::Orchestration::Stack] @return [Models::Orchestration::Stack]

# 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) click to toggle source

Fetch stack template

@param stack [Stack] @return [Smash] stack template

# 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) click to toggle source

Validate stack template

@param stack [Stack] @return [NilClass, String] nil if valid, string error message if invalid

# 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) { |tf_stack| ... } click to toggle source

Generate wrapper stack

# 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