|
5 | 5 | from typing import Optional |
6 | 6 |
|
7 | 7 | from gooddata_api_client.exceptions import NotFoundException |
| 8 | +from gooddata_api_client.model.declarative_export_templates import DeclarativeExportTemplates |
8 | 9 | from gooddata_api_client.model.declarative_notification_channels import DeclarativeNotificationChannels |
9 | 10 | from gooddata_api_client.model.json_api_csp_directive_in_document import JsonApiCspDirectiveInDocument |
| 11 | +from gooddata_api_client.model.json_api_export_template_in_document import JsonApiExportTemplateInDocument |
| 12 | +from gooddata_api_client.model.json_api_export_template_post_optional_id_document import ( |
| 13 | + JsonApiExportTemplatePostOptionalIdDocument, |
| 14 | +) |
10 | 15 | from gooddata_api_client.model.json_api_identity_provider_in_document import JsonApiIdentityProviderInDocument |
11 | 16 | from gooddata_api_client.model.json_api_organization_setting_in_document import JsonApiOrganizationSettingInDocument |
12 | 17 |
|
| 18 | +from gooddata_sdk import CatalogDeclarativeExportTemplate, CatalogExportTemplate |
13 | 19 | from gooddata_sdk.catalog.catalog_service_base import CatalogServiceBase |
14 | 20 | from gooddata_sdk.catalog.organization.entity_model.directive import CatalogCspDirective |
15 | 21 | from gooddata_sdk.catalog.organization.entity_model.identity_provider import CatalogIdentityProvider |
@@ -424,6 +430,86 @@ def patch_identity_provider_attributes(self, identity_provider_id: str, attribut |
424 | 430 | identity_provider_id, CatalogIdentityProvider.to_api_patch(identity_provider_id, attributes) |
425 | 431 | ) |
426 | 432 |
|
| 433 | + def create_or_update_export_template(self, export_template: CatalogExportTemplate) -> None: |
| 434 | + """Create a new export template or overwrite an existing export template with the same id. |
| 435 | +
|
| 436 | + Args: |
| 437 | + export_template (CatalogExportTemplate): |
| 438 | + Catalog export template object to be created or updated. |
| 439 | +
|
| 440 | + Returns: |
| 441 | + None |
| 442 | +
|
| 443 | + Raises: |
| 444 | + ValueError: Export template cannot be updated. |
| 445 | + """ |
| 446 | + try: |
| 447 | + self.get_export_template(export_template.id) |
| 448 | + self._entities_api.update_entity_export_templates( |
| 449 | + id=export_template.id, |
| 450 | + json_api_export_template_in_document=JsonApiExportTemplateInDocument.from_dict( |
| 451 | + {"data": export_template.to_dict()} |
| 452 | + ), |
| 453 | + ) |
| 454 | + except NotFoundException: |
| 455 | + self._entities_api.create_entity_export_templates( |
| 456 | + json_api_export_template_post_optional_id_document=JsonApiExportTemplatePostOptionalIdDocument( |
| 457 | + data=export_template.to_api() |
| 458 | + ) |
| 459 | + ) |
| 460 | + |
| 461 | + def get_export_template(self, export_template_id: str) -> CatalogExportTemplate: |
| 462 | + """Get an individual export template. |
| 463 | +
|
| 464 | + Args: |
| 465 | + export_template_id (str): |
| 466 | + Export template identification string e.g. "demo" |
| 467 | +
|
| 468 | + Returns: |
| 469 | + CatalogJwk: |
| 470 | + Catalog export template object containing the structure of the export template. |
| 471 | + """ |
| 472 | + export_template_api = self._entities_api.get_entity_export_templates(id=export_template_id).data |
| 473 | + return CatalogExportTemplate.from_api(export_template_api) |
| 474 | + |
| 475 | + def delete_export_template(self, export_template_id: str) -> None: |
| 476 | + """Delete an export template. |
| 477 | +
|
| 478 | + Args: |
| 479 | + export_template_id (str): |
| 480 | + Export template identification string e.g. "demo" |
| 481 | +
|
| 482 | + Returns: |
| 483 | + None |
| 484 | +
|
| 485 | + Raises: |
| 486 | + ValueError: |
| 487 | + Export template does not exist. |
| 488 | + """ |
| 489 | + try: |
| 490 | + self._entities_api.delete_entity_export_templates(export_template_id) |
| 491 | + except NotFoundException: |
| 492 | + raise ValueError( |
| 493 | + f"Can not delete {export_template_id} export template. This export template does not exist." |
| 494 | + ) |
| 495 | + |
| 496 | + def list_export_templates(self) -> list[CatalogExportTemplate]: |
| 497 | + """Returns a list of all export templates in the current organization. |
| 498 | +
|
| 499 | + Returns: |
| 500 | + list[CatalogExportTemplate]: |
| 501 | + List of export templates in the current organization. |
| 502 | + """ |
| 503 | + get_export_templates = functools.partial( |
| 504 | + self._entities_api.get_all_entities_export_templates, |
| 505 | + _check_return_type=False, |
| 506 | + ) |
| 507 | + export_templates = load_all_entities_dict(get_export_templates, camel_case=False) |
| 508 | + return [ |
| 509 | + CatalogExportTemplate.from_dict(export_template, camel_case=False) |
| 510 | + for export_template in export_templates["data"] |
| 511 | + ] |
| 512 | + |
427 | 513 | # Layout APIs |
428 | 514 |
|
429 | 515 | def get_declarative_notification_channels(self) -> list[CatalogDeclarativeNotificationChannel]: |
@@ -480,3 +566,36 @@ def put_declarative_identity_providers(self, identity_providers: list[CatalogDec |
480 | 566 | """ |
481 | 567 | api_idps = [idp.to_api() for idp in identity_providers] |
482 | 568 | self._layout_api.set_identity_providers(declarative_identity_provider=api_idps) |
| 569 | + |
| 570 | + def get_declarative_export_templates(self) -> list[CatalogDeclarativeExportTemplate]: |
| 571 | + """ |
| 572 | + Get all declarative export templates in the current organization. |
| 573 | +
|
| 574 | + Returns: |
| 575 | + list[CatalogDeclarativeExportTemplate]: |
| 576 | + List of declarative export templates. |
| 577 | + """ |
| 578 | + export_templates_api = self._layout_api.get_export_templates_layout() |
| 579 | + if hasattr(export_templates_api, "export_templates"): |
| 580 | + return [ |
| 581 | + CatalogDeclarativeExportTemplate.from_api(template) |
| 582 | + for template in export_templates_api.export_templates |
| 583 | + ] |
| 584 | + else: |
| 585 | + return [] |
| 586 | + |
| 587 | + def put_declarative_export_templates(self, export_templates: list[CatalogDeclarativeExportTemplate]) -> None: |
| 588 | + """ |
| 589 | + Put declarative export templates in the current organization. |
| 590 | +
|
| 591 | + Args: |
| 592 | + export_templates (list[CatalogDeclarativeExportTemplate]): |
| 593 | + List of declarative export templates. |
| 594 | +
|
| 595 | + Returns: |
| 596 | + None |
| 597 | + """ |
| 598 | + api_export_templates = [export_template.to_api() for export_template in export_templates] |
| 599 | + self._layout_api.set_export_templates( |
| 600 | + declarative_export_templates=DeclarativeExportTemplates(export_templates=api_export_templates) |
| 601 | + ) |
0 commit comments