forked from logicalclocks/hopsworks-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
250 lines (213 loc) · 7.3 KB
/
project.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#
# Copyright 2022 Logical Clocks AB
#
# 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.
#
from __future__ import annotations
import humps
import json
from hopsworks import util, client, constants
from hopsworks.client.external import Client
from hopsworks.core import (
job_api,
git_api,
dataset_api,
kafka_api,
opensearch_api,
environment_api,
flink_cluster_api,
)
from hsfs import feature_store
class Project:
def __init__(
self,
archived=None,
created=None,
description=None,
docker_image=None,
hops_examples=None,
inodeid=None,
is_old_docker_image=None,
is_preinstalled_docker_image=None,
owner=None,
project_id=None,
project_name=None,
project_team=None,
quotas=None,
retention_period=None,
services=None,
datasets=None,
creation_status=None,
**kwargs,
):
self._id = project_id
self._name = project_name
self._owner = owner
self._description = description
self._created = created
self._opensearch_api = opensearch_api.OpenSearchApi(project_id, project_name)
self._kafka_api = kafka_api.KafkaApi(project_id, project_name)
self._jobs_api = job_api.JobsApi(project_id, project_name)
self._flink_cluster_api = flink_cluster_api.FlinkClusterApi(
project_id, project_name
)
self._git_api = git_api.GitApi(project_id, project_name)
self._dataset_api = dataset_api.DatasetApi(project_id)
self._environment_api = environment_api.EnvironmentApi(project_id, project_name)
@classmethod
def from_response_json(cls, json_dict):
if json_dict:
json_decamelized = humps.decamelize(json_dict)
return cls(**json_decamelized)
else:
return None
@property
def id(self):
"""Id of the project"""
return self._id
@property
def name(self):
"""Name of the project"""
return self._name
@property
def owner(self):
"""Owner of the project"""
return self._owner
@property
def description(self):
"""Description of the project"""
return self._description
@property
def created(self):
"""Timestamp when the project was created"""
return self._created
def get_feature_store(self, name: str = None) -> feature_store.FeatureStore:
"""Connect to Project's Feature Store.
Defaulting to the project name of default feature store. To get a
shared feature store, the project name of the feature store is required.
# Returns
`hsfs.feature_store.FeatureStore`: The Feature Store API
# Raises
`RestAPIError`: If unable to connect
"""
from hsfs import connection
_client = client.get_instance()
if type(_client) == Client: # If external client
engine = None
if _client._host == constants.HOSTS.APP_HOST:
engine = "python"
return connection(
host=_client._host,
port=_client._port,
project=self.name,
api_key_value=_client._auth._token,
engine=engine,
).get_feature_store(name)
else:
return connection().get_feature_store(name) # If internal client
def get_model_registry(self):
"""Connect to Project's Model Registry API.
# Returns
`hsml.model_registry.ModelRegistry`: The Model Registry API
# Raises
`RestAPIError`: If unable to connect
"""
from hsml import connection
_client = client.get_instance()
if type(_client) == Client: # If external client
return connection(
host=_client._host,
port=_client._port,
project=self.name,
api_key_value=_client._auth._token,
).get_model_registry()
else:
return connection().get_model_registry() # If internal client
def get_model_serving(self):
"""Connect to Project's Model Serving API.
# Returns
`hsml.model_serving.ModelServing`: The Model Serving API
# Raises
`RestAPIError`: If unable to connect
"""
from hsml import connection
_client = client.get_instance()
if type(_client) == Client: # If external client
return connection(
host=_client._host,
port=_client._port,
project=self.name,
api_key_value=_client._auth._token,
).get_model_serving()
else:
return connection().get_model_serving() # If internal client
def get_kafka_api(self):
"""Get the kafka api for the project.
# Returns
`KafkaApi`: The Kafka Api handle
"""
_client = client.get_instance()
if type(_client) == Client:
_client.download_certs(self.name)
return self._kafka_api
def get_opensearch_api(self):
"""Get the opensearch api for the project.
# Returns
`OpenSearchApi`: The OpenSearch Api handle
"""
_client = client.get_instance()
if type(_client) == Client:
_client.download_certs(self.name)
return self._opensearch_api
def get_jobs_api(self):
"""Get the jobs api for the project.
# Returns
`JobsApi`: The Jobs Api handle
"""
return self._jobs_api
def get_flink_cluster_api(self):
"""Get the flink cluster api for the project.
# Returns
`FlinkClusterApi`: The Flink Cluster Api handle
"""
return self._flink_cluster_api
def get_git_api(self):
"""Get the git repository api for the project.
# Returns
`GitApi`: The Git Api handle
"""
return self._git_api
def get_dataset_api(self):
"""Get the dataset api for the project.
# Returns
`DatasetApi`: The Datasets Api handle
"""
return self._dataset_api
def get_environment_api(self):
"""Get the Python environment AP
# Returns
`EnvironmentApi`: The Python Environment Api handle
"""
return self._environment_api
def json(self):
return json.dumps(self, cls=util.Encoder)
def __str__(self):
return self.json()
def __repr__(self):
if self._description is not None:
return f"Project({self._name!r}, {self._owner!r}, {self._description!r})"
else:
return f"Project({self._name!r}, {self._owner!r})"
def get_url(self):
path = "/p/" + str(self.id)
return util.get_hostname_replaced_url(path)