Module: ChefSpec::ServerMethods
- Included in:
- ServerRunner
- Defined in:
- lib/chefspec/server_methods.rb
Overview
This module contains the list of methods that are specific to creating and managing resources within an Chef Zero instance. It is designed to be included in a class which exposes a ‘server` instance variable or method that returns a ChefZero::Server instance.
Class Method Summary collapse
Instance Method Summary collapse
-
#client(name) ⇒ Chef::Client?
Find a client at the given name.
-
#clients ⇒ Array<Hash>
The list of client on the Chef Server.
-
#create_client(name, data = {}) ⇒ Object
Create a new client on the Chef Server.
-
#create_data_bag(name, data = {}) ⇒ Object
Create a new data_bag on the Chef Server.
-
#create_environment(name, data = {}) ⇒ Object
Create a new environment on the Chef Server.
-
#create_node(object, data = {}) ⇒ Object
(also: #update_node)
Create a new node on the Chef Server.
-
#create_role(name, data = {}) ⇒ Object
Create a new role on the Chef Server.
-
#data ⇒ Array<Hash>
The list of data_bag on the Chef Server.
-
#data_bag(name) ⇒ Chef::DataBag?
Find a data_bag at the given name.
-
#environment(name) ⇒ Chef::Environment?
Find a environment at the given name.
-
#environments ⇒ Array<Hash>
The list of environment on the Chef Server.
-
#get(*args) ⇒ Object
Get the path to an item in the data store.
-
#has_client?(name) ⇒ true, false
Determine if the Chef Server has the given client.
-
#has_data_bag?(name) ⇒ true, false
Determine if the Chef Server has the given data_bag.
-
#has_environment?(name) ⇒ true, false
Determine if the Chef Server has the given environment.
-
#has_node?(name) ⇒ true, false
Determine if the Chef Server has the given node.
-
#has_role?(name) ⇒ true, false
Determine if the Chef Server has the given role.
-
#load_data(name, key, data = {}) ⇒ Object
Shortcut method for loading data into Chef Zero.
-
#node(name) ⇒ Chef::Node?
Find a node at the given name.
-
#nodes ⇒ Array<Hash>
The list of node on the Chef Server.
-
#role(name) ⇒ Chef::Role?
Find a role at the given name.
-
#roles ⇒ Array<Hash>
The list of role on the Chef Server.
-
#server ⇒ ChefZero::Server
The actual Chef Zero Server object.
Class Method Details
.entity(method, klass, key) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/chefspec/server_methods.rb', line 51 def self.entity(method, klass, key) class_eval <<-EOH, __FILE__, __LINE__ + 1 def create_#{method}(name, data = {}) # Automatically set the "name" if no explicit one was given data[:name] ||= name # Convert it to JSON data = JSON.fast_generate(data) load_data(name, '#{key}', data) end def get_#{method}(name) data = get('#{key}', name) json = JSON.parse(data) case when #{klass}.respond_to?(:json_create) #{klass}.json_create(json) when #{klass}.respond_to?(:from_hash) #{klass}.from_hash(json) else #{klass}.new(json) end rescue ChefZero::DataStore::DataNotFoundError nil end def get_#{key} get('#{key}') end def has_#{method}?(name) !get('#{key}', name).nil? rescue ChefZero::DataStore::DataNotFoundError false end EOH end |
Instance Method Details
#client(name) ⇒ Chef::Client?
Find a client at the given name
91 |
# File 'lib/chefspec/server_methods.rb', line 91 entity :client, Chef::Client, "clients" |
#clients ⇒ Array<Hash>
The list of client on the Chef Server
91 |
# File 'lib/chefspec/server_methods.rb', line 91 entity :client, Chef::Client, "clients" |
#create_client(name, data = {}) ⇒ Object
Create a new client on the Chef Server
91 |
# File 'lib/chefspec/server_methods.rb', line 91 entity :client, Chef::Client, "clients" |
#create_data_bag(name, data = {}) ⇒ Object
Create a new data_bag on the Chef Server. This overrides the method created by entity
106 |
# File 'lib/chefspec/server_methods.rb', line 106 entity :data_bag, Chef::DataBag, "data" |
#create_environment(name, data = {}) ⇒ Object
Create a new environment on the Chef Server
93 |
# File 'lib/chefspec/server_methods.rb', line 93 entity :environment, Chef::Environment, "environments" |
#create_node(object, data = {}) ⇒ Object Also known as: update_node
Create a new node on the Chef Server. This overrides the method created by entity, permitting users to pass a raw Chef::Node
object in addition to a hash.
131 |
# File 'lib/chefspec/server_methods.rb', line 131 entity :node, Chef::Node, "nodes" |
#create_role(name, data = {}) ⇒ Object
Create a new role on the Chef Server
95 |
# File 'lib/chefspec/server_methods.rb', line 95 entity :role, Chef::Role, "roles" |
#data ⇒ Array<Hash>
The list of data_bag on the Chef Server
92 |
# File 'lib/chefspec/server_methods.rb', line 92 entity :data_bag, Chef::DataBag, "data" |
#data_bag(name) ⇒ Chef::DataBag?
Find a data_bag at the given name
92 |
# File 'lib/chefspec/server_methods.rb', line 92 entity :data_bag, Chef::DataBag, "data" |
#environment(name) ⇒ Chef::Environment?
Find a environment at the given name
93 |
# File 'lib/chefspec/server_methods.rb', line 93 entity :environment, Chef::Environment, "environments" |
#environments ⇒ Array<Hash>
The list of environment on the Chef Server
93 |
# File 'lib/chefspec/server_methods.rb', line 93 entity :environment, Chef::Environment, "environments" |
#get(*args) ⇒ Object
Get the path to an item in the data store.
163 164 165 166 167 168 169 170 171 |
# File 'lib/chefspec/server_methods.rb', line 163 def get(*args) args.unshift("organizations", "chef") if args.size == 3 server.data_store.list(args) else server.data_store.get(args) end end |
#has_client?(name) ⇒ true, false
Determine if the Chef Server has the given client
91 |
# File 'lib/chefspec/server_methods.rb', line 91 entity :client, Chef::Client, "clients" |
#has_data_bag?(name) ⇒ true, false
Determine if the Chef Server has the given data_bag
92 |
# File 'lib/chefspec/server_methods.rb', line 92 entity :data_bag, Chef::DataBag, "data" |
#has_environment?(name) ⇒ true, false
Determine if the Chef Server has the given environment
93 |
# File 'lib/chefspec/server_methods.rb', line 93 entity :environment, Chef::Environment, "environments" |
#has_node?(name) ⇒ true, false
Determine if the Chef Server has the given node
94 |
# File 'lib/chefspec/server_methods.rb', line 94 entity :node, Chef::Node, "nodes" |
#has_role?(name) ⇒ true, false
Determine if the Chef Server has the given role
95 |
# File 'lib/chefspec/server_methods.rb', line 95 entity :role, Chef::Role, "roles" |
#load_data(name, key, data = {}) ⇒ Object
Shortcut method for loading data into Chef Zero.
156 157 158 |
# File 'lib/chefspec/server_methods.rb', line 156 def load_data(name, key, data = {}) ChefSpec::ZeroServer.load_data(name, key, data) end |
#node(name) ⇒ Chef::Node?
Find a node at the given name
94 |
# File 'lib/chefspec/server_methods.rb', line 94 entity :node, Chef::Node, "nodes" |
#nodes ⇒ Array<Hash>
The list of node on the Chef Server
94 |
# File 'lib/chefspec/server_methods.rb', line 94 entity :node, Chef::Node, "nodes" |
#role(name) ⇒ Chef::Role?
Find a role at the given name
95 |
# File 'lib/chefspec/server_methods.rb', line 95 entity :role, Chef::Role, "roles" |
#roles ⇒ Array<Hash>
The list of role on the Chef Server
95 |
# File 'lib/chefspec/server_methods.rb', line 95 entity :role, Chef::Role, "roles" |
#server ⇒ ChefZero::Server
The actual Chef Zero Server object.
12 13 14 |
# File 'lib/chefspec/server_methods.rb', line 12 def server ChefSpec::ZeroServer.server end |