Module: Tfrb::Resource

Defined Under Namespace

Modules: AwsDbInstance, AwsDbSubnetGroup, AwsDynamodbTable, AwsEbsVolume, AwsElasticacheReplicationGroup, AwsElasticacheSubnetGroup, AwsIamPolicy, AwsIamRole, AwsIamRolePolicyAttachment, AwsInstance, AwsKmsKey, AwsS3Bucket, AwsSecurityGroup, AwsStoragegatewayCache, AwsStoragegatewayGateway, AwsStoragegatewayNfsFileShare, AwsSubnet, AwsVolumeAttachment, AwsVpc

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object



5
6
7
8
9
10
# File 'lib/tfrb/resource.rb', line 5

def extended(mod)
  Tfrb::Config[:extra_modules].each do |extra_module|
    mod.send(:include, extra_module)
    mod.send(:extend, extra_module)
  end
end

.get_custom_resource(resource_type) ⇒ Object



78
79
80
81
82
83
# File 'lib/tfrb/resource.rb', line 78

def get_custom_resource(resource_type)
  custom_resource_name = self.constants.find { |c| resource_type == c.to_s.gsub(/(.)([A-Z])/,'\1_\2').downcase }
  if custom_resource_name
    Kernel.const_get("Tfrb::Resource::#{custom_resource_name}")
  end
end

.get_state(base, resource, name) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/tfrb/resource.rb', line 69

def get_state(base, resource, name)
  tf_state_args = ['terraform', 'state', 'show', "#{resource}.#{name}"]
  tf_state_args << "-state=#{base.local_state_path}" if base.dry_run
  tf_state = Mixlib::ShellOut.new(*tf_state_args, cwd: base.temp_path)
  tf_state.run_command
  tf_state.error!
  tf_state.stdout.split("\n").each_with_object({}) { |line, hash| key, value = line.split(' = '); hash[key.strip] = value }
end

.load(tfrb) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tfrb/resource.rb', line 52

def load(tfrb)
  tfrb.environments.each do |environment_name, environment|
    if environment['resource']
      environment['resource'].each do |resource_type, resources|
        if custom_resource = get_custom_resource(resource_type)
          new_resources = resources.select { |resource_name, _| !tfrb.state.has_key?(resource_type) || !tfrb.state[resource_type].has_key?(resource_name) }
          if custom_resource.singleton_methods.include?(:load) && new_resources.size > 0
            custom_resource.load(tfrb, environment_name, resource_type, new_resources)
          end
        else
          printf "\033[31mWarning: no custom resource definition found for %s\n         consider creating one or you may not receive the desired result!\033[0m\n", resource_type, resource_type
        end
      end
    end
  end
end

.load_helpers!Object



12
13
14
15
16
17
18
# File 'lib/tfrb/resource.rb', line 12

def load_helpers!
  # Include helper methods from resources for use in ERB/YAML
  Tfrb::Resource::constants.each do |constant|
    mod = Kernel.const_get("Tfrb::Resource::#{constant}")
    Tfrb::Base.send(:include, mod) if mod.is_a?(Module)
  end
end

.preload(tfrb) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tfrb/resource.rb', line 20

def preload(tfrb)
  tfrb.environments.each do |environment_name, environment|
    if environment['resource']
      environment['resource'].each do |resource_type, resources|
        # Call resource preload methods
        if custom_resource = get_custom_resource(resource_type)
          if custom_resource.singleton_methods.include?(:preload)
            custom_resource.preload(tfrb, environment_name, resource_type, resources)
          end
        end

        # Inject overrides from Config
        if Tfrb::Config[:overrides].has_key?('resource') && Tfrb::Config[:overrides]['resource'].has_key?(resource_type)
          custom_resource.instance_exec(tfrb, environment_name, resource_type, resources, &Tfrb::Config[:overrides]['resource'][resource_type])
        end

        # Preload resources from local state wherever possible
        unless tfrb.s3_state?
          tfrb.state[resource_type] = {} unless tfrb.state.has_key?(resource_type)
          resources.each do |resource_name, resource|
            if ::File.exist?(tfrb.temp_path('terraform.tfstate')) && !tfrb.state[resource_type].has_key?(resource_name)
              printf "\033[1m%s.%s: Loading state...\033[0m\n", resource_type, resource_name
              state = get_state(tfrb, resource_type, resource_name)
              tfrb.state[resource_type][resource_name] = state if state && state.keys.size > 0
            end
          end
        end
      end
    end
  end
end

Instance Method Details

#aws_options(base, resource) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/tfrb/resource.rb', line 102

def aws_options(base, resource)
  if resource.has_key?('provider')
    aws_providers = base.environments.find { |_, e|
      e['provider'] &&
      e['provider']['aws'] &&
      e['provider']['aws']['alias'] &&
      e['provider']['aws']['alias'] == resource['provider'].sub(/^aws\./, '')
    }
  end
  aws_providers = base.environments.find { |_, e| e['provider'] && e['provider']['aws'] } unless aws_providers
  if aws_provider = aws_providers[1]['provider']['aws']
    {
      region: aws_provider['region'],
      access_key_id: aws_provider['access_key'],
      secret_access_key: aws_provider['secret_key'],
      session_token: aws_provider['token']
    }
  end
end

#import!(base, resource, name, id) ⇒ Object



122
123
124
# File 'lib/tfrb/resource.rb', line 122

def import!(base, resource, name, id)
  base.import!(resource, name, id)
end

#resolve_tfvar(base, resource_type, resource_name, var) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tfrb/resource.rb', line 86

def resolve_tfvar(base, resource_type, resource_name, var)
  base.environments.each do |environment_name, environment|
    if environment.has_key?('resource') &&
       environment['resource'].has_key?(resource_type) &&
       environment['resource'][resource_type].has_key?(resource_name) &&
       environment['resource'][resource_type][resource_name].has_key?(var)
      return environment['resource'][resource_type][resource_name][var].gsub(/\$\{([^}]+)\}/) { |match| match.sub(/\$\{([^}]+)\}/, '\1').split('.').inject(base.state) { |state, key| state[key] if state } }
    end
  end
  nil
end

#set_default(entity, key, value) ⇒ Object



98
99
100
# File 'lib/tfrb/resource.rb', line 98

def set_default(entity, key, value)
  entity[key] = value unless entity.has_key?(key)
end