Merge "Remove Compass from genesis."
[genesis.git] / opensteak / tools / opensteak / foreman_objects / hostgroups.py
1 #!/usr/bin/python3
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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
13 # under the License.
14 #
15 # Authors:
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
18
19 from opensteak.foreman_objects.objects import ForemanObjects
20 from opensteak.foreman_objects.itemHostsGroup import ItemHostsGroup
21 from pprint import pprint as pp
22
23
24 class HostGroups(ForemanObjects):
25     """
26     HostGroups class
27     """
28     objName = 'hostgroups'
29     payloadObj = 'hostgroup'
30
31     def list(self):
32         """ Function list
33         list the hostgroups
34
35         @return RETURN: List of ItemHostsGroup objects
36         """
37         return list(map(lambda x: ItemHostsGroup(self.api, x['id'], x),
38                         self.api.list(self.objName)))
39
40     def __getitem__(self, key):
41         """ Function __getitem__
42         Get an hostgroup
43
44         @param key: The hostgroup name or ID
45         @return RETURN: The ItemHostsGroup object of an host
46         """
47         # Because Hostgroup did not support get by name we need to do it by id
48         if type(key) is not int:
49             key = self.getId(key)
50         ret = self.api.get(self.objName, key)
51         return ItemHostsGroup(self.api, key, ret)
52
53     def __delitem__(self, key):
54         """ Function __delitem__
55         Delete an hostgroup
56
57         @param key: The hostgroup name or ID
58         @return RETURN: The API result
59         """
60         # Because Hostgroup did not support get by name we need to do it by id
61         if type(key) is not int:
62             key = self.getId(key)
63         return self.api.delete(self.objName, key)
64
65     def checkAndCreate(self, key, payload,
66                        hostgroupConf,
67                        hostgroupParent,
68                        puppetClassesId):
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
75
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
79                               foreman.conf
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
83         """
84         if key not in self:
85             self[key] = payload
86         oid = self[key]['id']
87         if not oid:
88             return False
89
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")
95                 return False
96
97         # Set params
98         if 'params' in hostgroupConf.keys():
99             if not self[key].checkAndCreateParams(hostgroupConf['params']):
100                 print("Failed in params")
101                 return False
102
103         return oid