From 848e7c20486187129a05ddec4b00ce2339b7db17 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 18 Apr 2024 16:58:16 +0200 Subject: [PATCH] Create rule S6975 (#3892) * Create rule S6975 * SONARIAC-1424 Split S6956 RSPEC into 2 rules * Code review remarks --------- Co-authored-by: mstachniuk Co-authored-by: Marcin Stachniuk --- .../S6956/azureresourcemanager/metadata.json | 2 +- rules/S6956/azureresourcemanager/rule.adoc | 83 +------------- .../S6975/azureresourcemanager/metadata.json | 23 ++++ rules/S6975/azureresourcemanager/rule.adoc | 104 ++++++++++++++++++ rules/S6975/metadata.json | 2 + 5 files changed, 135 insertions(+), 79 deletions(-) create mode 100644 rules/S6975/azureresourcemanager/metadata.json create mode 100644 rules/S6975/azureresourcemanager/rule.adoc create mode 100644 rules/S6975/metadata.json diff --git a/rules/S6956/azureresourcemanager/metadata.json b/rules/S6956/azureresourcemanager/metadata.json index 15ae22b2aec..5031102ade8 100644 --- a/rules/S6956/azureresourcemanager/metadata.json +++ b/rules/S6956/azureresourcemanager/metadata.json @@ -1,5 +1,5 @@ { - "title": "The properties and elements inside a template should appear in the recommended order", + "title": "The properties should appear in the recommended order", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/rules/S6956/azureresourcemanager/rule.adoc b/rules/S6956/azureresourcemanager/rule.adoc index 7eb9d5bd4d3..e0e91a29278 100644 --- a/rules/S6956/azureresourcemanager/rule.adoc +++ b/rules/S6956/azureresourcemanager/rule.adoc @@ -1,16 +1,14 @@ == Why is this an issue? -According to the best practices defined by Azure, a consistent order of properties and elements in a templates is recommended. +According to the best practices defined by Azure, a consistent order of properties in a templates is recommended. This makes it easier to read and understand the template. Not following this convention has no technical impact, but will reduce the template's readability because most developers are used to the standard order. -Sorting the resources according to deployment order is recommended as well, as this will convey the intent of the orchestration. - == How to fix it in ARM Templates -*Recommended order of the top-level template properties*: +*Recommended order of properties*: [source,json] ---- @@ -27,40 +25,9 @@ Sorting the resources according to deployment order is recommended as well, as t } ---- -*Recommended order of the resource properties*: - -[source,json] ----- -{ - "resources": [ - { - "comments": "if any", - "condition": true, - "scope": "% parent scope %", - "type": "Microsoft.Compute/virtualMachines", - "apiVersion": "2023-09-01", - "name": "resourceName", - "location": "[parameters('location')]", - "zones": [], - "sku": {}, - "kind": "", - "scale": "", - "plan": {}, - "identity": {}, - "copy": {}, - "dependsOn": [], - "tags": {}, - "properties": {} - } - ] -} ----- - -Any other properties or elements not listed here should be placed before the `properties` object for the resource. - == How to fix it in Bicep -*Recommended order of the top-level template properties*: +*Recommended order of elements*: [source,bicep] ---- @@ -75,31 +42,6 @@ module ... output ... ---- -*Recommended order of the resource properties and decorators*: - -[source,bicep] ----- -@description -@batchSize -resource resourceName - parent - scope - name - location/extendedLocation - zones - sku - kind - scale - plan - identity - dependsOn - tags - properties ----- - -Any other decorated not listed here should be placed before the `resource` object and after the other decorators. -Any other properties or elements not listed here should be placed before the `properties` object for the resource. - == Resources === Documentation @@ -115,26 +57,11 @@ ifdef::env-github,rspecator-view[] === Message -In case of wrong order of top-level and resource elements: - -* Reorder the elements to match the recommended order. - -In case of wrong order in decorators: - -Reorder the decorators to match the recommended order. +Reorder the elements to match the recommended order. === Highlighting -In general, we want to highlight all the keys of elements that are in the wrong order. - -For wrong order in top-level elements: - -* The first wrongly ordered key should be highlighted as primary issue and the others as secondary locations. - -For wrong order in a resource: - -* The name of the resource / key of the resource should be highlighted as a primary issues. -All the wrongly ordered key should be highlighted as secondary locations. +In general, we want to highlight the first key that is in the wrong order. ''' == Comments And Links diff --git a/rules/S6975/azureresourcemanager/metadata.json b/rules/S6975/azureresourcemanager/metadata.json new file mode 100644 index 00000000000..8959f04d70c --- /dev/null +++ b/rules/S6975/azureresourcemanager/metadata.json @@ -0,0 +1,23 @@ +{ + "title": "The resource elements should appear in the recommended order", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-6975", + "sqKey": "S6975", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S6975/azureresourcemanager/rule.adoc b/rules/S6975/azureresourcemanager/rule.adoc new file mode 100644 index 00000000000..f92d2164487 --- /dev/null +++ b/rules/S6975/azureresourcemanager/rule.adoc @@ -0,0 +1,104 @@ +== Why is this an issue? + +According to the best practices defined by Azure, a consistent order of elements in a templates is recommended. +This makes it easier to read and understand the template. + +Not following this convention has no technical impact, +but will reduce the template's readability because most developers are used to the standard order. + +== How to fix it in ARM Templates + +*Recommended order of the resource elements*: + +[source,json] +---- +{ + "resources": [ + { + "comments": "if any", + "condition": true, + "scope": "% parent scope %", + "type": "Microsoft.Compute/virtualMachines", + "apiVersion": "2023-09-01", + "name": "resourceName", + "location": "[parameters('location')]", + "zones": [], + "sku": {}, + "kind": "", + "scale": "", + "plan": {}, + "identity": {}, + "copy": {}, + "dependsOn": [], + "tags": {}, + "properties": {} + } + ] +} +---- + +Any other properties not listed here should be placed before the `properties` object for the resource. + +== How to fix it in Bicep + +*Recommended order of the resource elements and decorators*: + +[source,bicep] +---- +@description +@batchSize +resource resourceName + parent + scope + name + location/extendedLocation + zones + sku + kind + scale + plan + identity + dependsOn + tags + properties +---- + +Any other decorated not listed here should be placed before the `resource` object and after the other decorators. +Any other elements not listed here should be placed before the `properties` object for the resource. + +== Resources +=== Documentation + +* Azure quickstart templates best practices - https://github.com/Azure/azure-quickstart-templates/blob/master/1-CONTRIBUTION-GUIDE/best-practices.md#sort-order-of-properties[Sort order of properties] + +ifdef::env-github,rspecator-view[] + +''' +== Implementation Specification +(visible only on this page) + +=== Message + +In case of wrong order of resource elements: + +* Reorder the elements to match the recommended order. + +In case of wrong order in decorators: + +* Reorder the decorators to match the recommended order. + +=== Highlighting + +For wrong order in a resource: + +We want to highlight the first key that is in the wrong order. + +For wrong order decorators: + +We want to highlight the first decorator that is in the wrong order. + +''' +== Comments And Links +(visible only on this page) + +endif::env-github,rspecator-view[] diff --git a/rules/S6975/metadata.json b/rules/S6975/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S6975/metadata.json @@ -0,0 +1,2 @@ +{ +}