Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new s3 backend endpoints option #44

Merged
merged 18 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ on:
name: Build and Test
jobs:
build_test:
strategy:
matrix:
tf-version: ["1.5.7", "latest"]
timeout-minutes: 30
runs-on: ubuntu-latest
env:
AWS_DEFAULT_REGION: us-east-1
DNS_ADDRESS: 127.0.0.1

steps:
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ matrix.tf-version}}
- name: Check out code
uses: actions/checkout@v3
- name: Pull LocalStack Docker image
Expand Down
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Custom
.envrc
.env
tmp/

.DS_Store
*.egg-info/
Expand Down Expand Up @@ -101,12 +102,7 @@ fabric.properties
!.idea/runConfigurations

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
.vscode/

# Local History for Visual Studio Code
.history/
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ publish: ## Publish the library to the central PyPi repository

clean: ## Clean up
rm -rf $(VENV_DIR)
rm -rf dist/*
rm -rf dist
rm -rf *.egg-info

.PHONY: clean publish install usage lint test
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ please refer to the man pages of `terraform --help`.

## Change Log

* v0.17.0: Add option to use new endpoints S3 backend options
* v0.16.1: Update Setuptools to exclude tests during packaging
* v0.16.0: Introducing semantic versioning and AWS_ENDPOINT_URL variable
* v0.15: Update endpoint overrides for Terraform AWS provider 5.22.0
Expand Down
54 changes: 42 additions & 12 deletions bin/tflocal
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import os
import sys
import glob
import subprocess
import json

from packaging import version
from urllib.parse import urlparse

PARENT_FOLDER = os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))
Expand All @@ -34,14 +36,15 @@ TF_CMD = os.environ.get("TF_CMD") or "terraform"
LS_PROVIDERS_FILE = os.environ.get("LS_PROVIDERS_FILE") or "localstack_providers_override.tf"
LOCALSTACK_HOSTNAME = urlparse(AWS_ENDPOINT_URL).hostname or os.environ.get("LOCALSTACK_HOSTNAME") or "localhost"
EDGE_PORT = int(urlparse(AWS_ENDPOINT_URL).port or os.environ.get("EDGE_PORT") or 4566)
TF_VERSION = None
lakkeger marked this conversation as resolved.
Show resolved Hide resolved
TF_PROVIDER_CONFIG = """
provider "aws" {
access_key = "<access_key>"
secret_key = "test"
skip_credentials_validation = true
skip_metadata_api_check = true
<configs>
endpoints {
endpoints {
<endpoints>
}
}
Expand All @@ -56,10 +59,7 @@ terraform {

access_key = "test"
secret_key = "test"
endpoint = "<s3_endpoint>"
iam_endpoint = "<iam_endpoint>"
sts_endpoint = "<sts_endpoint>"
dynamodb_endpoint = "<dynamodb_endpoint>"
<endpoints>
skip_credentials_validation = true
skip_metadata_api_check = true
}
Expand Down Expand Up @@ -126,7 +126,7 @@ def create_provider_config_file(provider_aliases=None):
"<access_key>",
get_access_key(provider) if CUSTOMIZE_ACCESS_KEY else DEFAULT_ACCESS_KEY
)
endpoints = "\n".join([f'{s} = "{get_service_endpoint(s)}"' for s in services])
endpoints = "\n".join([f' {s} = "{get_service_endpoint(s)}"' for s in services])
provider_config = provider_config.replace("<endpoints>", endpoints)
additional_configs = []
if use_s3_path_style():
Expand All @@ -139,7 +139,7 @@ def create_provider_config_file(provider_aliases=None):
region = provider.get("region") or get_region()
if isinstance(region, list):
region = region[0]
additional_configs += [f' region = "{region}"']
additional_configs += [f'region = "{region}"']
provider_config = provider_config.replace("<configs>", "\n".join(additional_configs))
provider_configs.append(provider_config)

Expand Down Expand Up @@ -203,17 +203,33 @@ def generate_s3_backend_config() -> str:
"key": "terraform.tfstate",
"dynamodb_table": "tf-test-state",
"region": get_region(),
"s3_endpoint": get_service_endpoint("s3"),
"iam_endpoint": get_service_endpoint("iam"),
"sts_endpoint": get_service_endpoint("sts"),
"dynamodb_endpoint": get_service_endpoint("dynamodb"),
"endpoints": {
"s3": get_service_endpoint("s3"),
"iam": get_service_endpoint("iam"),
"sso": get_service_endpoint("sso"),
"sts": get_service_endpoint("sts"),
"dynamodb": get_service_endpoint("dynamodb"),
},
}
configs.update(backend_config)
get_or_create_bucket(configs["bucket"])
get_or_create_ddb_table(configs["dynamodb_table"], region=configs["region"])
result = TF_S3_BACKEND_CONFIG
for key, value in configs.items():
value = str(value).lower() if isinstance(value, bool) else str(value)
if isinstance(value, bool):
value = str(value).lower()
elif isinstance(value, dict):
if key == "endpoints":
lakkeger marked this conversation as resolved.
Show resolved Hide resolved
if TF_VERSION.major > 1 or (TF_VERSION.major == 1 and TF_VERSION.minor > 5):
value = f"{key} = {{\n" + "\n".join([f' {k} = "{v}"' for k, v in value.items()]) + "\n }"
else:
value = f"""endpoint = "{value["s3"]}"
lakkeger marked this conversation as resolved.
Show resolved Hide resolved
iam_endpoint = "{value["iam"]}"
sts_endpoint = "{value["sts"]}"
dynamodb_endpoint = "{value["dynamodb"]}"
"""
lakkeger marked this conversation as resolved.
Show resolved Hide resolved
else:
value = str(value)
result = result.replace(f"<{key}>", value)
return result

Expand Down Expand Up @@ -347,6 +363,12 @@ def parse_tf_files() -> dict:
return result


def get_tf_version(env):
global TF_VERSION
output = subprocess.run(["terraform", "version", "-json"], env=env, check=True, capture_output=True).stdout.decode("utf-8")
lakkeger marked this conversation as resolved.
Show resolved Hide resolved
lakkeger marked this conversation as resolved.
Show resolved Hide resolved
TF_VERSION = version.parse(json.loads(output)["terraform_version"])


def run_tf_exec(cmd, env):
"""Run terraform using os.exec - can be useful as it does not require any I/O
handling for stdin/out/err. Does *not* allow us to perform any cleanup logic."""
Expand Down Expand Up @@ -395,6 +417,14 @@ def main():
env = dict(os.environ)
cmd = [TF_CMD] + sys.argv[1:]

try:
get_tf_version(env)
if not TF_VERSION:
raise ValueError
except (FileNotFoundError, ValueError):
print("Terraform not found.")
lakkeger marked this conversation as resolved.
Show resolved Hide resolved
exit(1)

# create TF provider config file
providers = determine_provider_aliases()
config_file = create_provider_config_file(providers)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = terraform-local
version = 0.16.1
version = 0.17.0
url = https://github.com/localstack/terraform-local
author = LocalStack Team
author_email = [email protected]
Expand Down
Loading