Skip to content

Commit

Permalink
improve gravyvalet integration
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed May 13, 2024
1 parent 671f188 commit 074878f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
30 changes: 22 additions & 8 deletions addons/base/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import requests
import markupsafe
from os.path import basename
from website.settings import MFR_SERVER_URL
Expand Down Expand Up @@ -58,18 +59,31 @@ def format_last_known_metadata(auth, node, file, error_type):

class GravyValetAddonAppConfig:
class MockNodeSetting:
def __init__(self, resource, request, legacy_config):
...
def __init__(self, resource, auth, legacy_config):
resp = requests.get(
settings.GV_RESOURCE_DOMAIN.format(owner_uri=resource.absolute_url),
auth=auth
)

resp.raise_for_status()
data = resp.json()['data']

requests.get(data['relationships'], auth=auth)

class MockUserSetting:
def __init__(self, resource, request, legacy_config):
...
def __init__(self, resource, auth, legacy_config):
requests.get(
settings.GV_USER_DOMAIN.format(owner_uri=resource.absolute_url),
auth=auth
)

def __init__(self, gravyvalet_data, resource, auth):
self.gravyvalet_data = gravyvalet_data
def __init__(self, data, external_storage_service_data, resource, auth):
self.data = data

self.external_storage_service_data = external_storage_service_data
# TODO: Names in GV must be exact matches?
self.legacy_config = settings.ADDONS_AVAILABLE_DICT[self.gravyvalet_data['data']['attributes']['name']]
self.name = self.external_storage_service_data['data']['attributes']['name']
self.legacy_config = settings.ADDONS_AVAILABLE_DICT[self.name]
self.resource = resource
self.auth = auth
self.FOLDER_SELECTED = self.legacy_config.FOLDER_SELECTED
Expand All @@ -87,4 +101,4 @@ def user_settings(self):

@property
def configured(self):
return self.legacy_config.configured
return True
3 changes: 0 additions & 3 deletions api_tests/providers/test_files_with_gv.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from osf import features
from api.base.settings.defaults import API_BASE
from api_tests import utils as api_utils
from osf_tests.factories import (
AuthUserFactory,
Expand Down Expand Up @@ -150,5 +149,3 @@ def test_get_file_provider(self, app, user, file_url, file, provider_gv_id):
assert attributes['provider'] == str(provider_gv_id)
assert attributes['name'] == str(provider_gv_id)
assert res.json['data']['id'] == f'{file.target._id}:{str(provider_gv_id)}'


14 changes: 10 additions & 4 deletions osf/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2441,14 +2441,20 @@ def get_addon(self, name, is_deleted=False):
if 'node' in addon.added_default:
default_addons.append(addon.short_name)

if waffle.flag_is_active(request, features.ENABLE_GV) and name not in ['osfstorage', 'wiki']:
if hasattr(request, 'user') and not isinstance(request.user, AnonymousUser) and waffle.flag_is_active(request, features.ENABLE_GV) and name not in ['osfstorage', 'wiki']:
resp = requests.get(
settings.GV_EXTERNAL_STORAGE_ENDPOINT.format(service_id=name),
settings.GV_NODE_ADDON_ENDPOINT.format(addon_id=name),
auth=(request.user.username, request.user.password)
)
data = resp.json()
configured_storage_addon = resp.json()
resp = requests.get(
configured_storage_addon['data']['relationships']['external_storage_service']['links']['related'],
auth=(request.user.username, request.user.password)
)
external_storage_service_data = resp.json()
return GravyValetAddonAppConfig(
data,
configured_storage_addon,
external_storage_service_data,
self,
auth=(request.user.username, request.user.password)
)
Expand Down
28 changes: 18 additions & 10 deletions osf/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,25 +1225,33 @@ def create_unregistered(cls, fullname, email=None):

return user

def get_addon(self, name, is_deleted=False, auth=None):
def get_addon(self, name, is_deleted=False):
request, user_id = get_request_and_user_id()

default_addons = ['wiki']
for addon in website_settings.ADDONS_AVAILABLE:
if 'user' in addon.added_default:
for addon in settings.ADDONS_AVAILABLE:
if 'node' in addon.added_default:
default_addons.append(addon.short_name)

if waffle.flag_is_active(request, features.ENABLE_GV) and name not in ['osfstorage', 'wiki']:
if hasattr(request, 'user') and waffle.flag_is_active(request, features.ENABLE_GV) and name not in ['osfstorage', 'wiki']:
resp = requests.get(
settings.GV_NODE_ADDON_ENDPOINT.format(addon_id=name),
auth=(request.user.username, request.user.password)
)
configured_storage_addon = resp.json()
resp = requests.get(
website_settings.GV_USER_ADDON_ENDPOINT.format(account_id=name),
auth=auth
configured_storage_addon['data']['relationships']['external_storage_service']['links']['related'],
auth=(request.user.username, request.user.password)
)
external_storage_service_data = resp.json()
return GravyValetAddonAppConfig(
configured_storage_addon,
external_storage_service_data,
self,
auth=(request.user.username, request.user.password)
)
resp.raise_for_status()
data = resp.json()['data']
return GravyValetAddonAppConfig(data, self, auth)
else:
return super().get_addon(name, is_deleted)

def update_guessed_names(self):
"""Updates the CSL name fields inferred from the the full name.
"""
Expand Down
2 changes: 1 addition & 1 deletion website/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -2149,5 +2149,5 @@ def from_node_usage(cls, usage_bytes, private_limit=None, public_limit=None):
GV_USER_ENDPOINT = GV_API_ROOT + 'user-references/?filter[user_uri]={owner_uri}'
# These two are for `get_addon` vs `get_addons`
GV_USER_ADDON_ENDPOINT = 'http://192.168.168.167:8004/v1/authorized-storage-accounts/{account_id}'
GV_NODE_ADDON_ENDPOINT = 'http://192.168.168.167:8004/v1/configured-storage-addons/{addon_id}'
GV_NODE_ADDON_ENDPOINT = 'http://192.168.168.167:8004/v1/configured-storage-addons/{addon_id}/base_account/'
GV_EXTERNAL_STORAGE_ENDPOINT = 'http://192.168.168.167:8004/v1/external-storage-services/{service_id}'

0 comments on commit 074878f

Please sign in to comment.