This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdsctl.py
executable file
·363 lines (302 loc) · 11.4 KB
/
dsctl.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python
# Copyright (c) 2022 Snowplow Analytics Ltd. All rights reserved.
#
# This program is licensed to you under the Apache License Version 2.0,
# and you may not use this file except in compliance with the Apache License Version 2.0.
# You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the Apache License Version 2.0 is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
import os
from dataclasses import dataclass
from enum import Enum
from json import JSONDecodeError, dumps, load
from os.path import join, dirname
import logging
import sys
import argparse
from typing import Dict, Literal, Optional, TextIO, cast
from dotenv import load_dotenv
from requests import get, post, RequestException, Response
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
class CLIArguments(argparse.Namespace):
token_only: bool
token: str | None
file: TextIO
type: Literal["event", "entity"]
includes_meta: bool
promote_to_dev: bool
promote_to_prod: bool
allow_patch: bool
message: str | None
@dataclass
class Config:
console_host: str
organization_id: str
api_key: str
base_url: str
ds_url: str
@dataclass
class DataStructure:
vendor: str
name: str
format: str
@dataclass
class Version:
model: int
revision: int
addition: int
def __str__(self) -> str:
return f"{self.model}-{self.revision}-{self.addition}"
@dataclass
class Deployment:
data_structure: DataStructure
version: Version
class SchemaType(str, Enum):
EVENT = 'event'
ENTITY = 'entity'
def get_config() -> Optional[Config]:
"""Returns an endpoint configuration object"""
try:
org_id = os.environ['CONSOLE_ORGANIZATION_ID']
api_key = os.environ['CONSOLE_API_KEY']
except KeyError:
logger.error("Environment variables CONSOLE_ORGANIZATION_ID and/or CONSOLE_API_KEY are not set")
return None
host = os.environ.get('CONSOLE_HOST', 'console')
base_url = f"https://{host}.snowplowanalytics.com/api/msc/v1/organizations/{org_id}"
return Config(
console_host=host,
organization_id=org_id,
api_key=api_key,
base_url=base_url,
ds_url=f"{base_url}/data-structures/v1"
)
def get_token(config: Config) -> Optional[str]:
"""
Retrieves a JWT from BDP Console.
:return: The token
"""
response = None
body = None
try:
response = get(
f"{config.base_url}/credentials/v2/token",
headers={"X-API-Key": config.api_key}
)
body = response.json()
if not isinstance(body, dict):
raise TypeError()
return cast(str, body["accessToken"])
except RequestException as e:
logger.error(f"Could not contact BDP Console: {e}")
return None
except JSONDecodeError:
logger.error(f"get_token: Response was not valid JSON: {response and response.text}")
return None
except (KeyError, TypeError):
logger.error(f"get_token: Invalid response body: {dumps(body, indent=2)}")
return None
def get_base_headers(auth_token: str) -> Dict[str, str]:
return {
"Authorization": f"Bearer {auth_token}"
}
def handle_response(response: Response, action: str) -> bool:
"""
Generic response handler for validation and promotion operations. Confirms that it all went well.
:param response: The Response object to operate on
:param action: The action ('validation' or 'promotion') that created the Response object
:return: None
"""
if response.ok:
try:
body = response.json()
if not isinstance(body, dict) or not body.get("success"):
logger.error(f"Data structure {action} failed: {body}")
return False
return True
except JSONDecodeError:
logger.error(f"handle_response: Response was not valid JSON: {response.text}")
return False
else:
logger.error(f"Data structure {action} failed: {response.text}")
return False
def validate(config: Config, data_structure: dict, auth_token: str, stype: str, contains_meta: bool) -> bool:
"""
Validates a data structure against the BDP API.
:param config: Endpoint configuration object
:param data_structure: A dictionary representing the data structure
:param auth_token: The JWT to use
:param stype: The type of the data structure (event or entity)
:param contains_meta: A flag to indicate whether the `meta` section already exists in the dictionary
:return:
"""
if stype not in (SchemaType.EVENT, SchemaType.ENTITY):
logger.error('Data structure type must be either "event" or "entity"')
return False
try:
response = post(
f"{config.ds_url}/validation-requests",
json={
"meta": {
"hidden": False,
"schemaType": stype,
"customData": {}
},
"data": data_structure
} if not contains_meta else data_structure,
headers=get_base_headers(auth_token)
)
except RequestException as e:
logger.error(f"Could not contact BDP Console: {e}")
return False
return handle_response(response, 'validation')
def promote(
config: Config,
deployment: Deployment,
auth_token: str,
deployment_message: str,
to_production: bool = False,
request_patch: bool = False,
) -> bool:
"""
Promotes a data structure to staging or production.
:param config: Endpoint configuration object
:param deployment: The Deployment class to use
:param auth_token: The JWT to use
:param deployment_message: A message describing the changes applied to the data structure
:param to_production: A flag to indicate if the data structure should be deployed to production (default: staging)
:param request_patch: A flag to indicate if the data structure deployment should request patch support (default: False)
:return: None
"""
try:
response = post(
f"{config.ds_url}/deployment-requests",
json={
"name": deployment.data_structure.name,
"vendor": deployment.data_structure.vendor,
"format": deployment.data_structure.format,
"version": "{}-{}-{}".format(deployment.version.model, deployment.version.revision,
deployment.version.addition),
"source": "VALIDATED" if not to_production else "DEV",
"target": "DEV" if not to_production else "PROD",
"message": deployment_message
},
params=dict(patch=request_patch),
headers=get_base_headers(auth_token),
)
except RequestException as e:
logger.error(f"Could not contact BDP Console: {e}")
return False
return handle_response(response, 'promotion')
def resolve(data_structure: dict | None, includes_meta: bool) -> Optional[Deployment]:
"""
Reads a data structure and extracts the self-describing section.
:param data_structure: A dictionary representing the data structure
:param includes_meta: A flag to indicate whether the `meta` section already exists in the dictionary
:return: A Deployment instance
"""
try:
if not isinstance(data_structure, dict):
raise TypeError()
_self = (
data_structure["self"]
if not includes_meta
else data_structure["data"]["self"]
)
vendor = _self['vendor']
name = _self['name']
ds_format = _self['format']
version = _self['version']
ds = DataStructure(vendor, name, ds_format)
v = Version(*version.split('-'))
return Deployment(ds, v)
except (ValueError, TypeError):
logger.error("Data structure spec is incorrect: Vendor, name, format or version is invalid")
return None
except KeyError:
logger.error("Data structure does not include a correct 'self' element")
return None
def parse_arguments() -> CLIArguments:
"""Parses and returns CLI parameters"""
parser = argparse.ArgumentParser()
parser.add_argument("--token-only", action="store_true", help="only get an access token and print it on stdout")
parser.add_argument("--token", type=str, help="use this token to authenticate")
parser.add_argument(
"--file",
type=argparse.FileType(),
default="-",
help="read data structure from file (absolute path) instead of stdin",
)
parser.add_argument(
"--type", choices=("event", "entity"), default="event", help="document type"
)
parser.add_argument("--includes-meta", action="store_true",
help="the input document already contains the meta field")
parser.add_argument("--promote-to-dev", action="store_true",
help="promote from validated to dev; reads parameters from stdin or --file parameter")
parser.add_argument("--promote-to-prod", action="store_true",
help="promote from dev to prod; reads parameters from stdin or --file parameter")
parser.add_argument(
"--allow-patch",
action="store_true",
help="request patch support in promotion request",
)
parser.add_argument("--message", type=str, help="message to add to version deployment")
return parser.parse_args(namespace=CLIArguments())
def parse_input_file(file: TextIO) -> Optional[dict]:
"""
Loads schema from a file or standard input.
:param file: File to read from
:return: The schema JSON
"""
try:
return load(file)
except JSONDecodeError as e:
logger.error(f"Provided input is not valid JSON: {e}")
return None
except Exception as e:
logger.error(f"Could not read {file.name if file.name else 'stdin'}: {e}")
return None
finally:
file.close()
def flow(args: CLIArguments, config: Config) -> bool:
"""Main operation actually invoking the DS API to validate or promote a data structure"""
message = args.message if args.message else "No message provided"
token = args.token if args.token else get_token(config)
schema = parse_input_file(args.file)
schema_type = args.type
spec = resolve(schema, args.includes_meta)
if not token or not schema or not spec:
return False
if args.promote_to_dev or args.promote_to_prod:
return promote(
config,
spec,
token,
message,
to_production=args.promote_to_prod,
request_patch=args.allow_patch,
)
else:
return validate(config, schema, token, schema_type, args.includes_meta)
def main() -> None:
arguments = parse_arguments()
config = get_config()
if not config:
sys.exit(1)
if arguments.token_only:
token = get_token(config)
if not token:
sys.exit(1)
sys.stdout.write(token)
else:
if not flow(arguments, config):
sys.exit(1)
if __name__ == "__main__":
main()