feat(alzlibtool): add --library-overwrite-enabled to check, generate and document - #249
Open
Yingjie Chen (Y1ngJ1eChen) wants to merge 1 commit into
Open
feat(alzlibtool): add --library-overwrite-enabled to check, generate and document#249Yingjie Chen (Y1ngJ1eChen) wants to merge 1 commit into
Yingjie Chen (Y1ngJ1eChen) wants to merge 1 commit into
Conversation
Only `cache create` exposed an opt-in for AlzLib.Options.AllowOverwrite. The remaining commands constructed the library with NewAlzLib(nil), so a member that intentionally redefines an asset provided by one of its dependencies failed during Alzlib.Init with "already exists in the library", before any check, generation or documentation step ran. Register --library-overwrite-enabled (default false) on `check library`, `generate architecture` and `document library`, and assign it to Options.AllowOverwrite before Init. Fixing only `check library` would leave a member that validates but cannot be generated or documented, which is the worst failure mode for a validation tool. Add doc.AlzlibReadmeMdWithOptions so the document command can pass options through; AlzlibReadmeMd delegates to it with nil options and keeps its signature and error text unchanged. Correct the `cache create` long description and flag help, which claimed that definitions are overwritten. They are not: for policy definitions and policy set definitions the flag only suppresses the duplicate error and the earlier library's version is kept. All four help strings now describe the same limitations, and generate/document/cache emit a warning on stderr when the flag is set so that JSON and Markdown artifacts on stdout stay machine readable. No engine behaviour is changed. alzlib.go, assets and deployment are untouched, so consumers such as terraform-provider-alz are unaffected. Refs Azure/Azure-Landing-Zones#4191
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #249 +/- ##
==========================================
+ Coverage 47.93% 49.58% +1.65%
==========================================
Files 54 54
Lines 4915 4967 +52
==========================================
+ Hits 2356 2463 +107
+ Misses 2272 2209 -63
- Partials 287 295 +8 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds an opt-in
--library-overwrite-enabledflag to thealzlibtoolcommands thatbuild an
AlzLib, so that a library member which intentionally redefines assetssupplied by its dependencies can be validated, generated and documented. Corrects the
pre-existing help text on
cache create, which over-promised what the flag does.Refs Azure/Azure-Landing-Zones#4191.
No engine behaviour is changed.
alzlib.go,assetsanddeploymentare untouched.Problem
AllowOverwritehas existed in the engine for a long time, and bothterraform-provider-alz(library_overwrite_enabled) andalzlibtool cache create(
--library-overwrite-enabled, added in v0.31.0) expose it. The remaining commands didnot, and initialised with
alzlib.NewAlzLib(nil):Error: library init error: Alzlib.Init: error adding processed result to AlzLib:
Alzlib.addProcessedResult: policy assignment Deploy-MCSB2-Monitoring already exists in the library
Because the failure happens during
Init, every check is skipped rather than just theconflicting asset, so the only workaround was to drop the validation step from CI
altogether.
Changes
cmd/alzlibtool/command/check/library.goOptions.AllowOverwritebeforeInit; document the limitationscmd/alzlibtool/command/generate/architecture.gocmd/alzlibtool/command/document/library.gocmd/alzlibtool/command/cache/create.gointernal/doc/doc.goAlzlibReadmeMdWithOptions;AlzlibReadmeMddelegates withnilAll four flags share one name, matching the provider attribute and avoiding a third
spelling (
--allow-overwritefrom the issue title was deliberately not added).What the flag does, and does not do
Verified against the current engine and pinned by tests:
Assets are replaced in full, never merged field by field, so an overriding file must
contain the complete asset.
Note also that replacing an archetype can legitimately make a dependency-supplied
definition unreferenced, which then fails
CheckAllDefinitionsAreReferenced. That isexisting behaviour, not a regression, and is now stated in the help text.
Out of scope
AllowOverwritesemantics, including the policy (set) definition rule above.Whether "allow duplicates" or "last one wins" is the intended contract needs a separate
design decision; this PR freezes and documents the current behaviour instead.
terraform-provider-alz,avm-ptn-alzor the accelerator. A docs-onlyclarification of the provider's
library_overwrite_enableddescription is worth afollow-up, since it currently reads as though definitions are replaced.
Testing
New command-level test scaffolding (
TestMainplus a child-process helper) makes theos.Exitcalls in these commands observable as exit codes, and separates stdout fromstderr so artifacts can be asserted independently of warnings. It is reusable for any
future CLI flag.
Fixtures live in a single shared directory,
cmd/alzlibtool/command/testdata/, consumedby all three command packages. Every case is offline and needs no Azure credentials.
check library: fails without the flag with the reported message; passes with it. Themember defaults require a parameter that only exists on the definition referenced by the
member's assignment, so a passing defaults check proves the member version won.
generate architecture: same pair, plus an artifact test over--outputasserting thatthe member policy assignment is written but a redefined policy definition still comes
from the dependency, and that stdout remains valid JSON.
document library: same pair, asserting the member archetype appears in the Markdown andthat the warning does not.
internal/doc:AlzlibReadmeMdandAlzlibReadmeMdWithOptions(..., nil, ...)producebyte-identical output.
and duplicate archetype override limitations are pinned so they cannot change silently.
Backwards compatibility
The default is
false, identical to the previously hard-coded value, so any invocationwithout the flag behaves exactly as before. This was confirmed by building the CLI from
this branch and from the base commit and diffing the output of
check libraryanddocument libraryon the same input: stdout, stderr and exit codes are byte-identical.The only intentional output difference is the corrected
cache create --helptext.internal/doccannot be imported from outside the module, and the existingAlzlibReadmeMdentry point is preserved, so there is no API break.