-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CloudFormation docs in schema (#2816)
- Loading branch information
Showing
6 changed files
with
235,330 additions
and
155,646 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
""" | ||
Script to augment CloudFormation JSON schema with Markdown documentation. | ||
""" | ||
|
||
import argparse | ||
import json | ||
import sys | ||
from pathlib import Path | ||
from typing import Iterator | ||
|
||
|
||
def log(s: str) -> None: | ||
print(s, file=sys.stderr) | ||
|
||
|
||
def guess_slugs(s: str) -> Iterator[str]: | ||
""" | ||
Guesses likely documentation page "slugs" from a schema definition name. | ||
Used to map a definition in the JSON schema to the corresponding documentation | ||
blurb. There are probably more sensible ways to do this. | ||
For example, the definition for AWS::EC2::CapacityReservationFleet.TagSpecification | ||
corresponds to aws-properties-ec2-capacityreservation-tagspecification in the | ||
documentation: https://github.com/awsdocs/aws-cloudformation-user-guide/blob/main/doc_source/aws-properties-ec2-capacityreservation-tagspecification.md | ||
Typically a definition with only the resource name corresponds to aws-resource-*, | ||
but there are exceptions such as AWS::S3::Bucket, which starts with aws-properties-*: | ||
https://github.com/awsdocs/aws-cloudformation-user-guide/blob/main/doc_source/aws-properties-s3-bucket.md | ||
""" | ||
slug = s.split("::", 1)[1].lower().replace(".", "-").replace("::", "-") | ||
yield "aws-properties-" + slug | ||
yield "aws-resource-" + slug | ||
|
||
|
||
def main() -> None: | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument( | ||
"--schema", | ||
help="CloudFormation JSON schema", | ||
type=Path, | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"--docs", | ||
help="CloudFormation documentation (as generated by parse_docs.py)", | ||
type=Path, | ||
required=True, | ||
) | ||
args = parser.parse_args() | ||
|
||
schema = json.loads(args.schema.read_text()) | ||
docs = json.loads(args.docs.read_text())["properties"] | ||
|
||
# Assumes schema is from GoFormation and has consistent structure | ||
# TODO: Use a more generic walk | ||
# TODO: Use a more robust cross-referencing method than guessing slugs, doesn't always work | ||
# e.g. AWS::EC2::Instance.Ebs corresponds to aws-properties-ec2-blockdev-template | ||
for def_name, def_schema in schema["definitions"].items(): | ||
if not def_name.startswith("AWS::"): | ||
log(f"Skipping {def_name}: does not start with AWS::") | ||
continue | ||
# If e.g. AWS::S3::Bucket, we only look under Properties | ||
# TODO: Support resource attributes et al. | ||
props = def_schema["properties"] if "." in def_name else def_schema["properties"]["Properties"]["properties"] | ||
for slug in guess_slugs(def_name): | ||
if slug not in docs: | ||
log(f"Skipping {def_name}: {slug} not in docs") | ||
continue | ||
for prop_name, prop_schema in props.items(): | ||
if prop_name not in docs[slug]: | ||
log(f"Skipping {def_name}: {prop_name} not in {slug} docs") | ||
continue | ||
prop_schema["markdownDescription"] = docs[slug][prop_name] | ||
# GoFormation schema doesn't include it, so VS Code defaults to something unrelated (e.g. "Resources") | ||
prop_schema["title"] = prop_name | ||
|
||
print( | ||
json.dumps( | ||
schema, | ||
indent=2, | ||
sort_keys=True, | ||
) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.