-
Notifications
You must be signed in to change notification settings - Fork 59
/
datasource_mgmt.py
564 lines (449 loc) · 17.2 KB
/
datasource_mgmt.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
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
"""This is the demo script to show how to manage datasources.
This script will not work without replacing parameters with real values.
Its basic goal is to present what can be done with this module and to
ease its usage.
"""
from mstrio.datasources import (
DatasourceConnection,
DatasourceInstance,
DatasourceLogin,
DatasourceMap,
DatasourceType,
Dbms,
Driver,
ExecutionMode,
Gateway,
list_available_dbms,
list_datasource_connections,
list_datasource_instances,
list_datasource_logins,
list_datasource_mappings,
list_drivers,
list_gateways,
)
from mstrio.connection import get_connection
from mstrio.server import list_languages, Language
# Define variables which can be later used in a script
PROJECT_ID = $project_id # Project ID to connect to
PROJECT_NAME = $project_name # Insert project name to connect to
conn = get_connection(workstationData, project_name=PROJECT_NAME)
# Manage datasource logins
# Get a list of datasource logins
datasource_login_list = list_datasource_logins(connection=conn)
print(datasource_login_list)
# Define variables which can be later used in a script
# Insert ID for login to datasource here
DATASOURCE_LOGIN_ID = $datasource_login_id
# Insert name for login to datasource here
DATASOURCE_LOGIN_NAME = $datasource_login_name
# Get datasource login by id
datasource_login = DatasourceLogin(conn, id=DATASOURCE_LOGIN_ID)
print(datasource_login)
# Get datasource login by name
datasource_login = DatasourceLogin(conn, name=DATASOURCE_LOGIN_NAME)
print(datasource_login)
# Define variables which can be later used in a script
# Insert a new name for new login to datasource here
NEW_DATASOURCE_LOGIN_NAME = $new_datasource_login_name
# Insert a new name of user in Datasource here
NEW_DATASOURCE_USERNAME = $new_datasource_username
# Insert a new datasource password here
NEW_DATASOURCE_PASSWORD = $new_datasource_password
# Insert a new datasource login description
NEW_DATASOURCE_LOGIN_DESCRIPTION = $new_datasource_login_description
# Create new datasource login
datasource_login = DatasourceLogin.create(
connection=conn,
name=NEW_DATASOURCE_LOGIN_NAME,
username=NEW_DATASOURCE_USERNAME,
password=NEW_DATASOURCE_PASSWORD,
description=NEW_DATASOURCE_LOGIN_DESCRIPTION,
)
print(datasource_login)
# Define variables which can be later used in a script
# Insert new name for login to datasource here
DATASOURCE_LOGIN_NEW_NAME = $datasource_login_new_name
# Insert new datasource login description
DATASOURCE_LOGIN_NEW_DESCRIPTION = $datasource_login_new_description
# Update a datasource login
datasource_login.alter(
name=DATASOURCE_LOGIN_NEW_NAME,
description=DATASOURCE_LOGIN_NEW_DESCRIPTION,
)
print(datasource_login)
# List properties of a datasource login
print(datasource_login.list_properties())
# Delete a datasource login
datasource_login.delete(force=True)
# Manage datasource connections
# List all datasource connections
datasource_conn_list = list_datasource_connections(connection=conn)
print(datasource_conn_list)
# Define variables which can be later used in a script
# Insert ID of datasource connection here
DATASOURCE_CONNECTION_ID = $datasource_connection_id
# Insert name of datasource connection here
DATASOURCE_CONNECTION_NAME = $datasource_connection_name
# Get datasource connection by id
datasource_connection = DatasourceConnection(conn, id=DATASOURCE_CONNECTION_ID)
print(datasource_connection)
# Get datasource connection by name
datasource_connection = DatasourceConnection(conn, name=DATASOURCE_CONNECTION_NAME)
print(datasource_connection)
# Define variables which can be later used in a script
# Insert new name of datasource connection here
NEW_DATASOURCE_CONNECTION_NAME = $new_datasource_connection_name
# Insert new description of datasource connection here
NEW_DATASOURCE_CONNECTION_DESCRIPTION = $new_datasource_connection_description
# Create a new datasource connection
datasource_connection = DatasourceConnection.create(
connection=conn,
name=NEW_DATASOURCE_CONNECTION_NAME,
description=NEW_DATASOURCE_CONNECTION_DESCRIPTION,
# the ExecutionMode values can be found in datasources/datasource_connection.py
execution_mode=ExecutionMode.SYNCHRONOUS,
datasource_login=DATASOURCE_LOGIN_ID,
)
# Define variables which can be later used in a script
# Insert new name of datasource connection
DATASOURCE_CONNECTION_NEW_NAME = $datasource_connection_new_name
# Insert new description of datasource connection
DATASOURCE_CONNECTION_NEW_DESCRIPTION = $datasource_connection_new_description
# Update a datasource connection
datasource_connection.alter(
name=DATASOURCE_CONNECTION_NEW_NAME,
description=DATASOURCE_CONNECTION_NEW_DESCRIPTION,
)
print(datasource_connection)
# List properties of a datasource connection
print(datasource_connection.list_properties())
# Delete a datasource connection
datasource_connection.delete(force=True)
# Note that conversion to DSN-less is supported on certified gateways:
# Amazon Redshift; Azure Synapse Analytics; ExasolI;
# BM Db2 for Linux, UNIX and Windows; MySQL; Oracle; PostgreSQL;
# Salesforce; Cloudera Hive; Cloudera Impala; Google BigQuery; Spark SQL;
# SAP HANA; Snowflake; SQL Server; Teradata
# Define variables which can be later used in a script
# Insert ID of datasource connection that had DSN connection string
DATASOURCE_CONNECTION_ID_DSN = $datasource_connection_id_dsn
# Initialize datasource connection object
ds_conn = DatasourceConnection(conn, id=DATASOURCE_CONNECTION_ID_DSN)
# List datasource connection properties before conversion
print(ds_conn.list_properties())
# This method is supported from Update 9 server version
# Convert datasource connection to DSN-less
ds_conn.convert_to_dsn_less()
# List datasource connection properties after conversion
print(ds_conn.list_properties())
# Define variables which can be later used in a script
# Insert IDs for datasource connections here
DATASOURCE_CONNECTION_ID_DSN_LIST = $datasource_connection_id_dsn_list
# Convert datasource connections one by one from list
for ds_conn_id in DATASOURCE_CONNECTION_ID_DSN_LIST:
ds_conn = DatasourceConnection(conn, id=ds_conn_id)
# This method is supported from Update 9 server version
ds_conn.convert_to_dsn_less()
# Manage datasource instances
# List all datasources
datasources = list_datasource_instances(connection=conn)
print(datasources)
# List all datasources by project
datasources = list_datasource_instances(connection=conn, project=PROJECT_ID)
print(datasources)
# List all datasources by datasource connection
datasources = list_datasource_instances(
connection=conn, datasource_connection={'id': DATASOURCE_CONNECTION_ID}
)
print(datasources)
# Define variables which can be later used in a script
# Insert ID for datasource instance here
DATASOURCE_INSTANCE_ID = $datasource_instance_id
# Insert name for datasource instance here
DATASOURCE_INSTANCE_NAME = $datasource_instance_name
# Get datasource instance by id
datasource_instance = DatasourceInstance(conn, id=DATASOURCE_INSTANCE_ID)
print(datasource_instance)
# Get datasource instance by name
datasource_instance = DatasourceInstance(conn, name=DATASOURCE_INSTANCE_NAME)
print(datasource_instance)
# List dbms
dbms = list_available_dbms(connection=conn)
print(dbms)
# Define variables which can be later used in a script
DBMS_ID = $dbms_id # Insert ID of DBMS that you want to find
DBMS_NAME = $dbms_name # Insert name of DBMS that you want to find
# Get a DBMS by id
dbms = Dbms(conn, id=DBMS_ID)
print(dbms)
# Get a DBMS by name
dbms = Dbms(conn, name=DBMS_NAME)
print(dbms)
# Define variables which can be later used in a script
# Insert a new name for datasource instance here
NEW_DATASOURCE_INSTANCE_NAME = $new_datasource_instance_name
# Insert a new description for datasource instance here
NEW_DATASOURCE_INSTANCE_DESCRIPTION = $new_datasource_instance_description
# Insert a new table prefix for datasource instance here
NEW_DATASOURCE_INSTANCE_TABLE_PREFIX = $new_datasource_instance_table_prefix
# Create a datasource instance
datasource_instance = DatasourceInstance.create(
connection=conn,
name=NEW_DATASOURCE_INSTANCE_NAME,
description=NEW_DATASOURCE_INSTANCE_DESCRIPTION,
dbms=DBMS_ID,
datasource_connection=DATASOURCE_CONNECTION_ID,
table_prefix=NEW_DATASOURCE_INSTANCE_TABLE_PREFIX,
# The DatasourceType values can be found in datasources/datasource_instance.py
datasource_type=DatasourceType.RESERVED
)
print(datasource_instance)
# Define variables which can be later used in a script
# Insert new name for edited datasource instance here
DATASOURCE_INSTANCE_NEW_NAME = $datasource_instance_new_name
# Insert new description for edited datasource instance here
DATASOURCE_INSTANCE_NEW_DESCRIPTION = $datasource_instance_new_description
# Insert new table prefix for edited datasource instance here
DATASOURCE_INSTANCE_TABLE_NEW_PREFIX = $datasource_instance_table_new_prefix
# Update a datasource instance
datasource_instance.alter(
name=DATASOURCE_INSTANCE_NEW_NAME,
description=DATASOURCE_INSTANCE_NEW_DESCRIPTION,
table_prefix=DATASOURCE_INSTANCE_TABLE_NEW_PREFIX
)
print(datasource_instance)
# List properties of a datasource instance
print(datasource_instance.list_properties())
# Delete a datasource instance
datasource_instance.delete(force=True)
# Note that conversion to DSN-less is supported on certified gateways:
# Amazon Redshift; Azure Synapse Analytics; ExasolI;
# BM Db2 for Linux, UNIX and Windows; MySQL; Oracle; PostgreSQL;
# Salesforce; Cloudera Hive; Cloudera Impala; Google BigQuery; Spark SQL;
# SAP HANA; Snowflake; SQL Server; Teradata
# Define variables which can be later used in a script
# Insert ID for datasource instance here
DATASOURCE_INSTANCE_ID_DSN = $datasource_instance_id_dsn
# Get datasource instance by id
ds_instance = DatasourceInstance(conn, id=DATASOURCE_INSTANCE_ID_DSN)
# This method is supported from Update 9 server version
# Convert datasource embedded connection from DSN to DSN-less format
ds_instance.convert_ds_connection_to_dsn_less()
# Define variables which can be later used in a script
# Insert IDs for datasource instances here
DATASOURCE_INSTANCE_ID_DSN_LIST = $datasource_instance_id_dsn_list
# Convert datasource instance embedded connection one by one from list
for ds_id in DATASOURCE_INSTANCE_ID_DSN_LIST:
ds_instance = DatasourceInstance(conn, id=ds_id)
# This method is supported from Update 9 server version
ds_instance.convert_ds_connection_to_dsn_less()
# Manage connection mappings
# List all locales
language_list = list_languages(connection=conn)
print(language_list)
# Define variables which can be later used in a script
LANGUAGE_ID = $language_id
LANGUAGE_NAME = $language_name
# Initialize language object by id
language = Language(conn, id=LANGUAGE_ID)
print(language)
# Initialize language object by name
language = Language(conn, name=LANGUAGE_NAME)
print(language)
# List all connection mappings
connection_mapping_list = list_datasource_mappings(connection=conn)
print(connection_mapping_list)
# Define a variable which can be later used in a script
USER_OR_USER_GROUP_ID = $user_or_user_group_id # Insert ID of a user or user group
# that you wish to be mapped
# with a connection mapping
# List connection mappings filtered by user
connection_mapping_list = list_datasource_mappings(
connection=conn,
user=USER_OR_USER_GROUP_ID,
)
print(connection_mapping_list)
# List connection mappings filtered by datasource connection
connection_mapping_list = list_datasource_mappings(
connection=conn,
ds_connection=DATASOURCE_CONNECTION_ID,
)
print(connection_mapping_list)
# List connection mappings filtered by datasource instance
connection_mapping_list = list_datasource_mappings(
connection=conn,
datasource=DATASOURCE_INSTANCE_ID,
)
print(connection_mapping_list)
# List connection mappings filtered by datasource login
connection_mapping_list = list_datasource_mappings(
connection=conn,
login=DATASOURCE_LOGIN_ID,
)
print(connection_mapping_list)
# List connection mappings filtered by language ID
connection_mapping_list = list_datasource_mappings(
connection=conn,
locale=LANGUAGE_ID,
)
print(connection_mapping_list)
# List connection mappings filtered by language name
connection_mapping_list = list_datasource_mappings(
connection=conn,
locale=LANGUAGE_NAME,
)
print(connection_mapping_list)
# List default connection mappings for a project
connection_mapping_list = list_datasource_mappings(
connection=conn,
default_connection_map=True,
project=PROJECT_ID,
)
print(connection_mapping_list)
# Define a variable which can be later used in a script
CONNECTION_MAPPING_ID = $connection_mapping_id # Insert ID of connection mapping
# Initialise a connection mapping
connection_mapping = DatasourceMap(connection=conn, id=CONNECTION_MAPPING_ID)
print(connection_mapping)
# Initialise a default connection mapping by id for a project
connection_mapping = DatasourceMap(
connection=conn,
id=CONNECTION_MAPPING_ID,
default_connection_map=True,
project=PROJECT_ID,
)
print(connection_mapping)
# Initialise a default connection mapping for a project if only one default
# connection mapping exists for a project
connection_mapping = DatasourceMap(
connection=conn,
default_connection_map=True,
project=PROJECT_ID,
)
print(connection_mapping)
# Create a connection mapping with default locale
connection_mapping = DatasourceMap.create(
connection=conn,
project=PROJECT_ID,
user=USER_OR_USER_GROUP_ID,
ds_connection=DATASOURCE_CONNECTION_ID,
datasource=DATASOURCE_INSTANCE_ID,
login=DATASOURCE_LOGIN_ID,
)
print(connection_mapping)
# Create a connection mapping with locale's id
connection_mapping = DatasourceMap.create(
connection=conn,
project=PROJECT_ID,
user=USER_OR_USER_GROUP_ID,
ds_connection=DATASOURCE_CONNECTION_ID,
datasource=DATASOURCE_INSTANCE_ID,
login=DATASOURCE_LOGIN_ID,
locale=LANGUAGE_ID
)
print(connection_mapping)
# Create a connection mapping with locale's name
connection_mapping = DatasourceMap.create(
connection=conn,
project=PROJECT_ID,
user=USER_OR_USER_GROUP_ID,
ds_connection=DATASOURCE_CONNECTION_ID,
datasource=DATASOURCE_INSTANCE_ID,
login=DATASOURCE_LOGIN_ID,
locale=LANGUAGE_NAME
)
print(connection_mapping)
# Define variables which can be later used in a script
# Insert new user or usergroup ID for connection mapping
CONNECTION_MAPPING_NEW_USER_OR_USER_GROUP = $connection_mapping_new_user_or_user_group
# Insert new login ID for connection mapping
CONNECTION_MAPPING_NEW_LOGIN = $connection_mapping_new_login
# Alter connection mapping
# NOTE: Altering connection mapping will change its ID
connection_mapping.alter(
user=CONNECTION_MAPPING_NEW_USER_OR_USER_GROUP,
login=CONNECTION_MAPPING_NEW_LOGIN,
)
print(connection_mapping)
# Alter connection mapping's locale by id
connection_mapping.alter(locale=LANGUAGE_ID)
print(connection_mapping)
# Alter connection mapping's locale by name
connection_mapping.alter(locale=LANGUAGE_NAME)
print(connection_mapping)
# Delete a connection mapping
connection_mapping.delete(force=True)
# Manage Drivers
# List all drivers
drivers = list_drivers(conn)
print(drivers)
# Define variables which can be later used in a script
DRIVER_ID = $driver_id
# List drivers by id
drivers = list_drivers(conn, id=DRIVER_ID)
print(drivers)
# Define variables which can be later used in a script
DRIVER_NAME = $driver_name
# List drivers by name
drivers = list_drivers(conn, name=DRIVER_NAME)
print(drivers)
# List enabled drivers
drivers = list_drivers(conn, is_enabled=True)
print(drivers)
# List disabled drivers
drivers = list_drivers(conn, is_enabled=False)
print(drivers)
# List ODBC drivers
drivers = list_drivers(conn, is_odbc=True)
print(drivers)
# List non-ODBC drivers
drivers = list_drivers(conn, is_odbc=False)
print(drivers)
# Initialize driver by id
driver = Driver(conn, id=DRIVER_ID)
print(driver)
# Initialize driver by name
driver = Driver(conn, name=DRIVER_NAME)
print(driver)
# Enable driver
driver.enable()
# Disable driver
driver.disable()
# Alter driver
driver.alter(is_enabled=True)
# Managing Gateways
# List all gateways
gateways = list_gateways(conn)
print(gateways)
# Define variables which can be later used in a script
GATEWAY_ID = $gateway_id
# List gateways by id
gateways = list_gateways(conn, id=GATEWAY_ID)
print(gateways)
# Define variables which can be later used in a script
GATEWAY_NAME = $gateway_name
# List gateways by name
gateways = list_gateways(conn, name=GATEWAY_NAME)
print(gateways)
# Define variables which can be later used in a script
GATEWAY_TYPE = $gateway_type
# List gateways by gateway type
gateways = list_gateways(conn, gateway_type=GATEWAY_TYPE)
print(gateways)
# Define variables which can be later used in a script
DB_TYPE = $db_type
# List gateways by database type
gateways = list_gateways(conn, db_type=DB_TYPE)
print(gateways)
# List certified gateways
gateways = list_gateways(conn, is_certified=True)
print(gateways)
# List not certified gateways
gateways = list_gateways(conn, is_certified=False)
print(gateways)
# Initialize gateway by id
gateway = Gateway(conn, id=GATEWAY_ID)
print(gateway)
# Initialize gateway by name
gateway = Gateway(conn, name=GATEWAY_NAME)
print(gateway)