-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcurrent_workspace.py
More file actions
570 lines (490 loc) · 18.6 KB
/
Copy pathcurrent_workspace.py
File metadata and controls
570 lines (490 loc) · 18.6 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
"""Workspace-related classes and functions.
See https://github.com/BLSQ/openhexa/wiki/User-manual#about-workspaces for more information.
"""
import os
from dataclasses import fields, make_dataclass
from warnings import warn
from openhexa.graphql.graphql_client import GetCountriesWorkspaceCountries
from openhexa.utils import stringcase
from ..datasets import Dataset
from ..utils import OpenHexaClient, graphql
from .connection import (
ConnectionClasses,
CustomConnection,
DHIS2Connection,
GCSConnection,
IASOConnection,
PostgreSQLConnection,
S3Connection,
)
class WorkspaceConfigError(Exception):
"""Raised whenever the system cannot find an environment variable required to configure the current workspace."""
pass
class ConnectionDoesNotExist(Exception):
"""Raised whenever an attempt is made to get a connection through an invalid identifier."""
pass
class CurrentWorkspace:
"""Represents the currently configured OpenHEXA workspace, with its filesystem, database and connections."""
@property
def _connected(self):
return "HEXA_SERVER_URL" in os.environ
@property
def _token(self) -> str:
try:
return os.environ["HEXA_TOKEN"]
except KeyError:
raise WorkspaceConfigError("The workspace token is not available in this environment.")
@property
def slug(self) -> str:
"""The unique slug of the workspace.
Slugs are used to identify the workspace.
"""
try:
return os.environ["HEXA_WORKSPACE"]
except KeyError:
raise WorkspaceConfigError("The workspace slug is not available in this environment.")
@property
def countries(self) -> list[GetCountriesWorkspaceCountries]:
"""The countries of the workspace."""
try:
return OpenHexaClient().get_countries(workspace_slug=self.slug).workspace.countries
except KeyError:
raise WorkspaceConfigError("The workspace countries are not available in this environment.")
@property
def database_host(self) -> str:
"""The workspace database host."""
try:
return os.environ["WORKSPACE_DATABASE_HOST"]
except KeyError:
raise WorkspaceConfigError(
"No database has been configured. Did you forget to provide a database entry"
" in your workspace.yaml file?"
)
@property
def database_username(self) -> str:
"""The workspace database username."""
try:
return os.environ["WORKSPACE_DATABASE_USERNAME"]
except KeyError:
raise WorkspaceConfigError(
"No database has been configured. Did you forget to provide a database entry"
" in your workspace.yaml file?"
)
@property
def database_password(self):
"""The workspace database password."""
try:
return os.environ.get("WORKSPACE_DATABASE_PASSWORD")
except KeyError:
raise WorkspaceConfigError(
"No database has been configured. Did you forget to provide a database entry"
" in your workspace.yaml file?"
)
@property
def database_name(self):
"""The workspace database name."""
try:
return os.environ["WORKSPACE_DATABASE_DB_NAME"]
except KeyError:
raise WorkspaceConfigError(
"No database has been configured. Did you forget to provide a database entry"
" in your workspace.yaml file?"
)
@property
def database_port(self):
"""The workspace database port."""
try:
return int(os.environ["WORKSPACE_DATABASE_PORT"])
except KeyError:
raise WorkspaceConfigError(
"No database has been configured. Did you forget to provide a database entry"
" in your workspace.yaml file?"
)
@property
def database_url(self):
"""The workspace database URL.
The URL follows the official PostgreSQL specification.
(See https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING for more information)
"""
return (
f"postgresql://{self.database_username}:{self.database_password}"
f"@{self.database_host}:{self.database_port}/{self.database_name}"
)
@property
def files_path(self) -> str:
"""The base path to the filesystem, without trailing slash.
Examples
--------
>>> f"{workspace.files_path}/some/path"
/home/hexa/workspace/some/path
"""
# FIXME: This is a hack to make the SDK work in the context of the `python pipeline.py` command.
# We can remove this once we deprecate this way of running pipelines and only use /home/hexa/workspace
return os.environ.get("WORKSPACE_FILES_PATH", "/home/hexa/workspace")
@property
def tmp_path(self) -> str:
"""The base path to the tmp directory, without trailing slash.
Examples
--------
>>> f"{workspace.tmp_path}/some/path"
/home/hexa/tmp/some/path
"""
# FIXME: This is a hack to make the SDK work in the context of the `python pipeline.py` command.
# We can remove this once we deprecate this way of running pipelines
return os.environ["WORKSPACE_TMP_PATH"] if "WORKSPACE_TMP_PATH" in os.environ else "/home/hexa/tmp"
def _get_local_connection_fields(self, env_variable_prefix: str):
connection_fields = {}
connection_type = os.getenv(env_variable_prefix).upper()
# Get fields for the connection type
_fields = fields(ConnectionClasses[connection_type])
if _fields:
for field in _fields:
env_var = f"{env_variable_prefix}_{field.name.upper()}"
connection_fields[field.name] = os.getenv(env_var)
else:
# custom connections
prefix = f"{env_variable_prefix}_"
connection_fields = {
key[len(prefix) :].lower(): val for key, val in os.environ.items() if key.startswith(prefix)
}
# need to map the correct name for s3 and postgres connection to ensure compatibility
# with the one coming from the API
if connection_type == "S3":
connection_fields.pop("secret_access_key")
connection_fields["access_key_secret"] = os.getenv(f"{env_variable_prefix}_ACCESS_KEY_SECRET")
if connection_type == "POSTGRESQL":
connection_fields.pop("database_name")
connection_fields["db_name"] = os.getenv(f"{env_variable_prefix}_DB_NAME")
return connection_fields
def get_connection(
self, identifier: str
) -> (
DHIS2Connection | PostgreSQLConnection | IASOConnection | S3Connection | GCSConnection | CustomConnection | None
):
"""Get a connection by its identifier.
Parameters
----------
identifier : str
The identifier of the connection in the OpenHEXA backend
Returns
-------
Connection
The connection
Raises
------
ValueError
If the connection does not exist
"""
connection_fields = {}
connection_type = None
if self._connected:
response = graphql(
"""
query getConnection($workspaceSlug:String!, $connectionSlug: String!) {
connectionBySlug(workspaceSlug:$workspaceSlug, connectionSlug: $connectionSlug) {
type
fields {
code
value
}
}
}
""",
{"workspaceSlug": self.slug, "connectionSlug": identifier.lower()},
)
data = response["connectionBySlug"]
if data is None:
raise ValueError(f"Connection {identifier} does not exist.")
for d in data["fields"]:
connection_fields[d.get("code")] = d.get("value")
connection_type = data["type"].upper()
else:
try:
env_variable_prefix = stringcase.constcase(identifier.lower())
connection_type = os.environ[f"{env_variable_prefix}"].upper()
connection_fields = self._get_local_connection_fields(env_variable_prefix)
except KeyError:
raise ValueError
if not connection_type:
raise ValueError(f"Connection {identifier} does not exist.")
# In connected mode (API call) the secret_access_key field and db_name name are
# different from the offline ones
if connection_type == "S3":
secret_access_key = connection_fields.pop("access_key_secret")
return S3Connection(secret_access_key=secret_access_key, **connection_fields)
if connection_type == "POSTGRESQL":
db_name = connection_fields.pop("db_name")
port = int(connection_fields.pop("port"))
return PostgreSQLConnection(
database_name=db_name,
port=port,
**connection_fields,
)
if connection_type == "CUSTOM":
dataclass = make_dataclass(
stringcase.pascalcase(identifier),
connection_fields.keys(),
bases=(CustomConnection,),
repr=False,
)
return dataclass(**connection_fields)
return ConnectionClasses[connection_type](**connection_fields)
def dhis2_connection(self, identifier: str = None, slug: str = None) -> DHIS2Connection:
"""Get a DHIS2 connection by its identifier.
Parameters
----------
identifier : str
The identifier of the connection in the OpenHEXA backend
slug : str
Deprecated, same as identifier
"""
identifier = identifier or slug
if slug is not None:
warn(
"'slug' is deprecated. Use 'identifier' instead.",
DeprecationWarning,
stacklevel=2,
)
try:
connection = self.get_connection(identifier)
assert isinstance(connection, DHIS2Connection), "Connection is not a DHIS2Connection"
return connection
except ValueError:
raise ConnectionDoesNotExist(f'No DHIS2 connection for "{identifier}"')
def postgresql_connection(self, identifier: str = None, slug: str = None) -> PostgreSQLConnection:
"""Get a PostgreSQL connection by its identifier.
Parameters
----------
identifier : str
The identifier of the connection in the OpenHEXA backend
slug : str
Deprecated, same as identifier
"""
identifier = identifier or slug
if slug is not None:
warn(
"'slug' is deprecated. Use 'identifier' instead.",
DeprecationWarning,
stacklevel=2,
)
try:
connection = self.get_connection(identifier)
assert isinstance(connection, PostgreSQLConnection), "Connection is not a PostgreSQLConnection"
return connection
except ValueError:
raise ConnectionDoesNotExist(f'No PostgreSQL connection for "{identifier}"')
def s3_connection(self, identifier: str = None, slug: str = None) -> S3Connection:
"""Get an AWS S3 connection by its identifier.
Parameters
----------
identifier : str
The identifier of the connection in the OpenHEXA backend
slug : str
Deprecated, same as identifier
"""
identifier = identifier or slug
if slug is not None:
warn(
"'slug' is deprecated. Use 'identifier' instead.",
DeprecationWarning,
stacklevel=2,
)
try:
connection = self.get_connection(identifier)
assert isinstance(connection, S3Connection), "Connection is not a S3Connection"
return connection
except ValueError:
raise ConnectionDoesNotExist(f'No S3 connection for "{identifier}"')
def gcs_connection(self, identifier: str = None, slug: str = None) -> GCSConnection:
"""Get a Google Cloud Storage connection by its identifier.
Parameters
----------
identifier : str
The identifier of the connection in the OpenHEXA backend
slug : str
Deprecated, same as identifier
"""
identifier = identifier or slug
if slug is not None:
warn(
"'slug' is deprecated. Use 'identifier' instead.",
DeprecationWarning,
stacklevel=2,
)
try:
connection = self.get_connection(identifier)
assert isinstance(connection, GCSConnection), "Connection is not a GCSConnection"
return connection
except ValueError:
raise ConnectionDoesNotExist(f'No GCS connection for "{identifier}"')
def iaso_connection(self, identifier: str = None, slug: str = None) -> IASOConnection:
"""Get a IASO connection by it identifier.
Parameters
----------
identifier : str
The identifier of the connection in the OpenHEXA backend
slug : str
Deprecated, same as identifier
"""
identifier = identifier or slug
if slug is not None:
warn(
"'slug' is deprecated. Use 'identifier' instead.",
DeprecationWarning,
stacklevel=2,
)
try:
connection = self.get_connection(identifier)
assert isinstance(connection, IASOConnection), "Connection is not a IASOConnection"
return connection
except ValueError:
raise ConnectionDoesNotExist(f'No IASO connection for "{identifier}"')
def custom_connection(self, identifier: str = None, slug: str = None) -> CustomConnection:
"""Get a custom connection by its identifier.
Parameters
----------
identifier : str
The identifier of the connection in the OpenHEXA backend
slug : str
Deprecated, same as identifier
"""
identifier = identifier or slug
if slug is not None:
warn(
"'slug' is deprecated. Use 'identifier' instead.",
DeprecationWarning,
stacklevel=2,
)
try:
connection = self.get_connection(identifier)
assert isinstance(connection, CustomConnection), "Connection is not a CustomConnection"
return connection
except ValueError:
raise ConnectionDoesNotExist(f'No Custom connection for "{identifier}"')
def create_dataset(self, name: str, description: str):
"""Create a new dataset.
Parameters
----------
name: str
The name of the dataset
description: str
The description of the dataset
Returns
-------
Dataset
The created dataset
Raises
------
ValueError
If the dataset could not be created
"""
rsp = graphql(
"""
mutation createDataset($input: CreateDatasetInput!) {
createDataset(input: $input) {
success
errors
dataset {
slug
}
}
}
""",
{
"input": {
"workspaceSlug": self.slug,
"name": name,
"description": description,
}
},
)
if rsp["createDataset"]["success"] is False:
raise ValueError(rsp["createDataset"]["errors"][0])
identifier = rsp["createDataset"]["dataset"]["slug"]
return self.get_dataset(identifier)
def get_dataset(self, identifier: str) -> Dataset:
"""Get a dataset by its identifier.
Parameters
----------
identifier : str
The identifier of the dataset in the OpenHEXA backend
Returns
-------
Dataset
The dataset
Raises
------
ValueError
If the dataset does not exist
"""
response = graphql(
"""
query getDataset($datasetSlug: String!, $workspaceSlug: String!) {
datasetLinkBySlug(datasetSlug: $datasetSlug, workspaceSlug: $workspaceSlug) {
id
workspace {
slug
name
}
dataset {
id
slug
name
description
latestVersion {
id
name
description
}
}
}
}
""",
{"datasetSlug": identifier, "workspaceSlug": self.slug},
)
data = response["datasetLinkBySlug"]
if data is None:
raise ValueError(f"Dataset {identifier} does not exist.")
return Dataset(
id=data["dataset"]["id"],
slug=data["dataset"]["slug"],
name=data["dataset"]["name"],
description=data["dataset"]["description"],
)
def list_datasets(self) -> list[Dataset]:
"""List datasets in a workspace.
Returns
-------
List of Datasets
"""
response = graphql(
"""
query getWorkspaceDatasets($slug: String!) {
workspace(slug: $slug) {
datasets {
items {
id
dataset {
id
slug
name
description
}
}
}
}
}
""",
{"slug": self.slug},
)
data = response["workspace"]["datasets"]["items"]
datasets = [
Dataset(
id=d["dataset"]["id"],
slug=d["dataset"]["slug"],
name=d["dataset"]["name"],
description=d["dataset"]["description"],
)
for d in data
]
return datasets