diff --git a/datacontract/export/terraform_converter.py b/datacontract/export/terraform_converter.py index 64734afe..7178aa40 100644 --- a/datacontract/export/terraform_converter.py +++ b/datacontract/export/terraform_converter.py @@ -1,5 +1,5 @@ from datacontract.model.data_contract_specification import \ - DataContractSpecification + DataContractSpecification, Server import re @@ -18,20 +18,37 @@ def to_terraform(data_contract_spec: DataContractSpecification, server_id: str = return result.strip() -def server_to_terraform_resource(data_contract_spec, result, server, server_name): +def server_to_terraform_resource(data_contract_spec, result, server : Server, server_name): tag_data_contract = data_contract_spec.id tag_name = data_contract_spec.info.title tag_server = server_name bucket_name = extract_bucket_name(server) - resource_id = f"{data_contract_spec.id}_{server.type}_{server_name}" - result += f""" + resource_id = f"{data_contract_spec.id}_{server_name}" + data_product_id = server.dataProductId + + if data_product_id is not None: + result += f""" +resource "aws_s3_bucket" "{resource_id}" {{ + bucket = "{bucket_name}" + + tags = {{ + Name = "{tag_name}" + DataContract = "{tag_data_contract}" + Server = "{tag_server}" + DataProduct = "{data_product_id}" + }} +}} + +""" + else: + result += f""" resource "aws_s3_bucket" "{resource_id}" {{ bucket = "{bucket_name}" tags = {{ - Name = "{tag_name}" - DataContractId = "{tag_data_contract}" - DataContractServer = "{tag_server}" + Name = "{tag_name}" + DataContract = "{tag_data_contract}" + Server = "{tag_server}" }} }} diff --git a/tests/examples/export/datacontract_s3.yaml b/tests/examples/export/datacontract_s3.yaml index 39bfb688..32f67f69 100644 --- a/tests/examples/export/datacontract_s3.yaml +++ b/tests/examples/export/datacontract_s3.yaml @@ -19,6 +19,7 @@ servers: location: s3://datacontract-example-orders-latest/data/{model}/*.json format: json delimiter: new_line + dataProductId: orders models: orders: description: The orders model diff --git a/tests/test_export_terraform.py b/tests/test_export_terraform.py index f67575aa..67a0dd5a 100644 --- a/tests/test_export_terraform.py +++ b/tests/test_export_terraform.py @@ -23,13 +23,14 @@ def test_cli(): def test_to_terraform(): data_contract = DataContractSpecification.from_file("./examples/export/datacontract_s3.yaml") expected_terraform_file = """ -resource "aws_s3_bucket" "orders-unit-test_s3_production" { +resource "aws_s3_bucket" "orders-unit-test_production" { bucket = "datacontract-example-orders-latest" tags = { - Name = "Orders Unit Test" - DataContractId = "orders-unit-test" - DataContractServer = "production" + Name = "Orders Unit Test" + DataContract = "orders-unit-test" + Server = "production" + DataProduct = "orders" } } """.strip()