-
Notifications
You must be signed in to change notification settings - Fork 2
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
refactor: DBTP-1630 Terraform environment domain tests refactor #715
Open
DeveloperConnor
wants to merge
31
commits into
main
Choose a base branch
from
DBTP-1630-terraform-environment-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
fd35780
Add command level test for without the version flag
ksugden d47c6ad
Refactor parameter names and tidy test pydocs
ksugden c673198
Add test for warning on deprecated flag
ksugden c5239a0
Remove test covered in test_command_environment.py
ksugden 2bc06c6
DBTP-1630 remove --vpc flag depreaction warning and tests
DeveloperConnor 96c03db
first pass at moving files methods into FilesProvider
DeveloperConnor cf8adf6
add TODO
DeveloperConnor 46d6e19
separate manifest generation from file writing
ksugden 078b792
Move private methods inside the class
ksugden 05dbbd3
inline a bunch of stuff
ksugden 6c7ec8c
inline more stuff
ksugden 58695dd
inline it all!
ksugden 03fc0d3
Add PlatformTerraformManifestGenerator class
ksugden 456ae88
remove print
ksugden 543f1d7
add tests for generate_manifest
ksugden 2b188f9
use PlatformTerraformManifestGenerator in generate function
ksugden 11c8d38
Add file provider dependency for writing manifest file
ksugden 54fec1a
inline environment_config
ksugden ee4b436
Add further assertions on the expected manifest content
ksugden 53cbcb3
add get_enriched_config function
ksugden c924ec3
Add comments
ksugden 03e9603
move get_enriched_config into config provider
ksugden 9673547
fixes order of parameters
ksugden 95f93de
added assertions on write_manifest and echo_fn in terraform_generate
DeveloperConnor e03a3ce
Merge branch 'main' into DBTP-1630-terraform-environment-tests
DeveloperConnor 4000135
Add todo
ksugden bb17344
remove obsolete test
ksugden 42ff1b5
Adds exception for generate called on non existent env
ksugden e51456b
Add todo for redundant exception handling
ksugden 828ddaa
remove obsolete function
ksugden 4a7b6d2
Corrects invalid modules version format
ksugden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,72 @@ | ||
import click | ||
|
||
from dbt_platform_helper.constants import DEFAULT_TERRAFORM_PLATFORM_MODULES_VERSION | ||
from dbt_platform_helper.utils.files import mkfile | ||
from dbt_platform_helper.providers.files import FileProvider | ||
from dbt_platform_helper.utils.template import setup_templates | ||
|
||
|
||
def _generate_terraform_environment_manifests( | ||
application, env, env_config, cli_terraform_platform_modules_version | ||
): | ||
env_template = setup_templates().get_template("environments/main.tf") | ||
class PlatformTerraformManifestGenerator: | ||
def __init__(self, file_provider): | ||
self.file_provider = file_provider | ||
self.manifest_template = setup_templates().get_template("environments/main.tf") | ||
|
||
terraform_platform_modules_version = _determine_terraform_platform_modules_version( | ||
env_config, cli_terraform_platform_modules_version | ||
) | ||
|
||
contents = env_template.render( | ||
{ | ||
"application": application, | ||
"environment": env, | ||
"config": env_config, | ||
"terraform_platform_modules_version": terraform_platform_modules_version, | ||
} | ||
) | ||
|
||
click.echo(mkfile(".", f"terraform/environments/{env}/main.tf", contents, overwrite=True)) | ||
def generate_manifest( | ||
self, | ||
environment_name: str, | ||
application_name: str, | ||
environment_config: dict, | ||
terraform_platform_modules_version_override: str = None, | ||
): | ||
terraform_platform_modules_version = ( | ||
terraform_platform_modules_version_override | ||
or environment_config.get("versions", {}).get( | ||
"terraform-platform-modules", DEFAULT_TERRAFORM_PLATFORM_MODULES_VERSION | ||
) | ||
) | ||
|
||
return self.manifest_template.render( | ||
{ | ||
"application": application_name, | ||
"environment": environment_name, | ||
"config": environment_config, | ||
"terraform_platform_modules_version": terraform_platform_modules_version, | ||
} | ||
) | ||
|
||
def _determine_terraform_platform_modules_version(env_conf, cli_terraform_platform_modules_version): | ||
cli_terraform_platform_modules_version = cli_terraform_platform_modules_version | ||
env_conf_terraform_platform_modules_version = env_conf.get("versions", {}).get( | ||
"terraform-platform-modules" | ||
) | ||
version_preference_order = [ | ||
cli_terraform_platform_modules_version, | ||
env_conf_terraform_platform_modules_version, | ||
DEFAULT_TERRAFORM_PLATFORM_MODULES_VERSION, | ||
] | ||
return [version for version in version_preference_order if version][0] | ||
def write_manifest(self, environment_name: str, manifest_content: str): | ||
return self.file_provider.mkfile( | ||
".", | ||
f"terraform/environments/{environment_name}/main.tf", | ||
manifest_content, | ||
overwrite=True, | ||
) | ||
|
||
|
||
class TerraformEnvironment: | ||
def __init__(self, config_provider): | ||
def __init__( | ||
self, | ||
config_provider, | ||
manifest_generator: PlatformTerraformManifestGenerator = None, | ||
echo_fn=click.echo, | ||
): | ||
self.echo = echo_fn | ||
self.config_provider = config_provider | ||
self.manifest_generator = manifest_generator or PlatformTerraformManifestGenerator( | ||
FileProvider() | ||
) | ||
|
||
def generate(self, name, terraform_platform_modules_version): | ||
config = self.config_provider.load_and_validate_platform_config() | ||
enriched_config = self.config_provider.apply_environment_defaults(config) | ||
def generate(self, environment_name, terraform_platform_modules_version_override=None): | ||
config = self.config_provider.get_enriched_config() | ||
|
||
manifest = self.manifest_generator.generate_manifest( | ||
environment_name=environment_name, | ||
application_name=config["application"], | ||
environment_config=config["environments"][environment_name], | ||
terraform_platform_modules_version_override=terraform_platform_modules_version_override, | ||
) | ||
|
||
env_config = enriched_config["environments"][name] | ||
_generate_terraform_environment_manifests( | ||
config["application"], name, env_config, terraform_platform_modules_version | ||
self.echo( | ||
self.manifest_generator.write_manifest( | ||
environment_name=environment_name, manifest_content=manifest | ||
) | ||
) |
100 changes: 100 additions & 0 deletions
100
dbt_platform_helper/domain/test_platform_terraform_manifest_generator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from unittest.mock import Mock | ||
|
||
from dbt_platform_helper.constants import DEFAULT_TERRAFORM_PLATFORM_MODULES_VERSION | ||
from dbt_platform_helper.domain.terraform_environment import ( | ||
PlatformTerraformManifestGenerator, | ||
) | ||
from dbt_platform_helper.providers.files import FileProvider | ||
|
||
|
||
class TestPlatformTerraformManifestGenerator: | ||
|
||
def test_generator_generates_expected_manifest_content_with_version_override(self): | ||
test_environment_config = { | ||
"vpc": "vpc3", | ||
"accounts": { | ||
"deploy": {"name": "non-prod-acc", "id": "1122334455"}, | ||
"dns": {"name": "non-prod-dns-acc", "id": "6677889900"}, | ||
}, | ||
"versions": {"terraform-platform-modules": 3}, | ||
} | ||
expected_header = "# WARNING: This is an autogenerated file, not for manual editing." | ||
expected_modules = "git::https://github.com/uktrade/terraform-platform-modules.git//extensions?depth=1&ref=123456" | ||
expected_moved_block = ( | ||
"moved {\n from = module.extensions-tf\n to = module.extensions\n}\n" | ||
) | ||
|
||
result = PlatformTerraformManifestGenerator(Mock()).generate_manifest( | ||
"test", "test-app", test_environment_config, 123456 | ||
) | ||
|
||
assert expected_header in result | ||
assert expected_modules in result | ||
assert expected_moved_block in result | ||
assert 'environment = "test"' in result | ||
assert 'application = "test-app"' in result | ||
assert 'vpc_name = "vpc3"' | ||
|
||
def test_generator_generates_expected_manifest_content_with_tpm_version_set_in_config(self): | ||
test_environment_config = { | ||
"vpc": "vpc3", | ||
"accounts": { | ||
"deploy": {"name": "non-prod-acc", "id": "1122334455"}, | ||
"dns": {"name": "non-prod-dns-acc", "id": "6677889900"}, | ||
}, | ||
"versions": {"terraform-platform-modules": 3}, | ||
} | ||
expected_header = "# WARNING: This is an autogenerated file, not for manual editing." | ||
expected_modules = "git::https://github.com/uktrade/terraform-platform-modules.git//extensions?depth=1&ref=3" | ||
expected_moved_block = ( | ||
"moved {\n from = module.extensions-tf\n to = module.extensions\n}\n" | ||
) | ||
|
||
result = PlatformTerraformManifestGenerator(Mock()).generate_manifest( | ||
"test", "test-app", test_environment_config | ||
) | ||
|
||
assert expected_header in result | ||
assert expected_modules in result | ||
assert expected_moved_block in result | ||
assert 'environment = "test"' in result | ||
assert 'application = "test-app"' in result | ||
assert 'vpc_name = "vpc3"' | ||
|
||
def test_generator_generates_expected_manifest_content_with_default_version(self): | ||
test_environment_config = { | ||
"vpc": "vpc3", | ||
"accounts": { | ||
"deploy": {"name": "non-prod-acc", "id": "1122334455"}, | ||
"dns": {"name": "non-prod-dns-acc", "id": "6677889900"}, | ||
}, | ||
} | ||
expected_header = "# WARNING: This is an autogenerated file, not for manual editing." | ||
expected_modules = f"git::https://github.com/uktrade/terraform-platform-modules.git//extensions?depth=1&ref={DEFAULT_TERRAFORM_PLATFORM_MODULES_VERSION}" | ||
expected_moved_block = ( | ||
"moved {\n from = module.extensions-tf\n to = module.extensions\n}\n" | ||
) | ||
|
||
result = PlatformTerraformManifestGenerator(Mock()).generate_manifest( | ||
"test", "test-app", test_environment_config | ||
) | ||
|
||
assert expected_header in result | ||
assert expected_modules in result | ||
assert expected_moved_block in result | ||
assert 'environment = "test"' in result | ||
assert 'application = "test-app"' in result | ||
assert 'vpc_name = "vpc3"' | ||
|
||
def test_generator_write_manifest_makes_the_expected_manifest_file(self): | ||
mock_file_provider = Mock(spec=FileProvider) | ||
PlatformTerraformManifestGenerator(mock_file_provider).write_manifest( | ||
"test-environment", "test-manifest-content" | ||
) | ||
|
||
mock_file_provider.mkfile.assert_called_once_with( | ||
".", | ||
f"terraform/environments/test-environment/main.tf", | ||
"test-manifest-content", | ||
overwrite=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the deprecation warning when providing a vpc to generate-terraform - its been here for 7months now, seems like no one should be impacted /shrug