Skip to content

Conversation

@codezninja
Copy link
Contributor

@codezninja codezninja commented Mar 30, 2022

This PR will support the ability to pass tags through a generated tfvars file instead. This simplifies the cli command.

This is beneficial for custom plugins that might need wrap the full terraform commands with some other commands for example su ec2-user -c "{generate terraform}" without worrying about all the escaping of multiple quotes

Fixes #432

Merge Checklist

  • Have you linked this Pull Request to an Issue?
  • Have you updated any relevant README files, to explain how this new feature works (with examples)?
  • Have you added relevant test cases for your change?
  • Do all tests and code style checks pass with ./gradlew check --info?
  • Have you successfully run your change in a pipeline in a Jenkins instance?
  • Have you updated the CHANGELOG?

@codecov-commenter
Copy link

Codecov Report

Merging #431 (ea20c95) into master (d1a5e98) will decrease coverage by 0.66%.
The diff coverage is 7.69%.

@@            Coverage Diff             @@
##           master     #431      +/-   ##
==========================================
- Coverage   82.44%   81.78%   -0.67%     
==========================================
  Files          54       54              
  Lines        1487     1499      +12     
  Branches      316      319       +3     
==========================================
  Hits         1226     1226              
- Misses        115      126      +11     
- Partials      146      147       +1     
Impacted Files Coverage Δ
src/Jenkinsfile.groovy 75.38% <0.00%> (-2.40%) ⬇️
src/TerraformPlanCommand.groovy 85.93% <0.00%> (-8.90%) ⬇️
src/TagPlugin.groovy 81.81% <20.00%> (-8.19%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d1a5e98...ea20c95. Read the comment docs.

@codezninja codezninja self-assigned this Mar 30, 2022
@codezninja codezninja force-pushed the feature/pass_tags_through_file branch from f5c5658 to 9adceda Compare March 30, 2022 02:44
@codezninja codezninja marked this pull request as ready for review March 30, 2022 02:51
@codezninja codezninja requested a review from kmanning March 30, 2022 02:52
@codezninja
Copy link
Contributor Author

codezninja commented Apr 1, 2022

@kmanning Let us know if the pr need to be updated.

.build()
```

If you want to simplify the cli command you can pass the tags using `-var-file` instaed.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Type: 'instaed'

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be clarified?
If you want to simplify the cli command, you can use the .writeToFile() method to write tags out to a file and pass them using -var-file instead

Copy link
Contributor

Choose a reason for hiding this comment

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

Type: 'instaed'

corrected

def validate = new TerraformValidateStage()
// Tag variables will be passsed via `-var-file`
Copy link
Collaborator

Choose a reason for hiding this comment

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

typo passsed

Copy link
Contributor

Choose a reason for hiding this comment

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

corrected

String value = convertMapToCliString(values)
Jenkinsfile.writeFile(fileName, "${key}=${value}")
return withVariableFile(fileName)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

In a future refactoring of this feature, I could imagine pushing the writing of the file up to the TagPlugin. That could reduce duplication a bit.

String value = convertMapToCliString(values)
Jenkinsfile.writeFile(fileName, "${key}=${value}")
return withVariableFile(fileName)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

In a future refactoring of this feature, I could imagine pushing the writing of the file up into TagPlugin. It would reduce duplication a bit.

public static writeToFile() {
writeToFile = true
return this
}
Copy link
Collaborator

@kmanning kmanning Apr 4, 2022

Choose a reason for hiding this comment

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

In a future refactoring of this feature, I could imagine writeToFile accepting an option string, to overwrite the file name that is written. The default value of the optional string parameter could be the filename that you're currently using <environment>-tags.tfvars


verify(command, times(1)).withVariable(anyString(), anyMap())
verifyNoMoreInteractions(command)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine as it is, but in this test, you're actually testing apply and not writeToFile. You could optionally group this under ApplyForApplyCommand.

I would also consider that this specific test case is already covered by

public void addsTheTagArgument() {

Copy link
Contributor

@treylittle treylittle Apr 14, 2022

Choose a reason for hiding this comment

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

Understood. I guess I was just verifying the behavior is changed between "normally" and "writesToFile". I will just remove this one.

verify(command, times(1)).withVariableFile(anyString(), anyMap())
verifyNoMoreInteractions(command)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine as it is, but in this test, you're actually testing apply and not writeToFile. You could optionally group this under ApplyForApplyCommand.

Further, you're testing a specific condition of apply - the condition = when writeToFile is used. I might consider nesting that, similar to what is happening with WithVariableName

Copy link
Contributor

Choose a reason for hiding this comment

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

True. I'll move it

verify(original).writeFile(file: filename.toString(), text: content.toString())
verifyNoMoreInteractions(original)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine, but I think this test case wouldn't exist, if writing-the-file behavior lived in TagPlugin.

def content = "${myKey}={expectedKey=\"${expectedValue}\"}"
verify(original).writeFile(file: filename.toString(), text: content.toString())
verifyNoMoreInteractions(original)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine, but I think this test case wouldn't exist as-is, if writing-the-file behavior lived in TagPlugin.

verifyNoMoreInteractions(command)
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

You've tested TagPlugin with TerraformApplyCommand, but not TerraformPlanCommand? Gross, that's because the interface for apply is not very clean... Disregard this comment.

Copy link
Collaborator

@kmanning kmanning left a comment

Choose a reason for hiding this comment

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

:shipit:

@kmanning
Copy link
Collaborator

Please squash and merge this request when ready.

@codezninja codezninja merged commit b4cec15 into master Apr 18, 2022
@codezninja codezninja deleted the feature/pass_tags_through_file branch April 18, 2022 18:26
@codezninja codezninja restored the feature/pass_tags_through_file branch August 15, 2022 16:29
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.

Support passing TagsPlugin via tfvars.

5 participants