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
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 90 91 92 |
# File 'lib/chefspec/server_methods.rb', line 57 def self.entity(method, klass, key) class_eval " def create_\#{method}(name, data = {})\n # Automatically set the \"name\" if no explicit one was given\n data[:name] ||= name\n\n # Convert it to JSON\n data = JSON.fast_generate(data)\n\n load_data(name, '\#{key}', data)\n end\n\n def get_\#{method}(name)\n data = get('\#{key}', name)\n json = JSON.parse(data)\n\n if \#{klass}.respond_to?(:json_create)\n \#{klass}.json_create(json)\n else\n \#{klass}.new(json)\n end\n rescue ChefZero::DataStore::DataNotFoundError\n nil\n end\n\n def get_\#{key}\n get('\#{key}')\n end\n\n def has_\#{method}?(name)\n !get('\#{key}', name).nil?\n rescue ChefZero::DataStore::DataNotFoundError\n false\n end\n EOH\nend\n", __FILE__, __LINE__ + 1 |
Instance Method Details
#client(name) ⇒ Chef::Client?
Find a client at the given name
94 |
# File 'lib/chefspec/server_methods.rb', line 94 entity :client, Chef::Client, 'clients' |
#clients ⇒ Array<Hash>
The list of client on the Chef Server
94 |
# File 'lib/chefspec/server_methods.rb', line 94 entity :client, Chef::Client, 'clients' |
#create_client(name, data = {}) ⇒ Object
Create a new client on the Chef Server
94 |
# File 'lib/chefspec/server_methods.rb', line 94 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
109 |
# File 'lib/chefspec/server_methods.rb', line 109 entity :data_bag, Chef::DataBag, 'data' |
#create_environment(name, data = {}) ⇒ Object
Create a new environment on the Chef Server
96 |
# File 'lib/chefspec/server_methods.rb', line 96 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.
134 |
# File 'lib/chefspec/server_methods.rb', line 134 entity :node, Chef::Node, 'nodes' |
#create_role(name, data = {}) ⇒ Object
Create a new role on the Chef Server
98 |
# File 'lib/chefspec/server_methods.rb', line 98 entity :role, Chef::Role, 'roles' |
#data ⇒ Array<Hash>
The list of data_bag on the Chef Server
95 |
# File 'lib/chefspec/server_methods.rb', line 95 entity :data_bag, Chef::DataBag, 'data' |
#data_bag(name) ⇒ Chef::DataBag?
Find a data_bag at the given name
95 |
# File 'lib/chefspec/server_methods.rb', line 95 entity :data_bag, Chef::DataBag, 'data' |
#environment(name) ⇒ Chef::Environment?
Find a environment at the given name
96 |
# File 'lib/chefspec/server_methods.rb', line 96 entity :environment, Chef::Environment, 'environments' |
#environments ⇒ Array<Hash>
The list of environment on the Chef Server
96 |
# File 'lib/chefspec/server_methods.rb', line 96 entity :environment, Chef::Environment, 'environments' |
#get(*args) ⇒ Object
Get the path to an item in the data store.
166 167 168 169 170 171 172 173 174 |
# File 'lib/chefspec/server_methods.rb', line 166 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
94 |
# File 'lib/chefspec/server_methods.rb', line 94 entity :client, Chef::Client, 'clients' |
#has_data_bag?(name) ⇒ true, false
Determine if the Chef Server has the given data_bag
95 |
# File 'lib/chefspec/server_methods.rb', line 95 entity :data_bag, Chef::DataBag, 'data' |
#has_environment?(name) ⇒ true, false
Determine if the Chef Server has the given environment
96 |
# File 'lib/chefspec/server_methods.rb', line 96 entity :environment, Chef::Environment, 'environments' |
#has_node?(name) ⇒ true, false
Determine if the Chef Server has the given node
97 |
# File 'lib/chefspec/server_methods.rb', line 97 entity :node, Chef::Node, 'nodes' |
#has_role?(name) ⇒ true, false
Determine if the Chef Server has the given role
98 |
# File 'lib/chefspec/server_methods.rb', line 98 entity :role, Chef::Role, 'roles' |
#load_data(name, key, data = {}) ⇒ Object
Shortcut method for loading data into Chef Zero.
159 160 161 |
# File 'lib/chefspec/server_methods.rb', line 159 def load_data(name, key, data = {}) @server.load_data({ key => { name => data } }) end |
#node(name) ⇒ Chef::Node?
Find a node at the given name
97 |
# File 'lib/chefspec/server_methods.rb', line 97 entity :node, Chef::Node, 'nodes' |
#nodes ⇒ Array<Hash>
The list of node on the Chef Server
97 |
# File 'lib/chefspec/server_methods.rb', line 97 entity :node, Chef::Node, 'nodes' |
#role(name) ⇒ Chef::Role?
Find a role at the given name
98 |
# File 'lib/chefspec/server_methods.rb', line 98 entity :role, Chef::Role, 'roles' |
#roles ⇒ Array<Hash>
The list of role on the Chef Server
98 |
# File 'lib/chefspec/server_methods.rb', line 98 entity :role, Chef::Role, 'roles' |
#server ⇒ ChefZero::Server
The actual Chef Zero Server object.
12 13 14 15 16 17 18 19 20 |
# File 'lib/chefspec/server_methods.rb', line 12 def server @server ||= ChefZero::Server.new( # Set the log level from RSpec, defaulting to warn log_level: RSpec.configuration.log_level || :warn, # Set a random port so ChefSpec may be run in multiple contexts port: port, ) end |