diff --git a/.travis.yml b/.travis.yml index bdd67ca..df32801 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,5 +6,4 @@ before_script: bundle exec berks install script: bundle exec strainer test --except kitchen notifications: slack: - secure: pUcsaMf3Opj49aybdfZz7xU5h+MxVd45p8QUglm8jKPgqM2QWZhdIemo42L25UmptV8ZBUB/QT2dgpBeCT6MbTm2U+JNPcuazLeXl5OTz3zCxAUA4gQ1rbkFAyzzScV301zz9xpnRPjhL96UNlqN+BV6ZE3Vic9edu/zcnMUxFM= - + secure: r1q5cfMiRowYnEPv3qUoBWHWQsSYwFk93/HUjhCP4QCS9lYxTQ/mko60MRrgfGh95T7DwXqrTkpPJrOMUWPPpBvZHwkgEc/nM63Z5diYh1HEj7FhsH2ybReiMfxwQNuPesZr2ObGLt9u4qjgSSyb082ANgAdfGTBrtvByDbBP00= diff --git a/CHANGELOG.md b/CHANGELOG.md index dc26c06..a4ce78e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,16 @@ machine_tag Cookbook CHANGELOG This file is used to list changes made in each version of the machine_tag cookbook. +v1.1.1 +------ + +- Added support for match_all and defaulting to false + +v1.1.0 +------ + +- Added support for RightLink 10 + v1.0.9 ------ diff --git a/libraries/machine_tag_base.rb b/libraries/machine_tag_base.rb index abc0b26..8651b04 100644 --- a/libraries/machine_tag_base.rb +++ b/libraries/machine_tag_base.rb @@ -67,7 +67,7 @@ def list # def search(query_tags, options = {}) query_tags = [query_tags] if query_tags.kind_of?(String) - tags_set_array = do_query(query_tags) + tags_set_array = do_query(query_tags,options) unless options[:required_tags].nil? || options[:required_tags].empty? # Set timeout for querying for required_tags. By default, the timeout is set @@ -86,7 +86,7 @@ def search(query_tags, options = {}) Kernel.sleep(sleep_sec) Chef::Log.info "Re-querying for '#{tag}'..." - tags_set_array = do_query(query_tags) + tags_set_array = do_query(query_tags,options) end end end @@ -107,7 +107,7 @@ def search(query_tags, options = {}) # # @return [Array] the list of all tags on the servers that match the query # - def do_query(query_tags) + def do_query(query_tags,options = {}) not_implemented end diff --git a/libraries/machine_tag_rightscale.rb b/libraries/machine_tag_rightscale.rb index 3280fb1..84c4e52 100644 --- a/libraries/machine_tag_rightscale.rb +++ b/libraries/machine_tag_rightscale.rb @@ -60,7 +60,7 @@ def list # # @return [Array] the tags on the servers that match the query # - def do_query(query_tags) + def do_query(query_tags, options = {}) tags_hash = JSON.parse(run_rs_tag_util("--query", query_tags.join(' '))) tags_set_array = [] tags_hash.values.each do |value| diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index 6716283..d02f4d8 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -71,19 +71,26 @@ def list # # @return [Array] the tags on the servers that match the query # - def do_query(query_tags) - resources = api_client.tags.by_tag(resource_type: 'instances', tags: [query_tags] ) - + def do_query(query_tags, options = {}) + query_tags = [query_tags] if query_tags.kind_of?(String) + Chef::Log.info "Tagged query_tags: #{query_tags}" + match_all = options.fetch(:match_all, false) + resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags, match_all: match_all) + Chef::Log.info "Tagged resources: #{resources}" + tags_hash = {} if resources.first links = resources.first.links if links - links.each {|link| - resource_tags = api_client.tags.by_resource(resource_hrefs:[link["href"]])#.first.tags - tags_hash[link["href"]]={ - "tags"=> resource_tags.first.tags.map{|tag| tag["name"]} - } - } + links.each do |link| + Chef::Log.info "Tagged Resource State:#{api_client.resource(link["href"]).state}" + if api_client.resource(link["href"]).state == 'operational' + resource_tags = api_client.tags.by_resource(resource_hrefs:[link["href"]])#.first.tags + tags_hash[link["href"]]={ + "tags"=> resource_tags.first.tags.map{|tag| tag["name"]} + } + end + end end end tags_set_array = [] diff --git a/libraries/machine_tag_vagrant.rb b/libraries/machine_tag_vagrant.rb index 0586864..875c144 100644 --- a/libraries/machine_tag_vagrant.rb +++ b/libraries/machine_tag_vagrant.rb @@ -66,7 +66,7 @@ def list # # @return [Array] the tags on the VMs that match the query # - def do_query(query_tags) + def do_query(query_tags, options = {}) query_result = [] # Return empty array if no tags are found in the query string diff --git a/metadata.rb b/metadata.rb index 49b556f..f47e902 100644 --- a/metadata.rb +++ b/metadata.rb @@ -4,7 +4,7 @@ license 'Apache 2.0' description 'Installs/Configures machine_tag' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '1.1.0' +version '1.1.1' depends 'apt', '~> 2.9.2' depends 'build-essential' diff --git a/spec/unit_test/machine_tag_base_spec.rb b/spec/unit_test/machine_tag_base_spec.rb index 4e63a77..8b94c31 100644 --- a/spec/unit_test/machine_tag_base_spec.rb +++ b/spec/unit_test/machine_tag_base_spec.rb @@ -44,14 +44,14 @@ context 'when no query options are specified' do it 'should do a query and return the tags matching the query' do - base.should_receive(:do_query).with(['database:active=true']).and_return([tag_set]) + base.should_receive(:do_query).with(['database:active=true'],{}).and_return([tag_set]) search_output = base.search('database:active=true') search_output.should == [tag_set] base.should_receive(:do_query).with([ 'database:active=true', 'rs_monitoring:state=active' - ]).and_return([tag_set]) + ],{}).and_return([tag_set]) search_output = base.search(['database:active=true', 'rs_monitoring:state=active']) search_output.should == [tag_set] @@ -65,7 +65,7 @@ # initially, but appears in the query sometime later tag_set_partial = tag_set.union(['database:master=true']) tag_set_full = tag_set_partial.union(['database:repl=active']) - base.should_receive(:do_query).with(['database:active=true']).exactly(4).and_return( + base.should_receive(:do_query).with(['database:active=true'],{:required_tags=>["database:master=true", "database:repl=active"]}).exactly(4).and_return( [tag_set], [tag_set_partial], [tag_set_partial], @@ -84,13 +84,13 @@ it 'should raise a Timeout exception' do query_tag = 'database:active=true' - base.should_receive(:do_query).with([query_tag]).at_least(:once).and_return([tag_set]) - query_options = { required_tags: ['database:master=true'], query_timeout: 1, } + base.should_receive(:do_query).with([query_tag],query_options).at_least(:once).and_return([tag_set]) + expect do base.search('database:active=true', query_options) end.to raise_error(Timeout::Error) diff --git a/spec/unit_test/machine_tag_rl10_spec.rb b/spec/unit_test/machine_tag_rl10_spec.rb index 2cdaceb..e7b3c69 100644 --- a/spec/unit_test/machine_tag_rl10_spec.rb +++ b/spec/unit_test/machine_tag_rl10_spec.rb @@ -71,6 +71,7 @@ let(:client_stub) do client = double('RightApi::Client', :log => nil) client.stub(:get_instance).and_return(instance_stub) + client.stub_chain(:resource,:state).and_return('operational') client end @@ -82,6 +83,7 @@ double('resources', :links=>[{"href"=>"/some_href"}], )} + let(:resource_tags_stub) { double('tag_resources', :tags=> rs_raw_output) } before(:each) do @@ -121,15 +123,15 @@ describe "#do_query" do it "should return an array of tag sets containing the query tag" do client_stub.tags.should_receive(:by_tag). - with(hash_including(resource_type: 'instances', tags: ['database:active=true'])). + with(hash_including(resource_type: 'instances', tags: ['database:active=true'], match_all: false)). and_return([resources_stub]) - + client_stub.tags.should_receive(:by_resource). with(hash_including(resource_hrefs: ["/some_href"])). and_return([resource_tags_stub]) - tags = provider.send(:do_query,'database:active=true') + tags = provider.send(:do_query,'database:active=true',{ match_all: false }) tags.should be_a(Array) tags.first.should be_a(MachineTag::Set) @@ -164,7 +166,7 @@ with(hash_including(resource_type: 'instances', tags: ["something"])). and_return([]) - tags = provider.send(:do_query,'something') + tags = provider.send(:do_query,'something',{ match_all: false }) tags.should be_a(Array) expected_output = []