From 6352cd7fdba52fac74079d89015d89f50b64e5aa Mon Sep 17 00:00:00 2001 From: Benedetto Debora <Debora.Benedetto@hpecds.com> Date: Mon, 28 Feb 2022 12:11:12 +0100 Subject: [PATCH] Refactoring: move to FastAPI, create template directory, update Dockerfile --- .gitignore | 154 ++++++++++ DB-templates/AWStemplateDB.tpl | 14 - DB-templates/AZUREtemplateDB.tpl | 15 - DB-templates/GCPtemplateDB.tpl | 14 - DB-templates/templatepostgresql.tpl | 8 - Dockerfile | 2 +- ICG.py | 26 -- Output-file-example/Nginx-openstack/main.tf | 111 ------- VM-templates/templatevm.tpl | 16 - __pycache__/aws.cpython-39.pyc | Bin 3178 -> 0 bytes __pycache__/terraformBuilder.cpython-39.pyc | Bin 738 -> 0 bytes __pycache__/terraformUtils.cpython-39.pyc | Bin 7484 -> 0 bytes api/InfrastructureTemplateController.py | 40 +++ api/__init__.py | 0 app.py | 26 -- aws.py | 163 ---------- azure.py | 1 - gcp.py | 1 - input_file_example/nginx/parameter.json | 55 ++++ .../parametri.JSON | 0 .../parametri.txt | 0 .../parametriaz.JSON | 0 .../parametriaz.txt | 0 .../parametrig.JSON | 0 .../parametrig.txt | 0 .../properties.JSON | 0 .../properties.yaml | 0 .../propertiesaz.yaml | 0 .../propertiesg.yaml | 0 .../toscainput.yaml | 0 .../wordpress/parameters.json | 0 main.py | 16 + .../nginx_openstack/ansible/nginx.play | 45 +++ .../nginx_openstack/terraform/main.tf | 101 +++++++ .../wordpress_azure/ansible}/ansible.cfg | 0 .../wordpress_azure/ansible}/docker-play.yml | 0 .../wordpress_azure/ansible}/hosts.ini | 0 .../wordpress_azure/ansible}/mysql-play.yml | 0 .../wordpress_azure/ansible}/vars.yml | 0 .../wordpress_azure/ansible}/vars2.yml | 0 .../terraform}/.terraform.lock.hcl | 0 .../wordpress_azure/terraform}/README.md | 0 .../wordpress_azure/terraform}/main.tf | 0 .../wordpress_azure/terraform}/output.tf | 0 .../terraform}/ssh_keys/wordpress_rsa | 0 .../terraform}/ssh_keys/wordpress_rsa.pub | 0 .../terraform}/terraform.tfstate | 0 .../terraform}/terraform.tfstate.backup | 0 .../wordpress_azure/terraform}/variables.tf | 0 plugin/AnsiblePlugin.py | 20 ++ plugin/TemplateUtils.py | 41 +++ plugin/TerraformPlugin.py | 28 ++ plugin/__init__.py | 0 requirements.txt | 4 +- template-location.properties | 8 + .../Databases-templates}/mysql-play.tpl | 0 .../Databases-templates}/mysql-vars.tpl | 0 .../Databases-templates}/postgres-play.tpl | 0 .../Databases-templates}/postgres-vars.tpl | 0 .../wordpress-play.tpl | 0 .../wordpress-vars.tpl | 0 templates/ansible/ubuntu/nginx.tpl | 45 +++ .../terraform/aws/network.tpl | 0 .../terraform/aws/vm.tpl | 1 - .../terraform/azure/network.tpl | 0 .../terraform/azure/vm.tpl | 0 .../terraform/google_cloud/network.tpl | 0 .../terraform/google_cloud/vm.tpl | 0 templates/terraform/open_stack/init.tpl | 18 ++ templates/terraform/open_stack/network.tpl | 29 ++ templates/terraform/open_stack/port_rule.tpl | 10 + .../terraform/open_stack/virtual_machine.tpl | 28 ++ terraformBuilder.py | 27 -- terraformUtils.py | 284 ------------------ 74 files changed, 642 insertions(+), 709 deletions(-) create mode 100644 .gitignore delete mode 100644 DB-templates/AWStemplateDB.tpl delete mode 100644 DB-templates/AZUREtemplateDB.tpl delete mode 100644 DB-templates/GCPtemplateDB.tpl delete mode 100644 DB-templates/templatepostgresql.tpl delete mode 100644 ICG.py delete mode 100644 Output-file-example/Nginx-openstack/main.tf delete mode 100644 VM-templates/templatevm.tpl delete mode 100644 __pycache__/aws.cpython-39.pyc delete mode 100644 __pycache__/terraformBuilder.cpython-39.pyc delete mode 100644 __pycache__/terraformUtils.cpython-39.pyc create mode 100644 api/InfrastructureTemplateController.py create mode 100644 api/__init__.py delete mode 100644 app.py delete mode 100644 aws.py delete mode 100644 azure.py delete mode 100644 gcp.py create mode 100644 input_file_example/nginx/parameter.json rename {Input-file-examples => input_file_example}/parametri.JSON (100%) rename {Input-file-examples => input_file_example}/parametri.txt (100%) rename {Input-file-examples => input_file_example}/parametriaz.JSON (100%) rename {Input-file-examples => input_file_example}/parametriaz.txt (100%) rename {Input-file-examples => input_file_example}/parametrig.JSON (100%) rename {Input-file-examples => input_file_example}/parametrig.txt (100%) rename {Input-file-examples => input_file_example}/properties.JSON (100%) rename {Input-file-examples => input_file_example}/properties.yaml (100%) rename {Input-file-examples => input_file_example}/propertiesaz.yaml (100%) rename {Input-file-examples => input_file_example}/propertiesg.yaml (100%) rename {Input-file-examples => input_file_example}/toscainput.yaml (100%) rename parameters.json => input_file_example/wordpress/parameters.json (100%) create mode 100644 main.py create mode 100644 output_file_example/nginx_openstack/ansible/nginx.play create mode 100644 output_file_example/nginx_openstack/terraform/main.tf rename {Output-file-example/Wordpress-azure/Ansible => output_file_example/wordpress_azure/ansible}/ansible.cfg (100%) rename {Output-file-example/Wordpress-azure/Ansible => output_file_example/wordpress_azure/ansible}/docker-play.yml (100%) rename {Output-file-example/Wordpress-azure/Ansible => output_file_example/wordpress_azure/ansible}/hosts.ini (100%) rename {Output-file-example/Wordpress-azure/Ansible => output_file_example/wordpress_azure/ansible}/mysql-play.yml (100%) rename {Output-file-example/Wordpress-azure/Ansible => output_file_example/wordpress_azure/ansible}/vars.yml (100%) rename {Output-file-example/Wordpress-azure/Ansible => output_file_example/wordpress_azure/ansible}/vars2.yml (100%) rename {Output-file-example/Wordpress-azure/Terraform => output_file_example/wordpress_azure/terraform}/.terraform.lock.hcl (100%) rename {Output-file-example/Wordpress-azure/Terraform => output_file_example/wordpress_azure/terraform}/README.md (100%) rename {Output-file-example/Wordpress-azure/Terraform => output_file_example/wordpress_azure/terraform}/main.tf (100%) rename {Output-file-example/Wordpress-azure/Terraform => output_file_example/wordpress_azure/terraform}/output.tf (100%) rename {Output-file-example/Wordpress-azure/Terraform => output_file_example/wordpress_azure/terraform}/ssh_keys/wordpress_rsa (100%) rename {Output-file-example/Wordpress-azure/Terraform => output_file_example/wordpress_azure/terraform}/ssh_keys/wordpress_rsa.pub (100%) rename {Output-file-example/Wordpress-azure/Terraform => output_file_example/wordpress_azure/terraform}/terraform.tfstate (100%) rename {Output-file-example/Wordpress-azure/Terraform => output_file_example/wordpress_azure/terraform}/terraform.tfstate.backup (100%) rename {Output-file-example/Wordpress-azure/Terraform => output_file_example/wordpress_azure/terraform}/variables.tf (100%) create mode 100644 plugin/AnsiblePlugin.py create mode 100644 plugin/TemplateUtils.py create mode 100644 plugin/TerraformPlugin.py create mode 100644 plugin/__init__.py create mode 100644 template-location.properties rename {Databases-templates => templates/ansible/Databases-templates}/mysql-play.tpl (100%) rename {Databases-templates => templates/ansible/Databases-templates}/mysql-vars.tpl (100%) rename {Databases-templates => templates/ansible/Databases-templates}/postgres-play.tpl (100%) rename {Databases-templates => templates/ansible/Databases-templates}/postgres-vars.tpl (100%) rename {Docker-services-templates => templates/ansible/Docker-services-templates}/wordpress-play.tpl (100%) rename {Docker-services-templates => templates/ansible/Docker-services-templates}/wordpress-vars.tpl (100%) create mode 100644 templates/ansible/ubuntu/nginx.tpl rename Network-templates/AWStemplateNetwork.tpl => templates/terraform/aws/network.tpl (100%) rename VM-templates/AWStemplateVM.tpl => templates/terraform/aws/vm.tpl (99%) rename Network-templates/AZUREtemplateNetwork.tpl => templates/terraform/azure/network.tpl (100%) rename VM-templates/AZUREtemplateVM.tpl => templates/terraform/azure/vm.tpl (100%) rename Network-templates/GCPtemplateNetwork.tpl => templates/terraform/google_cloud/network.tpl (100%) rename VM-templates/GCPtemplateVM.tpl => templates/terraform/google_cloud/vm.tpl (100%) create mode 100644 templates/terraform/open_stack/init.tpl create mode 100644 templates/terraform/open_stack/network.tpl create mode 100644 templates/terraform/open_stack/port_rule.tpl create mode 100644 templates/terraform/open_stack/virtual_machine.tpl delete mode 100644 terraformBuilder.py delete mode 100644 terraformUtils.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0193e37 --- /dev/null +++ b/.gitignore @@ -0,0 +1,154 @@ +.idea + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ \ No newline at end of file diff --git a/DB-templates/AWStemplateDB.tpl b/DB-templates/AWStemplateDB.tpl deleted file mode 100644 index d486f22..0000000 --- a/DB-templates/AWStemplateDB.tpl +++ /dev/null @@ -1,14 +0,0 @@ -resource "aws_db_instance" "{{ identifier }}" { - identifier = "{{ identifier }}" - instance_class = "{{ instance }}" - allocated_storage = {{ storage }} - engine = "{{ engine }}" - engine_version = "{{ version }}" - username = "{{ username }}" - password = {{ password }} - db_subnet_group_name = {{ subnet }} - vpc_security_group_ids = {{ security }} - parameter_group_name = {{ parameter }} - publicly_accessible = {{ accessible }} - skip_final_snapshot = {{ skip }} -} \ No newline at end of file diff --git a/DB-templates/AZUREtemplateDB.tpl b/DB-templates/AZUREtemplateDB.tpl deleted file mode 100644 index a8d4600..0000000 --- a/DB-templates/AZUREtemplateDB.tpl +++ /dev/null @@ -1,15 +0,0 @@ -resource "azurerm_sql_database" "test2" { - name = "{{ name }}" - resource_group_name = "{{ group_name}}" - location = "{{ location }}" - server_name = "{{ server_name }}" - - threat_detection_policy { - state = "{{ state }}" - email_addresses = {{ email }} - retention_days = "{{ days }}" - storage_account_access_key = "{{ access_key }}" - storage_endpoint = "{{ endpoint }}" - use_server_default = "{{ default }}" - } -} \ No newline at end of file diff --git a/DB-templates/GCPtemplateDB.tpl b/DB-templates/GCPtemplateDB.tpl deleted file mode 100644 index 7d0b978..0000000 --- a/DB-templates/GCPtemplateDB.tpl +++ /dev/null @@ -1,14 +0,0 @@ -resource "google_sql_database" "database" { - name = "{{ name }}" - instance = {{ instance }} -} - -resource "google_sql_database_instance" "instance" { - name = "{{ instance_name }}" - region = "{{ region }}" - settings { - tier = "{{ tier }}" - } - - deletion_protection = "{{ deletion_protection }}" -} \ No newline at end of file diff --git a/DB-templates/templatepostgresql.tpl b/DB-templates/templatepostgresql.tpl deleted file mode 100644 index dffff85..0000000 --- a/DB-templates/templatepostgresql.tpl +++ /dev/null @@ -1,8 +0,0 @@ -resource "postgresql_database" {{ name }} { - name = {{ name }} - owner = {{ owner }} - template = {{ template }} - lc_collate = {{ lc_collate }} - connection_limit = {{ connection_limit }} - allow_connections = {{ allow_connections }} -} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3775b6c..e26e070 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,4 +5,4 @@ COPY . /opt/ RUN pip install -r requirements.txt -CMD [ "python3", "/opt/app.py"] \ No newline at end of file +CMD ["uvicorn", "app.main:fast_api", "--host", "0.0.0.0", "--port", "8080"] \ No newline at end of file diff --git a/ICG.py b/ICG.py deleted file mode 100644 index 5cc58a8..0000000 --- a/ICG.py +++ /dev/null @@ -1,26 +0,0 @@ -from ansibleBuilder import * -from terraformBuilder import * -import json, sys, os - -def ICG_call(parameters): - os.system('rm -f /opt/Output-code/*') - for step in parameters["steps"]: - if step["programming_language"] == "ansible": - input_data = InputData(app_type=step["type"], code_path=step["output_path"], template_type=step["info"]["name"], template_path=step["info"]["template_path"], template_data=step["data"]) - icg = AnsibleICG() - icg.generate_code(input_data) - elif step["programming_language"] == "terraform": - input_data = step["data"] - TerraformICG(input_data) - -if __name__ == '__main__': - arg_len = len(sys.argv) - if arg_len > 1: - file_name = sys.argv[1] - else: - print("Add parameters file name") - sys.exit() - - input_file = open(file_name, "r") - parameters = json.load(input_file) - ICG_call(parameters) \ No newline at end of file diff --git a/Output-file-example/Nginx-openstack/main.tf b/Output-file-example/Nginx-openstack/main.tf deleted file mode 100644 index eb0a8e5..0000000 --- a/Output-file-example/Nginx-openstack/main.tf +++ /dev/null @@ -1,111 +0,0 @@ -terraform { -required_version = ">= 0.14.0" - required_providers { - openstack = { - source = "terraform-provider-openstack/openstack" - version = "~> 1.35.0" - } - } -} - -# Configure the OpenStack Provider -provider "openstack" { - user_name = "admin" - tenant_name = "admin" - password = "pwd" - auth_url = "http://myauthurl:5000/v2.0" - region = "RegionOne" -} - -resource "openstack_compute_keypair_v2" "user_key" { - name = "user1" - public_key = "ssh-rsa XXXXXX" -} - -# Router creation. UUID external gateway -resource "openstack_networking_router_v2" "generic" { - name = "router-generic" - external_network_id = "f67f0d72-0ddf-11e4-9d95-e1f29f417e2f" -} - -# Network creation -resource "openstack_networking_network_v2" "generic" { - name = "ostack2" -} - -#### HTTP SUBNET #### - -# Subnet http configuration -resource "openstack_networking_subnet_v2" "nginx" { - name = "subnet-nginx" - network_id = openstack_networking_network_v2.generic.id - cidr = "16.0.0.0/24" - dns_nameservers = ["8.8.8.8", "8.8.8.4"] -} - -# Router interface configuration -resource "openstack_networking_router_interface_v2" "nginx" { - router_id = openstack_networking_router_v2.generic.id - subnet_id = openstack_networking_subnet_v2.nginx.id -} - -resource "openstack_compute_secgroup_v2" "http" { - name = "http" - description = "Open input http port" - rule { - from_port = 80 - to_port = 80 - ip_protocol = "tcp" - cidr = "0.0.0.0/0" - } -} - -resource "openstack_compute_secgroup_v2" "ssh" { - name = "ssh" - description = "Open input ssh port" - rule { - from_port = 22 - to_port = 22 - ip_protocol = "tcp" - cidr = "0.0.0.0/0" - } -} - -# -# Create instance -# -resource "openstack_compute_instance_v2" "nginx" { - name = "nginx-host" - image_name = "ubuntu-20.04.3" - flavor_name = "t2.small" - key_pair = openstack_compute_keypair_v2.user_key.name - network { - port = openstack_networking_port_v2.nginx.id - } -} - -# Create network port -resource "openstack_networking_port_v2" "nginx" { - name = "nginx" - network_id = openstack_networking_network_v2.generic.id - admin_state_up = true - security_group_ids = [ - openstack_compute_secgroup_v2.ssh.id, - openstack_compute_secgroup_v2.http.id, - ] - fixed_ip { - subnet_id = openstack_networking_subnet_v2.nginx.id - } -} - -# Create floating ip -resource "openstack_networking_floatingip_v2" "nginx" { - pool = "external-network" - fixed_ip = "16.0.0.1" -} - -# Attach floating ip to instance -resource "openstack_compute_floatingip_associate_v2" "nginx" { - floating_ip = openstack_networking_floatingip_v2.nginx.address - instance_id = openstack_compute_instance_v2.nginx.id -} \ No newline at end of file diff --git a/VM-templates/templatevm.tpl b/VM-templates/templatevm.tpl deleted file mode 100644 index 33b6b1b..0000000 --- a/VM-templates/templatevm.tpl +++ /dev/null @@ -1,16 +0,0 @@ - -data "{{ vm }}" "ami{{ id }}" { - #executable_users = {{ executable_users }} - most_recent = {{ mostrecent }} - name_regex = "{{ name_regex }}" - #owners = {{ owners }} - {{ filters }} - owners = ["099720109477"] # Canonical -} -resource "aws_instance" "instance{{ id }}" { - ami = data.aws_ami.ami{{ id }}.id - instance_type = "{{ instance_type }}" - tags = { - Name = "{{ name }}" - } -} diff --git a/__pycache__/aws.cpython-39.pyc b/__pycache__/aws.cpython-39.pyc deleted file mode 100644 index 832a1cf7c8084844fa0bd7f7307559bccc153cea..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3178 zcmYe~<>g{vU|`7DGCxsBl!4(fh=Yuo7#J8F7#J9eMHm<uQW#PgTNt7kQkYVhTNt7k zQ&@r-G+C3Gku-qVAU)0?J$slL7)lsw7@8StK{R7CV=ZG1Qwn1?Q;|*$Lkbg^W=vrQ z(IB&Gm{M4?nTj^lFr=_$GZiJ(Fs86)GZlHHa0D~dFoMNGQaHhEu56~FH7VS|40&EP z3@IR9OATWRPc~CgSqd*$1s_-ie=tLyMhyd4W=0KTia<6~(To(qV1^oo6tIdKi2Z9) zgn}6+Fz0d9Fr^4*GZi1IVM-CnW}3iQG!N>UTTt_F)i9-qiZg&s0NMMgh7oMiry8ad zF{n5XSSKIEz6p#)d^Jod;!rt0u$&ge-U*CFS~W~55>Pn-u$)^BV~Qk{#grlim172r zfWkx?%3?~9fy&8(Me1r8Q{<p5rWARo9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okt10g?yTFAOOl zHmJS;mGCK`dInMo6cvDL7etK%szX{*_`&9bY9EjuP`x%IMF1=YGJj1CBe;fI1Fd&J zW*>l>cK}rHKuX-AE07YU=t_z(STD#tP$>g4??s9TSPW#}pBly#(QKxoKPh5hF(z=C zFH*ypA`W4tNPxvbB|l@4P7Py<6j)p*MH(zF3g)@gFs8^rSShk#aSkvq2I8^_j72di za$s>#xx-jg0&(30#-frGd9XOB^kFROfrRn|#-g4S1+X}%9Aqq70&(R8#-b%DiePbw zJNLldxhF*lEDmue$oCL;UP(~~i$mP`2JX%`DJo!bP<hTShUq6RLnSyI%&;w)gE zObsI__9ieD$w13-kiVFUOdzo^fvLy@T8@MK#Z=@2^)FMA540Qy`HQJ21?pd>q7-O3 z4)PaMQ4Q3;Ohq-Ia=ZqdRwghNoq?pR2~0&(Qgp%T3F4kbDSBWY#1$J-^uauc-TOeL zEZqJxDTZKwK;)jl?SGPD1Qv(53l#gHkOalQaWDfY?P{9%6@l_}5hzy_ftp}NtPBhc zMQk8~9Yk<|2u=_IYVH+rgIGKuf)_;afe3yOApjx-85kI<xN^<XVTCRyhz&09Ai|LH zvWgod3@uEHgg~0WBG7WLNEj{xErg3eZO9@~kT4HOE36<c5<>_<%i=1IToY(X&zWli zEn%y;a!p_*ESL?o3CxDtQN^8W0x#hq9H@~+;vm0CfUE+kftBn<k|1Fz5FrgBWI%*0 zNELgDF}UO|k^}KT#WlF#uHpn0+>qJ`BnSx?kRUWzxJry+t}K#gU|`T>DpCX)sstiH z#W}c4FH&V-V7SEzFO)+;W%3+Q@dhq?L8UIJYzCFVFbpf7L2|e-X8DR<-s0zj%3>Bq sE=E)gGqp&afq}tKlL=fVgE<htp!tf!CO1E&G$+*#lqicqiIO7(0NptMW&i*H diff --git a/__pycache__/terraformBuilder.cpython-39.pyc b/__pycache__/terraformBuilder.cpython-39.pyc deleted file mode 100644 index 7dbd1d0d2b16a694a8d2ecade079a9045c0dec54..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 738 zcmYe~<>g{vU|?8vY<}W0W(J1GAPzESVqjo!U|?V<)?r{^NMT4}%wdRv(2P-xDGVu0 zIZPoCK64av3Udle3qur33S%&XChJR(Iewaqw-~jO89{6)2C<nL7#N&E#@+xK%UHuu z#!z&nh9QMXk|BjTo2e)$1tO+V!cfDI!YIkm%*e=)!U9%jQ^Huokir5LV+D&{DPgK% zNMVJD<uyS~V#{VKno`1yVM1LA3l<YtkxX!an!ujTRCJ|;4a0;3CG1#C;6Rv=!Vt`$ z$>A5pRF)gXQBag$mYI@T6vdoaUL3`qms(PuUz8oil#&$1oSs||#hO@ET9m5E8O54d zUR;(N#RXLXQVkMHNs3}kPc8r%1(ivU;!MxaPtQp$F3gEy2Wv0OjpBsLCRRbkQj#<o zZ*dhQ7A5AUmZTOHuVg4<VPIhR<?n126Iz^FR2*Y!U|?!!Xkr-SpO@-Vlv$RlpsV2Q z5*!ndnUkmxl3HAn9~0o|=<FKg3a3j_i;5D{@{4kvN;7j(Qj7EoDsS<Gzyv*=-Pu7A zs{x8_Mh-?EB+SRi1Lq^Ni`W<#7&KXLv1AqJ=S8tpCg$cu@xiSLEy>I&zQtTzSqxSO i_5l*X%D}*Ii^C>2KczG$)ee-nia8h<7&sWgkQo4le6M5x diff --git a/__pycache__/terraformUtils.cpython-39.pyc b/__pycache__/terraformUtils.cpython-39.pyc deleted file mode 100644 index 256ea0ad1790954c3e118e20b1e0d4f204bc97f5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 7484 zcmYe~<>g{vU|^_Uw<B?nF$2S65C<7EF)%PVFfcF_S1>Rzq%fo~<}l<kMlt3xMKR?v zM=>*k#F%neqF7QGQkZjCLl{z6QdnCUqS#W{QrKG<qS#Y7QaD=}qBv5xQn*_fqBv7{ zQg~Y!qPSA{QutdKqPSB8QUqHVqIgn-QiNL=qIgq8Qbb!AqWDt8Qp8&rqWDvof*CX= zUV_}D$$X0=BsI4nC$S{;7Po6&S!PjwUT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7$-IT<V#H7UH)GDp?{QUHs)Ob*? zfC<LKDiu9A15|}ni6Cjzg-KO$BMlINDp1`tLtStM3aPKyz)83WRE^w147Y;J0}X1X zXXfYKVkrStWx^?`IjNxW<M@K2{F2mU5EG%dC>fN{Qb0s1h)4qwsFh3+5owVV;@#rH z98kBS7}REjq(%-#A)KiZsq#oiPkEprOi;@YTzOn!U|;}G+AyXt!6$7PQ;;TWQaI<Z z)iR}U)i9JX6j{_TfJXwEQXnINATggB2JlEAQwleD8U`emP{RP8hG9zKv0+Hz1?w%T zVF0H<rW8mD1gYz&VE|V<Oev6R2P8J9h5=mdFs1O@Fw`(EV6I_W$XLq^9$6FswI>%c z*D{wdf+o0{nQB>TSQfC=FoUNfSdhjNMf{4qK>-f(X%s7H;5aD?G=5x^pOYHJ2^%>w zxW(xF^8f$;|B++<7Atrd{}u;K)h(`^<oM+L9PmV$KyrRw9we^gb24)?OKu4!=H%p; z$0Nj{eUdDY{n;QQ2Snt82++_mQh|gT|C+3j4g+}15dUx?7p$3An8V4)z)%btaAaT< zB9J7&L&^CJ3=EnAQEcF0b|XlSh8g5G77zg%&%DK)SY8YsaxMZ5F&2Rao{B)D5k;UT zO%bRcQ3Ps}6oE>$B2WS=0vQS!%>;E!AT02>CWlRKeoARhsvW4iRScTg5CQc}m^dss QM8QyyLyAL{LzzPo0Hpf=nE(I) diff --git a/api/InfrastructureTemplateController.py b/api/InfrastructureTemplateController.py new file mode 100644 index 0000000..0706024 --- /dev/null +++ b/api/InfrastructureTemplateController.py @@ -0,0 +1,40 @@ +import logging +import tarfile + +from fastapi import APIRouter, Body +from fastapi.responses import FileResponse + +from plugin import TerraformPlugin +from plugin import AnsiblePlugin + + +api_router = APIRouter() + +@api_router.post("/infrastructure/files") +def create_infrastructure_files(intermediate_representation: dict = Body(...)): + logging.info("Received intermediate representation create_infrastructure_files request") + choose_plugin(intermediate_representation) + logging.info("Creating compress folder with iac files") + output_template_folder = intermediate_representation["output_path"] + compress_file_name = "outputIaC.tar.gz" + compress_file_path = compress_file(output_template_folder, compress_file_name) + return FileResponse(compress_file_path, media_type='application/octet-stream', filename=compress_file_name) + +def choose_plugin(parameters): + # os.system('rm -f /opt/Output-code/*') + for step in parameters["steps"]: + if step["programming_language"] == "ansible": + input_data = step["data"] + AnsiblePlugin.create_files(input_data, parameters["output_path"]) + elif step["programming_language"] == "terraform": + input_data = step["data"] + TerraformPlugin.create_files(input_data, parameters["output_path"]) + +def compress_file(source_folder, dest_file_name): + # prefix_path = "/opt/" + prefix_path = "" + logging.info("Compressing folder %s into destination %s", prefix_path + source_folder, + prefix_path + dest_file_name) + with tarfile.open(prefix_path + dest_file_name, "w:gz") as tar: + tar.add(source_folder, arcname='.') + return prefix_path + dest_file_name diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app.py b/app.py deleted file mode 100644 index a500035..0000000 --- a/app.py +++ /dev/null @@ -1,26 +0,0 @@ -from ICG import * -from flask import Flask, request, send_file -import tarfile -import os.path - -def create_app(test_config=None): - app = Flask(__name__) - - app.config["CLIENT_IMAGES"] = "/opt" - - @app.post("/") - def ICG(): - if request.is_json: - parameters = request.get_json() - ICG_call(parameters) - with tarfile.open("/opt/outputIaC.tar.gz", "w:gz") as tar: - tar.add("/opt/Output-code", arcname=os.path.basename("/opt/Output-code")) - file_name = "/opt/outputIaC.tar.gz" - return send_file(file_name, attachment_filename='outputIaC.tar.gz'), 201 - return {"error": "Request must be JSON"}, 415 - return app - -APP = create_app() - -if __name__ == '__main__': - APP.run(host='0.0.0.0', port=5000, debug=True) \ No newline at end of file diff --git a/aws.py b/aws.py deleted file mode 100644 index 85c01dc..0000000 --- a/aws.py +++ /dev/null @@ -1,163 +0,0 @@ -def vmcatalog1 (vcpu, gib, sigla): - vcpu = str(vcpu) - gib = str(gib) - if sigla == "mac" and vcpu=='12' and gib=='32': - return "mac1.metal" - elif sigla == "t2": - if vcpu == '1': - if gib == '0.5': - return "t2.nano" - elif gib == '1': - return "t2.micro" - elif gib == '2': - return "t2.small" - elif vcpu == '2': - if gib == '4': - return "t2.medium" - elif gib == '8': - return "t2.large" - elif vcpu == '4' and gib == '16': - return "t2.xlarge" - elif vcpu == '8' and gib == '32': - return "t2.2xlarge" - elif sigla == "m6g" or sigla == "m6gd": - if vcpu == '1' and gib == '4': - return sigla+".medium" - if vcpu == '2' and gib == '8': - return sigla+".large" - if vcpu == '4' and gib == '16': - return sigla+".xlarge" - if vcpu == '8' and gib == '32': - return sigla+".2xlarge" - if vcpu == '16' and gib == '64': - return sigla+".4xlarge" - if vcpu == '32' and gib == '128': - return sigla+".8xlarge" - if vcpu == '48' and gib == '192': - return sigla+".12xlarge" - if vcpu == '64' and gib == '256': - return sigla+".16xlarge" - elif "m5" in sigla or sigla == "m4": - if vcpu == '2' and gib == '8': - return sigla+".large" - if vcpu == '4' and gib == '16': - return sigla+".xlarge" - if vcpu == '8' and gib == '32': - return sigla+".2xlarge" - if vcpu == '16' and gib == '64': - return sigla+".4xlarge" - if vcpu == '32' and gib == '128': - return sigla+".8xlarge" - if vcpu == '48' and gib == '192': - return sigla+".12xlarge" - if vcpu == '64' and gib == '256': - return sigla+".16xlarge" - if vcpu == '96' and gib == '384': - return sigla+".24xlarge" - elif sigla == "a1": - if vcpu == '1' and gib == '2': - return "a1.media" - if vcpu == '2' and gib == '4': - return "a1.large" - if vcpu == '4' and gib == '8': - return "a1.xlarge" - if vcpu == '8' and gib == '16': - return "a1.2xlarge" - if vcpu == '16' and gib == '32': - return "a1.4xlarge" - elif vcpu=='2': - if gib == '0.5': - return sigla+".nano" - elif gib == '1': - return sigla+".micro" - elif gib == '2': - return sigla+".small" - elif gib == '4': - return sigla+".medium" - elif gib == '8': - return sigla+".large" - elif vcpu == '4' and gib == '16': - return sigla+".xlarge" - elif vcpu == '8' and gib == '32': - return sigla+".2xlarge" - return "t3.micro" - -def vmcatalog (vcpu, gib): - vcpu = str(vcpu) - gib = str(gib) - if vcpu=='12' and gib=='32': - return "mac1.metal" - if vcpu == '1': - if gib == '0.5': - return "t2.nano" - if gib == '1': - return "t2.micro" - if gib == '2': - return "t2.small" - if vcpu == '2': - if gib == '4': - return "t2.medium" - if gib == '8': - return "t2.large" - if vcpu == '4' and gib == '16': - return "t2.xlarge" - if vcpu == '8' and gib == '32': - return "t2.2xlarge" - if vcpu == '1' and gib == '4': - return "m6g.medium" - if vcpu == '2' and gib == '8': - return "m6g.large" - if vcpu == '4' and gib == '16': - return "m6g.xlarge" - if vcpu == '8' and gib == '32': - return "m6g.2xlarge" - if vcpu == '16' and gib == '64': - return "m6g.4xlarge" - if vcpu == '32' and gib == '128': - return "m6g.8xlarge" - if vcpu == '48' and gib == '192': - return "m6g.12xlarge" - if vcpu == '64' and gib == '256': - return "m6g.16xlarge" - if vcpu == '2' and gib == '8': - return "m4.large" - if vcpu == '4' and gib == '16': - return "m4.xlarge" - if vcpu == '8' and gib == '32': - return "m4.2xlarge" - if vcpu == '16' and gib == '64': - return "m4.4xlarge" - if vcpu == '32' and gib == '128': - return "m4.8xlarge" - if vcpu == '48' and gib == '192': - return "m4.12xlarge" - if vcpu == '64' and gib == '256': - return "m4.16xlarge" - if vcpu == '96' and gib == '384': - return "m4.24xlarge" - if vcpu == '1' and gib == '2': - return "a1.media" - if vcpu == '2' and gib == '4': - return "a1.large" - if vcpu == '4' and gib == '8': - return "a1.xlarge" - if vcpu == '8' and gib == '16': - return "a1.2xlarge" - if vcpu == '16' and gib == '32': - return "a1.4xlarge" - if vcpu=='2': - if gib == '0.5': - return "t3.nano" - if gib == '1': - return "t3.micro" - if gib == '2': - return "t3.small" - if gib == '4': - return "t3.medium" - if gib == '8': - return "t3.large" - if vcpu == '4' and gib == '16': - return "t3.xlarge" - if vcpu == '8' and gib == '32': - return "t3.2xlarge" - return "t2.micro" diff --git a/azure.py b/azure.py deleted file mode 100644 index 2f1a933..0000000 --- a/azure.py +++ /dev/null @@ -1 +0,0 @@ -# Azure specific functionalities \ No newline at end of file diff --git a/gcp.py b/gcp.py deleted file mode 100644 index 9b40466..0000000 --- a/gcp.py +++ /dev/null @@ -1 +0,0 @@ -# Google Clud Platform specific functionalities \ No newline at end of file diff --git a/input_file_example/nginx/parameter.json b/input_file_example/nginx/parameter.json new file mode 100644 index 0000000..87c35d8 --- /dev/null +++ b/input_file_example/nginx/parameter.json @@ -0,0 +1,55 @@ +{ + "output_path": "output_file_example/nginx_openstack/", + "steps": [ + { + "programming_language": "terraform", + "data": { + "provider": "openstack", + "vm": [{ + "name": "nginx-host", + "flavor": "small", + "vm_security_groups": "default", + "ssh_user": "ubuntu", + "ssh_key_file": "/home/user1/.ssh/openstack.key", + "address": "16.0.0.1", + "image": "ubuntu-20.04.3", + "network_name": "ostack2" + }], + "net": [{ + "name": "ostack2", + "address": "16.0.0.0/24", + "protocol": "tcp/ip", + "rules_name": ["rule_1", "rule_2"] + }], + "sg": [{ + "name": "rule_1", + "from_port": 80, + "to_port": 80, + "ip_protocol": "tcp", + "ipv6_cidr_blocks": "0.0.0.0/0" + }, { + "name": "rule_2", + "from_port": 22, + "to_port": 22, + "ip_protocol": "tcp", + "ipv6_cidr_blocks": "0.0.0.0/0" + }] + } + }, + { + "programming_language": "ansible", + "data": { + "operating_system": "ubuntu", + "nginx": { + "ssh_user": "ubuntu", + "ssh_key_file": "/home/user1/.ssh/openstack.key", + "address": "16.0.0.1", + "source_code": [ + "/var/www/html/index.html", + "/usr/share/nginx/html/index.html" + ] + } + } + } + ] +} \ No newline at end of file diff --git a/Input-file-examples/parametri.JSON b/input_file_example/parametri.JSON similarity index 100% rename from Input-file-examples/parametri.JSON rename to input_file_example/parametri.JSON diff --git a/Input-file-examples/parametri.txt b/input_file_example/parametri.txt similarity index 100% rename from Input-file-examples/parametri.txt rename to input_file_example/parametri.txt diff --git a/Input-file-examples/parametriaz.JSON b/input_file_example/parametriaz.JSON similarity index 100% rename from Input-file-examples/parametriaz.JSON rename to input_file_example/parametriaz.JSON diff --git a/Input-file-examples/parametriaz.txt b/input_file_example/parametriaz.txt similarity index 100% rename from Input-file-examples/parametriaz.txt rename to input_file_example/parametriaz.txt diff --git a/Input-file-examples/parametrig.JSON b/input_file_example/parametrig.JSON similarity index 100% rename from Input-file-examples/parametrig.JSON rename to input_file_example/parametrig.JSON diff --git a/Input-file-examples/parametrig.txt b/input_file_example/parametrig.txt similarity index 100% rename from Input-file-examples/parametrig.txt rename to input_file_example/parametrig.txt diff --git a/Input-file-examples/properties.JSON b/input_file_example/properties.JSON similarity index 100% rename from Input-file-examples/properties.JSON rename to input_file_example/properties.JSON diff --git a/Input-file-examples/properties.yaml b/input_file_example/properties.yaml similarity index 100% rename from Input-file-examples/properties.yaml rename to input_file_example/properties.yaml diff --git a/Input-file-examples/propertiesaz.yaml b/input_file_example/propertiesaz.yaml similarity index 100% rename from Input-file-examples/propertiesaz.yaml rename to input_file_example/propertiesaz.yaml diff --git a/Input-file-examples/propertiesg.yaml b/input_file_example/propertiesg.yaml similarity index 100% rename from Input-file-examples/propertiesg.yaml rename to input_file_example/propertiesg.yaml diff --git a/Input-file-examples/toscainput.yaml b/input_file_example/toscainput.yaml similarity index 100% rename from Input-file-examples/toscainput.yaml rename to input_file_example/toscainput.yaml diff --git a/parameters.json b/input_file_example/wordpress/parameters.json similarity index 100% rename from parameters.json rename to input_file_example/wordpress/parameters.json diff --git a/main.py b/main.py new file mode 100644 index 0000000..960b240 --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +import json +import logging +from fastapi import FastAPI + +import api.InfrastructureTemplateController +from api import InfrastructureTemplateController + +fast_api = FastAPI() + +fast_api.include_router(api.InfrastructureTemplateController.api_router) +logging.getLogger().setLevel(logging.INFO) + +if __name__ == '__main__': + parameters_file = open("input_file_example/nginx/parameter.json") + parameters_file = json.load(parameters_file) + InfrastructureTemplateController.choose_plugin(parameters_file) diff --git a/output_file_example/nginx_openstack/ansible/nginx.play b/output_file_example/nginx_openstack/ansible/nginx.play new file mode 100644 index 0000000..c8790e1 --- /dev/null +++ b/output_file_example/nginx_openstack/ansible/nginx.play @@ -0,0 +1,45 @@ +--- +- hosts: 16.0.0.1 + gather_facts: no + become: yes + vars: + ansible_ssh_private_key_file: "/home/user1/.ssh/openstack.key" + ansible_ssh_user: "ubuntu" + tasks: + - name: Update repositories + apt: + update_cache: yes + + - name: Install nginx + package: + name: nginx + + - name: Start nginx + service: + name: nginx + enabled: yes + state: started + + - name: Set attributes + set_stats: + data: + site_config_dir: /etc/nginx/conf.d + + - name: Install sample site + copy: + dest: "" + content: | + <!doctype html> + <html lang="en"> + <head> + <title>Hello World!</title> + </head> + <body> + <h1>Sample web page</h1> + <p>With little content ;)</p> + </body> + </html> + with_items: + - /var/www/html/index.html + - /usr/share/nginx/html/index.html + diff --git a/output_file_example/nginx_openstack/terraform/main.tf b/output_file_example/nginx_openstack/terraform/main.tf new file mode 100644 index 0000000..a91ec88 --- /dev/null +++ b/output_file_example/nginx_openstack/terraform/main.tf @@ -0,0 +1,101 @@ +terraform { +required_version = ">= 0.14.0" + required_providers { + openstack = { + source = "terraform-provider-openstack/openstack" + version = "~> 1.35.0" + } + } +} + +# Configure the OpenStack Provider +provider "openstack" { + user_name = "admin" + tenant_name = "test" + password = "wRpuXgVqBzQqGwx8Bu0sylEeb8FgjSYG" + auth_url = "https://127.0.0.1:5000/v3" + insecure = true +} +# Create virtual machine +resource "openstack_compute_instance_v2" "nginx-host" { + name = "nginx-host" + image_name = "ubuntu-20.04.3" + flavor_name = "small" + key_pair = openstack_compute_keypair_v2.nginx-host_ssh_key.name + network { + port = openstack_networking_port_v2.ostack2.id + } +} + +# Create ssh keys +resource "openstack_compute_keypair_v2" "nginx-host_ssh_key" { + name = "ubuntu" + public_key = "/home/user1/.ssh/openstack.key" +} + +# Create floating ip +resource "openstack_networking_floatingip_v2" "nginx-host_floating_ip" { + pool = "external" + # fixed_ip = "16.0.0.1" +} + +# Attach floating ip to instance +resource "openstack_compute_floatingip_associate_v2" "nginx-host_floating_ip_association" { + floating_ip = openstack_networking_floatingip_v2.nginx-host_floating_ip.address + instance_id = openstack_compute_instance_v2.nginx-host.id +} + +## Network + +# Create Network +resource "openstack_networking_network_v2" "ostack2" { + name = "ostack2" +} + +# Create Subnet +resource "openstack_networking_subnet_v2" "ostack2_subnet" { + name = "ostack2_subnet" + network_id = openstack_networking_network_v2.ostack2.id + cidr = "16.0.0.0/24" + dns_nameservers = ["8.8.8.8", "8.8.8.4"] +} + +# Attach networking port +resource "openstack_networking_port_v2" "ostack2" { + name = "ostack2" + network_id = openstack_networking_network_v2.ostack2.id + admin_state_up = true + security_group_ids = [ + + openstack_compute_secgroup_v2.rule_1_secgroup.id, + + openstack_compute_secgroup_v2.rule_2_secgroup.id, + + ] + fixed_ip { + subnet_id = openstack_networking_subnet_v2.ostack2_subnet.id + } +} + +resource "openstack_compute_secgroup_v2" "rule_1_secgroup" { + name = "rule_1" + description = "Security group rule for port 80-80" + rule { + from_port = 80 + to_port = 80 + ip_protocol = "tcp" + cidr = "0.0.0.0/0" + } +} + +resource "openstack_compute_secgroup_v2" "rule_2_secgroup" { + name = "rule_2" + description = "Security group rule for port 22-22" + rule { + from_port = 22 + to_port = 22 + ip_protocol = "tcp" + cidr = "0.0.0.0/0" + } +} + diff --git a/Output-file-example/Wordpress-azure/Ansible/ansible.cfg b/output_file_example/wordpress_azure/ansible/ansible.cfg similarity index 100% rename from Output-file-example/Wordpress-azure/Ansible/ansible.cfg rename to output_file_example/wordpress_azure/ansible/ansible.cfg diff --git a/Output-file-example/Wordpress-azure/Ansible/docker-play.yml b/output_file_example/wordpress_azure/ansible/docker-play.yml similarity index 100% rename from Output-file-example/Wordpress-azure/Ansible/docker-play.yml rename to output_file_example/wordpress_azure/ansible/docker-play.yml diff --git a/Output-file-example/Wordpress-azure/Ansible/hosts.ini b/output_file_example/wordpress_azure/ansible/hosts.ini similarity index 100% rename from Output-file-example/Wordpress-azure/Ansible/hosts.ini rename to output_file_example/wordpress_azure/ansible/hosts.ini diff --git a/Output-file-example/Wordpress-azure/Ansible/mysql-play.yml b/output_file_example/wordpress_azure/ansible/mysql-play.yml similarity index 100% rename from Output-file-example/Wordpress-azure/Ansible/mysql-play.yml rename to output_file_example/wordpress_azure/ansible/mysql-play.yml diff --git a/Output-file-example/Wordpress-azure/Ansible/vars.yml b/output_file_example/wordpress_azure/ansible/vars.yml similarity index 100% rename from Output-file-example/Wordpress-azure/Ansible/vars.yml rename to output_file_example/wordpress_azure/ansible/vars.yml diff --git a/Output-file-example/Wordpress-azure/Ansible/vars2.yml b/output_file_example/wordpress_azure/ansible/vars2.yml similarity index 100% rename from Output-file-example/Wordpress-azure/Ansible/vars2.yml rename to output_file_example/wordpress_azure/ansible/vars2.yml diff --git a/Output-file-example/Wordpress-azure/Terraform/.terraform.lock.hcl b/output_file_example/wordpress_azure/terraform/.terraform.lock.hcl similarity index 100% rename from Output-file-example/Wordpress-azure/Terraform/.terraform.lock.hcl rename to output_file_example/wordpress_azure/terraform/.terraform.lock.hcl diff --git a/Output-file-example/Wordpress-azure/Terraform/README.md b/output_file_example/wordpress_azure/terraform/README.md similarity index 100% rename from Output-file-example/Wordpress-azure/Terraform/README.md rename to output_file_example/wordpress_azure/terraform/README.md diff --git a/Output-file-example/Wordpress-azure/Terraform/main.tf b/output_file_example/wordpress_azure/terraform/main.tf similarity index 100% rename from Output-file-example/Wordpress-azure/Terraform/main.tf rename to output_file_example/wordpress_azure/terraform/main.tf diff --git a/Output-file-example/Wordpress-azure/Terraform/output.tf b/output_file_example/wordpress_azure/terraform/output.tf similarity index 100% rename from Output-file-example/Wordpress-azure/Terraform/output.tf rename to output_file_example/wordpress_azure/terraform/output.tf diff --git a/Output-file-example/Wordpress-azure/Terraform/ssh_keys/wordpress_rsa b/output_file_example/wordpress_azure/terraform/ssh_keys/wordpress_rsa similarity index 100% rename from Output-file-example/Wordpress-azure/Terraform/ssh_keys/wordpress_rsa rename to output_file_example/wordpress_azure/terraform/ssh_keys/wordpress_rsa diff --git a/Output-file-example/Wordpress-azure/Terraform/ssh_keys/wordpress_rsa.pub b/output_file_example/wordpress_azure/terraform/ssh_keys/wordpress_rsa.pub similarity index 100% rename from Output-file-example/Wordpress-azure/Terraform/ssh_keys/wordpress_rsa.pub rename to output_file_example/wordpress_azure/terraform/ssh_keys/wordpress_rsa.pub diff --git a/Output-file-example/Wordpress-azure/Terraform/terraform.tfstate b/output_file_example/wordpress_azure/terraform/terraform.tfstate similarity index 100% rename from Output-file-example/Wordpress-azure/Terraform/terraform.tfstate rename to output_file_example/wordpress_azure/terraform/terraform.tfstate diff --git a/Output-file-example/Wordpress-azure/Terraform/terraform.tfstate.backup b/output_file_example/wordpress_azure/terraform/terraform.tfstate.backup similarity index 100% rename from Output-file-example/Wordpress-azure/Terraform/terraform.tfstate.backup rename to output_file_example/wordpress_azure/terraform/terraform.tfstate.backup diff --git a/Output-file-example/Wordpress-azure/Terraform/variables.tf b/output_file_example/wordpress_azure/terraform/variables.tf similarity index 100% rename from Output-file-example/Wordpress-azure/Terraform/variables.tf rename to output_file_example/wordpress_azure/terraform/variables.tf diff --git a/plugin/AnsiblePlugin.py b/plugin/AnsiblePlugin.py new file mode 100644 index 0000000..0065bdf --- /dev/null +++ b/plugin/AnsiblePlugin.py @@ -0,0 +1,20 @@ +import logging +from plugin import TemplateUtils + + +def create_files(parameters, output_path): + language = "ansible" + operating_system = parameters["operating_system"] + resources = parameters.keys() + for resource_name in resources: + logging.info("Creating template for resource '%s'", resource_name) + template_path = TemplateUtils.find_template_path(language, operating_system, resource_name) + if template_path: + #for resource_params in parameters[resource_name]: + resource_params = parameters[resource_name] + output_file_path = output_path + "/".join([language, resource_name]) + ".play" + template = TemplateUtils.read_template(template_path) + template_filled = TemplateUtils.edit_template(template, resource_params) + TemplateUtils.write_template(template_filled, output_file_path) + logging.info("File available at: {}".format(output_path)) + diff --git a/plugin/TemplateUtils.py b/plugin/TemplateUtils.py new file mode 100644 index 0000000..c90ff5d --- /dev/null +++ b/plugin/TemplateUtils.py @@ -0,0 +1,41 @@ +import configparser +import logging +import jinja2 +from jinja2 import Template + + +def find_template_path(iac_language, key, resource_name): + try: + properties_reader = configparser.ConfigParser() + properties_reader.read("template-location.properties") + template_path = properties_reader.get(iac_language + "." + key, resource_name) + logging.info("Chosen template at: '%s'", template_path) + return template_path + except configparser.NoOptionError as error: + logging.warning("%s. Please check properties file", error) + pass + + +def edit_template(template, parameters: dict): + logging.info("Starting editing template") + render = template.render(parameters) + template_with_custom_params = ""+render+"\n" + return template_with_custom_params + + +def read_template(template_path): + logging.info("Reading template at: '%s'", template_path) + try: + template = Template(open(template_path, "r").read()) + return template + except jinja2.exceptions.TemplateSyntaxError as exc: + # TODO or error? + logging.warning('Syntax error on template %s, %s', template_path, exc) + pass + + +def write_template(template, output_path_file): + file = open(output_path_file, "w+") + file.write(template) + logging.info("Writing file at: '%s'", output_path_file) + file.close() diff --git a/plugin/TerraformPlugin.py b/plugin/TerraformPlugin.py new file mode 100644 index 0000000..4ca1617 --- /dev/null +++ b/plugin/TerraformPlugin.py @@ -0,0 +1,28 @@ +import logging +from plugin import TemplateUtils + + +def create_files(parameters, output_path): + language = "terraform" + provider = parameters["provider"] + resources = parameters.keys() + terraform_file = create_init_file(language, provider) + for resource_name in resources: + logging.info("Creating template for resource '%s'", resource_name) + template_path = TemplateUtils.find_template_path(language, provider, resource_name) + if template_path: + for resource_params in parameters[resource_name]: + template = TemplateUtils.read_template(template_path) + # resource = parameters[resource_name] + template_filled = TemplateUtils.edit_template(template, resource_params) + terraform_file = terraform_file + template_filled + "\n" + output_file_path = output_path + "/".join([language, "main.tf"]) + TemplateUtils.write_template(terraform_file, output_file_path) + logging.info("File available at: {}".format(output_path)) + + +def create_init_file(language, provider): + logging.info("Creating init %s file for provider %s", language, provider) + template_path = TemplateUtils.find_template_path(language, provider, "init") + template = TemplateUtils.read_template(template_path) + return template.render() + "\n" diff --git a/plugin/__init__.py b/plugin/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt index 24019da..7c2d192 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ Jinja2==3.0.3 flask==2.0.2 -PyYAML==6.0 \ No newline at end of file +PyYAML==6.0 +fastapi~=0.74.1 +uvicorn=0.17.5 \ No newline at end of file diff --git a/template-location.properties b/template-location.properties new file mode 100644 index 0000000..eb216af --- /dev/null +++ b/template-location.properties @@ -0,0 +1,8 @@ +[terraform.openstack] +init = templates/terraform/open_stack/init.tpl +vm = templates/terraform/open_stack/virtual_machine.tpl +net = templates/terraform/open_stack/network.tpl +sg = templates/terraform/open_stack/port_rule.tpl + +[ansible.ubuntu] +nginx = templates/ansible/ubuntu/nginx.tpl \ No newline at end of file diff --git a/Databases-templates/mysql-play.tpl b/templates/ansible/Databases-templates/mysql-play.tpl similarity index 100% rename from Databases-templates/mysql-play.tpl rename to templates/ansible/Databases-templates/mysql-play.tpl diff --git a/Databases-templates/mysql-vars.tpl b/templates/ansible/Databases-templates/mysql-vars.tpl similarity index 100% rename from Databases-templates/mysql-vars.tpl rename to templates/ansible/Databases-templates/mysql-vars.tpl diff --git a/Databases-templates/postgres-play.tpl b/templates/ansible/Databases-templates/postgres-play.tpl similarity index 100% rename from Databases-templates/postgres-play.tpl rename to templates/ansible/Databases-templates/postgres-play.tpl diff --git a/Databases-templates/postgres-vars.tpl b/templates/ansible/Databases-templates/postgres-vars.tpl similarity index 100% rename from Databases-templates/postgres-vars.tpl rename to templates/ansible/Databases-templates/postgres-vars.tpl diff --git a/Docker-services-templates/wordpress-play.tpl b/templates/ansible/Docker-services-templates/wordpress-play.tpl similarity index 100% rename from Docker-services-templates/wordpress-play.tpl rename to templates/ansible/Docker-services-templates/wordpress-play.tpl diff --git a/Docker-services-templates/wordpress-vars.tpl b/templates/ansible/Docker-services-templates/wordpress-vars.tpl similarity index 100% rename from Docker-services-templates/wordpress-vars.tpl rename to templates/ansible/Docker-services-templates/wordpress-vars.tpl diff --git a/templates/ansible/ubuntu/nginx.tpl b/templates/ansible/ubuntu/nginx.tpl new file mode 100644 index 0000000..05b342b --- /dev/null +++ b/templates/ansible/ubuntu/nginx.tpl @@ -0,0 +1,45 @@ +--- +- hosts: {{ address }} + gather_facts: no + become: yes + vars: + ansible_ssh_private_key_file: "{{ ssh_key_file }}" + ansible_ssh_user: "{{ ssh_user }}" + tasks: + - name: Update repositories + apt: + update_cache: yes + + - name: Install nginx + package: + name: nginx + + - name: Start nginx + service: + name: nginx + enabled: yes + state: started + + - name: Set attributes + set_stats: + data: + site_config_dir: /etc/nginx/conf.d + + - name: Install sample site + copy: + dest: "{{ item }}" + content: | + <!doctype html> + <html lang="en"> + <head> + <title>Hello World!</title> + </head> + <body> + <h1>Sample web page</h1> + <p>With little content ;)</p> + </body> + </html> + with_items: + - /var/www/html/index.html + - /usr/share/nginx/html/index.html + diff --git a/Network-templates/AWStemplateNetwork.tpl b/templates/terraform/aws/network.tpl similarity index 100% rename from Network-templates/AWStemplateNetwork.tpl rename to templates/terraform/aws/network.tpl diff --git a/VM-templates/AWStemplateVM.tpl b/templates/terraform/aws/vm.tpl similarity index 99% rename from VM-templates/AWStemplateVM.tpl rename to templates/terraform/aws/vm.tpl index 33b6b1b..41ec39a 100644 --- a/VM-templates/AWStemplateVM.tpl +++ b/templates/terraform/aws/vm.tpl @@ -1,4 +1,3 @@ - data "{{ vm }}" "ami{{ id }}" { #executable_users = {{ executable_users }} most_recent = {{ mostrecent }} diff --git a/Network-templates/AZUREtemplateNetwork.tpl b/templates/terraform/azure/network.tpl similarity index 100% rename from Network-templates/AZUREtemplateNetwork.tpl rename to templates/terraform/azure/network.tpl diff --git a/VM-templates/AZUREtemplateVM.tpl b/templates/terraform/azure/vm.tpl similarity index 100% rename from VM-templates/AZUREtemplateVM.tpl rename to templates/terraform/azure/vm.tpl diff --git a/Network-templates/GCPtemplateNetwork.tpl b/templates/terraform/google_cloud/network.tpl similarity index 100% rename from Network-templates/GCPtemplateNetwork.tpl rename to templates/terraform/google_cloud/network.tpl diff --git a/VM-templates/GCPtemplateVM.tpl b/templates/terraform/google_cloud/vm.tpl similarity index 100% rename from VM-templates/GCPtemplateVM.tpl rename to templates/terraform/google_cloud/vm.tpl diff --git a/templates/terraform/open_stack/init.tpl b/templates/terraform/open_stack/init.tpl new file mode 100644 index 0000000..5f82e60 --- /dev/null +++ b/templates/terraform/open_stack/init.tpl @@ -0,0 +1,18 @@ +terraform { +required_version = ">= 0.14.0" + required_providers { + openstack = { + source = "terraform-provider-openstack/openstack" + version = "~> 1.35.0" + } + } +} + +# Configure the OpenStack Provider +provider "openstack" { + user_name = "admin" + tenant_name = "test" + password = "wRpuXgVqBzQqGwx8Bu0sylEeb8FgjSYG" + auth_url = "https://127.0.0.1:5000/v3" + insecure = true +} \ No newline at end of file diff --git a/templates/terraform/open_stack/network.tpl b/templates/terraform/open_stack/network.tpl new file mode 100644 index 0000000..938cc0d --- /dev/null +++ b/templates/terraform/open_stack/network.tpl @@ -0,0 +1,29 @@ +## Network + +# Create Network +resource "openstack_networking_network_v2" "{{ name }}" { + name = "{{ name }}" +} + +# Create Subnet +resource "openstack_networking_subnet_v2" "{{ name ~ "_subnet" }}" { + name = "{{ name ~ "_subnet" }}" + network_id = openstack_networking_network_v2.{{ name }}.id + cidr = "{{ address }}" + dns_nameservers = ["8.8.8.8", "8.8.8.4"] +} + +# Attach networking port +resource "openstack_networking_port_v2" "{{ name }}" { + name = "{{ name }}" + network_id = openstack_networking_network_v2.{{ name }}.id + admin_state_up = true + security_group_ids = [ + {% for rule_name in rules_name %} + openstack_compute_secgroup_v2.{{ rule_name ~ "_secgroup" }}.id, + {% endfor %} + ] + fixed_ip { + subnet_id = openstack_networking_subnet_v2.{{ name ~ "_subnet" }}.id + } +} \ No newline at end of file diff --git a/templates/terraform/open_stack/port_rule.tpl b/templates/terraform/open_stack/port_rule.tpl new file mode 100644 index 0000000..bb02290 --- /dev/null +++ b/templates/terraform/open_stack/port_rule.tpl @@ -0,0 +1,10 @@ +resource "openstack_compute_secgroup_v2" "{{ name ~ "_secgroup" }}" { + name = "{{ name }}" + description = "Security group rule for port {{ from_port }}-{{ to_port }}" + rule { + from_port = {{ from_port }} + to_port = {{ to_port }} + ip_protocol = "{{ ip_protocol }}" + cidr = "{{ ipv6_cidr_blocks }}" + } +} \ No newline at end of file diff --git a/templates/terraform/open_stack/virtual_machine.tpl b/templates/terraform/open_stack/virtual_machine.tpl new file mode 100644 index 0000000..50b203c --- /dev/null +++ b/templates/terraform/open_stack/virtual_machine.tpl @@ -0,0 +1,28 @@ +# Create virtual machine +resource "openstack_compute_instance_v2" "{{ name }}" { + name = "{{ name }}" + image_name = "{{ image }}" + flavor_name = "{{ flavor }}" + key_pair = openstack_compute_keypair_v2.{{ name ~ "_ssh_key" }}.name + network { + port = openstack_networking_port_v2.{{ network_name }}.id + } +} + +# Create ssh keys +resource "openstack_compute_keypair_v2" "{{ name ~ "_ssh_key" }}" { + name = "{{ ssh_user }}" + public_key = "{{ ssh_key_file }}" +} + +# Create floating ip +resource "openstack_networking_floatingip_v2" "{{name ~ "_floating_ip"}}" { + pool = "external" + # fixed_ip = "{{ address }}" +} + +# Attach floating ip to instance +resource "openstack_compute_floatingip_associate_v2" "{{ name ~ "_floating_ip_association" }}" { + floating_ip = openstack_networking_floatingip_v2.{{ name ~ "_floating_ip" }}.address + instance_id = openstack_compute_instance_v2.{{ name }}.id +} diff --git a/terraformBuilder.py b/terraformBuilder.py deleted file mode 100644 index 9f768fa..0000000 --- a/terraformBuilder.py +++ /dev/null @@ -1,27 +0,0 @@ -import json -import yaml -from terraformUtils import * -import sys - - -def TerraformICG(parameters): - if 'vm' in parameters: - if (parameters['provider']=='aws'): - if 'vm' in parameters: - awsvm(parameters['vm']) - if 'network' in parameters: - networkaws(parameters['network']) - if 'db' in parameters: - awsdb(parameters['db']) - elif (parameters['provider']=='gcp'): - gcpvm(parameters['vm']) - if 'network' in parameters: - networkg(parameters['network']) - if 'db' in parameters: - googlesql(parameters['db']) - elif (parameters['provider']=='azure'): - azurevm(parameters['vm']) - if 'network' in parameters: - networkaz(parameters['network']) - if 'db' in parameters: - azuredb(parameters['db']) \ No newline at end of file diff --git a/terraformUtils.py b/terraformUtils.py deleted file mode 100644 index 0420f11..0000000 --- a/terraformUtils.py +++ /dev/null @@ -1,284 +0,0 @@ -from jinja2 import Template, Environment, FileSystemLoader -import re -from aws import * - -def awsvm(parametri): - finalString = "" - jinjaTemplate = Template(open("VM-templates/AWStemplateVM.tpl", "r").read()) - for parameter in parametri: - my_dict = {'vm':'aws_ami', - 'id': '1', - 'id': 'vm1', - 'os': 'ubuntu', - 'executable_users':["self"], - 'mostrecent': 'true', - 'name_regex': "^myami-\\d{3}", - 'owners': ["self"], - 'type':None, - 'cpu':'2', - 'ram':'2', - 'filter': [{'name': 'name', 'values':["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]}, - {'name': 'virtualization-type', 'values': ["hvm"]}] - } - if 'vm' in parameter: - my_dict['vm'] = parameter['vm'] - if 'id' in parameter: - my_dict['id'] = parameter['id'] - if 'name' in parameter: - my_dict['name'] = parameter['name'] - if 'os' in parameter: - my_dict['name_regex'] = parameter['os']+"*" - if 'executable_users'in parameter: - my_dict['executable_users'] = parameter['executable_users'] - if 'mostrecent' in parameter: - my_dict['mostrecent'] = parameter['mostrecent'] - if 'name_regex' in parameter: - my_dict['name_regex'] = parameter['name_regex'] - if 'owners' in parameter: - my_dict['owners'] = parameter['owners'] - if 'filter' in parameter: - my_dict['filter'] = parametri['filter'] - if 'cpu' in parameter: - my_dict['cpu'] = parameter['cpu'] - if 'ram' in parameter: - my_dict['ram'] = parameter['ram'] - if 'type' in parameter: - my_dict['type'] = parameter['type'] - my_dict['instance_type'] = vmcatalog1(my_dict['cpu'], my_dict['ram'], my_dict['sigla']) - if my_dict['type'] is None: - my_dict['instance_type'] = vmcatalog(my_dict['cpu'], my_dict['ram']) - if 'instance_type' in parameter: - my_dict['instance_type'] = parameter['instance_type'] - tm = Template("filter {\n name = \"{{ name }}\"\n values = {{ values }}\n }") - string = '' - render = jinjaTemplate.render(my_dict, filters=string) - render = re.sub("'", "\"", render) - finalString = finalString+render+"\n" - - create_file = open("Output-code/outputvm.tf", "w") - create_file.write(finalString) - create_file.close() - -def gcpvm(parametri): - finalString = "" - jinjaTemplate = Template(open("VM-templates/GCPtemplateVM.tpl", "r").read()) - for parameter in parametri: - my_dict = {'default': 'default', 'name': 'test', 'machine_type': 'e2-medium', 'zone': 'us-central1-a'} - if 'default' in parameter: - my_dict['default'] = parameter['default'] - if 'name' in parameter: - my_dict['name'] = parameter['name'] - if 'machine_type' in parameter: - my_dict['machine_type'] = parameter['machine_type'] - if 'zone' in parameter: - my_dict['zone'] = parameter['zone'] - render = jinjaTemplate.render(my_dict) - render = re.sub("'", "\"", render) - finalString = finalString+render+"\n" - - create_file = open("Output-code/outputvm.tf", "w") - create_file.write(finalString) - create_file.close() - -def azurevm(parametri): - finalString = "" - jinjaTemplate = Template(open("VM-templates/AZUREtemplateVM.tpl", "r").read()) - for parameter in parametri: - my_dict = {'source': "hashicorp/azurerm", 'version': "~>2.0", 'name': "<resource_group_name>", 'location': "<location>"} - if 'source' in parameter: - my_dict['source'] = parameter['source'] - if 'version' in parameter: - my_dict['version'] = parameter['version'] - if 'name' in parameter: - my_dict['name'] = parameter['name'] - if 'location' in parameter: - my_dict['location'] = parameter['location'] - render = jinjaTemplate.render(my_dict) - render = re.sub("'", "\"", render) - finalString = finalString+render+"\n" - - create_file = open("Output-code/outputvm.tf", "w") - create_file.write(finalString) - create_file.close() - -def networkaws(parametri): - finalString = "" - jinjaTemplate = Template(open("Network-templates/AWStemplateNetwork.tpl", "r").read()) - for parameter in parametri: - my_dict = {'subname':'subname', 'vpcname': 'vpcname', 'subnet_cidrblock':'subnet_cidrblock', 'vpc_cidr': 'vpc_cidr'} - if 'subnet_cidrblock' in parameter: - my_dict['subnet_cidrblock'] = parameter['subnet_cidrblock'] - if 'vpc_cidr' in parameter: - my_dict['vpc_cidr'] = parameter['vpc_cidr'] - if 'subnetname' in parameter: - my_dict['subnetname'] = parameter['subnetname'] - if 'vpcname' in parameter: - my_dict['vpcname'] = parameter['vpcname'] - render = jinjaTemplate.render(my_dict) - render = re.sub("'", "\"", render) - finalString = finalString+render+"\n" - - create_file = open("Output-code/outputNetwork.tf", "w") - create_file.write(finalString) - create_file.close() - -def networkg(parametri): - finalString = "" - jinjaTemplate = Template(open("Network-templates/GCPtemplateNetwork.tpl", "r").read()) - for parameter in parametri: - my_dict = {'network':'terraform-network', 'subnetwork': 'terraform-subnetwork'} - if 'network' in parameter: - my_dict['network'] = parameter['network'] - if 'subnetwork' in parameter: - my_dict['subnetwork'] = parameter['subnetwork'] - render = jinjaTemplate.render(my_dict) - render = re.sub("'", "\"", render) - finalString = finalString+render+"\n" - - create_file = open("Output-code/outputNetwork.tf", "w") - create_file.write(finalString) - create_file.close() - -def networkaz(parametri): - finalString = "" - jinjaTemplate = Template(open("Network-templates/AZUREtemplateNetwork.tpl", "r").read()) - for parameter in parametri: - my_dict = {'name':'my-resources', 'subnet_names': ["subnet1", "subnet2", "subnet3"]} - if 'name' in parameter: - my_dict['name'] = parameter['name'] - if 'subnet_names' in parameter: - my_dict['subnet_names'] = parameter['subnet_names'] - tm = Template("\"{{ name }}\" : [\"Microsoft.Sql\"]") - string = '' - i = len(my_dict['subnet_names']) - for elem in my_dict['subnet_names']: - rend = tm.render(name = elem) - string = string+rend - i = i-1 - if i>0: - string = string+",\n " - render = jinjaTemplate.render(my_dict, endpoints=string) - render = re.sub("'", "\"", render) - finalString = finalString+render+"\n" - - create_file = open("Output-code/outputNetwork.tf", "w") - create_file.write(finalString) - create_file.close() - -def awsdb(parametri): - finalString = "" - jinjaTemplate = Template(open("DB-templates/AWStemplateDB.tpl", "r").read()) - for parameter in parametri: - my_dict = {'identifier':'education', 'instance':'db.t3.micro', 'storage':'5', 'engine':'postgres', 'version':'13.1', 'username': 'edu', 'password':'var.db_password', 'subnet': 'aws_db_subnet_group.education.name', 'security': '[aws_security_group.rds.id]', 'parameter': 'aws_db_parameter_group.education.name', 'accessible': 'true', 'skip': 'true'} - if 'identifier' in parameter: - my_dict['identifier'] = parameter['identifier'] - if 'instance' in parameter: - my_dict['instance'] = parameter['instance'] - if 'storage' in parameter: - my_dict['storage'] = parameter['storage'] - if 'engine' in parameter: - my_dict['engine'] = parameter['engine'] - if 'version' in parameter: - my_dict['version'] = parameter['version'] - if 'username' in parameter: - my_dict['username'] = parameter['username'] - if 'password' in parameter: - my_dict['password'] = parameter['password'] - if 'subnet' in parameter: - my_dict['subnet'] = parameter['subnet'] - if 'security' in parameter: - my_dict['security'] = parameter['security'] - if 'parameter' in parameter: - my_dict['parameter'] = parameter['parameter'] - if 'accessible' in parameter: - my_dict['accessible'] = parameter['accessible'] - if 'skip' in parameter: - my_dict['skip'] = parameter['skip'] - render = jinjaTemplate.render(my_dict) - render = re.sub("'", "\"", render) - finalString = finalString+render+"\n" - - create_file = open("Output-code/outputdb.tf", "w") - create_file.write(finalString) - create_file.close() - -def azuredb(parametri): - finalString = "" - jinjaTemplate = Template(open("DB-templates/AZUREtemplateDB.tpl", "r").read()) - for parameter in parametri: - my_dict = {'name':'sqldbtf01', 'group_name':'${azurerm_resource_group.test2.name}', 'location':'North Central US', 'server_name':'${azurerm_sql_server.test2.name}', 'state':'Enabled', 'email':'["dbgrl93@gmail.com"]', 'days':'30', 'access_key':'${azurerm_storage_account.test2sa.primary_access_key}', 'endpoint':'${azurerm_storage_account.test2sa.primary_blob_endpoint}', 'default':'Enabled'} - if 'name' in parameter: - my_dict['name'] = parameter['name'] - if 'group_name' in parameter: - my_dict['group_name'] = parameter['group_name'] - if 'location' in parameter: - my_dict['location'] = parameter['location'] - if 'engine' in parameter: - my_dict['server_name'] = parameter['server_name'] - if 'version' in parameter: - my_dict['server_name'] = parameter['server_name'] - if 'state' in parameter: - my_dict['state'] = parameter['state'] - if 'email' in parameter: - my_dict['email'] = parameter['email'] - if 'days' in parameter: - my_dict['days'] = parameter['days'] - if 'access_key' in parameter: - my_dict['access_key'] = parameter['access_key'] - if 'endpoint' in parameter: - my_dict['endpoint'] = parameter['endpoint'] - if 'default' in parameter: - my_dict['default'] = parameter['v'] - render = jinjaTemplate.render(my_dict) - render = re.sub("'", "\"", render) - finalString = finalString+render+"\n" - - create_file = open("Output-code/outputdb.tf", "w") - create_file.write(finalString) - create_file.close() - -def googlesql(parametri): - finalString = "" - jinjaTemplate = Template(open("DB-templates/GCPtemplateDB.tpl", "r").read()) - for parameter in parametri: - my_dict = {'name':'my-database', 'instance':'google_sql_database_instance.instance.name', 'instance_name':'my-database-instance','region':'us-central1', 'tier':'db-f1-micro', 'deletion_protection':'true'} - if 'name' in parameter: - my_dict['name'] = parameter['name'] - if 'instance' in parameter: - my_dict['instance'] = parameter['instance'] - if 'instance_name' in parameter: - my_dict['instance_name'] = parameter['instance_name'] - if 'region' in parameter: - my_dict['region'] = parameter['region'] - if 'tier' in parameter: - my_dict['tier'] = parameter['tier'] - if 'deletion_protection' in parameter: - my_dict['deletion_protection'] = parameter['deletion_protection'] - render = jinjaTemplate.render(my_dict) - render = re.sub("'", "\"", render) - finalString = finalString+render+"\n" - - create_file = open("Output-code/outputdb.tf", "w") - create_file.write(finalString) - create_file.close() - -def postgresql(parametri): - jinjaTemplate = Template(open("DB-templates/AWStemplateDB.tpl", "r").read()) - my_dict = {'name': 'my_db', 'owner': 'my_role', 'template': 'template0', 'lc_collate':'C', 'connection_limit':-1, 'allow_connections': 'true'} - if 'name' in parametri: - my_dict['name'] = parametri['name'] - if 'owner' in parametri: - my_dict['owner'] = parametri['owner'] - if 'template' in parametri: - my_dict['template'] = parametri['template'] - if 'lc_collate' in parametri: - my_dict['lc_collate'] = parametri['lc_collate'] - if 'connection_limit' in parametri: - my_dict['connection_limit'] = parametri['connection_limit'] - if 'allow_connections' in parametri: - my_dict['allow_connections'] = parametri['allow_connections'] - render = jinjaTemplate.render(my_dict) - render = re.sub("'", "\"", render) - create_file = open("Output-code/outputdb.tf", "w") - create_file.write(render) - create_file.close() -- GitLab