Skip to content

Commit

Permalink
Merge pull request #29 from rightscale-cookbooks/PSASSET-479
Browse files Browse the repository at this point in the history
checking to see if they belong to the same cloud
  • Loading branch information
cdwilhelm committed Apr 12, 2016
2 parents c6e2a79 + d045fb5 commit db9e2b2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ machine_tag Cookbook CHANGELOG

This file is used to list changes made in each version of the machine_tag cookbook.

v1.2.1
------
- Only returns operational instances
- Only returns instances in the same cloud as the machine executing the call.

v1.2.0
------

Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ master.vm.host_name = "master"
See `Vagrantfile` for an example.

## RightScale Environment

### RightLink 6
For using this resource in a *RightScale Environment*, the system must be a
RightScale managed VM to have the required access to the [rs_tag utility][rs_tag].

[rs_tag]: http://support.rightscale.com/12-Guides/RightLink/01-RightLink_Overview/RightLink_Command_Line_Utilities#rs_tag

### RightLink 10
For using this resource in a *RightScale Environment*, the system must be a
RightScale managed VM to have the required access to the right_api_client
with proxy_mode for instance_tokens, the scope is now limited to
operational instances in the same cloud as the VM making the call.

# Usage

Expand Down
14 changes: 8 additions & 6 deletions libraries/machine_tag_rl10.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ def do_query(query_tags, options = {})
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"]]={
"tags"=> resource_tags.first.tags.map{|tag| tag["name"]}
}
Chef::Log.info "Tagged Resource Cloud:#{link["href"].split('/')[0..3].join('/')}"
if api_client.get_instance.show.cloud.href == link["href"].split('/')[0..3].join('/')
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
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.2.0'
version '1.2.1'

depends 'apt', '~> 2.9.2'
depends 'build-essential'
Expand Down
24 changes: 21 additions & 3 deletions spec/unit_test/machine_tag_rl10_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
client = double('RightApi::Client', :log => nil)
client.stub(:get_instance).and_return(instance_stub)
client.stub_chain(:resource,:state).and_return('operational')
client.stub_chain(:get_instance,:show,:cloud,:href).and_return('/api/clouds/6')
client
end

Expand All @@ -81,7 +82,12 @@

let(:resources_stub) {
double('resources',
:links=>[{"href"=>"/some_href"}],
:links=>[{"href"=>"/api/clouds/6/instances/1234"}],
)}

let(:resources_stub_fail) {
double('resources',
:links=>[{"href"=>"/api/clouds/1/instances/1234"}],
)}

let(:resource_tags_stub) { double('tag_resources', :tags=> rs_raw_output) }
Expand Down Expand Up @@ -127,10 +133,9 @@
and_return([resources_stub])

client_stub.tags.should_receive(:by_resource).
with(hash_including(resource_hrefs: ["/some_href"])).
with(hash_including(resource_hrefs: ["/api/clouds/6/instances/1234"])).
and_return([resource_tags_stub])


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 @@ -173,5 +178,18 @@

tags.should == expected_output
end
it "should raise error because of different cloud" do
client_stub.tags.should_receive(:by_tag).
with(hash_including(resource_type: 'instances', tags: ['database:active=true'], match_all: false)).
and_return([resources_stub_fail])

client_stub.tags.should_not_receive(:by_resource).
with(hash_including(resource_hrefs: ["/api/clouds/6/instances/1234"]))

tags = provider.send(:do_query,'database:active=true',{ match_all: false })
tags.should be_a(Array)
tags.should == []

end
end
end

0 comments on commit db9e2b2

Please sign in to comment.