|
1 | 1 | import logging |
2 | 2 | import re |
| 3 | +import os |
| 4 | +import ast |
3 | 5 |
|
4 | 6 | from botocore.exceptions import ClientError |
5 | 7 |
|
6 | 8 | from .sts import SessionHelper |
| 9 | +from .secrets_manager import SecretsManager |
| 10 | +from .parameter_store import ParameterStoreManager |
7 | 11 |
|
8 | 12 | logger = logging.getLogger('QuicksightHandler') |
9 | 13 | logger.setLevel(logging.DEBUG) |
@@ -262,3 +266,154 @@ def can_import_dashboard(AwsAccountId, region, UserName, DashboardId): |
262 | 266 | return True |
263 | 267 |
|
264 | 268 | return False |
| 269 | + |
| 270 | + @staticmethod |
| 271 | + def create_data_source_vpc(AwsAccountId, region, UserName, vpcConnectionId): |
| 272 | + client = Quicksight.get_quicksight_client(AwsAccountId, region) |
| 273 | + identity_region = 'us-east-1' |
| 274 | + |
| 275 | + user = Quicksight.register_user(AwsAccountId, UserName, UserRole='AUTHOR') |
| 276 | + try: |
| 277 | + response = client.describe_data_source( |
| 278 | + AwsAccountId=AwsAccountId, DataSourceId="dataall-metadata-db" |
| 279 | + ) |
| 280 | + |
| 281 | + except client.exceptions.ResourceNotFoundException: |
| 282 | + aurora_secret_arn = ParameterStoreManager.get_parameter_value(AwsAccountId=AwsAccountId, region=region, parameter_path=f'/dataall/{os.getenv("envname", "local")}/aurora/secret_arn') |
| 283 | + aurora_params = SecretsManager.get_secret_value( |
| 284 | + AwsAccountId=AwsAccountId, region=region, secretId=aurora_secret_arn |
| 285 | + ) |
| 286 | + aurora_params_dict = ast.literal_eval(aurora_params) |
| 287 | + response = client.create_data_source( |
| 288 | + AwsAccountId=AwsAccountId, |
| 289 | + DataSourceId="dataall-metadata-db", |
| 290 | + Name="dataall-metadata-db", |
| 291 | + Type="AURORA_POSTGRESQL", |
| 292 | + DataSourceParameters={ |
| 293 | + 'AuroraPostgreSqlParameters': { |
| 294 | + 'Host': aurora_params_dict["host"], |
| 295 | + 'Port': aurora_params_dict["port"], |
| 296 | + 'Database': aurora_params_dict["dbname"] |
| 297 | + } |
| 298 | + }, |
| 299 | + Credentials={ |
| 300 | + "CredentialPair": { |
| 301 | + "Username": aurora_params_dict["username"], |
| 302 | + "Password": aurora_params_dict["password"], |
| 303 | + } |
| 304 | + }, |
| 305 | + Permissions=[ |
| 306 | + { |
| 307 | + "Principal": f"arn:aws:quicksight:{region}:{AwsAccountId}:group/default/dataall", |
| 308 | + "Actions": [ |
| 309 | + "quicksight:UpdateDataSourcePermissions", |
| 310 | + "quicksight:DescribeDataSource", |
| 311 | + "quicksight:DescribeDataSourcePermissions", |
| 312 | + "quicksight:PassDataSource", |
| 313 | + "quicksight:UpdateDataSource", |
| 314 | + "quicksight:DeleteDataSource" |
| 315 | + ] |
| 316 | + } |
| 317 | + ], |
| 318 | + VpcConnectionProperties={ |
| 319 | + 'VpcConnectionArn': f"arn:aws:quicksight:{region}:{AwsAccountId}:vpcConnection/{vpcConnectionId}" |
| 320 | + } |
| 321 | + ) |
| 322 | + |
| 323 | + return "dataall-metadata-db" |
| 324 | + |
| 325 | + @staticmethod |
| 326 | + def create_data_set_from_source(AwsAccountId, region, UserName, dataSourceId, tablesToImport): |
| 327 | + client = Quicksight.get_quicksight_client(AwsAccountId, region) |
| 328 | + user = Quicksight.describe_user(AwsAccountId, UserName) |
| 329 | + if not user: |
| 330 | + return False |
| 331 | + |
| 332 | + data_source = client.describe_data_source( |
| 333 | + AwsAccountId=AwsAccountId, |
| 334 | + DataSourceId=dataSourceId |
| 335 | + ) |
| 336 | + |
| 337 | + if not data_source: |
| 338 | + return False |
| 339 | + |
| 340 | + for table in tablesToImport: |
| 341 | + |
| 342 | + response = client.create_data_set( |
| 343 | + AwsAccountId=AwsAccountId, |
| 344 | + DataSetId=f"dataall-imported-{table}", |
| 345 | + Name=f"dataall-imported-{table}", |
| 346 | + PhysicalTableMap={ |
| 347 | + 'string': { |
| 348 | + 'RelationalTable': { |
| 349 | + 'DataSourceArn': data_source.get('DataSource').get('Arn'), |
| 350 | + 'Catalog': 'string', |
| 351 | + 'Schema': 'dev', |
| 352 | + 'Name': table, |
| 353 | + 'InputColumns': [ |
| 354 | + { |
| 355 | + 'Name': 'string', |
| 356 | + 'Type': 'STRING' |
| 357 | + }, |
| 358 | + ] |
| 359 | + } |
| 360 | + }}, |
| 361 | + ImportMode='DIRECT_QUERY', |
| 362 | + Permissions=[ |
| 363 | + { |
| 364 | + 'Principal': user.get('Arn'), |
| 365 | + 'Actions': [ |
| 366 | + "quicksight:DescribeDataSet", |
| 367 | + "quicksight:DescribeDataSetPermissions", |
| 368 | + "quicksight:PassDataSet", |
| 369 | + "quicksight:DescribeIngestion", |
| 370 | + "quicksight:ListIngestions" |
| 371 | + ] |
| 372 | + }, |
| 373 | + ], |
| 374 | + ) |
| 375 | + |
| 376 | + return True |
| 377 | + |
| 378 | + @staticmethod |
| 379 | + def create_analysis(AwsAccountId, region, UserName): |
| 380 | + client = Quicksight.get_quicksight_client(AwsAccountId, region) |
| 381 | + user = Quicksight.describe_user(AwsAccountId, UserName) |
| 382 | + if not user: |
| 383 | + return False |
| 384 | + |
| 385 | + response = client.create_analysis( |
| 386 | + AwsAccountId=AwsAccountId, |
| 387 | + AnalysisId='dataallMonitoringAnalysis', |
| 388 | + Name='dataallMonitoringAnalysis', |
| 389 | + Permissions=[ |
| 390 | + { |
| 391 | + 'Principal': user.get('Arn'), |
| 392 | + 'Actions': [ |
| 393 | + 'quicksight:DescribeAnalysis', |
| 394 | + 'quicksight:DescribeAnalysisPermissions', |
| 395 | + 'quicksight:UpdateAnalysisPermissions', |
| 396 | + 'quicksight:UpdateAnalysis' |
| 397 | + ] |
| 398 | + }, |
| 399 | + ], |
| 400 | + SourceEntity={ |
| 401 | + 'SourceTemplate': { |
| 402 | + 'DataSetReferences': [ |
| 403 | + { |
| 404 | + 'DataSetPlaceholder': 'environment', |
| 405 | + 'DataSetArn': f"arn:aws:quicksight:{region}:{AwsAccountId}:dataset/<DATASET-ID>" |
| 406 | + }, |
| 407 | + ], |
| 408 | + 'Arn': '<TEMPLATE-THAT-WE-WANT-TO-MIGRATE' |
| 409 | + } |
| 410 | + }, |
| 411 | + Tags=[ |
| 412 | + { |
| 413 | + 'Key': 'application', |
| 414 | + 'Value': 'dataall' |
| 415 | + }, |
| 416 | + ] |
| 417 | + ) |
| 418 | + |
| 419 | + return True |
0 commit comments