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.item import ForemanItem
25 Parent class for Foreman Objects
28 def __init__(self, api, objName=None, payloadObj=None):
30 Init the foreman object
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
41 self.objName = objName
43 self.payloadObj = payloadObj
44 # For asynchronous creations
50 @return RETURN: The iteration of objects list
52 return iter(self.list())
54 def __getitem__(self, key):
55 """ Function __getitem__
57 @param key: The targeted object
58 @return RETURN: A ForemanItem
60 return ForemanItem(self.api,
64 self.api.get(self.objName, key))
66 def __setitem__(self, key, attributes):
67 """ Function __setitem__
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
74 payload = {self.payloadObj: {'name': key}}
75 payload[self.payloadObj].update(attributes)
76 return self.api.create(self.objName, payload, async=self.async)
79 def __delitem__(self, key):
80 """ Function __delitem__
82 @return RETURN: API result
84 return self.api.delete(self.objName, key)
86 def __contains__(self, key):
87 """ Function __contains__
89 @param key: The targeted object
90 @return RETURN: True if the object exists
92 return bool(key in self.listName().keys())
96 Get the id of an object
98 @param key: The targeted object
99 @return RETURN: The ID
101 return self.api.get_id_by_name(self.objName, key)
103 def list(self, limit=20):
105 Get the list of all objects
107 @param key: The targeted object
108 @param limit: The limit of items to return
109 @return RETURN: A ForemanItem list
111 return list(map(lambda x:
112 ForemanItem(self.api, x['id'],
113 self.objName, self.payloadObj,
115 self.api.list(self.objName, limit=limit)))
118 """ Function listName
119 Get the list of all objects name with Ids
121 @param key: The targeted object
122 @return RETURN: A dict of obejct name:id
124 return self.api.list(self.objName, limit=999999, only_id=True)
126 def checkAndCreate(self, key, payload):
127 """ Function checkAndCreate
128 Check if an object exists and create it if not
130 @param key: The targeted object
131 @param payload: The targeted object description
132 @return RETURN: The id of the object
136 return self[key]['id']