Skip to content

Commit

Permalink
Merge pull request #27 from rightscale-cookbooks/match_all
Browse files Browse the repository at this point in the history
adding match_all to string
  • Loading branch information
cdwilhelm committed Mar 30, 2016
2 parents 636f393 + 5fef9d4 commit 7d3b2e5
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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=
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------

Expand Down
6 changes: 3 additions & 3 deletions libraries/machine_tag_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -107,7 +107,7 @@ def search(query_tags, options = {})
#
# @return [Array<MachineTag::Set>] 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

Expand Down
2 changes: 1 addition & 1 deletion libraries/machine_tag_rightscale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def list
#
# @return [Array<MachineTag::Set>] 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|
Expand Down
5 changes: 3 additions & 2 deletions libraries/machine_tag_rl10.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ def list
#
# @return [Array<MachineTag::Set>] 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 = {}
Expand Down
2 changes: 1 addition & 1 deletion libraries/machine_tag_vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def list
#
# @return [Array<MachineTag::Set>] 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
Expand Down
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 5 additions & 5 deletions spec/unit_test/machine_tag_base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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],
Expand All @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions spec/unit_test/machine_tag_rl10_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -82,6 +83,7 @@
double('resources',
:links=>[{"href"=>"/some_href"}],
)}

let(:resource_tags_stub) { double('tag_resources', :tags=> rs_raw_output) }

before(:each) do
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit 7d3b2e5

Please sign in to comment.