Skip to content

Commit

Permalink
Update S1192 String literals should not be duplicated, add ARM
Browse files Browse the repository at this point in the history
  • Loading branch information
mstachniuk committed Mar 20, 2024
1 parent cad20a8 commit 943ce5b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 22 deletions.
4 changes: 4 additions & 0 deletions rules/S1192/azureresourcemanager/exceptions-arm.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The following are ignored:

* literals with fewer than 5 characters
* The `apiVersion` of a resource (see rule S6893)
2 changes: 2 additions & 0 deletions rules/S1192/azureresourcemanager/howtofix-arm.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Instead, use variables to replace the duplicated string literals.
Variables can be referenced from many places, but only need to be updated in a single place.
124 changes: 102 additions & 22 deletions rules/S1192/azureresourcemanager/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,44 +1,124 @@
FIXME: add a description
== Why is this an issue?

// If you want to factorize the description uncomment the following line and create the file.
//include::../description.adoc[]
include::../description.adoc[]

== Why is this an issue?
=== Exceptions

include::exceptions-arm.adoc[]

== How to fix it in ARM templates

include::howtofix-arm.adoc[]

=== Code examples

==== Noncompliant code example

With the default threshold of 3:

[source,json,diff-id=1,diff-type=noncompliant]
----
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-01-01",
"name": "appSuperStorage",
"tags": {
"displayName": "appSuperStorage",
"shortName" : "appSuperStorage"
}
}
]
}
----

FIXME: remove the unused optional headers (that are commented out)
==== Compliant solution

[source,json,diff-id=1,diff-type=compliant]
----
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"storageAccountName": "appSuperStorage"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-01-01",
"name": "[variables('storageAccountName')]",
"tags": {
"displayName": "[variables('storageAccountName')]",
"shortName" : "[variables('storageAccountName')]"
}
}
]
}
----

//=== What is the potential impact?
== How to fix it in Bicep

== How to fix it
//== How to fix it in FRAMEWORK NAME
include::howtofix-arm.adoc[]

=== Code examples

==== Noncompliant code example

[source,text,diff-id=1,diff-type=noncompliant]
With the default threshold of 3:

[source,bicep,diff-id=2,diff-type=noncompliant]
----
FIXME
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-01-01' = {
name: 'appSuperStorage'
tags: {
displayName: 'appSuperStorage'
shortName: 'appSuperStorage'
}
}
----

==== Compliant solution

[source,text,diff-id=1,diff-type=compliant]
[source,bicep,diff-id=2,diff-type=compliant]
----
FIXME
var storageAccountName = 'appSuperStorage'
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-01-01' = {
name: storageAccountName
tags: {
displayName: storageAccountName
shortName: storageAccountName
}
}
----

//=== How does this work?
== Resources

== Documentation

* Microsoft - https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/best-practices#variables[ARM template best practices]
* Microsoft - https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/best-practices[Best practices for Bicep]

=== Related rules

* S6893 - Use a constant value for the apiVersion

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

//=== Pitfalls
include::../parameters.adoc[]

//=== Going the extra mile
include::../highlighting.adoc[]

'''

//== Resources
//=== Documentation
//=== Articles & blog posts
//=== Conference presentations
//=== Standards
//=== External coding guidelines
//=== Benchmarks
endif::env-github,rspecator-view[]

0 comments on commit 943ce5b

Please sign in to comment.