Skip to content

Commit

Permalink
Merge pull request #10789 from opaduchak/fix/wb-settings-gravyvalet
Browse files Browse the repository at this point in the history
Implemented fetching settings from GV side
  • Loading branch information
brianjgeiger authored Nov 2, 2024
2 parents 1a01806 + df09420 commit 9113097
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
20 changes: 5 additions & 15 deletions osf/external/gravy_valet/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from addons.gitlab.apps import GitLabAddonConfig
from addons.googledrive.apps import GoogleDriveAddonConfig
from addons.s3.apps import S3AddonAppConfig
from addons.s3.settings import ENCRYPT_UPLOADS_DEFAULT
from . import request_helpers as gv_requests


Expand Down Expand Up @@ -95,6 +94,7 @@ class EphemeralNodeSettings:

# retrieved from WB on-demand and cached
_credentials: dict = None
_config: dict = None

@property
def short_name(self):
Expand Down Expand Up @@ -142,20 +142,9 @@ def serialize_waterbutler_credentials(self):
return self._credentials

def serialize_waterbutler_settings(self):
# sufficient for Box
# TODO: Define per-service translation (and/or common schemes)
match self.wb_key:
case _LegacyConfigsForWBKey.s3.name:
return {
'bucket': self.folder_id.split(':/')[0],
'id': self.folder_id,
'encrypt_uploads': ENCRYPT_UPLOADS_DEFAULT,
}

return {
'folder': self.folder_id.split(':')[1],
'service': self.short_name,
}
if self._config is None:
self._fetch_wb_config()
return self._config

def _fetch_wb_config(self):
result = gv_requests.get_waterbutler_config(
Expand All @@ -164,6 +153,7 @@ def _fetch_wb_config(self):
requesting_user=self.active_user
)
self._credentials = result.get_attribute('credentials')
self._config = result.get_attribute('config')

def create_waterbutler_log(self, *args, **kwargs):
pass
Expand Down
28 changes: 28 additions & 0 deletions osf_tests/external/gravy_valet/gv_fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ def _serialize_relationships(self):
accounts_relationship = self._format_relationship_entry(relationship_path='authorized_storage_accounts')
return {'authorized_storage_accounts': accounts_relationship}

@dataclasses.dataclass(frozen=True)
class _FakeWBCredentials(_FakeGVEntity):

RESOURCE_TYPE = 'waterbutler-credentials'
config: dict

def _serialize_attributes(self):
return {'config': self.config}

def _serialize_relationships(self):
return {}

@dataclasses.dataclass(frozen=True)
class _FakeResourceReference(_FakeGVEntity):

Expand Down Expand Up @@ -209,6 +221,7 @@ class FakeGravyValet:
r'v1/resource-references(/(?P<pk>\d+)|(\?filter\[resource_uri\]=(?P<resource_uri>[^&]+)))': '_get_resource',
r'v1/authorized-storage-accounts/(?P<pk>\d+)': '_get_account',
r'v1/configured-storage-addons/(?P<pk>\d+)': '_get_addon',
r'v1/configured-storage-addons/(?P<pk>\d+)/waterbutler-credentials': '_get_wb_settings',
r'v1/user-references/(?P<user_pk>\d+)/authorized_storage_accounts': '_get_user_accounts',
r'v1/resource-references/(?P<resource_pk>\d+)/configured_storage_addons': '_get_resource_addons',
}
Expand Down Expand Up @@ -374,6 +387,21 @@ def _get_user(
include_param=include_param,
)

def _get_wb_settings(
self,
headers: dict,
pk: str,
include_param: str = '',
) -> str:
creds = _FakeWBCredentials(
pk=10,
config={
'folder': pk,
'service': 'box',
}
)
return _format_response_body(creds)

def _get_resource(
self,
headers: dict,
Expand Down
20 changes: 10 additions & 10 deletions osf_tests/test_gv_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,13 @@ def test_make_ephemeral_node_settings(self, contributor, project, fake_box_addon
requesting_user=contributor,
requested_resource=project,
)
ephemeral_config = translations.make_ephemeral_node_settings(
addon_data, requesting_user=contributor, requested_resource=project
)
assert ephemeral_config.short_name == 'box'
assert ephemeral_config.gv_id == fake_box_addon.pk
assert ephemeral_config.config.name == 'addons.box'
assert ephemeral_config.serialize_waterbutler_settings() == {
'folder': fake_box_addon.root_folder.split(':')[1],
'service': 'box'
}
ephemeral_config = translations.make_ephemeral_node_settings(
addon_data, requesting_user=contributor, requested_resource=project
)
assert ephemeral_config.short_name == 'box'
assert ephemeral_config.gv_id == fake_box_addon.pk
assert ephemeral_config.config.name == 'addons.box'
assert ephemeral_config.serialize_waterbutler_settings() == {
'folder': fake_box_addon.root_folder.split(':')[1],
'service': 'box'
}

0 comments on commit 9113097

Please sign in to comment.