From 7b0abf660f37d58e54eabde2e2f566964741b63c Mon Sep 17 00:00:00 2001 From: mohammadll Date: Sun, 5 Jan 2025 22:07:54 +0330 Subject: [PATCH] feat(terraform-templates): implementing aws-cloudfront route --- app/media/terraform.tfvars | 7 +++++-- app/models/terraform_models.py | 8 ++++++++ app/routes/terraform.py | 16 +++++++++++++++- .../terraform/aws/CloudFront.py | 15 +++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 app/template_generators/terraform/aws/CloudFront.py diff --git a/app/media/terraform.tfvars b/app/media/terraform.tfvars index 8c5a92e..de49d73 100644 --- a/app/media/terraform.tfvars +++ b/app/media/terraform.tfvars @@ -1,2 +1,5 @@ -alb_create = true -create_security_group = true +create_distribution = true +create_origin_access_identity = true +create_origin_access_control = false +create_monitoring_subscription = false +create_vpc_origin = false diff --git a/app/models/terraform_models.py b/app/models/terraform_models.py index 0016c24..075ff4e 100644 --- a/app/models/terraform_models.py +++ b/app/models/terraform_models.py @@ -103,3 +103,11 @@ class IaCTemplateGenerationALB(BaseModel): alb_resources:bool = True security_group:bool = True + +class IaCTemplateGenerationCloudFront(BaseModel): + + distribution:bool = True + origin_access_identity:bool = True + origin_access_control:bool = False + monitoring_subscription:bool = False + vpc_origin:bool = False diff --git a/app/routes/terraform.py b/app/routes/terraform.py index 4ed8288..7585015 100644 --- a/app/routes/terraform.py +++ b/app/routes/terraform.py @@ -15,7 +15,8 @@ IaCTemplateGenerationArgoCD, IaCTemplateGenerationELB, IaCTemplateGenerationEFS, - IaCTemplateGenerationALB + IaCTemplateGenerationALB, + IaCTemplateGenerationCloudFront ) from fastapi import Response @@ -32,6 +33,7 @@ from app.template_generators.terraform.aws.ELB import (IaC_template_generator_elb) from app.template_generators.terraform.aws.EFS import (IaC_template_generator_efs) from app.template_generators.terraform.aws.ALB import (IaC_template_generator_alb) +from app.template_generators.terraform.aws.CloudFront import (IaC_template_generator_cloudfront) from app.template_generators.terraform.Installation.main import (select_install) import os @@ -155,3 +157,15 @@ async def IaC_template_generation_aws_alb(request:IaCTemplateGenerationALB) -> O return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars") + +@app.post("/api/IaC-template/aws/cloudfront") +async def IaC_template_generation_aws_cloudfront(request:IaCTemplateGenerationCloudFront) -> Output: + + dir = 'app/media/terraform.tfvars' + + file_response = IaC_template_generator_cloudfront(request) + with open(dir,'w')as f: + f.write(file_response) + + return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars") + diff --git a/app/template_generators/terraform/aws/CloudFront.py b/app/template_generators/terraform/aws/CloudFront.py new file mode 100644 index 0000000..3edd20c --- /dev/null +++ b/app/template_generators/terraform/aws/CloudFront.py @@ -0,0 +1,15 @@ +def IaC_template_generator_cloudfront(input) -> str: + + aws_cloudfront_create_distribution = 'true' if input.distribution else 'false' + aws_cloudfront_create_origin_access_identity = 'true' if input.origin_access_identity else 'false' + aws_cloudfront_create_origin_access_control = 'true' if input.origin_access_control else 'false' + aws_cloudfront_create_monitoring_subscription = 'true' if input.monitoring_subscription else 'false' + aws_cloudfront_create_vpc_origin = 'true' if input.vpc_origin else 'false' + + tfvars_file = f"""create_distribution = {aws_cloudfront_create_distribution} +create_origin_access_identity = {aws_cloudfront_create_origin_access_identity} +create_origin_access_control = {aws_cloudfront_create_origin_access_control} +create_monitoring_subscription = {aws_cloudfront_create_monitoring_subscription} +create_vpc_origin = {aws_cloudfront_create_vpc_origin} +""" + return tfvars_file