-
Notifications
You must be signed in to change notification settings - Fork 3
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
Create GVC and identity terraform configs from templates #230
Create GVC and identity terraform configs from templates #230
Conversation
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe changes introduce enhancements to the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Command
participant Generator
participant Gvc
participant Identity
User->>Command: cpflow terraform generate -a $APP_NAME
Command->>Generator: Initialize with templates
Generator->>Gvc: Generate GVC configuration
Gvc-->>Generator: Return GVC configuration
Generator->>Identity: Generate Identity configuration
Identity-->>Generator: Return Identity configuration
Generator-->>Command: Return generated files
Command-->>User: Output generated files
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
66e4177
to
32e8790
Compare
@coderabbitai review |
Actions performedReview triggered.
|
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.
Actionable comments posted: 0
Outside diff range, codebase verification and nitpick comments (1)
lib/core/terraform_config/gvc.rb (1)
7-29
: LGTM! Consider using keyword arguments to improve readability.The code changes are approved. However, consider using keyword arguments to improve readability and maintainability. For example:
def initialize(name:, description: nil, tags: nil, domain: nil, locations: nil, pull_secrets: nil, env: nil, load_balancer: nil) super() @name = name @description = description @tags = tags @domain = domain @locations = locations @pull_secrets = pull_secrets @env = env @load_balancer = load_balancer&.transform_keys { |k| k.to_s.underscore.to_sym } end
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (12)
- docs/commands.md (1 hunks)
- lib/command/terraform/generate.rb (2 hunks)
- lib/core/terraform_config/dsl.rb (3 hunks)
- lib/core/terraform_config/generator.rb (1 hunks)
- lib/core/terraform_config/gvc.rb (1 hunks)
- lib/core/terraform_config/identity.rb (1 hunks)
- lib/cpflow.rb (1 hunks)
- lib/patches/string.rb (1 hunks)
- spec/command/terraform/generate_spec.rb (2 hunks)
- spec/core/terraform_config/generator_spec.rb (1 hunks)
- spec/core/terraform_config/gvc_spec.rb (1 hunks)
- spec/core/terraform_config/identity_spec.rb (1 hunks)
Additional comments not posted (23)
lib/core/terraform_config/identity.rb (1)
1-27
: LGTM!The code changes are approved. The new
Identity
class is well-structured and correctly generates the Terraform resource block forcpln_identity
.lib/patches/string.rb (1)
21-23
: LGTM!The code changes are approved. The new
underscore
method is a useful utility for string manipulation and the regular expression correctly converts the string to underscore case.spec/core/terraform_config/identity_spec.rb (1)
1-36
: LGTM!The code changes are approved. The new spec for the
Identity
class is well-written and covers the important aspects of the class. The use of a heredoc makes the spec readable and easy to understand. The spec passes and ensures that theto_tf
method generates the correct Terraform config.spec/command/terraform/generate_spec.rb (2)
12-12
: LGTM!The change enhances the test setup by providing a test app for each test case.
25-32
: LGTM!The changes enhance the test coverage by checking for multiple config files and improve the robustness of the test by using
:aggregate_failures
.lib/command/terraform/generate.rb (3)
20-27
: LGTM!The changes enhance the command's capability to generate terraform configs dynamically based on the provided templates. The conditional check adds a layer of validation to the template handling.
36-39
: LGTM!The method introduces a new dependency on the
TemplateParser
class to parse YAML template files from a specified directory.
43-43
: LGTM!The change ensures a clean state for the generated files by removing any existing directory before creating a new one.
spec/core/terraform_config/gvc_spec.rb (1)
1-52
: LGTM!The test case covers the required and optional arguments of the
TerraformConfig::Gvc
class and uses a heredoc to compare the generated config with the expected config.lib/core/terraform_config/gvc.rb (2)
31-44
: LGTM!The code changes are approved.
48-55
: LGTM!The code changes are approved.
lib/core/terraform_config/dsl.rb (3)
7-7
: LGTM!The code changes are approved.
49-49
: LGTM!The code changes are approved.
67-67
: LGTM!The code changes are approved.
lib/core/terraform_config/generator.rb (5)
7-10
: LGTM!The code changes are approved.
12-21
: LGTM!The code changes are approved.
23-32
: LGTM!The code changes are approved.
37-56
: LGTM!The code changes are approved.
58-65
: LGTM! But verify that theconfig.app
matches the GVC name.The code changes are approved. However, ensure that the
config.app
matches the GVC name.Run the following script to verify that the
config.app
matches the GVC name:Verification successful
Verification Successful:
config.app
correctly matches the GVC name.The
cpln_gvc
resource is defined and used in the codebase, and the use ofconfig.app
to construct the GVC name ingenerator.rb
is correct. The code changes are consistent with the intended functionality.
lib/core/terraform_config/generator.rb
:gvc: "cpln_gvc.#{config.app}.name"
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the `config.app` matches the GVC name. # Test: Search for the GVC name in the `gvc.tf` file. Expect: The GVC name matches the `config.app`. rg --type terraform $'resource "cpln_gvc" "' | rg --only-matching $'"[^"]*"'Length of output: 110
Script:
#!/bin/bash # Description: Locate Terraform files and verify that the `config.app` matches the GVC name. # Find all Terraform files and search for the `cpln_gvc` resource. fd --extension tf --exec rg 'resource "cpln_gvc" "'Length of output: 53
Script:
#!/bin/bash # Description: Search for any occurrences of `cpln_gvc` in the entire repository. # Search for `cpln_gvc` in all files to verify its existence. rg 'cpln_gvc'Length of output: 459
spec/core/terraform_config/generator_spec.rb (2)
10-67
: LGTM!The test for the "gvc" template looks comprehensive and is testing the generator thoroughly. It is checking the generated Terraform config object's attributes and the generated filename. No issues found.
70-91
: LGTM!The test for the "identity" template looks good and is testing the generator correctly. It is checking the generated Terraform config object's attributes and the generated filename. No issues found.
lib/cpflow.rb (1)
238-238
: The existing comment explains the reason for the change. No further comments are needed.docs/commands.md (1)
452-452
: LGTM! But verify the implementation.The documentation change looks good. The
cpflow terraform generate
command now requires the application name as an argument.Verify that the
cpflow terraform generate
command implementation has been updated to require the application name argument.Run the following script to verify the implementation:
Verification successful
Verification Successful: The
cpflow terraform generate
command requires the application name argument.The implementation in
lib/command/terraform/generate.rb
includesapp_option(required: true)
, confirming that the application name is a required argument for this command. This aligns with the documentation update.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the `cpflow terraform generate` command requires the application name argument. # Test: Search for the command implementation. Expect: The command to have the `-a` or `--app` option. rg --type ruby -A 5 $'desc \'terraform generate\''Length of output: 55
Script:
#!/bin/bash # Broaden the search to locate the `terraform generate` command implementation in the codebase. rg 'terraform generate'Length of output: 123
Script:
#!/bin/bash # Search for `cpflow` in the codebase to locate command-line interface definitions. rg 'cpflow'Length of output: 60153
Script:
#!/bin/bash # Search for command definitions or option parsing logic related to `terraform generate` or `cpflow`. rg 'terraform generate' --type ruby rg 'option' --type rubyLength of output: 24988
d47aa92
to
2630c3c
Compare
@coderabbitai review |
✅ Actions performedReview triggered.
|
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.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (5)
spec/command/terraform/generate_spec.rb (3)
12-12
: LGTM! Consider adding a comment for clarity.The addition of
let!(:app)
is a good practice for setting up the test environment. It ensures that the dummy app is created before each test run.Consider adding a brief comment explaining what
dummy_test_app
represents, as it's not defined in this file:# Create a dummy application instance for testing let!(:app) { dummy_test_app }
24-28
: Improved test case with better assertions and error aggregation.The changes to the test case are excellent:
- Adding
:aggregate_failures
allows all expectations to be evaluated, improving test coverage.- Checking for non-existence of files before running the command ensures a clean state.
- Using
all(exist)
provides a clear and concise way to check all generated files.Consider adding a descriptive comment for the test case to explain its purpose:
# Ensures that the command generates all expected Terraform config files it "generates terraform config files", :aggregate_failures do # ... (rest of the test case) end
30-33
: Well-structured helper method for managing config file paths.The
config_file_paths
method is a great addition:
- It centralizes the logic for determining expected file paths.
- It improves maintainability and reduces duplication in the test.
- The method is flexible, allowing for easy addition of new config files in the future.
Consider adding a brief comment explaining the method's purpose and return value:
# Returns an array of Pathname objects for all expected Terraform config files def config_file_paths [TERRAFORM_CONFIG_DIR_PATH.join("providers.tf")] + %w[gvc.tf identities.tf].map do |config_file_path| TERRAFORM_CONFIG_DIR_PATH.join(app, config_file_path) end endlib/command/base.rb (1)
457-468
: LGTM! Consider enhancing with path validation and a more detailed description.The
dir_option
method is a well-structured addition that follows the existing pattern for option methods in the class. It provides flexibility with therequired
parameter and uses appropriate types for the directory option.To further improve this method, consider the following suggestions:
- Add path validation to ensure the provided directory is valid and accessible.
- Expand the description to provide more context, e.g., "Specify the output directory for generated files".
Here's a suggested enhancement to the method:
def self.dir_option(required: false) { name: :dir, params: { banner: "DIR", desc: "Specify the output directory for generated files", type: :string, required: required, validate: ->(value) { Dir.exist?(value) || raise("Directory does not exist: #{value}") } } } endThis change adds a simple validation to check if the directory exists and provides a more informative description.
lib/command/terraform/generate.rb (1)
52-52
: Review the file write mode for clarityThe
File.write
method is usingmode: "a+"
, which opens the file in read-write mode and appends content to it:File.write(terraform_app_dir.join(generator.filename), generator.tf_config.to_tf, mode: "a+")Since the application directory is recreated each time with
recreate_terraform_app_dir
, and files within it are removed, using append mode might not be necessary.Consider using the default write mode (
"w"
), which truncates the file before writing. This can prevent potential confusion and ensures that each generation produces a fresh set of configuration files:-File.write(terraform_app_dir.join(generator.filename), generator.tf_config.to_tf, mode: "a+") +File.write(terraform_app_dir.join(generator.filename), generator.tf_config.to_tf)If there is a specific reason for using append mode (e.g., multiple writes to the same file within a single run), please ensure that this behavior is documented to avoid confusion.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (3)
- lib/command/base.rb (1 hunks)
- lib/command/terraform/generate.rb (1 hunks)
- spec/command/terraform/generate_spec.rb (1 hunks)
🔇 Additional comments (4)
spec/command/terraform/generate_spec.rb (1)
14-16
: Improved file cleanup in thebefore
block.The change from
FileUtils.rm_r
toFileUtils.rm_rf
is a good improvement. It ensures a more robust cleanup process before each test by forcibly removing the directory and its contents, even if it doesn't exist.lib/command/base.rb (1)
470-472
: LGTM! The newdir_option
is automatically included inall_options
.The
all_options
method uses metaprogramming to collect all methods ending with_option
. This approach ensures that the newly addeddir_option
is automatically included without requiring any changes to theall_options
method itself.This demonstrates good use of metaprogramming for extensibility, making it easy to add new options in the future without modifying existing code.
lib/command/terraform/generate.rb (2)
49-50
: 🛠️ Refactor suggestionRemove hardcoding of template kinds to support extensibility
Currently, the code skips processing templates unless their
kind
is either'gvc'
or'identity'
:next unless %w[gvc identity].include?(template["kind"])Hardcoding template kinds can hinder future extensibility when new template types are added.
Consider handling all available template kinds dynamically or configuring the list of supported kinds. If certain kinds are not yet supported, you could log a warning or handle them appropriately.
Given the TODO comment indicates that this line should be deleted after all template kinds are supported, ensure that there is a plan to implement support for additional kinds. To verify if there are other template kinds present in the templates directory, you can run the following script:
#!/bin/bash # Description: Check for template kinds other than 'gvc' and 'identity' in templates/*.yml # Expected: No output if only 'gvc' and 'identity' are used. rg '^kind:' templates/*.yml | sed 's/.*kind: //' | sort | uniq | grep -v -E '^(gvc|identity)$'This script searches for all
kind
entries in your template files and lists any that are not'gvc'
or'identity'
. If the script outputs any kinds, it means there are templates that are currently being skipped and need to be supported or handled accordingly.
71-73
: Ensure the terraform directory path is correctly resolvedWhen fetching the
:dir
option fromconfig.options
, if the provided path is relative, it might not resolve correctly:full_path = config.options.fetch(:dir, Cpflow.root_path.join("terraform"))Verify that when a relative path is provided via the
:dir
option, it is correctly expanded relative to the current working directory or the application's root path. If not, consider expanding it usingFile.expand_path
or resolving it against a known base path.To confirm this behavior, you can run the following script:
This will show how relative paths are resolved in your environment and help ensure that the terraform directory is created in the intended location.
We need to clear out the temporary GVCs per the errors on CI. |
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.
LGTM 💯
Left just a few minors/nits, which you can address before merging if you want. No need to ask for a re-review.
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.
@zzaakiirr Actually, this line is causing issues in the tests, and may be affecting other areas as well:
config.class.define_method(:app) { app } |
When doing the cleanup at the end of the tests, it tries to delete the incorrect app, because the app
method has been overwritten and won't return the correct app:
This is why apps aren't being properly cleaned after running the tests.
We need to think of a better way to handle this.
@rafaelgomesxyz Thanks for finding problematic place in the code! I replaced
Both specs passed and test GVCs were deleted in controlplane. Without this fix |
What does this PR do?
TerraformConfig::Generator
class which takes YAML template and transforms them to Terraform config (.tf
format). For now, only 2 types of templates are supported -gvc
andidentity
Support for other template types will be added in later PRs
cpflow terraform generate
command now accepts 2 optional arguments: application name and directory to save generated terraform config files to.controlplane.yml
in folder with app name./terraform
cpflow terraform generate
is invoked configuration files are recreatedTerraform docs
https://registry.terraform.io/providers/controlplane-com/cpln/latest/docs/resources/gvc
https://registry.terraform.io/providers/controlplane-com/cpln/latest/docs/resources/identity
Examples
YAML template for CPLN gvc:
Will transform to:
Summary by CodeRabbit
New Features
cpflow terraform generate
command to include a new required argument,$APP_NAME
.Bug Fixes
Tests
TerraformConfig::Generator
,Gvc
, andIdentity
classes to ensure correct configuration generation.