Skip to content

Commit

Permalink
Added random names, set all values to sane defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbabenko committed Nov 16, 2018
1 parent c48d268 commit 381a560
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
65 changes: 57 additions & 8 deletions modulestf/convert.py
@@ -1,7 +1,13 @@
import json
import logging
import random
import string
import uuid
from hashlib import md5
from pprint import pformat, pprint

import petname


# Class which represents modules.tf definition of resource
class Resource:
Expand Down Expand Up @@ -33,6 +39,17 @@ def content(self):
}


# Generate human readable random names
def random_pet(words=2, separator="-"):
return petname.generate(words, separator)


def random_password(length=12):
chars = string.ascii_uppercase + string.ascii_lowercase + string.digits

return ''.join(random.choice(chars) for _ in range(length))


def get_node(G, node_id):
try:
return G.nodes.get(node_id)
Expand Down Expand Up @@ -113,6 +130,13 @@ def convert_graph_to_modulestf_config(graph): # noqa: C901
r.update_params({
"isMultiAZ": is_multi_az,
"db_subnet_group_name": "",
"identifier": random_pet(),
"username": random_pet(words=1),
"password": random_password(),
"allocated_storage": "5",
"major_engine_version": "",
"family": "",
"backup_retention_period": "0", # Disable backups to create DB faster
"vpc_security_group_ids": [],
})

Expand All @@ -123,16 +147,20 @@ def convert_graph_to_modulestf_config(graph): # noqa: C901

if sg_id:
r.append_dependency(sg_id)
r.update_dynamic_params("vpc_security_group_ids", "terraform_output." + sg_id + ".this_security_group_id")
r.update_dynamic_params("vpc_security_group_ids", "terraform_output." + sg_id + ".this_security_group_id.to_list")

if node.get("engine"):
r.update_params({
"engine": node.get("engine"),
"engine": node.get("engine"), # node.get("engine") has too many options (not just supported)
"port": "3306",
"engine_version": "5.7.19",
"major_engine_version": "5.7",
"family": "mysql5.7",
})

if node.get("instanceType") and node.get("instanceSize"):
r.update_params({
"instanceType": node.get("instanceType", "") + "." + node.get("instanceSize", ""),
"instanceType": "db." + node.get("instanceType", "") + "." + node.get("instanceSize", ""),
})

resources.append(r.content())
Expand All @@ -154,10 +182,12 @@ def convert_graph_to_modulestf_config(graph): # noqa: C901
r = Resource(asg_id, "autoscaling", node.get("text"))

r.update_params({
"name": random_pet(),
"min_size": 0,
"max_size": 0,
"desired_capacity": 0,
"image_id": "",
"health_check_type": "EC2",
"image_id": "ami-00035f41c82244dab",
"target_group_arns": [],
"vpc_zone_identifier": [],
"security_groups": [],
Expand All @@ -169,7 +199,7 @@ def convert_graph_to_modulestf_config(graph): # noqa: C901

if sg_id:
r.append_dependency(sg_id)
r.update_dynamic_params("security_groups", "terraform_output." + sg_id + ".this_security_group_id")
r.update_dynamic_params("security_groups", "terraform_output." + sg_id + ".this_security_group_id.to_list")

if elb_id:
r.append_dependency(elb_id)
Expand All @@ -187,6 +217,10 @@ def convert_graph_to_modulestf_config(graph): # noqa: C901
else:
r = Resource(key, "ec2-instance", node.get("text"))

r.update_params({
"name": random_pet(),
})

if node.get("instanceType") and node.get("instanceSize"):
r.update_params({
"instanceType": node.get("instanceType", "") + "." + node.get("instanceSize", ""),
Expand All @@ -203,17 +237,27 @@ def convert_graph_to_modulestf_config(graph): # noqa: C901
if node.get("elbType") == "application":
r = Resource(key, "alb", node.get("text"))

r.update_params({
"load_balancer_name": random_pet(),
"logging_enabled": "false",
})

if vpc_id:
r.append_dependency(vpc_id)
r.update_dynamic_params("subnets", "terraform_output." + vpc_id + ".public_subnets")
r.update_dynamic_params("vpc_id", "terraform_output." + vpc_id + ".vpc_id")

if sg_id:
r.append_dependency(sg_id)
r.update_dynamic_params("security_groups", "terraform_output." + sg_id + ".this_security_group_id")
r.update_dynamic_params("security_groups", "terraform_output." + sg_id + ".this_security_group_id.to_list")

else:
r = Resource(key, "elb", node.get("text"))

r.update_params({
"name": random_pet(),
})

# @todo: Use https://github.com/virtuald/pyhcl to convert to valid HCL in tfvars
# r.update_params({
# "listener": [{
Expand All @@ -237,7 +281,7 @@ def convert_graph_to_modulestf_config(graph): # noqa: C901

if sg_id:
r.append_dependency(sg_id)
r.update_dynamic_params("security_groups", "terraform_output." + sg_id + ".this_security_group_id")
r.update_dynamic_params("security_groups", "terraform_output." + sg_id + ".this_security_group_id.to_list")

resources.append(r.content())

Expand Down Expand Up @@ -268,6 +312,10 @@ def convert_graph_to_modulestf_config(graph): # noqa: C901
if node.get("type") == "sg":
r = Resource(key, "security-group", node.get("text"))

r.update_params({
"name": random_pet(),
})

if vpc_id:
r.append_dependency(vpc_id)
r.update_dynamic_params("vpc_id", "terraform_output." + vpc_id + ".vpc_id")
Expand All @@ -278,11 +326,12 @@ def convert_graph_to_modulestf_config(graph): # noqa: C901
r = Resource(key, "vpc", node.get("text"))

r.update_params({
"name": "",
"name": random_pet(),
"cidr": "",
"azs": [],
"public_subnets": [],
"private_subnets": [],
"database_subnets": [],
})

resources.append(r.content())
Expand Down
2 changes: 1 addition & 1 deletion modulestf/render.py
Expand Up @@ -192,7 +192,7 @@ def render_from_modulestf_config(config, source, regions):
v = dynamic_params[k].split(".")

# replace second element with real directory name
dynamic_params.update({k: v[0] + "." + dirs[v[1]] + "." + "".join(v[2:])})
dynamic_params.update({k: v[0] + "." + dirs[v[1]] + "." + ".".join(v[2:])})
except KeyError:
pass

Expand Down
1 change: 1 addition & 0 deletions requirements-lambda.txt
@@ -1,3 +1,4 @@
cookiecutter==1.6.0
networkx==2.1
petname==2.2
requests>=2.19.1
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -4,5 +4,6 @@ botocore==1.10.3
cookiecutter==1.6.0
matplotlib==2.2.2
networkx==2.1
petname==2.2
python-lambda-local==0.1.5
requests>=2.19.1
Expand Up @@ -7,9 +7,8 @@
# 2. get list of things to replace
# 3. go through the list of things to replace
# 4. get actual value
# 5. replace value in terraform.tfvars

# @todo: add support for lists
# 5. replace value in terraform.tfvars:
# a. When `to_list` is specified the type of the value will be converted to a list - ["sg-1234"]
############################

readonly tfvars_file="$1/terraform.tfvars"
Expand All @@ -25,11 +24,15 @@ if $(grep -q "$modulestf_disable_values_updates_flag" "$tfvars_file"); then
exit 0
fi

# Sample keys:
# @modulestf:terraform_output.security-group_5.this_security_group_id - the type of the value will not be modified
# @modulestf:terraform_output.security-group_5.this_security_group_id.to_list - the type of the value will be converted to list
keys_to_replace=($(grep -oh "$modulestf_terraform_output_prefix\.[^ ]*" "$tfvars_file" | sort | uniq))

for key_to_replace in "${keys_to_replace[@]}"; do
dir_name=$(cut -d "." -f 2 <<< "$key_to_replace")
output_name=$(cut -d "." -f 3 <<< "$key_to_replace")
convert_to_type=$(cut -d "." -f 4 <<< "$key_to_replace")

cd "${parent_dir}/${dir_name}"

Expand All @@ -42,7 +45,15 @@ for key_to_replace in "${keys_to_replace[@]}"; do
item_value="\"$item_value\""
fi

echo "Updating value of $key_to_replace with $item_value"
if [[ "$convert_to_type" == "to_list" ]]; then
item_value="[$item_value]"
fi

echo "Updating value of $key_to_replace with $item_value" in "$tfvars_file"

set -x # print command: on

sed -i -r "s|^(.+) =.+(\#)+(.*)${key_to_replace}(.*)|\1 = ${item_value} \2\3${key_to_replace}\4|g" "$tfvars_file"

sed -i -r "s/^(.+) =.+(\#)+(.*)${key_to_replace}(.*)/\1 = ${item_value} \2\3${key_to_replace}\4/g" "$tfvars_file"
set +x # print command: off
done

0 comments on commit 381a560

Please sign in to comment.