terrafying 1.7.4 → 1.7.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/terrafying +1 -0
- data/lib/terrafying.rb +28 -36
- data/lib/terrafying/aws.rb +204 -169
- data/lib/terrafying/cli.rb +22 -20
- data/lib/terrafying/dynamodb.rb +7 -5
- data/lib/terrafying/dynamodb/config.rb +7 -5
- data/lib/terrafying/dynamodb/named_lock.rb +55 -58
- data/lib/terrafying/dynamodb/state.rb +32 -32
- data/lib/terrafying/generator.rb +81 -80
- data/lib/terrafying/lock.rb +7 -5
- data/lib/terrafying/state.rb +7 -5
- data/lib/terrafying/util.rb +12 -15
- data/lib/terrafying/version.rb +4 -2
- metadata +44 -16
data/lib/terrafying/cli.rb
CHANGED
@@ -1,70 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'thor'
|
2
4
|
|
3
5
|
module Terrafying
|
4
6
|
class Cli < Thor
|
5
|
-
class_option :lock_timeout, :
|
6
|
-
class_option :no_lock, :
|
7
|
-
class_option :keep, :
|
8
|
-
class_option :target, :
|
9
|
-
class_option :scope, :
|
10
|
-
class_option :dynamodb, :
|
7
|
+
class_option :lock_timeout, type: :string, default: nil
|
8
|
+
class_option :no_lock, type: :boolean, default: false
|
9
|
+
class_option :keep, type: :boolean, default: false
|
10
|
+
class_option :target, type: :string, default: nil
|
11
|
+
class_option :scope, type: :string, default: nil
|
12
|
+
class_option :dynamodb, type: :boolean, default: true
|
11
13
|
|
12
|
-
desc
|
14
|
+
desc 'list PATH', 'List resources defined'
|
13
15
|
def list(path)
|
14
16
|
puts "Defined resources:\n\n"
|
15
17
|
Config.new(path, options).list.each do |name|
|
16
|
-
puts
|
18
|
+
puts name.to_s
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
20
|
-
desc
|
22
|
+
desc 'plan PATH', 'Show execution plan'
|
21
23
|
def plan(path)
|
22
24
|
exit Config.new(path, options).plan
|
23
25
|
end
|
24
26
|
|
25
|
-
desc
|
27
|
+
desc 'graph PATH', 'Show execution graph'
|
26
28
|
def graph(path)
|
27
29
|
exit Config.new(path, options).graph
|
28
30
|
end
|
29
31
|
|
30
|
-
desc
|
32
|
+
desc 'validate PATH', 'Validate the generated Terraform'
|
31
33
|
def validate(path)
|
32
34
|
exit Config.new(path, options).validate
|
33
35
|
end
|
34
36
|
|
35
|
-
desc
|
36
|
-
option :force, :
|
37
|
+
desc 'apply PATH', 'Apply changes to resources'
|
38
|
+
option :force, aliases: ['f'], type: :boolean, desc: 'Forcefully remove any pending locks'
|
37
39
|
def apply(path)
|
38
40
|
exit Config.new(path, options).apply
|
39
41
|
end
|
40
42
|
|
41
|
-
desc
|
42
|
-
option :force, :
|
43
|
+
desc 'destroy PATH', 'Destroy resources'
|
44
|
+
option :force, aliases: ['f'], type: :boolean, desc: 'Forcefully remove any pending locks'
|
43
45
|
def destroy(path)
|
44
46
|
exit Config.new(path, options).destroy
|
45
47
|
end
|
46
48
|
|
47
|
-
desc
|
49
|
+
desc 'json PATH', 'Show terraform JSON'
|
48
50
|
def json(path)
|
49
51
|
puts(Config.new(path, options).json)
|
50
52
|
end
|
51
53
|
|
52
|
-
desc
|
54
|
+
desc 'show-state PATH', 'Show state'
|
53
55
|
def show_state(path)
|
54
56
|
puts(Config.new(path, options).show_state)
|
55
57
|
end
|
56
58
|
|
57
|
-
desc
|
59
|
+
desc 'use-remote-state PATH', 'Migrate to using remote state storage'
|
58
60
|
def use_remote_state(path)
|
59
61
|
puts(Config.new(path, options).use_remote_state)
|
60
62
|
end
|
61
63
|
|
62
|
-
desc
|
64
|
+
desc 'use-local-state PATH', 'Migrate to using local state storage'
|
63
65
|
def use_local_state(path)
|
64
66
|
puts(Config.new(path, options).use_local_state)
|
65
67
|
end
|
66
68
|
|
67
|
-
desc
|
69
|
+
desc 'import PATH ADDR ID', 'Import existing infrastructure into your Terraform state'
|
68
70
|
def import(path, addr, id)
|
69
71
|
exit Config.new(path, options).import(addr, id)
|
70
72
|
end
|
data/lib/terrafying/dynamodb.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'aws-sdk-dynamodb'
|
2
4
|
require 'json'
|
3
5
|
require 'securerandom'
|
@@ -9,7 +11,7 @@ class ::Aws::DynamoDB::Client
|
|
9
11
|
begin
|
10
12
|
yield block
|
11
13
|
rescue ::Aws::DynamoDB::Errors::ResourceNotFoundException => e
|
12
|
-
if
|
14
|
+
if !retried
|
13
15
|
create_table(table_spec)
|
14
16
|
retry
|
15
17
|
else
|
@@ -22,10 +24,10 @@ end
|
|
22
24
|
module Terrafying
|
23
25
|
module DynamoDb
|
24
26
|
def self.client
|
25
|
-
@@client ||= ::Aws::DynamoDB::Client.new(
|
26
|
-
region: Terrafying::Context::REGION
|
27
|
-
#endpoint: 'http://localhost:8000',
|
28
|
-
|
27
|
+
@@client ||= ::Aws::DynamoDB::Client.new(
|
28
|
+
region: Terrafying::Context::REGION
|
29
|
+
# endpoint: 'http://localhost:8000',
|
30
|
+
)
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
@@ -1,17 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Terrafying
|
2
4
|
module DynamoDb
|
3
5
|
class Config
|
4
6
|
attr_accessor :state_table, :lock_table
|
5
|
-
|
7
|
+
|
6
8
|
def initialize
|
7
|
-
@state_table =
|
8
|
-
@lock_table =
|
9
|
+
@state_table = 'terrafying-state'
|
10
|
+
@lock_table = 'terrafying-state-lock'
|
9
11
|
end
|
10
12
|
end
|
11
|
-
|
13
|
+
|
12
14
|
def config
|
13
15
|
@config ||= Config.new
|
14
16
|
end
|
15
17
|
module_function :config
|
16
18
|
end
|
17
|
-
end
|
19
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'terrafying/dynamodb'
|
2
4
|
require 'terrafying/dynamodb/config'
|
3
5
|
|
@@ -12,18 +14,18 @@ module Terrafying
|
|
12
14
|
|
13
15
|
def status
|
14
16
|
@client.ensure_table(table) do
|
15
|
-
resp = @client.get_item(
|
17
|
+
resp = @client.get_item(
|
16
18
|
table_name: @table_name,
|
17
19
|
key: {
|
18
|
-
|
20
|
+
'name' => @name
|
19
21
|
},
|
20
|
-
consistent_read: true
|
21
|
-
|
22
|
+
consistent_read: true
|
23
|
+
)
|
22
24
|
if resp.item
|
23
25
|
return {
|
24
26
|
status: :locked,
|
25
|
-
locked_at: resp.item[
|
26
|
-
metadata: resp.item[
|
27
|
+
locked_at: resp.item['locked_at'],
|
28
|
+
metadata: resp.item['metadata']
|
27
29
|
}
|
28
30
|
else
|
29
31
|
return {
|
@@ -32,92 +34,87 @@ module Terrafying
|
|
32
34
|
end
|
33
35
|
end
|
34
36
|
end
|
35
|
-
|
37
|
+
|
36
38
|
def acquire
|
37
39
|
@client.ensure_table(table) do
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
raise "Unable to acquire lock: #{status.inspect}" # TODO
|
44
|
-
end
|
40
|
+
lock_id = SecureRandom.uuid
|
41
|
+
@client.update_item(acquire_request(lock_id))
|
42
|
+
return lock_id
|
43
|
+
rescue ::Aws::DynamoDB::Errors::ConditionalCheckFailedException
|
44
|
+
raise "Unable to acquire lock: #{status.inspect}" # TODO
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
def steal
|
49
49
|
@client.ensure_table(table) do
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
raise "Unable to steal lock: #{status.inspect}" # TODO
|
58
|
-
end
|
50
|
+
lock_id = SecureRandom.uuid
|
51
|
+
req = acquire_request(lock_id)
|
52
|
+
req.delete(:condition_expression)
|
53
|
+
@client.update_item(req)
|
54
|
+
return lock_id
|
55
|
+
rescue ::Aws::DynamoDB::Errors::ConditionalCheckFailedException
|
56
|
+
raise "Unable to steal lock: #{status.inspect}" # TODO
|
59
57
|
end
|
60
58
|
end
|
61
|
-
|
59
|
+
|
62
60
|
def release(lock_id)
|
63
61
|
@client.ensure_table(table) do
|
64
|
-
|
65
|
-
@
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
raise "Unable to release lock: #{status.inspect}" # TODO
|
79
|
-
end
|
62
|
+
@client.delete_item(
|
63
|
+
table_name: @table_name,
|
64
|
+
key: {
|
65
|
+
'name' => @name
|
66
|
+
},
|
67
|
+
return_values: 'NONE',
|
68
|
+
condition_expression: 'lock_id = :lock_id',
|
69
|
+
expression_attribute_values: {
|
70
|
+
':lock_id' => lock_id
|
71
|
+
}
|
72
|
+
)
|
73
|
+
nil
|
74
|
+
rescue ::Aws::DynamoDB::Errors::ConditionalCheckFailedException
|
75
|
+
raise "Unable to release lock: #{status.inspect}" # TODO
|
80
76
|
end
|
81
77
|
end
|
82
78
|
|
83
79
|
private
|
80
|
+
|
84
81
|
def acquire_request(lock_id)
|
85
82
|
{
|
86
83
|
table_name: @table_name,
|
87
84
|
key: {
|
88
|
-
|
85
|
+
'name' => @name
|
89
86
|
},
|
90
|
-
return_values:
|
91
|
-
update_expression:
|
92
|
-
condition_expression:
|
87
|
+
return_values: 'NONE',
|
88
|
+
update_expression: 'SET lock_id = :lock_id, locked_at = :locked_at, metadata = :metadata',
|
89
|
+
condition_expression: 'attribute_not_exists(lock_id)',
|
93
90
|
expression_attribute_values: {
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
}
|
99
|
-
}
|
91
|
+
':lock_id' => lock_id,
|
92
|
+
':locked_at' => Time.now.to_s,
|
93
|
+
':metadata' => {
|
94
|
+
'owner' => "#{`git config user.name`.chomp} (#{`git config user.email`.chomp})"
|
95
|
+
}
|
96
|
+
}
|
100
97
|
}
|
101
98
|
end
|
102
|
-
|
99
|
+
|
103
100
|
def table
|
104
101
|
{
|
105
102
|
table_name: @table_name,
|
106
103
|
attribute_definitions: [
|
107
104
|
{
|
108
|
-
attribute_name:
|
109
|
-
attribute_type:
|
110
|
-
}
|
105
|
+
attribute_name: 'name',
|
106
|
+
attribute_type: 'S'
|
107
|
+
}
|
111
108
|
],
|
112
109
|
key_schema: [
|
113
110
|
{
|
114
|
-
attribute_name:
|
115
|
-
key_type:
|
116
|
-
}
|
111
|
+
attribute_name: 'name',
|
112
|
+
key_type: 'HASH'
|
113
|
+
}
|
117
114
|
],
|
118
115
|
provisioned_throughput: {
|
119
116
|
read_capacity_units: 1,
|
120
|
-
write_capacity_units: 1
|
117
|
+
write_capacity_units: 1
|
121
118
|
}
|
122
119
|
}
|
123
120
|
end
|
@@ -1,31 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'digest'
|
2
4
|
require 'terrafying/dynamodb/config'
|
3
5
|
|
4
6
|
module Terrafying
|
5
7
|
module DynamoDb
|
6
8
|
class StateStore
|
7
|
-
def initialize(scope,
|
9
|
+
def initialize(scope, _opts = {})
|
8
10
|
@scope = scope
|
9
11
|
@client = Terrafying::DynamoDb.client
|
10
12
|
@table_name = Terrafying::DynamoDb.config.state_table
|
11
13
|
end
|
12
|
-
|
14
|
+
|
13
15
|
def get
|
14
16
|
@client.ensure_table(table) do
|
15
|
-
resp = @client.query(
|
17
|
+
resp = @client.query(
|
16
18
|
table_name: @table_name,
|
17
19
|
limit: 1,
|
18
20
|
key_conditions: {
|
19
|
-
|
21
|
+
'scope' => {
|
20
22
|
attribute_value_list: [@scope],
|
21
|
-
comparison_operator:
|
23
|
+
comparison_operator: 'EQ'
|
22
24
|
}
|
23
25
|
},
|
24
|
-
scan_index_forward: false
|
25
|
-
|
26
|
+
scan_index_forward: false
|
27
|
+
)
|
26
28
|
case resp.items.count
|
27
29
|
when 0 then return nil
|
28
|
-
when 1 then return resp.items.first[
|
30
|
+
when 1 then return resp.items.first['state']
|
29
31
|
else raise 'More than one item found when retrieving state. This is a bug and should never happen.' if resp.items.count != 1
|
30
32
|
end
|
31
33
|
end
|
@@ -35,58 +37,56 @@ module Terrafying
|
|
35
37
|
@client.ensure_table(table) do
|
36
38
|
sha256 = Digest::SHA256.hexdigest(state)
|
37
39
|
json = JSON.parse(state)
|
38
|
-
@client.update_item(
|
40
|
+
@client.update_item(
|
39
41
|
table_name: @table_name,
|
40
42
|
key: {
|
41
|
-
|
42
|
-
|
43
|
+
'scope' => @scope,
|
44
|
+
'serial' => json['serial'].to_i
|
43
45
|
},
|
44
|
-
return_values:
|
45
|
-
update_expression:
|
46
|
-
condition_expression:
|
46
|
+
return_values: 'NONE',
|
47
|
+
update_expression: 'SET sha256 = :sha256, #state = :state',
|
48
|
+
condition_expression: 'attribute_not_exists(serial) OR sha256 = :sha256',
|
47
49
|
expression_attribute_names: {
|
48
|
-
|
50
|
+
'#state' => 'state'
|
49
51
|
},
|
50
52
|
expression_attribute_values: {
|
51
|
-
|
52
|
-
|
53
|
+
':sha256' => sha256,
|
54
|
+
':state' => state
|
53
55
|
}
|
54
|
-
|
56
|
+
)
|
55
57
|
end
|
56
58
|
end
|
57
|
-
|
59
|
+
|
58
60
|
def table
|
59
61
|
{
|
60
62
|
table_name: @table_name,
|
61
63
|
attribute_definitions: [
|
62
64
|
{
|
63
|
-
attribute_name:
|
64
|
-
attribute_type:
|
65
|
+
attribute_name: 'scope',
|
66
|
+
attribute_type: 'S'
|
65
67
|
},
|
66
68
|
{
|
67
|
-
attribute_name:
|
68
|
-
attribute_type:
|
69
|
+
attribute_name: 'serial',
|
70
|
+
attribute_type: 'N'
|
69
71
|
}
|
70
72
|
],
|
71
73
|
key_schema: [
|
72
74
|
{
|
73
|
-
attribute_name:
|
74
|
-
key_type:
|
75
|
+
attribute_name: 'scope',
|
76
|
+
key_type: 'HASH'
|
75
77
|
},
|
76
78
|
{
|
77
|
-
attribute_name:
|
78
|
-
key_type:
|
79
|
-
}
|
80
|
-
|
79
|
+
attribute_name: 'serial',
|
80
|
+
key_type: 'RANGE'
|
81
|
+
}
|
82
|
+
|
81
83
|
],
|
82
84
|
provisioned_throughput: {
|
83
85
|
read_capacity_units: 1,
|
84
|
-
write_capacity_units: 1
|
86
|
+
write_capacity_units: 1
|
85
87
|
}
|
86
88
|
}
|
87
89
|
end
|
88
90
|
end
|
89
91
|
end
|
90
92
|
end
|
91
|
-
|
92
|
-
|
data/lib/terrafying/generator.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'json'
|
2
4
|
require 'base64'
|
3
5
|
require 'erb'
|
@@ -6,40 +8,36 @@ require 'deep_merge'
|
|
6
8
|
require 'terrafying/aws'
|
7
9
|
|
8
10
|
module Terrafying
|
9
|
-
|
10
|
-
ARG_PLACEHOLDER = "ARG_PLACEHOLDER123"
|
11
|
+
ARG_PLACEHOLDER = 'ARG_PLACEHOLDER123'
|
11
12
|
|
12
13
|
class Ref
|
13
|
-
|
14
14
|
def fn_call(fn, *args)
|
15
|
-
if args.
|
16
|
-
args = [ARG_PLACEHOLDER]
|
17
|
-
end
|
15
|
+
args = [ARG_PLACEHOLDER] if args.empty?
|
18
16
|
FnRef.new(fn: fn, args: args, ref: self)
|
19
17
|
end
|
20
18
|
|
21
19
|
def downcase
|
22
|
-
fn_call(
|
20
|
+
fn_call('lower')
|
23
21
|
end
|
24
22
|
|
25
23
|
def strip
|
26
|
-
fn_call(
|
24
|
+
fn_call('trimspace')
|
27
25
|
end
|
28
26
|
|
29
27
|
def split(separator)
|
30
|
-
fn_call(
|
28
|
+
fn_call('split', separator, ARG_PLACEHOLDER)
|
31
29
|
end
|
32
30
|
|
33
|
-
def slice(idx, length=0)
|
31
|
+
def slice(idx, length = 0)
|
34
32
|
if length != 0
|
35
|
-
fn_call(
|
33
|
+
fn_call('slice', ARG_PLACEHOLDER, idx, idx + length)
|
36
34
|
else
|
37
|
-
fn_call(
|
35
|
+
fn_call('element', ARG_PLACEHOLDER, idx)
|
38
36
|
end
|
39
37
|
end
|
40
38
|
|
41
39
|
def realise
|
42
|
-
|
40
|
+
''
|
43
41
|
end
|
44
42
|
|
45
43
|
def to_s
|
@@ -47,15 +45,15 @@ module Terrafying
|
|
47
45
|
end
|
48
46
|
|
49
47
|
def to_str
|
50
|
-
|
48
|
+
to_s
|
51
49
|
end
|
52
50
|
|
53
51
|
def <=>(other)
|
54
|
-
|
52
|
+
to_s <=> other.to_s
|
55
53
|
end
|
56
54
|
|
57
55
|
def ==(other)
|
58
|
-
|
56
|
+
to_s == other.to_s
|
59
57
|
end
|
60
58
|
|
61
59
|
def [](key)
|
@@ -66,18 +64,17 @@ module Terrafying
|
|
66
64
|
end
|
67
65
|
end
|
68
66
|
|
69
|
-
def []=(
|
67
|
+
def []=(_k, _v)
|
70
68
|
raise "You can't set a value this way"
|
71
69
|
end
|
72
|
-
|
73
70
|
end
|
74
71
|
|
75
72
|
class RootRef < Ref
|
76
73
|
def initialize(
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
74
|
+
kind: :resource,
|
75
|
+
type: '',
|
76
|
+
name:
|
77
|
+
)
|
81
78
|
@kind = kind
|
82
79
|
@type = type
|
83
80
|
@name = name
|
@@ -85,16 +82,14 @@ module Terrafying
|
|
85
82
|
|
86
83
|
def realise
|
87
84
|
type = [@type]
|
88
|
-
if @kind != :resource
|
89
|
-
type = [@kind, @type]
|
90
|
-
end
|
85
|
+
type = [@kind, @type] if @kind != :resource
|
91
86
|
|
92
|
-
(type + [@name]).reject(&:empty?).join(
|
87
|
+
(type + [@name]).reject(&:empty?).join('.')
|
93
88
|
end
|
94
89
|
|
95
90
|
def fn_call(fn, *args)
|
96
91
|
if @kind == :resource
|
97
|
-
self[
|
92
|
+
self['id'].fn_call(fn, *args)
|
98
93
|
else
|
99
94
|
super
|
100
95
|
end
|
@@ -111,9 +106,9 @@ module Terrafying
|
|
111
106
|
|
112
107
|
class AttributeRef < Ref
|
113
108
|
def initialize(
|
114
|
-
|
115
|
-
|
116
|
-
|
109
|
+
ref:,
|
110
|
+
key:
|
111
|
+
)
|
117
112
|
@ref = ref
|
118
113
|
@key = key
|
119
114
|
end
|
@@ -125,9 +120,9 @@ module Terrafying
|
|
125
120
|
|
126
121
|
class IndexRef < Ref
|
127
122
|
def initialize(
|
128
|
-
|
129
|
-
|
130
|
-
|
123
|
+
ref:,
|
124
|
+
idx:
|
125
|
+
)
|
131
126
|
@ref = ref
|
132
127
|
@idx = idx
|
133
128
|
end
|
@@ -139,10 +134,10 @@ module Terrafying
|
|
139
134
|
|
140
135
|
class FnRef < Ref
|
141
136
|
def initialize(
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
137
|
+
ref:,
|
138
|
+
fn:,
|
139
|
+
args: []
|
140
|
+
)
|
146
141
|
@ref = ref
|
147
142
|
@fn = fn
|
148
143
|
@args = args
|
@@ -150,7 +145,7 @@ module Terrafying
|
|
150
145
|
|
151
146
|
def realise
|
152
147
|
ref = @ref.realise
|
153
|
-
args = @args.map
|
148
|
+
args = @args.map do |arg|
|
154
149
|
if arg == ARG_PLACEHOLDER
|
155
150
|
ref
|
156
151
|
elsif arg.is_a? String
|
@@ -158,19 +153,18 @@ module Terrafying
|
|
158
153
|
else
|
159
154
|
arg
|
160
155
|
end
|
161
|
-
|
156
|
+
end.join(', ')
|
162
157
|
|
163
158
|
"#{@fn}(#{args})"
|
164
159
|
end
|
165
160
|
end
|
166
161
|
|
167
162
|
class Context
|
168
|
-
|
169
|
-
REGION = ENV.fetch("AWS_REGION", "eu-west-1")
|
163
|
+
REGION = ENV.fetch('AWS_REGION', 'eu-west-1')
|
170
164
|
|
171
165
|
PROVIDER_DEFAULTS = {
|
172
166
|
aws: { region: REGION }
|
173
|
-
}
|
167
|
+
}.freeze
|
174
168
|
|
175
169
|
def self.bundle(&block)
|
176
170
|
ctx = Context.new
|
@@ -182,7 +176,7 @@ module Terrafying
|
|
182
176
|
|
183
177
|
def initialize
|
184
178
|
@output = {
|
185
|
-
|
179
|
+
'resource' => {}
|
186
180
|
}
|
187
181
|
@children = []
|
188
182
|
end
|
@@ -195,6 +189,7 @@ module Terrafying
|
|
195
189
|
key = provider_key(name, spec)
|
196
190
|
@providers ||= {}
|
197
191
|
raise "Duplicate provider configuration detected for #{key}" if key_exists_spec_differs(key, name, spec)
|
192
|
+
|
198
193
|
@providers[key] = { name.to_s => spec }
|
199
194
|
@output['provider'] = @providers.values
|
200
195
|
key
|
@@ -209,40 +204,52 @@ module Terrafying
|
|
209
204
|
end
|
210
205
|
|
211
206
|
def local(name, value)
|
212
|
-
@output[
|
207
|
+
@output['locals'] ||= {}
|
213
208
|
|
214
|
-
raise "Local already exists #{name
|
209
|
+
raise "Local already exists #{name}" if @output['locals'].key? name.to_s
|
215
210
|
|
216
|
-
@output[
|
211
|
+
@output['locals'][name.to_s] = value
|
217
212
|
RootRef.new(kind: :local, name: name)
|
218
213
|
end
|
219
214
|
|
220
215
|
def var(name, spec)
|
221
|
-
@output[
|
216
|
+
@output['variable'] ||= {}
|
222
217
|
|
223
|
-
raise "Var already exists #{name
|
218
|
+
raise "Var already exists #{name}" if @output['variable'].key? name.to_s
|
224
219
|
|
225
|
-
@output[
|
220
|
+
@output['variable'][name.to_s] = spec
|
226
221
|
RootRef.new(kind: :var, name: name)
|
227
222
|
end
|
228
223
|
|
229
224
|
def data(type, name, spec)
|
230
|
-
@output[
|
231
|
-
@output[
|
225
|
+
@output['data'] ||= {}
|
226
|
+
@output['data'][type.to_s] ||= {}
|
227
|
+
|
228
|
+
raise "Data already exists #{type}.#{name}" if @output['data'][type.to_s].key? name.to_s
|
232
229
|
|
233
|
-
|
234
|
-
@output["data"][type.to_s][name.to_s] = spec
|
230
|
+
@output['data'][type.to_s][name.to_s] = spec
|
235
231
|
RootRef.new(kind: :data, type: type, name: name)
|
236
232
|
end
|
237
233
|
|
238
234
|
def resource(type, name, attributes)
|
239
|
-
@output[
|
235
|
+
@output['resource'][type.to_s] ||= {}
|
240
236
|
|
241
|
-
raise "Resource already exists #{type
|
242
|
-
|
237
|
+
raise "Resource already exists #{type}.#{name}" if @output['resource'][type.to_s].key? name.to_s
|
238
|
+
|
239
|
+
@output['resource'][type.to_s][name.to_s] = attributes
|
243
240
|
RootRef.new(kind: :resource, type: type, name: name)
|
244
241
|
end
|
245
242
|
|
243
|
+
def tf_module(name, spec)
|
244
|
+
@output['module'] ||= {}
|
245
|
+
|
246
|
+
raise "Module already exists #{name}" if @output['module'].key? name.to_s
|
247
|
+
|
248
|
+
@output['module'][name.to_s] = spec
|
249
|
+
|
250
|
+
RootRef.new(kind: :module, name: name)
|
251
|
+
end
|
252
|
+
|
246
253
|
def template(relative_path, params = {})
|
247
254
|
dir = caller_locations[0].path
|
248
255
|
filename = File.join(File.dirname(dir), relative_path)
|
@@ -255,8 +262,8 @@ module Terrafying
|
|
255
262
|
@children.inject(@output) { |out, c| out.deep_merge(c.output_with_children) }
|
256
263
|
end
|
257
264
|
|
258
|
-
def id_of(type,name)
|
259
|
-
output_of(type, name,
|
265
|
+
def id_of(type, name)
|
266
|
+
output_of(type, name, 'id')
|
260
267
|
end
|
261
268
|
|
262
269
|
def output_of(type, name, key)
|
@@ -270,8 +277,8 @@ module Terrafying
|
|
270
277
|
def resource_names
|
271
278
|
out = output_with_children
|
272
279
|
ret = []
|
273
|
-
|
274
|
-
|
280
|
+
out['resource'].keys.each do |type|
|
281
|
+
out['resource'][type].keys.each do |id|
|
275
282
|
ret << "#{type}.#{id}"
|
276
283
|
end
|
277
284
|
end
|
@@ -281,8 +288,8 @@ module Terrafying
|
|
281
288
|
def resources
|
282
289
|
out = output_with_children
|
283
290
|
ret = []
|
284
|
-
|
285
|
-
|
291
|
+
out['resource'].keys.each do |type|
|
292
|
+
out['resource'][type].keys.each do |id|
|
286
293
|
ret << "${#{type}.#{id}.id}"
|
287
294
|
end
|
288
295
|
end
|
@@ -295,23 +302,21 @@ module Terrafying
|
|
295
302
|
end
|
296
303
|
|
297
304
|
def tf_safe(str)
|
298
|
-
str.gsub(
|
305
|
+
str.gsub(%r{[\.\s/\?]}, '-').gsub(%r{\*}, "star")
|
299
306
|
end
|
300
|
-
|
301
307
|
end
|
302
308
|
|
303
309
|
class RootContext < Context
|
304
|
-
|
305
310
|
def initialize
|
306
311
|
super
|
307
312
|
@providers = {}
|
308
313
|
end
|
309
314
|
|
310
315
|
def backend(name, spec)
|
311
|
-
@output[
|
316
|
+
@output['terraform'] = {
|
312
317
|
backend: {
|
313
|
-
name => spec
|
314
|
-
}
|
318
|
+
name => spec
|
319
|
+
}
|
315
320
|
}
|
316
321
|
end
|
317
322
|
|
@@ -324,22 +329,19 @@ module Terrafying
|
|
324
329
|
end
|
325
330
|
|
326
331
|
def output_with_children
|
327
|
-
|
328
|
-
|
329
|
-
if not key_exists_spec_differs(provider_key(name, spec), name, spec)
|
332
|
+
PROVIDER_DEFAULTS.each do |name, spec|
|
333
|
+
unless key_exists_spec_differs(provider_key(name, spec), name, spec)
|
330
334
|
provider(name, spec)
|
331
335
|
end
|
332
|
-
|
336
|
+
end
|
333
337
|
|
334
338
|
super
|
335
339
|
end
|
336
|
-
|
337
340
|
end
|
338
341
|
|
339
342
|
Generator = RootContext.new
|
340
343
|
|
341
344
|
module DSL
|
342
|
-
|
343
345
|
%w[
|
344
346
|
add!
|
345
347
|
aws
|
@@ -349,16 +351,15 @@ module Terrafying
|
|
349
351
|
provider
|
350
352
|
resource
|
351
353
|
data
|
354
|
+
tf_module
|
352
355
|
template
|
353
356
|
tf_safe
|
354
357
|
id_of
|
355
358
|
output_of
|
356
|
-
].each
|
357
|
-
define_method(name)
|
359
|
+
].each do |name|
|
360
|
+
define_method(name) do |*args|
|
358
361
|
Generator.send(name, *args)
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
+
end
|
363
|
+
end
|
362
364
|
end
|
363
|
-
|
364
365
|
end
|