api-foundry 0.1.19__tar.gz → 0.1.20__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {api_foundry-0.1.19 → api_foundry-0.1.20}/.gitignore +3 -1
- {api_foundry-0.1.19 → api_foundry-0.1.20}/PKG-INFO +8 -2
- {api_foundry-0.1.19 → api_foundry-0.1.20}/api_foundry/iac/gateway_spec.py +98 -59
- api_foundry-0.1.20/api_foundry/iac/pulumi/api_foundry.py +207 -0
- {api_foundry-0.1.19 → api_foundry-0.1.20}/api_foundry/utils/model_factory.py +231 -52
- api_foundry-0.1.20/api_foundry/utils/schema_validator.py +197 -0
- {api_foundry-0.1.19 → api_foundry-0.1.20}/pyproject.toml +16 -3
- api_foundry-0.1.19/api_foundry/iac/pulumi/api_foundry.py +0 -119
- api_foundry-0.1.19/api_foundry/utils/schema_processor.py.x +0 -211
- api_foundry-0.1.19/api_foundry/utils/schema_validator.py +0 -56
- api_foundry-0.1.19/api_foundry/utils/spec_handler.py +0 -74
- {api_foundry-0.1.19 → api_foundry-0.1.20}/LICENSE +0 -0
- {api_foundry-0.1.19 → api_foundry-0.1.20}/api_foundry/__init__.py +0 -0
- {api_foundry-0.1.19 → api_foundry-0.1.20}/api_foundry/scripts/install_secret.py +0 -0
- {api_foundry-0.1.19 → api_foundry-0.1.20}/api_foundry/scripts/postgres_to_openapi.py +0 -0
- {api_foundry-0.1.19 → api_foundry-0.1.20}/api_foundry/utils/app_exception.py +0 -0
- {api_foundry-0.1.19 → api_foundry-0.1.20}/api_foundry/utils/logger.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: api-foundry
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.20
|
|
4
4
|
Summary: The `api_foundry` project is a powerful tool designed to automate the deployment of REST APIs on AWS using Lambda services to access and interact with relational databases (RDBMS). This project leverages the OpenAPI specification to define and manage the APIs
|
|
5
5
|
Project-URL: Documentation, https://github.com/DanRepik/api-foundry
|
|
6
6
|
Project-URL: Source, https://github.com/DanRepik/api-foundry
|
|
@@ -9,13 +9,19 @@ License-File: LICENSE
|
|
|
9
9
|
Classifier: Operating System :: OS Independent
|
|
10
10
|
Classifier: Programming Language :: Python :: 3.9
|
|
11
11
|
Requires-Python: >=3.9
|
|
12
|
-
Requires-Dist: api-foundry-query-engine
|
|
13
12
|
Requires-Dist: boto3
|
|
14
13
|
Requires-Dist: cloud-foundry
|
|
15
14
|
Requires-Dist: cryptography
|
|
15
|
+
Requires-Dist: fixture-foundry
|
|
16
16
|
Requires-Dist: pulumi
|
|
17
17
|
Requires-Dist: pulumi-aws
|
|
18
18
|
Requires-Dist: pyhumps
|
|
19
19
|
Requires-Dist: pyyaml
|
|
20
20
|
Requires-Dist: requests
|
|
21
21
|
Requires-Dist: simplejson
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: api-foundry-query-engine; extra == 'dev'
|
|
24
|
+
Requires-Dist: docker; extra == 'dev'
|
|
25
|
+
Requires-Dist: psycopg2-binary; extra == 'dev'
|
|
26
|
+
Requires-Dist: pulumi-automation; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
@@ -14,41 +14,75 @@ log = logger(__name__)
|
|
|
14
14
|
|
|
15
15
|
class APISpecEditor:
|
|
16
16
|
api_spec: dict
|
|
17
|
-
function: Function
|
|
17
|
+
function: Optional[Function]
|
|
18
18
|
integrations: list[dict]
|
|
19
19
|
|
|
20
|
-
def __init__(self, *, open_api_spec: dict,
|
|
20
|
+
def __init__(self, *, open_api_spec: Optional[dict], function: Optional[Function]):
|
|
21
21
|
self.function = function
|
|
22
22
|
self.integrations = []
|
|
23
|
-
self.editor = AWSOpenAPISpecEditor(
|
|
23
|
+
self.editor = AWSOpenAPISpecEditor(
|
|
24
|
+
copy.deepcopy(open_api_spec) if open_api_spec else None
|
|
25
|
+
)
|
|
24
26
|
|
|
25
27
|
def rest_api_spec(self) -> str:
|
|
26
28
|
schemas = self.editor.get_spec_part(["components", "schemas"], create=False)
|
|
27
29
|
if schemas:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
if isinstance(schemas, dict):
|
|
31
|
+
for schema_name, schema_object in schemas.items():
|
|
32
|
+
self.generate_crud_operations(schema_name, schema_object)
|
|
33
|
+
elif isinstance(schemas, list):
|
|
34
|
+
for schema_object in schemas:
|
|
35
|
+
schema_name = schema_object.get("name", None)
|
|
36
|
+
if schema_name:
|
|
37
|
+
self.generate_crud_operations(schema_name, schema_object)
|
|
30
38
|
|
|
31
|
-
self.editor.
|
|
39
|
+
# self.editor.remove_attributes_with_pattern("^x-af-.*$")
|
|
32
40
|
|
|
33
41
|
self.editor.correct_schema_names()
|
|
34
|
-
return self.editor.
|
|
42
|
+
return self.editor.yaml
|
|
35
43
|
|
|
36
44
|
def add_operation(
|
|
37
45
|
self,
|
|
38
46
|
path: str,
|
|
39
47
|
method: str,
|
|
40
48
|
operation: dict,
|
|
41
|
-
|
|
49
|
+
schema_name: str,
|
|
50
|
+
schema_object: dict,
|
|
51
|
+
function: Optional[Function] = None,
|
|
42
52
|
):
|
|
53
|
+
# Ensure any schema-level permissions are carried onto the operation if not set.
|
|
54
|
+
if "x-af-permissions" not in operation and isinstance(schema_object, dict):
|
|
55
|
+
permissions = schema_object.get("x-af-permissions")
|
|
56
|
+
if permissions:
|
|
57
|
+
operation["x-af-permissions"] = permissions
|
|
43
58
|
self.integrations.append(
|
|
44
|
-
{"path": path, "method": method, "function": self.function}
|
|
59
|
+
{"path": path, "method": method, "function": function or self.function}
|
|
60
|
+
)
|
|
61
|
+
self.editor.add_operation(
|
|
62
|
+
path=path,
|
|
63
|
+
method=method,
|
|
64
|
+
operation=operation,
|
|
65
|
+
schema_name=schema_name,
|
|
66
|
+
schema_object=schema_object,
|
|
45
67
|
)
|
|
46
|
-
self.editor.add_operation(path, method, operation, schema_object)
|
|
47
68
|
|
|
48
69
|
def generate_regex(self, property: dict[str, Any]) -> str:
|
|
49
70
|
regex_pattern = ""
|
|
50
71
|
|
|
51
|
-
|
|
72
|
+
# UUID (either explicit type 'uuid' or string with format 'uuid')
|
|
73
|
+
if property.get("type") == "uuid" or (
|
|
74
|
+
property.get("type") == "string" and property.get("format", None) == "uuid"
|
|
75
|
+
):
|
|
76
|
+
hex_pat = r"[0-9a-fA-F]"
|
|
77
|
+
regex_pattern = (
|
|
78
|
+
rf"{hex_pat}{{8}}-"
|
|
79
|
+
rf"{hex_pat}{{4}}-"
|
|
80
|
+
rf"[1-5]{hex_pat}{{3}}-"
|
|
81
|
+
rf"[89abAB]{hex_pat}{{3}}-"
|
|
82
|
+
rf"{hex_pat}{{12}}"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
elif property["type"] == "string" and property.get("format", None) == "date":
|
|
52
86
|
# Assuming ISO 8601 date format (YYYY-MM-DD)
|
|
53
87
|
regex_pattern = r"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])" # Date part: YYYY-MM-DD
|
|
54
88
|
|
|
@@ -151,7 +185,7 @@ class APISpecEditor:
|
|
|
151
185
|
if "$ref" in prop:
|
|
152
186
|
ref_parts = prop["$ref"].lstrip("#/").split("/")
|
|
153
187
|
referenced_schema = self.editor.get_spec_part(ref_parts)
|
|
154
|
-
if referenced_schema:
|
|
188
|
+
if isinstance(referenced_schema, dict):
|
|
155
189
|
prop = {**prop, **referenced_schema}
|
|
156
190
|
prop.pop("$ref")
|
|
157
191
|
|
|
@@ -176,7 +210,7 @@ class APISpecEditor:
|
|
|
176
210
|
required_properties = schema_object.get("required", [])
|
|
177
211
|
return [name for name in input_properties if name in required_properties]
|
|
178
212
|
|
|
179
|
-
def generate_crud_operations(self, schema_name, schema_object: dict):
|
|
213
|
+
def generate_crud_operations(self, schema_name: str, schema_object: dict):
|
|
180
214
|
path = f"/{schema_name.lower()}"
|
|
181
215
|
self.generate_create_operation(path, schema_name, schema_object)
|
|
182
216
|
self.generate_get_by_id_operation(path, schema_name, schema_object)
|
|
@@ -197,8 +231,8 @@ class APISpecEditor:
|
|
|
197
231
|
schema_object, include_primary_key=include_primary_key or False
|
|
198
232
|
)
|
|
199
233
|
self.add_operation(
|
|
200
|
-
path,
|
|
201
|
-
"post",
|
|
234
|
+
path=path,
|
|
235
|
+
method="post",
|
|
202
236
|
operation={
|
|
203
237
|
"summary": f"Create a new {schema_name}",
|
|
204
238
|
"requestBody": {
|
|
@@ -222,6 +256,7 @@ class APISpecEditor:
|
|
|
222
256
|
}
|
|
223
257
|
},
|
|
224
258
|
},
|
|
259
|
+
schema_name=schema_name,
|
|
225
260
|
schema_object=schema_object,
|
|
226
261
|
)
|
|
227
262
|
|
|
@@ -229,9 +264,9 @@ class APISpecEditor:
|
|
|
229
264
|
self, path: str, schema_name: str, schema_object: dict[str, Any]
|
|
230
265
|
):
|
|
231
266
|
self.add_operation(
|
|
232
|
-
path,
|
|
233
|
-
"get",
|
|
234
|
-
{
|
|
267
|
+
path=path,
|
|
268
|
+
method="get",
|
|
269
|
+
operation={
|
|
235
270
|
"summary": f"Retrieve all {schema_name}",
|
|
236
271
|
"parameters": self.generate_query_parameters(schema_object),
|
|
237
272
|
"responses": {
|
|
@@ -241,6 +276,8 @@ class APISpecEditor:
|
|
|
241
276
|
}
|
|
242
277
|
},
|
|
243
278
|
},
|
|
279
|
+
schema_name=schema_name,
|
|
280
|
+
schema_object=schema_object,
|
|
244
281
|
)
|
|
245
282
|
|
|
246
283
|
def generate_get_by_id_operation(
|
|
@@ -251,20 +288,18 @@ class APISpecEditor:
|
|
|
251
288
|
return
|
|
252
289
|
|
|
253
290
|
key_name = key[0]
|
|
254
|
-
key_properties = key[1]
|
|
255
|
-
|
|
256
291
|
self.add_operation(
|
|
257
|
-
f"{path}/{{{key_name}}}",
|
|
258
|
-
"get",
|
|
259
|
-
{
|
|
292
|
+
path=f"{path}/{{{key_name}}}",
|
|
293
|
+
method="get",
|
|
294
|
+
operation={
|
|
260
295
|
"summary": f"Retrieve {schema_name} by {key_name}",
|
|
261
296
|
"parameters": [
|
|
262
297
|
{
|
|
263
|
-
"name":
|
|
298
|
+
"name": key_name,
|
|
264
299
|
"in": "path",
|
|
265
300
|
"description": f"ID of the {schema_name} to get",
|
|
266
301
|
"required": True,
|
|
267
|
-
"schema": {"type":
|
|
302
|
+
"schema": {"type": "string"},
|
|
268
303
|
}
|
|
269
304
|
],
|
|
270
305
|
"responses": {
|
|
@@ -274,6 +309,8 @@ class APISpecEditor:
|
|
|
274
309
|
}
|
|
275
310
|
},
|
|
276
311
|
},
|
|
312
|
+
schema_name=schema_name,
|
|
313
|
+
schema_object=schema_object,
|
|
277
314
|
)
|
|
278
315
|
|
|
279
316
|
def generate_update_by_id_operation(
|
|
@@ -288,13 +325,10 @@ class APISpecEditor:
|
|
|
288
325
|
return
|
|
289
326
|
|
|
290
327
|
key_name = key[0]
|
|
291
|
-
key_properties = key[1]
|
|
292
|
-
|
|
293
|
-
# Update operation
|
|
294
328
|
self.add_operation(
|
|
295
|
-
f"{path}/{{{key_name}}}",
|
|
296
|
-
"put",
|
|
297
|
-
{
|
|
329
|
+
path=f"{path}/{{{key_name}}}",
|
|
330
|
+
method="put",
|
|
331
|
+
operation={
|
|
298
332
|
"summary": f"Update an existing {schema_name} by {key_name}",
|
|
299
333
|
"parameters": [
|
|
300
334
|
{
|
|
@@ -302,7 +336,7 @@ class APISpecEditor:
|
|
|
302
336
|
"in": "path",
|
|
303
337
|
"description": f"ID of the {schema_name} to update",
|
|
304
338
|
"required": True,
|
|
305
|
-
"schema":
|
|
339
|
+
"schema": {"type": "string"},
|
|
306
340
|
}
|
|
307
341
|
],
|
|
308
342
|
"requestBody": {
|
|
@@ -324,6 +358,8 @@ class APISpecEditor:
|
|
|
324
358
|
}
|
|
325
359
|
},
|
|
326
360
|
},
|
|
361
|
+
schema_name=schema_name,
|
|
362
|
+
schema_object=schema_object,
|
|
327
363
|
)
|
|
328
364
|
|
|
329
365
|
def generate_update_with_cc_operation(
|
|
@@ -335,19 +371,15 @@ class APISpecEditor:
|
|
|
335
371
|
return
|
|
336
372
|
|
|
337
373
|
key_name = key[0]
|
|
338
|
-
key_properties = key[1]
|
|
339
|
-
|
|
340
374
|
cc_tuple = self.get_concurrency_property(schema_object)
|
|
341
375
|
if not cc_tuple:
|
|
342
376
|
return
|
|
343
377
|
|
|
344
378
|
cc_property_name = cc_tuple[0]
|
|
345
|
-
cc_property = cc_tuple[1]
|
|
346
|
-
|
|
347
379
|
self.add_operation(
|
|
348
|
-
f"{path}/{{{key_name}}}/{cc_property_name}/{{{cc_property_name}}}",
|
|
349
|
-
"put",
|
|
350
|
-
{
|
|
380
|
+
path=f"{path}/{{{key_name}}}/{cc_property_name}/{{{cc_property_name}}}",
|
|
381
|
+
method="put",
|
|
382
|
+
operation={
|
|
351
383
|
"summary": f"Update an existing {schema_name} by ID",
|
|
352
384
|
"parameters": [
|
|
353
385
|
{
|
|
@@ -355,7 +387,7 @@ class APISpecEditor:
|
|
|
355
387
|
"in": "path",
|
|
356
388
|
"description": f"ID of the {schema_name} to update",
|
|
357
389
|
"required": True,
|
|
358
|
-
"schema":
|
|
390
|
+
"schema": {"type": "string"},
|
|
359
391
|
},
|
|
360
392
|
{
|
|
361
393
|
"name": cc_property_name,
|
|
@@ -364,7 +396,7 @@ class APISpecEditor:
|
|
|
364
396
|
cc_property_name + " of the " + schema_name + " to update"
|
|
365
397
|
),
|
|
366
398
|
"required": True,
|
|
367
|
-
"schema":
|
|
399
|
+
"schema": {"type": "string"},
|
|
368
400
|
},
|
|
369
401
|
],
|
|
370
402
|
"requestBody": {
|
|
@@ -386,6 +418,8 @@ class APISpecEditor:
|
|
|
386
418
|
}
|
|
387
419
|
},
|
|
388
420
|
},
|
|
421
|
+
schema_name=schema_name,
|
|
422
|
+
schema_object=schema_object,
|
|
389
423
|
)
|
|
390
424
|
|
|
391
425
|
def generate_update_many_operation(
|
|
@@ -397,9 +431,9 @@ class APISpecEditor:
|
|
|
397
431
|
|
|
398
432
|
# Update operation
|
|
399
433
|
self.add_operation(
|
|
400
|
-
path,
|
|
401
|
-
"put",
|
|
402
|
-
{
|
|
434
|
+
path=path,
|
|
435
|
+
method="put",
|
|
436
|
+
operation={
|
|
403
437
|
"summary": f"Update an existing {schema_name} by ID",
|
|
404
438
|
"parameters": self.generate_query_parameters(schema_object),
|
|
405
439
|
"requestBody": {
|
|
@@ -421,6 +455,8 @@ class APISpecEditor:
|
|
|
421
455
|
}
|
|
422
456
|
},
|
|
423
457
|
},
|
|
458
|
+
schema_name=schema_name,
|
|
459
|
+
schema_object=schema_object,
|
|
424
460
|
)
|
|
425
461
|
|
|
426
462
|
# Delete operation
|
|
@@ -433,19 +469,17 @@ class APISpecEditor:
|
|
|
433
469
|
return
|
|
434
470
|
|
|
435
471
|
cc_property_name = cc_tuple[0]
|
|
436
|
-
cc_property = cc_tuple[1]
|
|
437
472
|
|
|
438
473
|
key = self.get_primary_key(schema_object)
|
|
439
474
|
if not key:
|
|
440
475
|
return
|
|
441
476
|
|
|
442
477
|
key_name = key[0]
|
|
443
|
-
key_properties = key[1]
|
|
444
478
|
|
|
445
479
|
self.add_operation(
|
|
446
|
-
f"{path}/{{{key_name}}}/{cc_property_name}/{{{cc_property_name}}}",
|
|
447
|
-
"delete",
|
|
448
|
-
{
|
|
480
|
+
path=f"{path}/{{{key_name}}}/{cc_property_name}/{{{cc_property_name}}}",
|
|
481
|
+
method="delete",
|
|
482
|
+
operation={
|
|
449
483
|
"summary": f"Delete an existing {schema_name} by ID",
|
|
450
484
|
"parameters": [
|
|
451
485
|
{
|
|
@@ -453,7 +487,7 @@ class APISpecEditor:
|
|
|
453
487
|
"in": "path",
|
|
454
488
|
"description": f"ID of the {schema_name} to update",
|
|
455
489
|
"required": True,
|
|
456
|
-
"schema": {"type":
|
|
490
|
+
"schema": {"type": "string"},
|
|
457
491
|
},
|
|
458
492
|
{
|
|
459
493
|
"name": cc_property_name,
|
|
@@ -462,7 +496,7 @@ class APISpecEditor:
|
|
|
462
496
|
f"{cc_property_name} of the {schema_name} to update"
|
|
463
497
|
),
|
|
464
498
|
"required": True,
|
|
465
|
-
"schema":
|
|
499
|
+
"schema": {"type": "string"},
|
|
466
500
|
},
|
|
467
501
|
],
|
|
468
502
|
"responses": {
|
|
@@ -472,6 +506,8 @@ class APISpecEditor:
|
|
|
472
506
|
}
|
|
473
507
|
},
|
|
474
508
|
},
|
|
509
|
+
schema_name=schema_name,
|
|
510
|
+
schema_object=schema_object,
|
|
475
511
|
)
|
|
476
512
|
|
|
477
513
|
def generate_delete_by_id_operation(
|
|
@@ -485,12 +521,11 @@ class APISpecEditor:
|
|
|
485
521
|
return
|
|
486
522
|
|
|
487
523
|
key_name = key[0]
|
|
488
|
-
key_properties = key[1]
|
|
489
524
|
|
|
490
525
|
self.add_operation(
|
|
491
|
-
f"{path}/{{{key_name}}}",
|
|
492
|
-
"delete",
|
|
493
|
-
{
|
|
526
|
+
path=f"{path}/{{{key_name}}}",
|
|
527
|
+
method="delete",
|
|
528
|
+
operation={
|
|
494
529
|
"summary": f"Delete an existing {schema_name} by {key_name}",
|
|
495
530
|
"parameters": [
|
|
496
531
|
{
|
|
@@ -498,7 +533,7 @@ class APISpecEditor:
|
|
|
498
533
|
"in": "path",
|
|
499
534
|
"description": f"ID of the {schema_name} to update",
|
|
500
535
|
"required": True,
|
|
501
|
-
"schema": {"type":
|
|
536
|
+
"schema": {"type": "string"},
|
|
502
537
|
}
|
|
503
538
|
],
|
|
504
539
|
"responses": {
|
|
@@ -508,6 +543,8 @@ class APISpecEditor:
|
|
|
508
543
|
}
|
|
509
544
|
},
|
|
510
545
|
},
|
|
546
|
+
schema_name=schema_name,
|
|
547
|
+
schema_object=schema_object,
|
|
511
548
|
)
|
|
512
549
|
|
|
513
550
|
def generate_delete_many_operation(
|
|
@@ -517,9 +554,9 @@ class APISpecEditor:
|
|
|
517
554
|
return
|
|
518
555
|
|
|
519
556
|
self.add_operation(
|
|
520
|
-
path,
|
|
521
|
-
"delete",
|
|
522
|
-
{
|
|
557
|
+
path=path,
|
|
558
|
+
method="delete",
|
|
559
|
+
operation={
|
|
523
560
|
"summary": f"Delete many existing {schema_name} using query",
|
|
524
561
|
"parameters": self.generate_query_parameters(schema_object),
|
|
525
562
|
"responses": {
|
|
@@ -529,6 +566,8 @@ class APISpecEditor:
|
|
|
529
566
|
}
|
|
530
567
|
},
|
|
531
568
|
},
|
|
569
|
+
schema_name=schema_name,
|
|
570
|
+
schema_object=schema_object,
|
|
532
571
|
)
|
|
533
572
|
|
|
534
573
|
def transform_schemas(self, spec_dict):
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import json
|
|
3
|
+
import yaml
|
|
4
|
+
import boto3
|
|
5
|
+
from typing import Optional, Union
|
|
6
|
+
from pulumi import ComponentResource
|
|
7
|
+
|
|
8
|
+
import pulumi
|
|
9
|
+
import cloud_foundry
|
|
10
|
+
|
|
11
|
+
from api_foundry.iac.gateway_spec import APISpecEditor
|
|
12
|
+
from api_foundry.utils.model_factory import ModelFactory
|
|
13
|
+
from cloud_foundry import logger
|
|
14
|
+
|
|
15
|
+
log = logger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def is_valid_openapi_spec(spec_dict: dict) -> bool:
|
|
19
|
+
return (
|
|
20
|
+
isinstance(spec_dict, dict)
|
|
21
|
+
and "openapi" in spec_dict
|
|
22
|
+
and isinstance(spec_dict["openapi"], str)
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def load_api_spec(api_spec: Union[str, list[str]]) -> dict:
|
|
27
|
+
"""
|
|
28
|
+
Load one or more OpenAPI specs from files, directories,
|
|
29
|
+
S3, or inline YAML.
|
|
30
|
+
|
|
31
|
+
Accepts:
|
|
32
|
+
- File path(s)
|
|
33
|
+
- Directory path(s): loads all .yaml/.yml files (sorted)
|
|
34
|
+
- S3 URL(s): s3://bucket/key or s3://bucket/prefix/
|
|
35
|
+
- Inline YAML string with an OpenAPI document
|
|
36
|
+
|
|
37
|
+
Returns a shallow-merged spec dict; later documents win on conflicts.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def _is_s3_url(s: str) -> bool:
|
|
41
|
+
return isinstance(s, str) and s.startswith("s3://")
|
|
42
|
+
|
|
43
|
+
def _gather_inputs(specs: Union[str, list[str]]) -> list[Union[str, dict]]:
|
|
44
|
+
items = [specs] if isinstance(specs, str) else list(specs or [])
|
|
45
|
+
inputs: list[Union[str, dict]] = []
|
|
46
|
+
for spec in items:
|
|
47
|
+
if not isinstance(spec, str):
|
|
48
|
+
raise TypeError("api_spec entries must be strings")
|
|
49
|
+
|
|
50
|
+
# Local file
|
|
51
|
+
if os.path.isfile(spec):
|
|
52
|
+
inputs.append(spec)
|
|
53
|
+
continue
|
|
54
|
+
|
|
55
|
+
# Local directory: include .yaml/.yml files only
|
|
56
|
+
if os.path.isdir(spec):
|
|
57
|
+
files = [
|
|
58
|
+
os.path.join(spec, f)
|
|
59
|
+
for f in os.listdir(spec)
|
|
60
|
+
if f.endswith((".yaml", ".yml"))
|
|
61
|
+
]
|
|
62
|
+
inputs.extend(sorted(files))
|
|
63
|
+
continue
|
|
64
|
+
|
|
65
|
+
# S3 file or prefix
|
|
66
|
+
if _is_s3_url(spec):
|
|
67
|
+
bucket, key = spec[5:].split("/", 1)
|
|
68
|
+
if key.endswith("/"):
|
|
69
|
+
s3 = boto3.client("s3")
|
|
70
|
+
resp = s3.list_objects_v2(Bucket=bucket, Prefix=key)
|
|
71
|
+
contents = resp.get("Contents", [])
|
|
72
|
+
keys = [
|
|
73
|
+
o["Key"]
|
|
74
|
+
for o in contents
|
|
75
|
+
if o["Key"].endswith((".yaml", ".yml"))
|
|
76
|
+
]
|
|
77
|
+
inputs.extend([f"s3://{bucket}/{k}" for k in sorted(keys)])
|
|
78
|
+
else:
|
|
79
|
+
inputs.append(spec)
|
|
80
|
+
continue
|
|
81
|
+
|
|
82
|
+
# Inline YAML
|
|
83
|
+
try:
|
|
84
|
+
as_yaml = yaml.safe_load(spec)
|
|
85
|
+
except yaml.YAMLError as exc: # not YAML
|
|
86
|
+
raise ValueError(
|
|
87
|
+
f"Invalid OpenAPI spec source: {spec}"
|
|
88
|
+
) from exc
|
|
89
|
+
|
|
90
|
+
if is_valid_openapi_spec(as_yaml):
|
|
91
|
+
inputs.append(as_yaml)
|
|
92
|
+
else:
|
|
93
|
+
raise ValueError(f"Invalid OpenAPI spec provided: {spec}")
|
|
94
|
+
|
|
95
|
+
return inputs
|
|
96
|
+
|
|
97
|
+
merged: dict = {}
|
|
98
|
+
for source in _gather_inputs(api_spec):
|
|
99
|
+
if isinstance(source, dict):
|
|
100
|
+
spec_dict = source
|
|
101
|
+
elif _is_s3_url(source):
|
|
102
|
+
bucket, key = source[5:].split("/", 1)
|
|
103
|
+
s3 = boto3.client("s3")
|
|
104
|
+
obj = s3.get_object(Bucket=bucket, Key=key)
|
|
105
|
+
spec_dict = yaml.safe_load(obj["Body"].read().decode("utf-8"))
|
|
106
|
+
else:
|
|
107
|
+
with open(source, "r", encoding="utf-8") as f:
|
|
108
|
+
spec_dict = yaml.safe_load(f)
|
|
109
|
+
|
|
110
|
+
if not is_valid_openapi_spec(spec_dict):
|
|
111
|
+
raise ValueError(f"Invalid OpenAPI spec found in: {source}")
|
|
112
|
+
|
|
113
|
+
merged.update(spec_dict)
|
|
114
|
+
|
|
115
|
+
return merged
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class APIFoundry(ComponentResource):
|
|
119
|
+
api_spec_editor: APISpecEditor
|
|
120
|
+
|
|
121
|
+
def __init__(
|
|
122
|
+
self,
|
|
123
|
+
name,
|
|
124
|
+
*,
|
|
125
|
+
api_spec: Union[str, list[str]],
|
|
126
|
+
secrets: Optional[str] = None,
|
|
127
|
+
environment: Optional[
|
|
128
|
+
dict[str, Union[str, pulumi.Output[str]]]
|
|
129
|
+
] = None,
|
|
130
|
+
integrations: Optional[list[dict]] = None,
|
|
131
|
+
token_validators: Optional[list[dict]] = None,
|
|
132
|
+
policy_statements: Optional[list] = None,
|
|
133
|
+
vpc_config: Optional[dict] = None,
|
|
134
|
+
export_api: Optional[str] = None,
|
|
135
|
+
opts=None,
|
|
136
|
+
):
|
|
137
|
+
super().__init__("cloud_foundry:apigw:APIFoundry", name, None, opts)
|
|
138
|
+
|
|
139
|
+
api_spec_dict = load_api_spec(api_spec)
|
|
140
|
+
config_defaults = api_spec_dict.get("x-af-configuration", {})
|
|
141
|
+
|
|
142
|
+
secrets = secrets or config_defaults.get("secrets", "")
|
|
143
|
+
env_vars = environment or config_defaults.get(
|
|
144
|
+
"environment", {}
|
|
145
|
+
) # type: dict[str, Union[str, pulumi.Output[str]]]
|
|
146
|
+
integrations = integrations or config_defaults.get("integrations", [])
|
|
147
|
+
token_validators = token_validators or config_defaults.get(
|
|
148
|
+
"token_validators", []
|
|
149
|
+
)
|
|
150
|
+
policy_statements = policy_statements or config_defaults.get(
|
|
151
|
+
"policy_statements", []
|
|
152
|
+
)
|
|
153
|
+
vpc_config = vpc_config or config_defaults.get("vpc_config", {})
|
|
154
|
+
|
|
155
|
+
env_vars["SECRETS"] = secrets
|
|
156
|
+
|
|
157
|
+
# Grant read access to referenced secrets (single broad stmt)
|
|
158
|
+
if json.loads(secrets):
|
|
159
|
+
policy_statements.append(
|
|
160
|
+
{
|
|
161
|
+
"Effect": "Allow",
|
|
162
|
+
"Actions": ["secretsmanager:GetSecretValue"],
|
|
163
|
+
"Resources": ["*"],
|
|
164
|
+
}
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
self.api_function = cloud_foundry.python_function(
|
|
168
|
+
name=name,
|
|
169
|
+
environment=env_vars,
|
|
170
|
+
handler="api_foundry_query_engine.lambda_handler.handler",
|
|
171
|
+
sources={
|
|
172
|
+
"api_spec.yaml": yaml.safe_dump(
|
|
173
|
+
ModelFactory(api_spec_dict).get_config_output()
|
|
174
|
+
),
|
|
175
|
+
},
|
|
176
|
+
requirements=[
|
|
177
|
+
"psycopg2-binary",
|
|
178
|
+
"pyyaml",
|
|
179
|
+
"api_foundry_query_engine",
|
|
180
|
+
],
|
|
181
|
+
policy_statements=policy_statements,
|
|
182
|
+
vpc_config=vpc_config,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
gateway_spec = APISpecEditor(
|
|
186
|
+
open_api_spec=api_spec_dict, function=self.api_function
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
# Merge gateway_spec.integrations with user-provided integrations
|
|
190
|
+
specification = gateway_spec.rest_api_spec()
|
|
191
|
+
merged_integrations = (integrations or []) + (gateway_spec.integrations or [])
|
|
192
|
+
|
|
193
|
+
self.rest_api = cloud_foundry.rest_api(
|
|
194
|
+
name,
|
|
195
|
+
specification=[specification],
|
|
196
|
+
integrations=merged_integrations,
|
|
197
|
+
token_validators=token_validators or [],
|
|
198
|
+
export_api=export_api,
|
|
199
|
+
opts=pulumi.ResourceOptions(parent=self),
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
self.domain = self.rest_api.domain
|
|
203
|
+
|
|
204
|
+
self.register_outputs({f"{name}_domain": self.domain})
|
|
205
|
+
|
|
206
|
+
def integrations(self) -> list[dict]:
|
|
207
|
+
return self.api_spec_editor.integrations
|