|
4 | 4 | import boto3 |
5 | 5 | import jsonschema |
6 | 6 | import pytest |
7 | | -import logging |
8 | 7 |
|
9 | 8 |
|
10 | 9 | @pytest.fixture() |
11 | 10 | def validator(): |
12 | | - |
13 | 11 | def schema_validate(translator, resource): |
14 | | - schema_path = f'schema.{translator.tf_type}.json' |
| 12 | + schema_path = f"schema.{translator.tf_type}.json" |
15 | 13 | schema = load_data(schema_path) |
16 | 14 | if schema is None: |
17 | | - cfn = boto3.client('cloudformation') |
18 | | - rtype = cfn.describe_type( |
19 | | - TypeName=translator.cfn_type, Type='RESOURCE') |
20 | | - schema = json.loads(rtype['Schema']) |
21 | | - (Path(__file__).parent / "data" / schema_path).write_text(json.dumps(schema, indent=2)) |
| 15 | + cfn = boto3.client("cloudformation") |
| 16 | + rtype = cfn.describe_type(TypeName=translator.cfn_type, Type="RESOURCE") |
| 17 | + schema = json.loads(rtype["Schema"]) |
| 18 | + (Path(__file__).parent / "data" / schema_path).write_text( |
| 19 | + json.dumps(schema, indent=2) |
| 20 | + ) |
22 | 21 |
|
23 | 22 | props = set(resource) |
24 | 23 | sprops = set(schema["properties"].keys()) |
25 | 24 | unknown = props.difference(sprops) |
26 | 25 | if unknown: |
27 | | - raise KeyError("unknown resource keys %s" % (', '.join(unknown))) |
| 26 | + raise KeyError("unknown resource keys %s" % (", ".join(unknown))) |
28 | 27 |
|
29 | 28 | validator = jsonschema.Draft7Validator(schema) |
30 | | - |
| 29 | + |
31 | 30 | errors = list(validator.iter_errors(resource)) |
32 | 31 | if errors: |
33 | | - print( |
34 | | - "%s errors %d" % (translator.cfn_type, len(errors)) |
35 | | - ) |
| 32 | + print("%s errors %d" % (translator.cfn_type, len(errors))) |
36 | 33 |
|
37 | 34 | for e in errors: |
38 | | - print("Resource %s error:\n %s" % (translator.cfn_type, str(e))) |
| 35 | + print("Resource %s error:\n %s" % (translator.cfn_type, str(e))) |
39 | 36 |
|
40 | 37 | if errors: |
41 | 38 | raise ValueError( |
42 | | - f"resource type {translator.cfn_type} had translation errors") |
| 39 | + f"resource type {translator.cfn_type} had translation errors" |
| 40 | + ) |
43 | 41 |
|
44 | 42 | return schema_validate |
45 | 43 |
|
46 | | - |
47 | | - |
48 | 44 |
|
49 | 45 | def load_data(filename): |
50 | 46 | path = Path(__file__).parent / "data" / filename |
|
0 commit comments