Skip to content

Commit fe3335e

Browse files
authored
feat: Add mode_user_id into User detail API (#141)
* Add mode_user_id into User detail API * Update * Update * Update
1 parent df756a7 commit fe3335e

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

metadata_service/api/swagger_doc/template.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ components:
100100
manager_fullname:
101101
type: string
102102
description: 'User manager full name'
103+
other_key_values:
104+
type: object
105+
additionalProperties:
106+
type: string
107+
description: 'Additional key value properties'
103108
ColumnFields:
104109
type: object
105110
properties:

metadata_service/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import distutils.util
22
import os
3-
from typing import List, Dict, Optional # noqa: F401
3+
from typing import List, Dict, Optional, Set # noqa: F401
44

55
# PROXY configuration keys
66
PROXY_HOST = 'PROXY_HOST'
@@ -17,6 +17,7 @@
1717
}
1818

1919
IS_STATSD_ON = 'IS_STATSD_ON'
20+
USER_OTHER_KEYS = 'USER_OTHER_KEYS'
2021

2122

2223
class Config:
@@ -56,9 +57,12 @@ class Config:
5657

5758
USER_DETAIL_METHOD = None # type: Optional[function]
5859

60+
# On User detail method, these keys will be added into amundsen_common.models.user.User.other_key_values
61+
USER_OTHER_KEYS = {'mode_user_id'} # type: Set[str]
62+
5963

6064
class LocalConfig(Config):
61-
DEBUG = False
65+
DEBUG = True
6266
TESTING = False
6367
LOG_LEVEL = 'DEBUG'
6468
LOCAL_HOST = '0.0.0.0'

metadata_service/proxy/neo4j_proxy.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import (Any, Dict, List, Optional, Tuple, Union, # noqa: F401
66
no_type_check)
77

8+
import neo4j
89
from amundsen_common.models.dashboard import DashboardSummary
910
from amundsen_common.models.popular_table import PopularTable
1011
from amundsen_common.models.table import (Application, Column, Reader, Source,
@@ -14,9 +15,10 @@
1415
from amundsen_common.models.user import User as UserEntity
1516
from beaker.cache import CacheManager
1617
from beaker.util import parse_cache_config_options
18+
from flask import current_app, has_app_context
1719
from neo4j import BoltStatementResult, Driver, GraphDatabase # noqa: F401
18-
import neo4j
1920

21+
from metadata_service import config
2022
from metadata_service.entity.dashboard_detail import DashboardDetail as DashboardDetailEntity
2123
from metadata_service.entity.dashboard_query import DashboardQuery as DashboardQueryEntity
2224
from metadata_service.entity.description import Description
@@ -60,6 +62,7 @@ def __init__(self, *,
6062
value needs to be smaller than surrounding network environment's timeout.
6163
"""
6264
endpoint = f'{host}:{port}'
65+
LOGGER.info('NEO4J endpoint: {}'.format(endpoint))
6366
trust = neo4j.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES if validate_ssl else neo4j.TRUST_ALL_CERTIFICATES
6467
self._driver = GraphDatabase.driver(endpoint, max_connection_pool_size=num_conns,
6568
connection_timeout=10,
@@ -817,6 +820,19 @@ def get_users(self) -> List[UserEntity]:
817820

818821
@staticmethod
819822
def _build_user_from_record(record: dict, manager_name: str = '') -> UserEntity:
823+
"""
824+
Builds user record from Cypher query result. Other than the one defined in amundsen_common.models.user.User,
825+
you could add more fields from User node into the User model by specifying keys in config.USER_OTHER_KEYS
826+
:param record:
827+
:param manager_name:
828+
:return:
829+
"""
830+
other_key_values = {}
831+
if has_app_context() and current_app.config[config.USER_OTHER_KEYS]:
832+
for k in current_app.config[config.USER_OTHER_KEYS]:
833+
if k in record:
834+
other_key_values[k] = record[k]
835+
820836
return UserEntity(email=record['email'],
821837
first_name=record.get('first_name'),
822838
last_name=record.get('last_name'),
@@ -827,7 +843,8 @@ def _build_user_from_record(record: dict, manager_name: str = '') -> UserEntity:
827843
slack_id=record.get('slack_id'),
828844
employee_type=record.get('employee_type'),
829845
role_name=record.get('role_name'),
830-
manager_fullname=record.get('manager_fullname', manager_name))
846+
manager_fullname=record.get('manager_fullname', manager_name),
847+
other_key_values=other_key_values)
831848

832849
@staticmethod
833850
def _get_user_resource_relationship_clause(relation_type: UserResourceRel, id: str = None,

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from setuptools import setup, find_packages
44

5-
__version__ = '2.5.1'
5+
__version__ = '2.5.2'
66

77

88
requirements_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'requirements.txt')

tests/unit/proxy/test_neo4j_proxy.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,30 @@ def test_get_user(self) -> None:
538538
neo4j_user = neo4j_proxy.get_user(id='test_email')
539539
self.assertEquals(neo4j_user.email, 'test_email')
540540

541+
def test_get_user_other_key_values(self) -> None:
542+
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
543+
mock_execute.return_value.single.return_value = {
544+
'user_record': {
545+
'employee_type': 'teamMember',
546+
'full_name': 'test_full_name',
547+
'is_active': 'True',
548+
'github_username': 'test-github',
549+
'slack_id': 'test_id',
550+
'last_name': 'test_last_name',
551+
'first_name': 'test_first_name',
552+
'team_name': 'test_team',
553+
'email': 'test_email',
554+
'mode_user_id': 'mode_foo_bar',
555+
'etc': 'etc_foo_bar',
556+
},
557+
'manager_record': {
558+
'full_name': 'test_manager_fullname'
559+
}
560+
}
561+
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
562+
neo4j_user = neo4j_proxy.get_user(id='test_email')
563+
self.assertEquals(neo4j_user.other_key_values, {'mode_user_id': 'mode_foo_bar'})
564+
541565
def test_get_users(self) -> None:
542566
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
543567
test_user = {

0 commit comments

Comments
 (0)