Skip to content

Commit

Permalink
Create rule S6975 (#3892)
Browse files Browse the repository at this point in the history
* Create rule S6975

* SONARIAC-1424 Split S6956 RSPEC into 2 rules

* Code review remarks

---------

Co-authored-by: mstachniuk <[email protected]>
Co-authored-by: Marcin Stachniuk <[email protected]>
  • Loading branch information
3 people authored Apr 18, 2024
1 parent 173a43b commit 848e7c2
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 79 deletions.
2 changes: 1 addition & 1 deletion rules/S6956/azureresourcemanager/metadata.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
83 changes: 5 additions & 78 deletions rules/S6956/azureresourcemanager/rule.adoc
Original file line number Diff line number Diff line change
@@ -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]
----
Expand All @@ -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]
----
Expand All @@ -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

Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions rules/S6975/azureresourcemanager/metadata.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
104 changes: 104 additions & 0 deletions rules/S6975/azureresourcemanager/rule.adoc
Original file line number Diff line number Diff line change
@@ -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[]
2 changes: 2 additions & 0 deletions rules/S6975/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 848e7c2

Please sign in to comment.