Skip to content

Commit

Permalink
Allow use with terraform v0.12.x and add metadata options for aws_lau…
Browse files Browse the repository at this point in the history
…nch_config (#182)

* added support for terraform > v0.12.0 and metaadata-option for launch config

* fix test to include additional options

Co-authored-by: jaysiyani <jaysiyani>
  • Loading branch information
jaysiyani authored Aug 25, 2020
1 parent fe62284 commit 036a3d0
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 13 deletions.
4 changes: 4 additions & 0 deletions lib/terrafying/components/dynamicset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ def create_in(vpc, name, options = {})
subnets: vpc.subnets.fetch(:private, []),
depends_on: [],
rolling_update: :simple,
metadata_options: {},
vpc_endpoints_egress: []
}.merge(options)

metadata_options = options[:metadata_options]

ident = "#{tf_safe(vpc.name)}-#{name}"

@name = ident
Expand Down Expand Up @@ -85,6 +88,7 @@ def create_in(vpc, name, options = {})
lifecycle: {
create_before_destroy: true
},
metadata_options: options[:metadata_options],
depends_on: resource_name_from(options[:instance_profile])

if options[:instances][:track]
Expand Down
18 changes: 15 additions & 3 deletions lib/terrafying/components/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ def create_in(vpc, name, options = {})
instance_profile: nil,
ports: [],
tags: {},
security_groups: [],
depends_on: []
security_groups: nil,
metadata_options: nil,
depends_on: nil,
ipv6_cidr_blocks: nil,
prefix_list_ids: nil,
security_groups: nil,
self: nil,
description: nil,
}.merge(options)

ident = "#{tf_safe(vpc.name)}-#{name}"
Expand All @@ -55,7 +61,12 @@ def create_in(vpc, name, options = {})
from_port: 0,
to_port: 0,
protocol: -1,
cidr_blocks: ['0.0.0.0/0']
cidr_blocks: ['0.0.0.0/0'],
ipv6_cidr_blocks: options[:ipv6_cidr_blocks],
prefix_list_ids: options[:prefix_list_ids],
security_groups: options[:security_groups],
self: options[:self],
description: options[:description]
}
]

Expand Down Expand Up @@ -99,6 +110,7 @@ def create_in(vpc, name, options = {})
vpc.internal_ssh_security_group
].push(*options[:security_groups]),
user_data: options[:user_data],
metadata_options: options[:metadata_options],
lifecycle: {
create_before_destroy: true
},
Expand Down
4 changes: 2 additions & 2 deletions lib/terrafying/components/loadbalancer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'digest'
require 'digest/bubblebabble'
require 'terrafying/components/usable'
require 'terrafying/generator'

Expand Down Expand Up @@ -217,7 +217,7 @@ def autoscale(set, target_value:, disable_scale_in:)

def make_identifier(type, vpc_name, name)
gen_id = "#{type}-#{tf_safe(vpc_name)}-#{name}"
return Digest::SHA2.hexdigest(gen_id)[0..24] if @hex_ident || gen_id.size > 26
return Digest::SHA256.bubblebabble(gen_id)[0..15] if @hex_ident || gen_id.size > 26

gen_id[0..31]
end
Expand Down
4 changes: 4 additions & 0 deletions lib/terrafying/components/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def create_in(vpc, name, options = {})
subnets: vpc.subnets.fetch(:private, []),
startup_grace_period: 300,
depends_on: [],
metadata_options: {},
audit_role: "arn:aws:iam::#{aws.account_id}:role/auditd_logging",
metrics_ports: [],
vpc_endpoints_egress: []
Expand Down Expand Up @@ -96,6 +97,8 @@ def create_in(vpc, name, options = {})
@instance_profile = add! InstanceProfile.create(ident, statements: iam_statements)
end

metadata_options = options[:metadata_options]

tags = options[:tags].merge(service_name: name)

set = options[:instances].is_a?(Hash) ? DynamicSet : StaticSet
Expand All @@ -112,6 +115,7 @@ def create_in(vpc, name, options = {})
instance_set_options = {
instance_profile: @instance_profile,
depends_on: depends_on,
metadata_options: metadata_options,
tags: tags
}

Expand Down
14 changes: 12 additions & 2 deletions lib/terrafying/components/vpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,25 @@ def create(name, raw_cidr, options = {})
from_port: 22,
to_port: 22,
protocol: 'tcp',
cidr_blocks: [@cidr]
cidr_blocks: [@cidr],
description: nil,
ipv6_cidr_blocks: nil,
prefix_list_ids: nil,
security_groups: nil,
self: nil
}
],
egress: [
{
from_port: 22,
to_port: 22,
protocol: 'tcp',
cidr_blocks: [@cidr]
cidr_blocks: [@cidr],
description: nil,
ipv6_cidr_blocks: nil,
prefix_list_ids: nil,
security_groups: nil,
self: nil
}
]
self
Expand Down
10 changes: 5 additions & 5 deletions spec/terrafying/components/loadbalancer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'digest'
require 'digest/bubblebabble'
require 'terrafying'
require 'terrafying/components/instance'
require 'terrafying/components/loadbalancer'
Expand Down Expand Up @@ -91,15 +91,15 @@
expect(lb.name.length).to be <= 32
end

it 'should use hex identifiers when requested' do
name = 'abcdefghijklmnopqrstuvwxyz123456789'
expected_hex = Digest::SHA2.hexdigest("application-#{@vpc.name}-#{name}")[0..24]
it 'should use bubblebabble identifiers when requested' do
name = 'abcde-fghi-jklm'
expected_bubblebabble = Digest::SHA256.bubblebabble("application-#{@vpc.name}-#{name}")[0..15]

lb = Terrafying::Components::LoadBalancer.create_in(
@vpc, name, hex_ident: true
)

expect(lb.name).to eq(expected_hex)
expect(lb.name).to eq(expected_bubblebabble)
end

context('application load balancer') do
Expand Down
7 changes: 6 additions & 1 deletion spec/terrafying/components/vpc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@
from_port: 22,
to_port: 22,
protocol: 'tcp',
cidr_blocks: [cidr]
cidr_blocks: [cidr],
description: nil,
ipv6_cidr_blocks: nil,
prefix_list_ids: nil,
security_groups: nil,
self: nil
}

expect(ssh_security_group[:ingress][0]).to eq(rule)
Expand Down

0 comments on commit 036a3d0

Please sign in to comment.