Merge "Remove Compass from genesis."
[genesis.git] / opensteak / tools / opensteak / foreman_objects / objects.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.item import ForemanItem
20
21
22 class ForemanObjects:
23     """
24     ForemanObjects class
25     Parent class for Foreman Objects
26     """
27
28     def __init__(self, api, objName=None, payloadObj=None):
29         """ Function __init__
30         Init the foreman object
31
32         @param api: The foreman API
33         @param objName: The object name (linked with the Foreman API)
34         @param payloadObj: The object name inside the payload (in general
35                            the singular of objName)
36         @return RETURN: Itself
37         """
38
39         self.api = api
40         if objName:
41             self.objName = objName
42         if payloadObj:
43             self.payloadObj = payloadObj
44         # For asynchronous creations
45         self.async = False
46
47     def __iter__(self):
48         """ Function __iter__
49
50         @return RETURN: The iteration of objects list
51         """
52         return iter(self.list())
53
54     def __getitem__(self, key):
55         """ Function __getitem__
56
57         @param key: The targeted object
58         @return RETURN: A ForemanItem
59         """
60         return ForemanItem(self.api,
61                            key,
62                            self.objName,
63                            self.payloadObj,
64                            self.api.get(self.objName, key))
65
66     def __setitem__(self, key, attributes):
67         """ Function __setitem__
68
69         @param key: The targeted object
70         @param attributes: The attributes to apply to the object
71         @return RETURN: API result if the object was not present, or False
72         """
73         if key not in self:
74             payload = {self.payloadObj: {'name': key}}
75             payload[self.payloadObj].update(attributes)
76             return self.api.create(self.objName, payload, async=self.async)
77         return False
78
79     def __delitem__(self, key):
80         """ Function __delitem__
81
82         @return RETURN: API result
83         """
84         return self.api.delete(self.objName, key)
85
86     def __contains__(self, key):
87         """ Function __contains__
88
89         @param key: The targeted object
90         @return RETURN: True if the object exists
91         """
92         return bool(key in self.listName().keys())
93
94     def getId(self, key):
95         """ Function getId
96         Get the id of an object
97
98         @param key: The targeted object
99         @return RETURN: The ID
100         """
101         return self.api.get_id_by_name(self.objName, key)
102
103     def list(self, limit=20):
104         """ Function list
105         Get the list of all objects
106
107         @param key: The targeted object
108         @param limit: The limit of items to return
109         @return RETURN: A ForemanItem list
110         """
111         return list(map(lambda x:
112                         ForemanItem(self.api, x['id'],
113                                     self.objName, self.payloadObj,
114                                     x),
115                         self.api.list(self.objName, limit=limit)))
116
117     def listName(self):
118         """ Function listName
119         Get the list of all objects name with Ids
120
121         @param key: The targeted object
122         @return RETURN: A dict of obejct name:id
123         """
124         return self.api.list(self.objName, limit=999999, only_id=True)
125
126     def checkAndCreate(self, key, payload):
127         """ Function checkAndCreate
128         Check if an object exists and create it if not
129
130         @param key: The targeted object
131         @param payload: The targeted object description
132         @return RETURN: The id of the object
133         """
134         if key not in self:
135             self[key] = payload
136         return self[key]['id']