Skip to content

Commit

Permalink
Add terraform export
Browse files Browse the repository at this point in the history
  • Loading branch information
simonharrer committed Mar 12, 2024
1 parent c8d01f1 commit b661492
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
31 changes: 24 additions & 7 deletions datacontract/export/terraform_converter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datacontract.model.data_contract_specification import \
DataContractSpecification
DataContractSpecification, Server
import re


Expand All @@ -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}"
}}
}}
Expand Down
1 change: 1 addition & 0 deletions tests/examples/export/datacontract_s3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions tests/test_export_terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b661492

Please sign in to comment.