Remove config_by_role in favor of search_for
config_by_role is only used in once place: openstack-compute::nova-common. And, in this single place it happens to return inconsistent results. Now that we have search_for, which does return consistent results, move this one last config_by_role call to use that. In the interest of good clean living, I am removing this code. Bumping to version 0.4.0 Change-Id: Ib755a5d568008599ee7d663f43716e7fe47251f9
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
|
||||
This file is used to list changes made in each version of cookbook-openstack-common.
|
||||
|
||||
## 0.4.0:
|
||||
* Remove `#config_by_role` as it is no longer used and no longer suits our needs.
|
||||
|
||||
## 0.3.5:
|
||||
* Reverted change made in 8311869e5b99fecefd567ce3f1ad1cbdf8d5c5c6.
|
||||
|
||||
|
||||
14
README.md
14
README.md
@@ -106,20 +106,6 @@ template "/etc/cinder/cinder.conf" do
|
||||
end
|
||||
```
|
||||
|
||||
OpenStack Role Operations
|
||||
-------------------------
|
||||
|
||||
To find a hash (or partial hash) of configuration information given a named Chef
|
||||
role, use the `Openstack::config_by_role` routine. This routine takes as its
|
||||
first parameter the name of the Chef role to look for. An optional second parameter
|
||||
is the section of the node hash to return. If nil, the whole node hash is returned.
|
||||
|
||||
```ruby
|
||||
role_name = node["openstack"]["chef_roles"]["identity_server"]
|
||||
identity_conf = ::Openstack::config_by_role(role_name)
|
||||
identity_conf_creds_section = ::Openstack::config_by_role(role_name, "creds")
|
||||
```
|
||||
|
||||
URI Operations
|
||||
--------------
|
||||
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
#
|
||||
# Cookbook Name:: openstack-common
|
||||
# library:: roles
|
||||
#
|
||||
# Copyright 2012-2013, AT&T Services, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
module ::Openstack
|
||||
# Returns the hash for a node that has the supplied
|
||||
# role in its run list. An optional section parameter
|
||||
# may be passed which limits the returned hash to just
|
||||
# the section of the node hash matching the supplied key.
|
||||
#
|
||||
# If no node is found having the supplied role, nil is
|
||||
# returned.
|
||||
def config_by_role role, section=nil
|
||||
if node.run_list.roles.include?(role)
|
||||
# If we're on a node that contains the searched-for role, just
|
||||
# return the node hash or subsection
|
||||
section.nil? ? node : node[section]
|
||||
else
|
||||
# Otherwise, let's look up the role based on the Chef environment
|
||||
# of the current node and the searched-for role
|
||||
result = search_for role
|
||||
|
||||
if result.empty?
|
||||
log("Searched for role #{role} by found no nodes with that role in run list.") { level :debug }
|
||||
nil
|
||||
else
|
||||
section.nil? ? result[0] : result[0][section]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -4,7 +4,7 @@ maintainer_email "cookbooks@lists.tfoundry.com"
|
||||
license "Apache 2.0"
|
||||
description "Common OpenStack attributes, libraries and recipes."
|
||||
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
|
||||
version "0.3.5"
|
||||
version "0.4.0"
|
||||
|
||||
recipe "openstack-common", "Installs/Configures common recipes"
|
||||
recipe "openstack-common::logging", "Installs/Configures common logging"
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
require_relative "spec_helper"
|
||||
require ::File.join ::File.dirname(__FILE__), "..", "libraries", "roles"
|
||||
|
||||
describe ::Openstack do
|
||||
before do
|
||||
@chef_run = ::ChefSpec::ChefRunner.new ::CHEFSPEC_OPTS
|
||||
@chef_run.converge "openstack-common::default"
|
||||
@subject = ::Object.new.extend ::Openstack
|
||||
end
|
||||
|
||||
describe "#config_by_role" do
|
||||
it "returns nil when section not in node hash" do
|
||||
node = @chef_run.node
|
||||
node.run_list << "role[role1]"
|
||||
@subject.stub(:node).and_return node
|
||||
|
||||
expect(@subject.config_by_role("role1", "foo")).to be_nil
|
||||
end
|
||||
|
||||
it "returns section when section in node hash" do
|
||||
::Chef::Search::Query.stub(:new)
|
||||
node = @chef_run.node
|
||||
node.run_list << "role[role1]"
|
||||
node.set['foo'] = "bar"
|
||||
@subject.stub(:node).and_return node
|
||||
|
||||
expect(@subject.config_by_role("role1", "foo")).to eq "bar"
|
||||
end
|
||||
|
||||
it "returns nil when no such role found" do
|
||||
@subject.stub(:search_for).
|
||||
with("role1").
|
||||
and_return []
|
||||
node = @chef_run.node
|
||||
node.run_list << "role[role1]"
|
||||
@subject.stub(:node).and_return node
|
||||
|
||||
expect(@subject.config_by_role("role1", "bar")).to be_nil
|
||||
end
|
||||
|
||||
it "returns section when section in first search result" do
|
||||
nodes = [
|
||||
{ "foo" => "bar" }
|
||||
]
|
||||
@subject.stub(:search_for).
|
||||
with("role1").
|
||||
and_return nodes
|
||||
@subject.stub(:node).and_return @chef_run.node
|
||||
|
||||
expect(@subject.config_by_role("role1", "foo")).to eq "bar"
|
||||
end
|
||||
|
||||
it "returns full node hash when search match but no section supplied" do
|
||||
nodes = [
|
||||
{ "foo" => "bar" }
|
||||
]
|
||||
@subject.stub(:search_for).
|
||||
with("role1").
|
||||
and_return nodes
|
||||
@subject.stub(:node).and_return @chef_run.node
|
||||
|
||||
expect(@subject.config_by_role("role1")).to eq("foo" => "bar")
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user