Page MenuHomeDevCentral

No OneTemporary

diff --git a/_modules/node.py b/_modules/node.py
index b5621b8..4a88bc6 100644
--- a/_modules/node.py
+++ b/_modules/node.py
@@ -1,178 +1,208 @@
# -*- coding: utf-8 -*-
# -------------------------------------------------------------
# Salt — Node execution module
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Created: 2017-10-21
# Description: Functions related to the nodes pillar entry
# License: BSD-2-Clause
# -------------------------------------------------------------
from salt.exceptions import CommandExecutionError, SaltCloudConfigError
def _get_all_nodes():
return __pillar__.get('nodes', {})
def get_all_properties(nodename=None):
'''
A function to get a node pillar configuration.
CLI Example:
salt * node.get_all_properties
'''
if nodename is None:
nodename = __grains__['id']
all_nodes = _get_all_nodes()
if nodename not in all_nodes:
raise CommandExecutionError(
SaltCloudConfigError(
"Node {0} not declared in pillar.".format(nodename)
)
)
return all_nodes[nodename]
def get(key, nodename=None):
'''
A function to get a node pillar configuration key.
CLI Example:
salt * node.get hostname
'''
return _get_property(key, nodename, None)
def _explode_key(k): return k.split(':')
def _get_first_key(k): return _explode_key(k)[0]
def _strip_first_key(k): return ':'.join(_explode_key(k)[1:])
def _get_property(key, nodename, default_value, parent=None):
if parent is None:
parent = get_all_properties(nodename)
if ':' in key:
first_key = _get_first_key(key)
if first_key in parent:
return _get_property(
_strip_first_key(key), nodename,
default_value, parent[first_key]
)
elif key in parent:
return parent[key]
return default_value
def list(key, nodename=None):
'''
A function to get a node pillar configuration.
Returns a list if found, or an empty list if not found.
CLI Example:
salt * node.list network:ipv4_aliases
'''
return _get_property(key, nodename, [])
def has(key, nodename=None):
'''
A function to get a node pillar configuration.
Returns a boolean, False if not found.
CLI Example:
salt * node.has network:ipv6_tunnel
'''
value = _get_property(key, nodename, False)
return bool(value)
def has_role(role, nodename=None):
'''
A function to determine if a node has the specified role.
Returns a boolean, False if not found.
CLI Example:
salt * node.has_role devserver
'''
return role in list('roles', nodename)
def filter_by_role(pillar_key, nodename=None):
'''
A function to filter a dictionary by roles.
The dictionary must respect the following structure:
- keys are role to check the current node against
- values are list of items
If a key '*' is also present, it will be included
for every role.
Returns a list, extending all the filtered lists.
CLI Example:
salt * node.filter_by_role web_content_sls
'''
roles = list('roles', nodename)
dictionary = __pillar__.get(pillar_key, {})
filtered_list = []
for role, items in dictionary.items():
if role == '*' or role in roles:
filtered_list.extend(items)
return filtered_list
+def filter_by_name(pillar_key, nodename=None):
+ '''
+ A function to filter a dictionary by node name.
+
+ The dictionary must respect the following structure:
+ - keys are names to check the current node against
+ - values are list of items
+
+ If a key '*' is also present, it will be included
+ for every node.
+
+ Returns a list, extending all the filtered lists.
+
+ CLI Example:
+
+ salt * node.filter_by_name mars
+ '''
+ if nodename is None:
+ nodename = __grains__['id']
+
+ dictionary = __pillar__.get(pillar_key, {})
+ filtered_list = []
+
+ for name, items in dictionary.items():
+ if name == '*' or name == nodename:
+ filtered_list.extend(items)
+
+ return filtered_list
+
+
def has_web_content(content, nodename=None):
return content in filter_by_role('web_content_sls', nodename)
def get_wwwroot(nodename=None):
'''
A function to determine the wwwroot folder to use.
Returns a string depending of the FQDN.
CLI Example:
salt * node.get_wwwroot
'''
hostname = _get_property("hostname", nodename, None)
if hostname is None:
raise CommandExecutionError(
SaltCloudConfigError(
"Node {0} doesn't have a hostname property".format(nodename)
)
)
if hostname.count('.') < 2:
return "wwwroot/{0}/www".format(hostname)
fqdn = hostname.split(".")
return "wwwroot/{1}/{0}".format(
".".join(fqdn[0:-2]),
".".join(fqdn[-2:])
)
diff --git a/_tests/data/forests.yaml b/_tests/data/forests.yaml
index f2e53f4..96b3298 100644
--- a/_tests/data/forests.yaml
+++ b/_tests/data/forests.yaml
@@ -1,55 +1,65 @@
forests:
- brethil
- fangorn
- lothlorien
nodes:
egladil:
forest: lothlorien
hostname: egladil.lothlorien.forest
roles:
- treecity
entwash:
forest: fangorn
hostname: entwash.node
roles:
- border
items_by_role:
treecity:
- Caras Galadhon
border:
- Onodlo
items_by_role_with_star:
'*':
- Air
treecity:
- Caras Galadhon
border:
- Onodlo
+items_by_name:
+ egladil:
+ - Caras Galadhon
+
+items_by_name_with_star:
+ '*':
+ - Air
+ egladil:
+ - Caras Galadhon
+
shellgroups_ubiquity:
- ubiquity
shellgroups_by_forest:
lothlorien:
- caras_galadhon
shellgroups:
ubiquity: {}
caras_galadhon:
members:
- amdir
- amroth
shellusers:
amdir: {}
amroth: {}
galadriel: {}
web_content_sls:
treecity:
- .ll/carasgaladhon
border:
- .arda/onodlo
diff --git a/_tests/modules/test_node.py b/_tests/modules/test_node.py
index 0dc8860..1d3ce6a 100644
--- a/_tests/modules/test_node.py
+++ b/_tests/modules/test_node.py
@@ -1,81 +1,118 @@
import imp
import unittest
salt_test_case = imp.load_source('salt_test_case', "salt_test_case.py")
node = imp.load_source('node', "../_modules/node.py")
class Testinstance(unittest.TestCase, salt_test_case.SaltTestCase):
def setUp(self):
self.initialize_mocks()
self.instance = node
self.mock_pillar('data/forests.yaml')
self.mock_grains()
self.grains['id'] = 'egladil'
def test_get_wwwroot(self):
self.assertEqual("wwwroot/lothlorien.forest/egladil",
node.get_wwwroot())
self.assertEqual("wwwroot/entwash.node/www",
node.get_wwwroot('entwash'))
def test_has_web_content(self):
self.assertTrue(node.has_web_content('.ll/carasgaladhon'))
self.assertFalse(node.has_web_content('.arda/onodlo'))
self.assertTrue(node.has_web_content('.arda/onodlo', 'entwash'))
self.assertFalse(node.has_web_content('notexisting'))
def test_filter_by_role(self):
node_key = self.grains['id']
self.assertEqual(['Caras Galadhon'],
node.filter_by_role('items_by_role'))
self.assertEqual(['Onodlo'],
node.filter_by_role('items_by_role', 'entwash'))
# No role
self.pillar['nodes'][node_key]['roles'] = []
self.assertEqual([],
node.filter_by_role('items_by_role'))
# More than one role
self.pillar['nodes'][node_key]['roles'] = [
'border',
'treecity'
]
self.assertEqual(['Caras Galadhon', 'Onodlo'],
sorted(node.filter_by_role('items_by_role')))
def test_filter_by_role_with_star(self):
node_key = self.grains['id']
self.assertEqual(['Air', 'Caras Galadhon'],
node.filter_by_role('items_by_role_with_star'))
self.assertEqual(['Air', 'Onodlo'],
node.filter_by_role(
'items_by_role_with_star',
'entwash'
))
# No role
self.pillar['nodes'][node_key]['roles'] = []
self.assertEqual(['Air'],
node.filter_by_role('items_by_role_with_star'))
# More than one role
self.pillar['nodes'][node_key]['roles'] = [
'border',
'treecity'
]
self.assertEqual(
['Air', 'Caras Galadhon', 'Onodlo'],
sorted(node.filter_by_role('items_by_role_with_star'))
)
+
+ def test_filter_by_name(self):
+ node_key = self.grains['id']
+ self.assertEqual(
+ ['Caras Galadhon'],
+ node.filter_by_name('items_by_name')
+ )
+
+ self.assertEqual(
+ ['Caras Galadhon'],
+ node.filter_by_name('items_by_name', 'egladil')
+ )
+
+ self.grains['id'] = 'entwash'
+ self.assertEqual(
+ [],
+ node.filter_by_name('items_by_name')
+ )
+
+
+ def test_filter_by_name_with_star(self):
+ node_key = self.grains['id']
+ self.assertEqual(
+ ['Air', 'Caras Galadhon'],
+ node.filter_by_name('items_by_name_with_star')
+ )
+
+ self.assertEqual(
+ ['Air', 'Caras Galadhon'],
+ node.filter_by_name('items_by_name_with_star', 'egladil')
+ )
+
+ self.grains['id'] = 'entwash'
+ self.assertEqual(
+ ['Air'],
+ node.filter_by_name('items_by_name_with_star')
+ )

File Metadata

Mime Type
text/x-diff
Expires
Thu, Dec 26, 03:18 (19 h, 38 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2312609
Default Alt Text
(9 KB)

Event Timeline