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

Your wish is my command... #6

Conversation

NickLaMuro
Copy link

@NickLaMuro NickLaMuro commented Jul 15, 2019

@carbonin As you requested 😄

This bit of code to translate the credentials is now in 4 places 😭 if any of you want to help me work out a way to DRY that up I'd be grateful.

- @carbonin (July 15, 2019)

DRYs up code for translating credentials. Makes it as flexible as possible to fit into the multiple places it is being used.

Note: commit is [WIP] until I can run specs for the previous areas of the code to confirm this at least works a little bit.

Links

@NickLaMuro NickLaMuro force-pushed the ansible_credential_translation_mixin branch from fdcb211 to 5f1d8d2 Compare July 15, 2019 22:51
Copy link
Author

@NickLaMuro NickLaMuro left a comment

Choose a reason for hiding this comment

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

Just providing some preliminary explanations about the changes.

# Translates options hash with credential_ids into auth
#
# @param [Hash] options Hash containing the credential_ids
# @param [Hash] other_hash (options) Data to be updated if differs from <options>
Copy link
Author

Choose a reason for hiding this comment

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

# @param [Hash] other_hash ...

#namingIsHard™

AUTH_TYPES.each do |credential|
credential_sym = "#{credential}_id".to_sym
credential_id = mutate_options ? options.delete(credential_sym) : options[credential_sym]
other_hash[credential] = Authentication.find(credential_id).native_ref if credential_id.present?
Copy link
Author

Choose a reason for hiding this comment

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

I think I covered all of the corner cases with this by using :other_hash and :mutate_options, but please check my work. I already forgot to assign this to other_hash instead of options.

Probably want to test this mixin directly and heavily just in case someone comes and changes this method and does something unexpected.


module_function

# Translates options hash with credential_ids into auth
Copy link
Author

Choose a reason for hiding this comment

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

Along the same lines with my comment about adding specs, the docs on this probably could be fleshed out a bit too.

@@ -2,6 +2,8 @@ class ServiceTemplateAnsiblePlaybook < ServiceTemplateGeneric
before_destroy :check_retirement_potential, :prepend => true
around_destroy :around_destroy_callback, :prepend => true

include AnsibleRunnerAuthTranslations
Copy link
Author

Choose a reason for hiding this comment

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

Fudged this one up... this needs to be in the class method space, so it should have been a extend, not include. Fixing...

module AnsibleRunnerAuthTranslations
AUTH_TYPES = %i[credential vault_credential cloud_credential network_credential]

module_function
Copy link
Author

Choose a reason for hiding this comment

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

By making this method a module_function, this can be called in two ways:

  • By includeing it, and calling it as we have done currently (as if it were a method in the class)
  • Calling it from it's module directly as if it was a class method (similar to FileUtils)

The second form would look like:

AnsibleRunnerAuthTranslations.translate_credentials(options)

Which has the advantage of not adding methods to the classes we are writing this for, though, when adding a module that has module_functions, it should add them into the private space (I think...).

DRYs up code for translating credentials.  Makes it as flexible as
possible to fit into the multiple places it is being used.
@NickLaMuro NickLaMuro force-pushed the ansible_credential_translation_mixin branch from 5f1d8d2 to d262dbb Compare July 15, 2019 23:07
@carbonin
Copy link
Owner

So I was thinking more along the lines of someone from GM's team helping to work out how we can dedup the whole processing in this part of the code base.

I don't really want to create a top-level mixin for one method that, realistically, I would like to get rid of entirely in time (there's no reason the change the keys since we're not passing them to tower anymore).

@NickLaMuro
Copy link
Author

Fair enough. My bad for misinterpreting.

Feel free to close then, since I think your broader plan makes more sense long term.

@carbonin carbonin closed this Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants