10
10
from typing import Generator , List
11
11
12
12
from c8y_api ._base_api import CumulocityRestApi
13
+ from c8y_api .model .applications import Application
13
14
from c8y_api .model ._base import SimpleObject , CumulocityResource
14
15
from c8y_api .model ._parser import SimpleObjectParser
15
- from model import Application
16
16
17
17
18
18
class Tenant (SimpleObject ):
@@ -75,8 +75,8 @@ def __init__(self,
75
75
self .creation_time = None
76
76
self .parent = None
77
77
self .status = None
78
- self ._applications = None
79
- self ._owned_applications = None
78
+ self ._applications = []
79
+ self ._owned_applications = []
80
80
81
81
domain = SimpleObject .UpdatableProperty ('_u_domain' )
82
82
admin_name = SimpleObject .UpdatableProperty ('_u_admin_name' )
@@ -102,9 +102,8 @@ def applications(self) -> Generator[Application]:
102
102
Returns:
103
103
A Generator for Application instances.
104
104
"""
105
- if self ._applications :
106
- for application_json in self ._applications :
107
- yield Application .from_json (application_json )
105
+ for application_json in self ._applications :
106
+ yield Application .from_json (application_json )
108
107
109
108
@property
110
109
def all_applications (self ) -> List [Application ]:
@@ -113,7 +112,7 @@ def all_applications(self) -> List[Application]:
113
112
Returns:
114
113
A list of Application instances.
115
114
"""
116
- return [ x for x in self .applications ]
115
+ return list ( self .applications )
117
116
118
117
@property
119
118
def owned_applications (self ) -> Generator [Application ]:
@@ -122,9 +121,8 @@ def owned_applications(self) -> Generator[Application]:
122
121
Returns:
123
122
A Generator for Application instances.
124
123
"""
125
- if self ._owned_applications :
126
- for application_json in self ._owned_applications :
127
- yield Application .from_json (application_json )
124
+ for application_json in self ._owned_applications :
125
+ yield Application .from_json (application_json )
128
126
129
127
@property
130
128
def all_owned_applications (self ) -> List [Application ]:
@@ -133,7 +131,7 @@ def all_owned_applications(self) -> List[Application]:
133
131
Returns:
134
132
A list of Application instances.
135
133
"""
136
- return [ x for x in self .owned_applications ]
134
+ return list ( self .owned_applications )
137
135
138
136
@classmethod
139
137
def from_json (cls , json : dict ) -> Tenant :
@@ -152,6 +150,7 @@ def from_json(cls, json: dict) -> Tenant:
152
150
obj = cls ._from_json (json , Tenant ())
153
151
# Extract (but don't parse) referenced application. Parsing is
154
152
# done lazily in property implementations
153
+ # pylint: disable=protected-access
155
154
if 'applications' in json :
156
155
obj ._applications = [x ['application' ] for x in json ['applications' ]['references' ]]
157
156
if 'ownedApplications' in json :
@@ -210,8 +209,19 @@ def get_current(self) -> Tenant:
210
209
tenant .c8y = self .c8y
211
210
return tenant
212
211
213
- def get (self , id : str ) -> Tenant :
214
- tenant = Tenant .from_json (self ._get_object (id ))
212
+ def get (self , tenant_id : str ) -> Tenant :
213
+ """Read a specific tenant from the database.
214
+
215
+ Args:
216
+ tenant_id (str|int): database ID of a tenant
217
+
218
+ Returns:
219
+ Tenant object
220
+
221
+ Raises:
222
+ KeyError: if the ID cannot be resolved.
223
+ """
224
+ tenant = Tenant .from_json (self ._get_object (tenant_id ))
215
225
tenant .c8y = self .c8y # inject c8y connection into instance
216
226
return tenant
217
227
0 commit comments