blob: 7adb41a8493fd7f2a8e3341bfc3c9089347cc8c3 [file] [log] [blame]
Artem Panchenko0594cd72017-06-12 13:25:26 +03001# Copyright 2017 Mirantis, Inc.
2#
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
14
15class K8sBaseResource(object):
16 """docstring for K8sBaseResource"""
17
18 def __init__(self, manager, data):
19 self._manager = manager
20 self._add_details(data)
21
22 def __repr__(self):
23 reprkeys = sorted(k
24 for k in self.__dict__.keys()
25 if k[0] != '_' and
26 k not in ['manager'])
27 info = ", ".join("%s=%s" % (k, getattr(self, k)) for k in reprkeys)
28 return "<%s %s>" % (self.__class__.__name__, info)
29
30 @property
31 def api_version(self):
32 return self._data.api_version
33
34 def _add_details(self, data):
35 self._data = data
36 for k in [k for k in dir(data)
37 if not any((k.startswith('_'), k in ('to_dict', 'to_str')))]:
38 try:
39 setattr(self, k, getattr(data, k))
40 except AttributeError:
41 # In this case we already defined the attribute on the class
42 pass
43
44 def __eq__(self, other):
45 if not isinstance(other, K8sBaseResource):
46 return NotImplemented
47 # two resources of different types are not equal
48 if not isinstance(other, self.__class__):
49 return False
50 return self._info == other._info
51
52
53class K8sBaseManager(object):
54
55 resource_class = None
56
57 def __init__(self, api, namespace):
58 self._api = api
59 self._namespace = namespace
60 self._raw = None
61
62 @property
63 def api(self):
64 return self._api
65
66 @property
67 def namespace(self):
68 return self._namespace
69
70 def get(self, *args, **kwargs):
71 if not hasattr(self, '_get'):
72 raise NotImplementedError(
73 '{} does not have {}'.format(self, '_get'))
74
75 return self.resource_class(self, self._get(*args, **kwargs))
76
77 def list(self, *args, **kwargs):
78 if not hasattr(self, '_list'):
79 raise NotImplementedError(
80 '{} does not have {}'.format(self, '_list'))
81
82 lst = self._list(*args, **kwargs)
83
84 return [self.resource_class(self, item) for item in lst.items]
85
86 def create(self, *args, **kwargs):
87 if not hasattr(self, '_create'):
88 raise NotImplementedError(
89 '{} does not have {}'.format(self, '_create'))
90 return self.resource_class(self, self._create(*args, **kwargs))
91
92 def replace(self, *args, **kwargs):
93 if not hasattr(self, '_replace'):
94 raise NotImplementedError(
95 '{} does not have {}'.format(self, '_replace'))
96 return self._replace(*args, **kwargs)
97
98 def delete(self, *args, **kwargs):
99 if not hasattr(self, '_delete'):
100 raise NotImplementedError(
101 '{} does not have {}'.format(self, '_delete'))
102 return self._delete(*args, **kwargs)
103
104 def deletecollection(self, *args, **kwargs):
105 if not hasattr(self, '_deletecollection'):
106 raise NotImplementedError(
107 '{} does not have {}'.format(self, '_deletecollection'))
108 return self._deletecollection(*args, **kwargs)