Skip to content

Commit

Permalink
Move null name handling to param definition
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbud committed May 13, 2024
1 parent 1e84cbf commit 65aa2b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
1 change: 1 addition & 0 deletions avm/ptn/authorization/resource-role-assignment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ The unique guid name for the role assignment.

- Required: No
- Type: string
- Default: `[guid(parameters('resourceId'), parameters('principalId'), if(contains(parameters('roleDefinitionId'), '/providers/Microsoft.Authorization/roleDefinitions/'), parameters('roleDefinitionId'), subscriptionResourceId('Microsoft.Authorization/roleDefinitions', parameters('roleDefinitionId'))))]`

### Parameter: `principalType`

Expand Down
18 changes: 8 additions & 10 deletions avm/ptn/authorization/resource-role-assignment/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ metadata owner = 'Azure/module-maintainers'
param resourceId string

@sys.description('Optional. The unique guid name for the role assignment.')
param name string?
param name string = guid(
resourceId,
principalId,
contains(roleDefinitionId, '/providers/Microsoft.Authorization/roleDefinitions/')
? roleDefinitionId
: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', roleDefinitionId)
)

@sys.description('Required. The role definition ID for the role assignment.')
param roleDefinitionId string
Expand Down Expand Up @@ -69,15 +75,7 @@ resource resourceRoleAssignment 'Microsoft.Resources/deployments@2023-07-01' = {
value: resourceId
}
name: {
value: !empty(name)
? name
: guid(
resourceId,
principalId,
contains(roleDefinitionId, '/providers/Microsoft.Authorization/roleDefinitions/')
? roleDefinitionId
: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', roleDefinitionId)
)
value: name
}
roleDefinitionId: {
value: contains(roleDefinitionId, '/providers/Microsoft.Authorization/roleDefinitions/')
Expand Down
19 changes: 9 additions & 10 deletions avm/ptn/authorization/resource-role-assignment/main.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.26.170.59819",
"templateHash": "9533603348987584092"
"templateHash": "12472585536276024107"
},
"name": "ResourceRole Assignments (All scopes)",
"description": "This module deploys a Role Assignment for a specific resource.",
Expand All @@ -21,7 +20,7 @@
},
"name": {
"type": "string",
"nullable": true,
"defaultValue": "[guid(parameters('resourceId'), parameters('principalId'), if(contains(parameters('roleDefinitionId'), '/providers/Microsoft.Authorization/roleDefinitions/'), parameters('roleDefinitionId'), subscriptionResourceId('Microsoft.Authorization/roleDefinitions', parameters('roleDefinitionId'))))]",
"metadata": {
"description": "Optional. The unique guid name for the role assignment."
}
Expand Down Expand Up @@ -133,8 +132,8 @@
}
}
},
"resources": {
"avmTelemetry": {
"resources": [
{
"condition": "[parameters('enableTelemetry')]",
"type": "Microsoft.Resources/deployments",
"apiVersion": "2023-07-01",
Expand All @@ -154,7 +153,7 @@
}
}
},
"resourceRoleAssignment": {
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2023-07-01",
"name": "[format('{0}-ResourceRoleAssignment', guid(parameters('resourceId'), parameters('principalId'), parameters('roleDefinitionId')))]",
Expand All @@ -169,7 +168,7 @@
"value": "[parameters('resourceId')]"
},
"name": {
"value": "[if(not(empty(parameters('name'))), parameters('name'), guid(parameters('resourceId'), parameters('principalId'), if(contains(parameters('roleDefinitionId'), '/providers/Microsoft.Authorization/roleDefinitions/'), parameters('roleDefinitionId'), subscriptionResourceId('Microsoft.Authorization/roleDefinitions', parameters('roleDefinitionId')))))]"
"value": "[parameters('name')]"
},
"roleDefinitionId": {
"value": "[if(contains(parameters('roleDefinitionId'), '/providers/Microsoft.Authorization/roleDefinitions/'), parameters('roleDefinitionId'), subscriptionResourceId('Microsoft.Authorization/roleDefinitions', parameters('roleDefinitionId')))]"
Expand All @@ -186,7 +185,7 @@
}
}
}
},
],
"outputs": {
"name": {
"type": "string",
Expand All @@ -207,7 +206,7 @@
"metadata": {
"description": "The resource ID of the Role Assignment."
},
"value": "[reference('resourceRoleAssignment').outputs.roleAssignmentId.value]"
"value": "[reference(resourceId('Microsoft.Resources/deployments', format('{0}-ResourceRoleAssignment', guid(parameters('resourceId'), parameters('principalId'), parameters('roleDefinitionId')))), '2023-07-01').outputs.roleAssignmentId.value]"
},
"resourceGroupName": {
"type": "string",
Expand All @@ -217,4 +216,4 @@
"value": "[resourceGroup().name]"
}
}
}
}

0 comments on commit 65aa2b9

Please sign in to comment.