-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
provider/aws: Add normalizeJsonString and validateJsonString functions. #8028
provider/aws: Add normalizeJsonString and validateJsonString functions. #8028
Conversation
Related to #7967. |
@stack72 the idea here is to add validation of JSON to catch any potential issues at the planning stag by explicitly notifying user about an error. At the moment, the The changes here are to make the Example usage: (before)
(after)
Let me know what do you think. |
} | ||
var j interface{} | ||
err := json.Unmarshal([]byte(jsonString.(string)), &j) | ||
if err != nil { | ||
return fmt.Sprintf("Error parsing JSON: %s", err) | ||
return "", err |
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.
I think it's best if we return exactly what the user provided (return jsonString.(string), err
) here so that normalizeJsonStateFunc
will just return the value unchanged when it's invalid.
The reason for this is that StateFunc
is called in some situations where ValidateFunc
isn't called, such as normalizing a value set from the upstream API during a resource's Read
, and so preserving the invalid value will allow it to show in a diff, rather than showing as the empty string.
This is definitely an edge case and mainly a cosmetic thing; I took a similar approach in #8350, which does something similar for normalizing/validating IAM policy JSON.
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.
Makes sense!
Hi @kwilczynski. Thanks for doing this! That behavior of returning the error string as the normalized value has been bugging me for a while now. This is a bit of an intimidating patch to accept because it touches many resources all at once and so would require running a lot of acceptance tests before merge. Could you possibly split this up in to one commit that adds the necessary shared code, and then a separate commit to update the S3 bucket resource, something like what I did in #8350? Then we can review and merge that smaller increment and attack the rest of these as separate PRs that can each be tested and merged in isolation. I'm happy to help do the split and re-submit as a new PR if you don't have time. I want to get this in. 😀 |
@apparentlymart thank you! I did bother me too (the lack of correct error handling), which is why this attempt to fix this. No issues with splitting the commits, I will get to it. I had a look at #8350, which is amazing, and it gave me an idea - what if we would to design an interface that would allow for abstracting JSON reading (parsing), writing and validation of us? The reason why I thought about an interface that could provide a concrete implementation for a specific use case e.g. IAM role, S3 bucket, etc., is because what I did was simply improve upon the After merging changes from #8350, it would make very little sense to add my changes, which is why I am thinking that perhaps there would be a better way and abstract universal elements out so that we could re-use these. What do you think? |
@kwilczynski it is true that #8350 overlaps with the parts of this change that concern IAM policies. What you did here does seem like it'd be useful for other JSON-ish things that aren't IAM policies, though. Do you have some thoughts about what a common abstraction (like you propose here) would look like? We already have one abstraction here in the idea of It would still be nice to get your improvements to |
I fully agree. I will continue. We can revise and refractor things later, if you want to, exactly like you say. :) |
7ae3060
to
2bb6d33
Compare
Things to do:
|
Acceptance tests to run:
Results tracked here: https://gist.github.com/kwilczynski/f89e7d1f0cfc4f744a98bdac15567954 |
The |
Related to #8615, since it removes the |
@apparentlymart @stack72 if you don't mind, I could use your help. I cannot run the AzureRM test. |
2bb6d33
to
e1888d7
Compare
I have rebased against master to accommodate recent changes. |
4e48133
to
335a883
Compare
After having a conversation with @radeksimko , who was very kind and provided me with an idea about making this more manageable, I will split this commit into a series of Pull Requests. This one will be revised and subsequent Pull Requests will be pushed. |
335a883
to
75e358a
Compare
Unit tests are passing:
|
Signed-off-by: Krzysztof Wilczynski <[email protected]>
Signed-off-by: Krzysztof Wilczynski <[email protected]>
Signed-off-by: Krzysztof Wilczynski <[email protected]>
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
This commit adds ValidateFunc to the policy attribute so that JSON parsing
errors can be caught early. Generally, when there is a ValidateFunc set for the
attribute, one can safely assume that before any of the creation and/or update
of the existing resource would happen it would have to succeed validation. Also
adds support for new helper function which is used to normalise JSON string.
Signed-off-by: Krzysztof Wilczynski [email protected]