Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Commit

Permalink
Merge branch 'improve-instance-tag-handling'
Browse files Browse the repository at this point in the history
* improve-instance-tag-handling:
  Verify instance tags in acceptance tests
  Custom setter for tags to allow changing tags after creation
  Expose tags to puppet resource
* closes #15
  • Loading branch information
Iristyle committed Nov 3, 2014
2 parents 6af24be + f94d8a4 commit 46ec608
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/puppet/provider/ec2_instance/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def self.prefetch(resources)
def self.instance_to_hash(region, instance)
name_tag = instance.tags.detect { |tag| tag.key == 'Name' }
monitoring = instance.monitoring.state == "enabled" ? true : false
tags = {}
instance.tags.each do |tag|
tags[tag.key] = tag.value unless tag.key == 'Name'
end
{
name: name_tag ? name_tag.value : nil,
instance_type: instance.instance_type,
Expand All @@ -45,6 +49,7 @@ def self.instance_to_hash(region, instance)
key_name: instance.key_name,
availability_zone: instance.placement.availability_zone,
ensure: instance.state.name.to_sym,
tags: tags,
region: region
}
end
Expand Down Expand Up @@ -130,6 +135,19 @@ def stop
@property_hash[:ensure] = :stopped
end

def tags=(value)
Puppet.info("Updating tags for #{name} in region #{region}")
ec2_client(resource[:region]).create_tags(
resources: [instance_id],
tags: value.collect { |k,v| { :key => k, :value => v } }
) unless value.empty?
missing_tags = tags.keys - value.keys
ec2_client(resource[:region]).delete_tags(
resources: [instance_id],
tags: missing_tags.collect { |k| { :key => k } }
) unless missing_tags.empty?
end

def destroy
Puppet.info("Deleting instance #{name} in region #{resource[:region]}")
instances = ec2_client(resource[:region]).describe_instances(filters: [
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/type/ec2_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
desc 'the security groups to associate the instance'
end

newparam(:tags, :array_matching => :all) do
newproperty(:tags) do
desc 'the tags for the instance'
end

Expand Down
5 changes: 5 additions & 0 deletions spec/acceptance/fixtures/instance.pp.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ ec2_instance { '{{name}}':
image_id => '{{image_id}}',
instance_type => '{{instance_type}}',
security_groups => ['default'],
tags => {
{{#tags}}
{{k}} => '{{v}}',
{{/tags}}
}
}
34 changes: 34 additions & 0 deletions spec/acceptance/instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def wait_until_status(name, status, max_wait = 15)
end
end

def has_matching_tags(instance, tags)
instance_tags = {}
instance.tags.each { |s| instance_tags[s.key.to_sym] = s.value if s.key != 'Name' }

symmetric_difference = tags.to_set ^ instance_tags.to_set
expect(symmetric_difference).to be_empty
end

describe 'should create a new instance' do

before(:all) do
Expand All @@ -41,6 +49,11 @@ def wait_until_status(name, status, max_wait = 15)
:region => @default_region,
:image_id => 'ami-41e85d5c',
:ensure => 'present',
:tags => {
:department => 'engineering',
:project => 'cloud',
:created_by => 'aws-acceptance'
}
}

PuppetManifest.new(@template, @config).apply
Expand All @@ -60,6 +73,10 @@ def wait_until_status(name, status, max_wait = 15)
expect(@instance.tags.detect { |tag| tag.key == 'Name' }.value).to eq(@config[:name])
end

it "with the specified tags" do
has_matching_tags(@instance, @config[:tags])
end

it "with the specified type" do
expect(@instance.instance_type).to eq(@config[:instance_type])
end
Expand All @@ -79,6 +96,11 @@ def wait_until_status(name, status, max_wait = 15)
:region => 'sa-east-1',
:image_id => 'ami-41e85d5c',
:ensure => 'present',
:tags => {
:department => 'engineering',
:project => 'cloud',
:created_by => 'aws-acceptance'
}
}

PuppetManifest.new(@template, @config).apply
Expand All @@ -92,6 +114,18 @@ def wait_until_status(name, status, max_wait = 15)
wait_until_status(@config[:name], 'shutting-down')
end

it 'that can have tags changed' do
wait_until_status(@config[:name], 'running', 45)
has_matching_tags(@instance, @config[:tags])

tags = {:created_by => 'aws-tests', :foo => 'bar'}
@config[:tags].update(tags)

PuppetManifest.new(@template, @config).apply
@instance = find_instance(@config[:name])
has_matching_tags(@instance, @config[:tags])
end

it "that can be stopped and restarted" do
wait_until_status(@config[:name], 'running', 45)

Expand Down
6 changes: 5 additions & 1 deletion spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class PuppetManifest < Mustache
def initialize(file, config)
@template_file = File.join(Dir.getwd, 'spec', 'acceptance', 'fixtures', file)
config.each do |key, value|
instance_variable_set("@#{key}".to_sym, value)
config_value = value
if (value.class == Hash)
config_value = value.map { |k, v| { :k => k, :v => v }}
end
instance_variable_set("@#{key}".to_sym, config_value)
self.class.send(:attr_accessor, key)
end
end
Expand Down

0 comments on commit 46ec608

Please sign in to comment.