Skip to content

Commit 5b671af

Browse files
committed
Merge pull request #20 from agx/hostgroup-parents
Hostgroup parents
2 parents 1e36886 + d041cfc commit 5b671af

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

foreman_hostgroup.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ def get_resource(module, resource_type, resource_func, resource_name, search_tit
132132
return result
133133

134134

135+
def split_parent(name):
136+
"""
137+
Split hostgroup name in parent part and name:
138+
139+
>>> split_parent("a/b/c")
140+
('c', 'a/b')
141+
"""
142+
if '/' in name:
143+
parent, name = name.rsplit('/',1)
144+
else:
145+
return name, None
146+
return name, parent
147+
148+
135149
def ensure(module):
136150
# Changes in one of the following keys fails with:
137151
# <key> is not allowed as nested parameter for hostgroups. Allowed parameters are puppetclass_id, location_id, organization_id
@@ -141,7 +155,8 @@ def ensure(module):
141155
hostgroup_updateable_keys = ['puppetclass_id', 'location_id', 'organization_id']
142156

143157
changed = False
144-
name = module.params['name']
158+
full_name = module.params['name']
159+
short_name, parent_name = split_parent(full_name)
145160
architecture_name = module.params[ARCHITECTURE]
146161
compute_profile_name = module.params[COMPUTE_PROFILE]
147162
domain_name = module.params[DOMAIN]
@@ -153,7 +168,6 @@ def ensure(module):
153168
subnet_name = module.params[SUBNET]
154169
state = module.params['state']
155170
parameters = module.params['parameters']
156-
157171
foreman_host = module.params['foreman_host']
158172
foreman_port = module.params['foreman_port']
159173
foreman_user = module.params['foreman_user']
@@ -164,7 +178,7 @@ def ensure(module):
164178
username=foreman_user,
165179
password=foreman_pass)
166180

167-
data = {'name': name}
181+
data = {'title': full_name, 'name': short_name}
168182

169183
try:
170184
hostgroup = theforeman.search_hostgroup(data=data)
@@ -244,6 +258,15 @@ def ensure(module):
244258
resource_name=subnet_name)
245259
data['subnet_id'] = subnet.get('id')
246260

261+
# Parent
262+
if parent_name:
263+
environment = get_resource(module=module,
264+
resource_type=HOSTGROUP,
265+
resource_func=theforeman.search_hostgroup,
266+
search_title=True,
267+
resource_name=parent_name)
268+
data['parent_id'] = environment.get('id')
269+
247270
if not hostgroup and state == 'present':
248271
try:
249272
hostgroup = theforeman.create_hostgroup(data=data)

0 commit comments

Comments
 (0)