Skip to content

Commit

Permalink
Merge pull request #296 from DontShaveTheYak/develop
Browse files Browse the repository at this point in the history
Release v0.8.0
  • Loading branch information
shadycuz committed May 2, 2024
2 parents 74c8b67 + b5d3cd6 commit 1db4bcd
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 149 deletions.
14 changes: 5 additions & 9 deletions .devcontainer/Dockerfile
@@ -1,18 +1,14 @@
FROM ubuntu:22.04
FROM ubuntu:24.04

ARG USERNAME=vscode
ARG USERNAME=ubuntu
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Ensure apt is in non-interactive to avoid prompts
ENV DEBIAN_FRONTEND=noninteractive

# Create the user
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
#
# [Optional] Add sudo support. Omit if you don't need to install software after connecting.
&& apt-get update \
RUN apt-get update \
&& apt-get install -y sudo \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME
Expand Down Expand Up @@ -43,12 +39,12 @@ SHELL ["/bin/bash","--login", "-c"]

# Stuff needed to make pyenv work correctly since the login shell
# above doesnt seem to run bachrc
ENV PYENV_ROOT="/home/vscode/.pyenv"
ENV PYENV_ROOT="/home/ubuntu/.pyenv"
ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH


# Install the required python versions (this takes a bit usually) so we do lots of cacheing
RUN --mount=type=cache,target=/home/vscode/.pyenv/cache,uid=1000 \
RUN --mount=type=cache,target=/home/ubuntu/.pyenv/cache,uid=1000 \
pyenv install -s 3.8.18 && \
pyenv install -s 3.9.18 && \
pyenv install -s 3.10.13 && \
Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Expand Up @@ -39,5 +39,5 @@
"postCreateCommand": "poetry install && pre-commit install",
// "postStartCommand": "",
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
"remoteUser": "ubuntu"
}
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
steps:

- name: Checkout Code
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.1.4
with:
fetch-depth: 0

Expand Down Expand Up @@ -75,7 +75,7 @@ jobs:
needs: tag
steps:
- name: Checkout Code
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.1.4

- name: Setup Python 3.11
uses: actions/setup-python@v5.1.0
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Python 3.11
steps:
- name: Checkout Code
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.1.4

- name: Setup Latest Python
uses: actions/setup-python@v5.1.0
Expand All @@ -39,7 +39,7 @@ jobs:
run: coverage xml --fail-under=0

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4.3.0
uses: codecov/codecov-action@v4.3.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
flags: unit
Expand All @@ -54,7 +54,7 @@ jobs:
name: Python ${{ matrix.python-version }}
steps:
- name: Checkout Code
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.1.4

- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v5.1.0
Expand Down
224 changes: 112 additions & 112 deletions poetry.lock

Large diffs are not rendered by default.

31 changes: 25 additions & 6 deletions src/cf2tf/conversion/overrides.py
@@ -1,8 +1,8 @@
from typing import TYPE_CHECKING, Callable, Dict

from cf2tf.terraform.hcl2.complex import ListType, MapType
from cf2tf.terraform.hcl2.custom import LiteralType
from cf2tf.terraform.hcl2.primitive import NullType, StringType, TerraformType
from cf2tf.terraform.hcl2.complex import ListType, MapType

if TYPE_CHECKING:
from cf2tf.convert import TemplateConverter
Expand Down Expand Up @@ -50,14 +50,33 @@ def tag_conversion(_tc: "TemplateConverter", params: CFParams) -> CFParams:
if isinstance(params["Tags"], dict):
return params

orginal_tags: ListType = params["Tags"] # type: ignore
original_tags: ListType = params["Tags"] # type: ignore

new_tags = {LiteralType(tag["Key"]): tag["Value"] for tag in orginal_tags}
first_item = original_tags[0]

del params["Tags"]
params["tags"] = MapType(new_tags)
# It's possible that the tags might be one or more
# conditional statements of LiteralType
# This wont fix every case, but it should fix most
# and it's better than nothing
if not isinstance(first_item, (dict, MapType)):
del params["Tags"]
params["tags"] = first_item
return params

return params
try:
new_tags = {LiteralType(tag["Key"]): tag["Value"] for tag in original_tags}

del params["Tags"]
params["tags"] = MapType(new_tags)
return params
except Exception:
del params["Tags"]
params["tags"] = LiteralType(
f"// Could not convert tags: {original_tags.render(4)}"
)
return params

raise Exception("Could not convert tags")


OVERRIDE_DISPATCH: ResourceOverride = {
Expand Down
4 changes: 2 additions & 2 deletions src/cf2tf/convert.py
Expand Up @@ -622,7 +622,7 @@ def add_space():
def perform_resource_overrides(
tf_type: str, params: Dict[str, TerraformType], tc: TemplateConverter
):
log.debug("Overiding params for {tf_type}")
log.debug(f"Overiding params for {tf_type}")
if tf_type not in OVERRIDE_DISPATCH:
return params

Expand All @@ -638,7 +638,7 @@ def perform_resource_overrides(
def perform_global_overrides(
tf_type: str, params: Dict[str, TerraformType], tc: TemplateConverter
):
log.debug("Performing global overrides for {tf_type}")
log.debug(f"Performing global overrides for {tf_type}")

for param, override in GLOBAL_OVERRIDES.items():
if param in params:
Expand Down
16 changes: 9 additions & 7 deletions src/cf2tf/terraform/code.py
@@ -1,17 +1,17 @@
import logging
import re
from pathlib import Path
from shutil import rmtree
from tempfile import gettempdir
from typing import Optional
from shutil import rmtree

import click
from click._termui_impl import ProgressBar
from git import RemoteProgress
from git.repo.base import Repo, InvalidGitRepositoryError
from git.repo.base import InvalidGitRepositoryError, Repo
from thefuzz import fuzz, process # type: ignore

import cf2tf.convert
# import cf2tf.convert

log = logging.getLogger("cf2tf")

Expand All @@ -35,7 +35,7 @@ def find(self, resource_type: str) -> Path:
ranking: int
doc_path: Path
resource_name, ranking, doc_path = process.extractOne(
name.lower(), files, scorer=fuzz.token_sort_ratio
name.lower(), files, scorer=fuzz.ratio
)

log.debug(
Expand Down Expand Up @@ -109,9 +109,11 @@ def resource_type_to_name(resource_type: str) -> str:

search_tokens = resource_type.replace("::", " ").replace("AWS", " ").split(" ")

for i, token in enumerate(search_tokens):
if len(token) >= 4:
search_tokens[i] = cf2tf.convert.camel_case_split(token)
# I will leave the logic for camel case splitting here for now.
# in case we want to use it later.
# for i, token in enumerate(search_tokens):
# if len(token) >= 4:
# search_tokens[i] = cf2tf.convert.camel_case_split(token)

search_term = " ".join(search_tokens).lower().strip()

Expand Down
7 changes: 3 additions & 4 deletions src/cf2tf/terraform/doc_file.py
@@ -1,9 +1,8 @@
import logging
import re
from io import TextIOWrapper
from pathlib import Path
from typing import List
import re

import logging

log = logging.getLogger("cf2tf")

Expand Down Expand Up @@ -89,7 +88,7 @@ def parse_items(file: TextIOWrapper):
continue

# These should be the attributes we are after
if line[0] == "*":
if line[0] == "*" or line[0] == "-":
regex = r"`([\w.*]+)`"

match = re.search(regex, line)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_terraform/test_code.py
Expand Up @@ -5,8 +5,8 @@
from cf2tf.terraform.code import (
SearchManager,
resource_type_to_name,
transform_file_name,
search_manager,
transform_file_name,
)


Expand All @@ -31,6 +31,7 @@ def test_sm_(mock_sm: SearchManager):
("AWS::Lambda::Function", "lambda_function.html.markdown"),
("AWS::Events::Rule", "cloudwatch_event_rule.html.markdown"),
("AWS::Cognito::UserPool", "cognito_user_pool.html.markdown"),
("AWS::AutoScaling::AutoScalingGroup", "autoscaling_group.html.markdown"),
]


Expand All @@ -51,8 +52,8 @@ def test_transform_file_name():

type_to_name_tests = [
("AWS::Ec2::Instance", "ec2 instance"),
("AWS::EC2::RouteTable", "ec2 route table"),
("AWS::RDS::DBSubnetGroup", "rds db subnet group"),
("AWS::EC2::RouteTable", "ec2 routetable"),
("AWS::RDS::DBSubnetGroup", "rds dbsubnetgroup"),
("AWS::S3::Bucket", "s3 bucket"),
]

Expand Down

0 comments on commit 1db4bcd

Please sign in to comment.