From bc341ff1b6604ee84ef207af1cfe4ffe69e5eee0 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Fri, 25 Mar 2016 16:20:32 -0500 Subject: [PATCH 01/14] splitting query_tags into array --- libraries/machine_tag_rl10.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index 6716283..c5d2300 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -72,7 +72,7 @@ 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] ) + resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags.split(' ') ) tags_hash = {} if resources.first From cf99d80d8e42a5e0f6653ffc443837940552e78e Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Fri, 25 Mar 2016 16:28:20 -0500 Subject: [PATCH 02/14] trying code from base --- libraries/machine_tag_rl10.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index c5d2300..8867761 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -72,7 +72,8 @@ 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.split(' ') ) + query_tags = [query_tags] if query_tags.kind_of?(String) + resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags ) tags_hash = {} if resources.first From 3e3acda8f73b59da58237a495a824685fe2edeac Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Fri, 25 Mar 2016 16:41:11 -0500 Subject: [PATCH 03/14] adding in options --- libraries/machine_tag_rl10.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index 8867761..9edf895 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -71,9 +71,9 @@ def list # # @return [Array] the tags on the servers that match the query # - def do_query(query_tags) + def do_query(query_tags, options = {}) query_tags = [query_tags] if query_tags.kind_of?(String) - resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags ) + resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags, options) tags_hash = {} if resources.first From b7d1cae62bd4c38cfe546add504bfd81b1b9e6f0 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Fri, 25 Mar 2016 16:45:57 -0500 Subject: [PATCH 04/14] adding match_all --- libraries/machine_tag_rl10.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index 9edf895..658e2b3 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -73,7 +73,8 @@ def list # def do_query(query_tags, options = {}) query_tags = [query_tags] if query_tags.kind_of?(String) - resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags, options) + match_all = options[:match_all] ? false + resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags, match_all: match_all) tags_hash = {} if resources.first From 9ba4ebb514f85f067ae3e4d4c870c7a23a63f7e1 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Fri, 25 Mar 2016 16:53:11 -0500 Subject: [PATCH 05/14] adding matchall --- libraries/machine_tag_rl10.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index 658e2b3..5465c6e 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -73,7 +73,7 @@ def list # def do_query(query_tags, options = {}) query_tags = [query_tags] if query_tags.kind_of?(String) - match_all = options[:match_all] ? false + match_all = !options[:match_all].nil? ? options[:match_all] : false resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags, match_all: match_all) tags_hash = {} From 6eac5a0d2596a8e08a3bb6c5d0a0ef2c9aebeba2 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Mon, 28 Mar 2016 10:11:49 -0500 Subject: [PATCH 06/14] adding logging to options --- libraries/machine_tag_rl10.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index 5465c6e..fcff64d 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -73,6 +73,7 @@ def list # def do_query(query_tags, options = {}) query_tags = [query_tags] if query_tags.kind_of?(String) + Chef::Log.info "Tag Query Options: #{options.inspect}" match_all = !options[:match_all].nil? ? options[:match_all] : false resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags, match_all: match_all) From b0f927b68f1be252c309779b321b8b93a4446a74 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Mon, 28 Mar 2016 10:37:09 -0500 Subject: [PATCH 07/14] only returning operational servers --- libraries/machine_tag_rl10.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index fcff64d..c9008c6 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -81,12 +81,14 @@ def do_query(query_tags, options = {}) 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| + 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 = [] From 97dd27ceb80e3149bb9933678a50f69576b914e7 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Mon, 28 Mar 2016 10:53:43 -0500 Subject: [PATCH 08/14] removing options since operational is implemented --- libraries/machine_tag_rl10.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index c9008c6..fb99d3b 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -71,11 +71,9 @@ def list # # @return [Array] the tags on the servers that match the query # - def do_query(query_tags, options = {}) + def do_query(query_tags) query_tags = [query_tags] if query_tags.kind_of?(String) - Chef::Log.info "Tag Query Options: #{options.inspect}" - match_all = !options[:match_all].nil? ? options[:match_all] : false - resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags, match_all: match_all) + resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags) tags_hash = {} if resources.first From 422b13495a7a8e0396998eb74c16b29362af3a0e Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Mon, 28 Mar 2016 11:41:55 -0500 Subject: [PATCH 09/14] adding resource state --- libraries/machine_tag_rl10.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index fb99d3b..823534c 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -80,6 +80,7 @@ def do_query(query_tags) links = resources.first.links if links 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"]]={ From 636f393af7b0a2ef355be089df89eb8f2b780418 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Mon, 28 Mar 2016 11:46:12 -0500 Subject: [PATCH 10/14] adding more logging --- libraries/machine_tag_rl10.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/machine_tag_rl10.rb b/libraries/machine_tag_rl10.rb index 823534c..0a2353a 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -73,8 +73,10 @@ def list # def do_query(query_tags) query_tags = [query_tags] if query_tags.kind_of?(String) + Chef::Log.info "Tagged query_tags: #{query_tags}" resources = api_client.tags.by_tag(resource_type: 'instances', tags: query_tags) - + Chef::Log.info "Tagged resources: #{resources}" + tags_hash = {} if resources.first links = resources.first.links From cd22e8467815cc0043128c4a21c4ae01146ec91a Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Mon, 28 Mar 2016 15:53:19 -0500 Subject: [PATCH 11/14] adding match_all to string --- libraries/machine_tag_base.rb | 6 +++--- libraries/machine_tag_rightscale.rb | 2 +- libraries/machine_tag_rl10.rb | 5 +++-- libraries/machine_tag_vagrant.rb | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) 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 0a2353a..d02f4d8 100644 --- a/libraries/machine_tag_rl10.rb +++ b/libraries/machine_tag_rl10.rb @@ -71,10 +71,11 @@ def list # # @return [Array] the tags on the servers that match the query # - def do_query(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}" - resources = api_client.tags.by_tag(resource_type: 'instances', 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 = {} 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 From ce9cadfabc22ca80f17d5a91b305d9695c66ae24 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Wed, 30 Mar 2016 15:18:46 -0500 Subject: [PATCH 12/14] updating cookbook version, changelog, and travis --- .travis.yml | 3 +-- CHANGELOG.md | 10 ++++++++++ metadata.rb | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) 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/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' From 915ed35c73057beb212f310da29ac4c05f3589a4 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Wed, 30 Mar 2016 15:58:49 -0500 Subject: [PATCH 13/14] fixing tests --- spec/unit_test/machine_tag_base_spec.rb | 10 +++++----- spec/unit_test/machine_tag_rl10_spec.rb | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) 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..778f47a 100644 --- a/spec/unit_test/machine_tag_rl10_spec.rb +++ b/spec/unit_test/machine_tag_rl10_spec.rb @@ -121,15 +121,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 +164,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 = [] From 5fef9d4ff75f28902c91c25a591b6ad3135bf0b6 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Wed, 30 Mar 2016 16:12:23 -0500 Subject: [PATCH 14/14] adding last test for operational --- spec/unit_test/machine_tag_rl10_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/unit_test/machine_tag_rl10_spec.rb b/spec/unit_test/machine_tag_rl10_spec.rb index 778f47a..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