summaryrefslogtreecommitdiffstats
path: root/opensteak/tools/opensteak/foreman_objects/objects.py
blob: c20c5a138a654ea39ebe860422c2d80ff026de51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Authors:
# @author: David Blaisonneau <david.blaisonneau@orange.com>
# @author: Arnaud Morin <arnaud1.morin@orange.com>

from opensteak.foreman_objects.item import ForemanItem


class ForemanObjects:
    """
    ForemanObjects class
    Parent class for Foreman Objects
    """

    def __init__(self, api, objName=None, payloadObj=None):
        """ Function __init__
        Init the foreman object

        @param api: The foreman API
        @param objName: The object name (linked with the Foreman API)
        @param payloadObj: The object name inside the payload (in general
                           the singular of objName)
        @return RETURN: Itself
        """

        self.api = api
        if objName:
            self.objName = objName
        if payloadObj:
            self.payloadObj = payloadObj
        # For asynchronous creations
        self.async = False

    def __iter__(self):
        """ Function __iter__

        @return RETURN: The iteration of objects list
        """
        return iter(self.list())

    def __getitem__(self, key):
        """ Function __getitem__

        @param key: The targeted object
        @return RETURN: A ForemanItem
        """
        return ForemanItem(self.api,
                           key,
                           self.objName,
                           self.payloadObj,
                           self.api.get(self.objName, key))

    def __setitem__(self, key, attributes):
        """ Function __setitem__

        @param key: The targeted object
        @param attributes: The attributes to apply to the object
        @return RETURN: API result if the object was not present, or False
        """
        if key not in self:
            payload = {self.payloadObj: {'name': key}}
            payload[self.payloadObj].update(attributes)
            return self.api.create(self.objName, payload, async=self.async)
        return False

    def __delitem__(self, key):
        """ Function __delitem__

        @return RETURN: API result
        """
        return self.api.delete(self.objName, key)

    def __contains__(self, key):
        """ Function __contains__

        @param key: The targeted object
        @return RETURN: True if the object exists
        """
        return bool(key in self.listName().keys())

    def getId(self, key):
        """ Function getId
        Get the id of an object

        @param key: The targeted object
        @return RETURN: The ID
        """
        return self.api.get_id_by_name(self.objName, key)

    def list(self, limit=20):
        """ Function list
        Get the list of all objects

        @param key: The targeted object
        @param limit: The limit of items to return
        @return RETURN: A ForemanItem list
        """
        return list(map(lambda x:
                        ForemanItem(self.api, x['id'],
                                    self.objName, self.payloadObj,
                                    x),
                        self.api.list(self.objName, limit=limit)))

    def listName(self):
        """ Function listName
        Get the list of all objects name with Ids

        @param key: The targeted object
        @return RETURN: A dict of obejct name:id
        """
        return self.api.list(self.objName, limit=999999, only_id=True)

    def checkAndCreate(self, key, payload):
        """ Function checkAndCreate
        Check if an object exists and create it if not

        @param key: The targeted object
        @param payload: The targeted object description
        @return RETURN: The id of the object
        """
        if key not in self:
            self[key] = payload
        return self[key]['id']