Class: Terraframe::State

Inherits:
Object
  • Object
show all
Defined in:
lib/terraframe/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, vars, contexts) ⇒ State

Returns a new instance of State.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/terraframe/state.rb', line 11

def initialize(logger, vars, contexts)
  @logger = logger
  logger.info "Initializing state."

  @vars = Hashie::Mash.new(vars)
  logger.debug "State variables:"
  logger.ap vars, :debug

  @__contexts = contexts

  @__output = {
    :provider => {},
    :variable => {},
    :resource => {}
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

anything that is not a provider or a variable should be interpreted



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/terraframe/state.rb', line 85

def method_missing(method_name, *args, &block)
  case method_name
    when "vars"
      @vars
    else
      if (args.length != 1)
        msg = "Too many arguments for resource invocation '#{method_name}'."
        logger.fatal(msg)
        raise msg
      end
      resource(method_name.to_sym, args[0], &block)
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/terraframe/state.rb', line 9

def logger
  @logger
end

#varsObject (readonly)

TODO: support outputs!



8
9
10
# File 'lib/terraframe/state.rb', line 8

def vars
  @vars
end

Instance Method Details

#__apply_script(script_name, script) ⇒ Object



35
36
37
38
39
# File 'lib/terraframe/state.rb', line 35

def __apply_script(script_name, script)
  logger.info "Applying script '#{script_name}' to state."
  instance_eval(script, script_name, 0)
  logger.info "Script '#{script_name}' applied successfully."
end

#__buildObject



28
29
30
31
32
33
# File 'lib/terraframe/state.rb', line 28

def __build()
  logger.info "Building Terraform script from state."
  logger.debug "Contexts:"
  @__contexts.each { |c| logger.debug " - #{c}" }
  @__output.to_json
end

#provider(type, &block) ⇒ Object

DSL FUNCTIONS BELOW ##



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/terraframe/state.rb', line 42

def provider(type, &block)
  if !@__contexts[type]
    msg = "Unknown provider type: '#{type}'."
    logger.fatal msg
    raise msg
  end

  if @__output[:provider][type]
    msg = "Duplicate provider type (sorry, blame Terraform): '#{type}'"
    logger.fatal msg
    raise msg
  end

  handling_context = @__contexts[type]
  provider = handling_context.provider_type.new(vars, handling_context, &block)
  logger.debug "Provider of type '#{type}': #{provider.inspect}"
  @__output[:provider][type] = provider

  provider
end

#resource(resource_type, resource_name, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/terraframe/state.rb', line 69

def resource(resource_type, resource_name, &block)
  handling_context_pair = @__contexts.find { |k, v| v.resources.include?(resource_type) }
  if handling_context_pair == nil
    msg = "Could not find a context that supports resource type '#{resource_type}'."
    logger.error msg
    raise msg
  end

  handling_context = handling_context_pair[1]
  resource_class = handling_context.resources[resource_type]

  @__output[:resource][resource_type] ||= {}
  @__output[:resource][resource_type][resource_name.to_s] = resource_class.new(resource_name, vars, handling_context, &block)
end

#variableObject



63
64
65
66
67
# File 'lib/terraframe/state.rb', line 63

def variable
  msg = "TODO: implement tfvar support."
  logger.fatal msg
  raise msg
end