forked from amundsen-io/amundsen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.py
238 lines (193 loc) · 9.1 KB
/
table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
import json
from http import HTTPStatus
from typing import Any, Iterable, Mapping, Optional, Union
from amundsen_common.entity.resource_type import ResourceType
from amundsen_common.models.lineage import LineageSchema
from amundsen_common.models.table import TableSchema
from flasgger import swag_from
from flask import request
from flask_restful import Resource, reqparse
from metadata_service.api import BaseAPI
from metadata_service.api.badge import BadgeCommon
from metadata_service.api.tag import TagCommon
from metadata_service.entity.dashboard_summary import DashboardSummarySchema
from metadata_service.exception import NotFoundException
from metadata_service.proxy import get_proxy_client
class TableDetailAPI(Resource):
"""
TableDetail API
"""
def __init__(self) -> None:
self.client = get_proxy_client()
@swag_from('swagger_doc/table/detail_get.yml')
def get(self, table_uri: str) -> Iterable[Union[Mapping, int, None]]:
try:
table = self.client.get_table(table_uri=table_uri)
schema = TableSchema()
return schema.dump(table), HTTPStatus.OK
except NotFoundException:
return {'message': 'table_uri {} does not exist'.format(table_uri)}, HTTPStatus.NOT_FOUND
class TableLineageAPI(Resource):
def __init__(self) -> None:
self.client = get_proxy_client()
self.parser = reqparse.RequestParser()
self.parser.add_argument('direction', type=str, required=False, default="both")
self.parser.add_argument('depth', type=int, required=False, default=1)
super(TableLineageAPI, self).__init__()
@swag_from('swagger_doc/table/lineage_get.yml')
def get(self, id: str) -> Iterable[Union[Mapping, int, None]]:
args = self.parser.parse_args()
direction = args.get('direction')
depth = args.get('depth')
try:
lineage = self.client.get_lineage(id=id,
resource_type=ResourceType.Table,
direction=direction,
depth=depth)
schema = LineageSchema()
return schema.dump(lineage), HTTPStatus.OK
except Exception as e:
return {'message': f'Exception raised when getting lineage: {e}'}, HTTPStatus.NOT_FOUND
class TableOwnerAPI(Resource):
"""
TableOwner API to add / delete owner info
"""
def __init__(self) -> None:
self.client = get_proxy_client()
@swag_from('swagger_doc/table/owner_put.yml')
def put(self, table_uri: str, owner: str) -> Iterable[Union[Mapping, int, None]]:
try:
self.client.add_owner(table_uri=table_uri, owner=owner)
return {'message': 'The owner {} for table_uri {} '
'is added successfully'.format(owner,
table_uri)}, HTTPStatus.OK
except Exception:
return {'message': 'The owner {} for table_uri {} '
'is not added successfully'.format(owner,
table_uri)}, HTTPStatus.INTERNAL_SERVER_ERROR
@swag_from('swagger_doc/table/owner_delete.yml')
def delete(self, table_uri: str, owner: str) -> Iterable[Union[Mapping, int, None]]:
try:
self.client.delete_owner(table_uri=table_uri, owner=owner)
return {'message': 'The owner {} for table_uri {} '
'is deleted successfully'.format(owner,
table_uri)}, HTTPStatus.OK
except Exception:
return {'message': 'The owner {} for table_uri {} '
'is not deleted successfully'.format(owner,
table_uri)}, HTTPStatus.INTERNAL_SERVER_ERROR
class TableDescriptionAPI(Resource):
"""
TableDescriptionAPI supports PUT and GET operation to upsert table description
"""
def __init__(self) -> None:
self.client = get_proxy_client()
super(TableDescriptionAPI, self).__init__()
@swag_from('swagger_doc/common/description_get.yml')
def get(self, id: str) -> Iterable[Any]:
"""
Returns description in Neo4j endpoint
"""
try:
description = self.client.get_table_description(table_uri=id)
return {'description': description}, HTTPStatus.OK
except NotFoundException:
return {'message': 'table_uri {} does not exist'.format(id)}, HTTPStatus.NOT_FOUND
except Exception:
return {'message': 'Internal server error!'}, HTTPStatus.INTERNAL_SERVER_ERROR
@swag_from('swagger_doc/common/description_put.yml')
def put(self, id: str) -> Iterable[Any]:
"""
Updates table description (passed as a request body)
:param table_uri:
:return:
"""
try:
description = json.loads(request.data).get('description')
self.client.put_table_description(table_uri=id, description=description)
return None, HTTPStatus.OK
except NotFoundException:
return {'message': 'table_uri {} does not exist'.format(id)}, HTTPStatus.NOT_FOUND
class TableTagAPI(Resource):
"""
TableTagAPI that supports GET, PUT and DELETE operation to add or delete tag
on table
"""
def __init__(self) -> None:
self.client = get_proxy_client()
self.parser = reqparse.RequestParser()
self.parser.add_argument('tag_type', type=str, required=False, default='default')
super(TableTagAPI, self).__init__()
self._tag_common = TagCommon(client=self.client)
@swag_from('swagger_doc/tag/tag_put.yml')
def put(self, id: str, tag: str) -> Iterable[Union[Mapping, int, None]]:
"""
API to add a tag to existing table uri.
:param table_uri:
:param tag:
:return:
"""
args = self.parser.parse_args()
# use tag_type to distinguish between tag and badge
tag_type = args.get('tag_type', 'default')
return self._tag_common.put(id=id,
resource_type=ResourceType.Table,
tag=tag,
tag_type=tag_type)
@swag_from('swagger_doc/tag/tag_delete.yml')
def delete(self, id: str, tag: str) -> Iterable[Union[Mapping, int, None]]:
"""
API to remove a association between a given tag and a table.
:param table_uri:
:param tag:
:return:
"""
args = self.parser.parse_args()
tag_type = args.get('tag_type', 'default')
return self._tag_common.delete(id=id,
resource_type=ResourceType.Table,
tag=tag,
tag_type=tag_type)
class TableBadgeAPI(Resource):
def __init__(self) -> None:
self.client = get_proxy_client()
self.parser = reqparse.RequestParser()
self.parser.add_argument('category', type=str, required=True)
super(TableBadgeAPI, self).__init__()
self._badge_common = BadgeCommon(client=self.client)
@swag_from('swagger_doc/badge/badge_put.yml')
def put(self, id: str, badge: str) -> Iterable[Union[Mapping, int, None]]:
args = self.parser.parse_args()
category = args.get('category', '')
return self._badge_common.put(id=id,
resource_type=ResourceType.Table,
badge_name=badge,
category=category)
@swag_from('swagger_doc/badge/badge_delete.yml')
def delete(self, id: str, badge: str) -> Iterable[Union[Mapping, int, None]]:
args = self.parser.parse_args()
category = args.get('category', '')
return self._badge_common.delete(id=id,
resource_type=ResourceType.Table,
badge_name=badge,
category=category)
class TableDashboardAPI(BaseAPI):
"""
TableDashboard API that supports GET operation providing list of Dashboards using a table.
"""
def __init__(self) -> None:
self.client = get_proxy_client()
super().__init__(DashboardSummarySchema, 'resources_using_table', self.client)
@swag_from('swagger_doc/table/dashboards_using_table_get.yml')
def get(self, *, id: Optional[str] = None) -> Iterable[Union[Mapping, int, None]]:
"""
Supports GET operation providing list of Dashboards using a table.
:param id: Table URI
:return: See Swagger doc for the schema. swagger_doc/table/dashboards_using_table_get.yml
"""
try:
return super().get_with_kwargs(id=id, resource_type=ResourceType.Dashboard)
except NotFoundException:
return {'message': 'table_id {} does not exist'.format(id)}, HTTPStatus.NOT_FOUND