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

Fix for decompiler to support templatespec #10329

Merged
merged 3 commits into from
Apr 10, 2023
Merged

Conversation

mishrapratikshya
Copy link
Contributor

@mishrapratikshya mishrapratikshya commented Apr 5, 2023

This PR adds support for template spec while decompiling [#3246 ].

It add supports for the case where the id has the resourceId which is not parameterized.

            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2020-10-01",
            "name": "test",
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                    "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.Resources/templateSpecs/testSpec/versions/V2"
                }
            }

Contributing a Pull Request

If you haven't already, read the full contribution guide. The guide may have changed since the last time you read it, so please double-check. Once you are done and ready to submit your PR, run through the relevant checklist below.

Contributing a feature

  • I have opened a new issue for the proposal, or commented on an existing one, and ensured that the Bicep maintainers are good with the design of the feature being implemented
  • I have included "Fixes #{issue_number}" in the PR description, so GitHub can link to the issue and close it when the PR is merged
  • I have appropriate test coverage of my new feature
Microsoft Reviewers: Open in CodeFlow

@mishrapratikshya mishrapratikshya changed the title initial commit for decompiler to support templatespec Fix for decompiler to support templatespec Apr 6, 2023
Comment on lines 1026 to 1030
ResourceGroupLevelResourceId id;
var templateSpec = "";
if (ResourceGroupLevelResourceId.TryParse(templateID, out id) is not true) {
throw new ConversionFailedException($"Unable to parse resourceId {templateID}", resource);
}
templateSpec = "ts:" + id.SubscriptionId + "/" + id.ResourceGroup + "/" + id.NameHierarchy.First() + ":" + id.NameHierarchy.Last();
return SyntaxFactory.CreateStringLiteral(templateSpec);
Copy link
Member

Choose a reason for hiding this comment

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

Some styling/simplification suggestions:

  1. Apart from places where it's necessary, I would recommend avoiding separating the declaration & assignment of variables. For example, instead of:
    ResourceGroupLevelResourceId id;
    var templateSpec = "";
    if (ResourceGroupLevelResourceId.TryParse(templateID, out id) is not true) {
       throw  new ConversionFailedException($"Unable to parse resourceId {templateID}", resource);
    }          
    templateSpec = "ts:" + id.SubscriptionId + "/" + id.ResourceGroup + "/" + id.NameHierarchy.First() + ":" + id.NameHierarchy.Last();
    You could write:
    if (ResourceGroupLevelResourceId.TryParse(templateID, out var id) is not true) {
       throw  new ConversionFailedException($"Unable to parse resourceId {templateID}", resource);
    }          
    var templateSpec = "ts:" + id.SubscriptionId + "/" + id.ResourceGroup + "/" + id.NameHierarchy.First() + ":" + id.NameHierarchy.Last();
  2. String interpolation usually offers a cleaner approach for building strings. Instead of:
    var templateSpec = "ts:" + id.SubscriptionId + "/" + id.ResourceGroup + "/" + id.NameHierarchy.First() + ":" + id.NameHierarchy.Last();
    You could write:
    var templateSpec = $"ts:{id.SubscriptionId}/{id.ResourceGroup}/{id.NameHierarchy.First()}:{id.NameHierarchy.Last()}";
  3. The is not true syntax you're using is a relatively new feature for C# - pattern matching. It's really powerful for some scenarios (such as checking for null), but checking a boolean value, it's simpler and more idiomatic to just reference it directly in the if statement. Instead of:
    if (ResourceGroupLevelResourceId.TryParse(templateID, out var id) is not true) {
    You could write:
    if (!ResourceGroupLevelResourceId.TryParse(templateID, out var id)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the code. Thanks!

Comment on lines 1411 to 1415
if ((pathProperty?.Value<string>() is not string && idProperty?.Value<string>() is not string ))
{
throw new ConversionFailedException($"Unable to find \"uri\" or \"relativePath\" properties under {resource["name"]}.properties.templateLink for linked template.", resource);
throw new ConversionFailedException($"Unable to find \"uri\" or \"relativePath\" or \"id\" properties under {resource["name"]}.properties.templateLink for linked template.", resource);
}
var templatePathString = idProperty?.Value<string>() ?? pathProperty?.Value<string>() ?? "";
Copy link
Member

Choose a reason for hiding this comment

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

I'd recommend restructuring this a bit (pulling your logic from line 1424 up), to cut down on the casts, usage of ?, improving variable naming, and avoiding conflating uses of the templatePathString property:

SyntaxBase modulePath;
Uri? jsonTemplateUri = null;
if (pathProperty?.Value<string>() is string templatePathString)
{
    modulePath = ...
}
else if (idProperty?.Value<string>() is string templateSpecIdString)
{
    modulePath = GetModuleFromId(templateSpecIdString, resource);
}
else
{
    throw new ConversionFailedException...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated it. Thanks!

ResourceGroupLevelResourceId id;
var templateSpec = "";
if (ResourceGroupLevelResourceId.TryParse(templateID, out id) is not true) {
throw new ConversionFailedException($"Unable to parse resourceId {templateID}", resource);
Copy link
Member

Choose a reason for hiding this comment

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

This exception is going to be shown to users - could you make it more descriptive? The context of why we're trying to parse this resourceId may not be clear.

Suggestion of something like the following: "Unable to interpret \"{templateID}\" as a valid template spec resource id under property {resource["name"]}.properties.templateLink.id"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated it. Thanks!!

@mishrapratikshya mishrapratikshya force-pushed the pratmishra/Fix3246 branch 2 times, most recently from 4d00906 to 8dfc69a Compare April 10, 2023 22:13
@mishrapratikshya mishrapratikshya merged commit c038655 into main Apr 10, 2023
@mishrapratikshya mishrapratikshya deleted the pratmishra/Fix3246 branch April 10, 2023 23:44
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