2 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
19 from opensteak.foreman_objects.objects import ForemanObjects
20 from opensteak.foreman_objects.itemHostsGroup import ItemHostsGroup
21 from pprint import pprint as pp
24 class HostGroups(ForemanObjects):
28 objName = 'hostgroups'
29 payloadObj = 'hostgroup'
35 @return RETURN: List of ItemHostsGroup objects
37 return list(map(lambda x: ItemHostsGroup(self.api, x['id'], x),
38 self.api.list(self.objName)))
40 def __getitem__(self, key):
41 """ Function __getitem__
44 @param key: The hostgroup name or ID
45 @return RETURN: The ItemHostsGroup object of an host
47 # Because Hostgroup did not support get by name we need to do it by id
48 if type(key) is not int:
50 ret = self.api.get(self.objName, key)
51 return ItemHostsGroup(self.api, key, ret)
53 def __delitem__(self, key):
54 """ Function __delitem__
57 @param key: The hostgroup name or ID
58 @return RETURN: The API result
60 # Because Hostgroup did not support get by name we need to do it by id
61 if type(key) is not int:
63 return self.api.delete(self.objName, key)
65 def checkAndCreate(self, key, payload,
69 """ Function checkAndCreate
70 check And Create procedure for an hostgroup
71 - check the hostgroup is not existing
72 - create the hostgroup
73 - Add puppet classes from puppetClassesId
74 - Add params from hostgroupConf
76 @param key: The hostgroup name or ID
77 @param payload: The description of the hostgroup
78 @param hostgroupConf: The configuration of the host group from the
80 @param hostgroupParent: The id of the parent hostgroup
81 @param puppetClassesId: The dict of puppet classes ids in foreman
82 @return RETURN: The ItemHostsGroup object of an host
90 # Create Hostgroup classes
91 hostgroupClassIds = self[key]['puppetclass_ids']
92 if 'classes' in hostgroupConf.keys():
93 if not self[key].checkAndCreateClasses(puppetClassesId.values()):
94 print("Failed in classes")
98 if 'params' in hostgroupConf.keys():
99 if not self[key].checkAndCreateParams(hostgroupConf['params']):
100 print("Failed in params")