Skip to content

Commit

Permalink
Merge pull request #381 from hkad98/psdk-161
Browse files Browse the repository at this point in the history
RELEASED: PSDK-161 add organizationSettings, cspDirectives support

Reviewed-by: Jan Soubusta
             https://github.com/jaceksan
  • Loading branch information
gdgate authored Oct 11, 2023
2 parents 25e98c9 + 8aad95f commit 7be4fd9
Show file tree
Hide file tree
Showing 14 changed files with 4,457 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gooddata-sdk/gooddata_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@
ExportSettings,
)
from gooddata_sdk.catalog.identifier import CatalogDatasetWorkspaceDataFilterIdentifier, CatalogWorkspaceIdentifier
from gooddata_sdk.catalog.organization.entity_model.directive import CatalogCspDirective
from gooddata_sdk.catalog.organization.entity_model.jwk import (
CatalogJwk,
CatalogJwkAttributes,
CatalogJwkDocument,
CatalogRsaSpecification,
)
from gooddata_sdk.catalog.organization.entity_model.organization import CatalogOrganization
from gooddata_sdk.catalog.organization.entity_model.setting import CatalogOrganizationSetting
from gooddata_sdk.catalog.organization.service import CatalogOrganizationService
from gooddata_sdk.catalog.permission.declarative_model.dashboard_assignees import (
CatalogAvailableAssignees,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# (C) 2023 GoodData Corporation
from __future__ import annotations

from typing import List, Type

import attr

from gooddata_api_client.model.json_api_csp_directive_in import JsonApiCspDirectiveIn
from gooddata_api_client.model.json_api_csp_directive_in_attributes import JsonApiCspDirectiveInAttributes
from gooddata_sdk.catalog.base import Base


@attr.s(auto_attribs=True, kw_only=True)
class CatalogCspDirective(Base):
id: str
attributes: CatalogCspDirectiveAttributes

@classmethod
def init(cls, directive_id: str, sources: List[str]) -> CatalogCspDirective:
return cls(id=directive_id, attributes=CatalogCspDirectiveAttributes(sources=sources))

@staticmethod
def client_class() -> Type[JsonApiCspDirectiveIn]:
return JsonApiCspDirectiveIn


@attr.s(auto_attribs=True, kw_only=True)
class CatalogCspDirectiveAttributes(Base):
sources: List[str]

@staticmethod
def client_class() -> Type[JsonApiCspDirectiveInAttributes]:
return JsonApiCspDirectiveInAttributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# (C) 2023 GoodData Corporation
from __future__ import annotations

from typing import Any, Dict, Optional, Type

import attr

from gooddata_api_client.model.json_api_organization_setting_in import JsonApiOrganizationSettingIn
from gooddata_api_client.model.json_api_organization_setting_in_attributes import JsonApiOrganizationSettingInAttributes
from gooddata_sdk.catalog.base import Base


@attr.s(auto_attribs=True, kw_only=True)
class CatalogOrganizationSetting(Base):
id: str
attributes: CatalogOrganizationSettingAttributes

@classmethod
def init(cls, setting_id: str, setting_type: str, content: Dict[str, Any]) -> CatalogOrganizationSetting:
return cls(id=setting_id, attributes=CatalogOrganizationSettingAttributes(type=setting_type, content=content))

@staticmethod
def client_class() -> Type[JsonApiOrganizationSettingIn]:
return JsonApiOrganizationSettingIn


@attr.s(auto_attribs=True, kw_only=True)
class CatalogOrganizationSettingAttributes(Base):
type: Optional[str]
content: Dict[str, Any]

@staticmethod
def client_class() -> Type[JsonApiOrganizationSettingInAttributes]:
return JsonApiOrganizationSettingInAttributes
192 changes: 191 additions & 1 deletion gooddata-sdk/gooddata_sdk/catalog/organization/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
from typing import List, Optional

from gooddata_api_client.exceptions import NotFoundException
from gooddata_api_client.model.json_api_csp_directive_in_document import JsonApiCspDirectiveInDocument
from gooddata_api_client.model.json_api_organization_setting_in_document import JsonApiOrganizationSettingInDocument
from gooddata_sdk.catalog.catalog_service_base import CatalogServiceBase
from gooddata_sdk.catalog.organization.entity_model.directive import CatalogCspDirective
from gooddata_sdk.catalog.organization.entity_model.jwk import CatalogJwk, CatalogJwkDocument
from gooddata_sdk.catalog.organization.entity_model.organization import CatalogOrganizationDocument
from gooddata_sdk.catalog.organization.entity_model.setting import CatalogOrganizationSetting
from gooddata_sdk.client import GoodDataApiClient
from gooddata_sdk.utils import load_all_entities

Expand Down Expand Up @@ -65,6 +69,21 @@ def update_name(self, name: str) -> None:
organization_document = CatalogOrganizationDocument(data=organization)
self._entities_api.update_entity_organizations(organization.id, organization_document.to_api())

def update_allowed_origins(self, allowed_origins: List[str]) -> None:
"""Updates the allowed origins of the organization.
Args:
allowed_origins (List[str]):
New allowed origins of the organization
Returns:
None
"""
organization = self.get_organization()
organization.attributes.allowed_origins = allowed_origins
organization_document = CatalogOrganizationDocument(data=organization)
self._entities_api.update_entity_organizations(organization.id, organization_document.to_api())

def create_or_update_jwk(self, jwk: CatalogJwk) -> None:
"""Create a new jwk or overwrite an existing jwk with the same id.
Expand Down Expand Up @@ -119,7 +138,7 @@ def delete_jwk(self, jwk_id: str) -> None:
raise ValueError(f"Can not delete {jwk_id} jwk. This jwk does not exist.")

def list_jwks(self) -> List[CatalogJwk]:
"""Returns a list of all jwks in current organization.
"""Returns a list of all jwks in the current organization.
Returns:
List[CatalogJwk]:
Expand All @@ -128,3 +147,174 @@ def list_jwks(self) -> List[CatalogJwk]:
get_jwks = functools.partial(self._entities_api.get_all_entities_jwks, _check_return_type=False)
jwks = load_all_entities(get_jwks)
return [CatalogJwk.from_api(jwk) for jwk in jwks.data]

def list_organization_settings(self) -> List[CatalogOrganizationSetting]:
"""Returns a list of all organization settings in the current organization.
Returns:
List[CatalogOrganizationSettings]:
List of organization settings in the current organization.
"""
get_organization_settings = functools.partial(
self._entities_api.get_all_entities_organization_settings, _check_return_type=False
)
organization_settings = load_all_entities(get_organization_settings)
return [
CatalogOrganizationSetting.from_api(organization_settings)
for organization_settings in organization_settings.data
]

def get_organization_setting(self, organization_setting_id: str) -> CatalogOrganizationSetting:
"""Get an individual organization setting.
Args:
organization_setting_id (str):
Organization setting identification string e.g. "demo"
Returns:
CatalogOrganizationSettings:
Catalog organization setting object containing structure of the organization setting.
"""
organization_setting_api = self._entities_api.get_entity_organization_settings(id=organization_setting_id).data
return CatalogOrganizationSetting.from_api(organization_setting_api)

def create_organization_setting(self, organization_setting: CatalogOrganizationSetting) -> None:
"""Create a new organization setting.
Args:
organization_setting (CatalogOrganizationSettings):
A catalog organization setting an object to be created.
Returns:
None
"""
organization_setting_document = JsonApiOrganizationSettingInDocument(data=organization_setting.to_api())
self._entities_api.create_entity_organization_settings(
json_api_organization_setting_in_document=organization_setting_document
)

def delete_organization_setting(self, organization_setting_id: str) -> None:
"""Delete an organization setting.
Args:
organization_setting_id (str):
Organization setting identification string e.g. "demo"
Returns:
None
Raises:
ValueError:
Organization setting does not exist.
"""
try:
self._entities_api.delete_entity_organization_settings(organization_setting_id)
except NotFoundException:
raise ValueError(
f"Can not delete {organization_setting_id} organization setting. "
f"This organization setting does not exist."
)

def update_organization_setting(self, organization_setting: CatalogOrganizationSetting) -> None:
"""Update an organization setting.
Args:
organization_setting (CatalogOrganizationSettings):
A catalog organization setting an object to be updated.
Returns:
None
Raises:
ValueError:
Organization setting does not exist.
"""
try:
organization_setting_document = JsonApiOrganizationSettingInDocument(data=organization_setting.to_api())
self._entities_api.update_entity_organization_settings(
organization_setting.id, organization_setting_document
)
except NotFoundException:
raise ValueError(
f"Can not update {organization_setting.id} organization setting. "
f"This organization setting does not exist."
)

def list_csp_directives(self) -> List[CatalogCspDirective]:
"""Returns a list of all csp directives in the current organization.
Returns:
List[CatalogOrganizationSettings]:
List of csp directives in the current organization.
"""
get_csp_directives = functools.partial(
self._entities_api.get_all_entities_csp_directives, _check_return_type=False
)
csp_directives = load_all_entities(get_csp_directives)
return [CatalogCspDirective.from_api(csp_directive) for csp_directive in csp_directives.data]

def get_csp_directive(self, directive_id: str) -> CatalogCspDirective:
"""Get an individual csp directive.
Args:
directive_id (str):
Csp directive identification string e.g. "demo"
Returns:
CatalogCspDirective:
Catalog csp directive object containing structure of the csp directive.
"""
csp_directive_api = self._entities_api.get_entity_csp_directives(id=directive_id).data
return CatalogCspDirective.from_api(csp_directive_api)

def create_csp_directive(self, csp_directive: CatalogCspDirective) -> None:
"""Create a new csp directive.
Args:
csp_directive (CatalogCspDirective):
A catalog csp directive an object to be created.
Returns:
None
"""
csp_directive_document = JsonApiCspDirectiveInDocument(data=csp_directive.to_api())
self._entities_api.create_entity_csp_directives(json_api_csp_directive_in_document=csp_directive_document)

def delete_csp_directive(self, csp_directive_id: str) -> None:
"""Delete a csp directive.
Args:
csp_directive_id (str):
Csp directive identification string e.g. "demo"
Returns:
None
Raises:
ValueError:
Csp directive does not exist.
"""
try:
self._entities_api.delete_entity_csp_directives(csp_directive_id)
except NotFoundException:
raise ValueError(f"Can not delete {csp_directive_id} csp directive. " f"This csp directive does not exist.")

def update_csp_directive(self, csp_directive: CatalogCspDirective) -> None:
"""Update a csp directive.
Args:
csp_directive (CatalogCspDirective):
A catalog csp directive an object to be updated.
Returns:
None
Raises:
ValueError:
Csp directive does not exist.
"""
try:
csp_directive_document = JsonApiCspDirectiveInDocument(data=csp_directive.to_api())
self._entities_api.update_entity_csp_directives(csp_directive.id, csp_directive_document)
except NotFoundException:
raise ValueError(f"Can not update {csp_directive.id} csp directive. " f"This csp directive does not exist.")
Loading

0 comments on commit 7be4fd9

Please sign in to comment.