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

Sync Terraform Templates from git repo #5

Merged
merged 7 commits into from
Apr 1, 2024

Conversation

putmanoj
Copy link
Contributor

@putmanoj putmanoj commented Mar 19, 2024

Sync Terraform Template

  • find directories containing .tf/.tf.json files in git repo worktree
  • add the relative-path as Template name(path)
  • add basic spec to verify ConfigurationScriptSource

@agrare
Copy link
Member

agrare commented Mar 19, 2024

@putmanoj Update app/models/manageiq/providers/embedded_terraform/automation_ma…nager/configuration_script_source.rb isn't a very helpful commit message and having 6 commits with the same commit message is confusing. If you're fixing rubocop warnings or other small items I like to rebase and amend the commit which introduced the issue rather than adding a new commit.

@agrare agrare added the enhancement New feature or request label Mar 19, 2024
@agrare
Copy link
Member

agrare commented Mar 19, 2024

TODO before merge:

@jrafanie
Copy link
Member

@putmanoj I added some tests and made a few changes to the existing ones on my fork: master...jrafanie:manageiq-providers-embedded_terraform:git-sync-templates

you can cherry-pick them or I can help you bring them over. Let me know what you think.

@putmanoj
Copy link
Contributor Author

@putmanoj I added some tests and made a few changes to the existing ones on my fork: master...jrafanie:manageiq-providers-embedded_terraform:git-sync-templates

you can cherry-pick them or I can help you bring them over. Let me know what you think.

Thanks, will cherry-pick.


# checkout files to temp dir, we need for parsing input/output vars
git_checkout_tempdir = Dir.mktmpdir("terraform-git")
checkout_git_repository(git_checkout_tempdir)
Copy link
Member

@Fryguy Fryguy Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this isn't necessary - everything should be available from the worktree. However, I admit that working with rugged objects is complicated, so this is fine for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also will this checkout happen on every refresh? I feel like that could be expensive. 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the update or the checkout expensive? Maybe just doing the checkout would be ok if it's already cloned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkout can be expensive. Under the covers we do bare repos, so it's literally having to do I/O for all of the files each time. This is why straight Rugged is better, because it's all in-memory and doesn't require creating a file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For review

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@putmanoj can you review this and comment here? Thanks.

Copy link
Member

@agrare agrare Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO use GitWorktree

next unless filepath.end_with?(".tf", ".tf.json")

parent_dir = File.dirname(filepath)
name = ManageIQ::Providers::EmbeddedTerraform::AutomationManager::ConfigurationScriptSource.template_name_from_git_repo_url(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name = ManageIQ::Providers::EmbeddedTerraform::AutomationManager::ConfigurationScriptSource.template_name_from_git_repo_url(
name = self.class.template_name_from_git_repo_url(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved

next if template_dirs.key?(name)

full_path = File.join(git_checkout_tempdir, parent_dir)
_log.info("Local full path : #{full_path}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this line? I'm concerned it will flood the logs. Perhaps make it debug?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved

_log.info("Local full path : #{full_path}")
files = Dir.children(full_path)

# :TODO add parsing for input/output vars
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# :TODO add parsing for input/output vars
# TODO: add parsing for input/output vars

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved

Comment on lines 20 to 26
def get_default_terraform_object(name)
default_terraform_objects.find_by(:name => name).try(:value).try(:to_i)
end

def set_default_terraform_object(name, value)
default_terraform_objects.find_or_initialize_by(:name => name).update(:value => value)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These helper methods don't see to be buying anything here - prefer just inlining these methods directly into the callers

(In general, in Ruby, we avoid get_ and set_ methods when a proper getter/setter method like you have on lines 10 and 14 will suffice)

@@ -0,0 +1,20 @@
class ManageIQ::Providers::EmbeddedTerraform::Provider < Provider
include DefaultTerraformObjects
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand why this is a module because I don't see it reused anywhere (or do you plan to reuse in the future? Prefer inlining the methods from the module directly here unless you plan on reuse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs review.

@@ -0,0 +1,27 @@
module ManageIQ::Providers::EmbeddedTerraform::Provider::DefaultTerraformObjects
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of these default_terraform_objects with respect to syncing terraform templates? I don't see any callers in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is copy&paste from embedded ansible, it isn't necessary here and should be deleted https://github.com/ManageIQ/manageiq/blob/master/app/models/manageiq/providers/embedded_ansible/provider/default_ansible_objects.rb

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs review.

@Fryguy
Copy link
Member

Fryguy commented Mar 26, 2024

@putmanoj Is this built on top of another PR? If so can you indicate that in the opening post? It's hard to follow with 24 commits.

@agrare
Copy link
Member

agrare commented Mar 27, 2024

@putmanoj please rebase on master rather than creating a new merge commit in this branch as it keeps the history cleaner

Merge branch 'git-sync-templates' of github.com:putmanoj/manageiq-providers-embedded_terraform into git-sync-templates
d10963e

@@ -0,0 +1,20 @@
class ManageIQ::Providers::EmbeddedTerraform::Provider < Provider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need an EmbeddedTerraform::Provider, this looks like another copy&paste from EmbeddedAnsible

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs review.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the included DefaultTerraformObjects module. It looks like the tests are creating provider/managers through create_in_provider, and perhaps that's why we have a provider.rb with code that does ensure_managers. I'm not sure how it's expected to work without a provider.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed Provider and fixed the factories and specs.

This was copied&pasted from EmbeddedAnsible, but EmbeddedAnsible seeds a Provider [ref] where EmbeddedTerraform does not [ref]. This meant that these specs did not match the behavior of how the embedded_terraform manager was being created.

@@ -0,0 +1,518 @@
FakeTerraformRepo = Spec::Support::FakeAnsibleRepo # TODO: replace FakeAnsibleRepo with FakeTerraformRepo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved

repo.git_branch_create("other_branch")
stub_const("GitRepository::GIT_REPO_DIRECTORY", repo_dir)

EvmSpecHelper.assign_embedded_terraform_role
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EvmSpecHelper.assign_embedded_terraform_role
EvmSpecHelper.assign_role("embedded_terraform")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved

Comment on lines 54 to 59
expect(result).to be_an(described_class)
expect(result.scm_type).to eq("git")
expect(result.scm_branch).to eq("master")
expect(result.status).to eq("successful")
expect(result.last_updated_on).to be_an(Time)
expect(result.last_update_error).to be_nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE you can use have_attributes to check multiple attributes at once:

expect(result).to have_attributes(
  :scm_type => "git",
  :scm_branch => "master"
  ...
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved


result = described_class.create_in_provider(manager.id, params)

expect(result).to be_an(described_class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small grammar fix

Suggested change
expect(result).to be_an(described_class)
expect(result).to be_a(described_class)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved

expect(result.scm_type).to eq("git")
expect(result.scm_branch).to eq("master")
expect(result.status).to eq("successful")
expect(result.last_updated_on).to be_an(Time)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably use be_within to check the actual timestamp here: expect(result.last_updated_on).to be_within(2.seconds).of(Time.now.utc)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved

@jrafanie
Copy link
Member

@putmanoj Ok, I think I resolved all of the comments with my 👍 in them. I think everything but provider.rb. I think that needs to be resolved. Let's remove that module and anything in there we don't need yet. We can add it later.

Please review my branch: at master...jrafanie:manageiq-providers-embedded_terraform:git-sync-templates

I squashed our fixup commits. My final commits can be combined once you review them. Note, I really deleted a lot of tests because they still passed when I commented out most of the configuration source class so they're not really testing it.

I didn't want to push directly to your branch here but mine is correctly tracking upstream here so if you want to use my branch, we can push that.

@putmanoj
Copy link
Contributor Author

@jrafanie jrafanie force-pushed the git-sync-templates branch 2 times, most recently from 4f3d599 to f8ad986 Compare March 28, 2024 13:28
@jrafanie
Copy link
Member

ok, @putmanoj, I force pushed to your fork. I've addressed all concerns not yet addressed other than the questions about provider.rb including DefaultTerraformObjects from Jason and Adam. Actually, I think Adam said you might not even need provider.rb here. Other than that, we need to set the role properly for the queue items. I marked those commented tests as pending so we can properly set the role. I assume we want to use the new server role. I'm not sure what else is needed. Can you finish up those things and we can get ready for merge and then I can help get your other work rebased and tested? Thanks!

@jrafanie jrafanie force-pushed the git-sync-templates branch from f8ad986 to 78e58f6 Compare March 28, 2024 13:40
putmanoj and others added 3 commits March 28, 2024 14:18
Add relative-path of dir's with .tf/.tf.json files

Add specs for git sync
* add spec for .create_in_provider
* Add EmbeddedTerraform Provider class
* add ensure_managers
* fixed .create_in_provider test
* Use basename of dir, as template-name pre-fix, & full git repo url details as suffix
* Add tests for:
   templates-in-repo
   update-in-provider
   update_in_provider_queue
   delete_in_provider_queue
* Add tests to describe behavior of template_name_from_git_repo_url
* Drop templates_for, test both name and payload
* We should test the payload contents are correct in addition to the name.
* Drop redundant test since we're testing names and payloads
* Add test verifying no longer existing payloads are removed on sync
* Mark pending tests where we need to set the role in the queue
* Create a fake terraform git repo for test
I changed configuration_script_source.rb to:

```ruby
class ManageIQ::Providers::EmbeddedTerraform::AutomationManager::ConfigurationScriptSource < ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptSource
  FRIENDLY_NAME = "Embedded Terraform Repository".freeze

  def sync
  end
end
```

and all of the removed tests still pass which tells me either the tests
aren't needed, the code needs to be implemented and/or the test needs
to test what unique behavior is provided by this class.
@jrafanie jrafanie force-pushed the git-sync-templates branch from 78e58f6 to bf0fd97 Compare March 28, 2024 18:19
@jrafanie
Copy link
Member

Note, #9 was extracted from here

Comment on lines +8 to +11
private_class_method def self.queue_role
"embedded_terraform"
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to fix the three pending specs in configuration_script_source_spec

@kbrock
Copy link
Member

kbrock commented Apr 1, 2024

I like the changes introduced by Adam and the other code looks good (from a person who doesn't use rugged enough)

LGTM

@kbrock kbrock merged commit ed4c9a1 into ManageIQ:master Apr 1, 2024
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants