@@ -20,7 +20,26 @@ locals {
2020 ]) : toset ([])
2121
2222 hana_lb_rules_ports_secondary = local. create_active_active_infra == 1 ? local. hana_lb_rules_ports : toset ([])
23+
2324 hostname = var. common_variables [" deployment_name_in_hostname" ] ? format (" %s-%s" , var. common_variables [" deployment_name" ], var. name ) : var. name
25+ hostnames_hana = [ for h in range (var. hana_count ) : format (" %s%02d" , local. hostname , h + 1 ) ]
26+ hostname_mm = format (" %s%s" , local. hostname , " mm" )
27+ hostnames = local. create_scale_out == 1 ? concat (local. hostnames_hana , [local . hostname_mm ]) : local. hostnames_hana
28+
29+ principal_ids = concat (azurerm_virtual_machine. hana . * . identity . 0 . principal_id , [module . hana_majority_maker . fence_principal_id ])
30+ fence_scopes = flatten ([
31+ for c in range (var. hana_count + local. create_scale_out ) : [
32+ for n in local . hostnames : {
33+ count = c
34+ node = element (local. hostnames , c)
35+ principal = element (local. principal_ids , c)
36+ scope = format (" /subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s" , data. azurerm_subscription . current . subscription_id , var. resource_group_name , n)
37+ }
38+ ]
39+ ])
40+ }
41+
42+ data "azurerm_subscription" "current" {
2443}
2544
2645resource "azurerm_availability_set" "hana-availability-set" {
@@ -401,7 +420,7 @@ resource "azurerm_virtual_machine" "hana" {
401420 }
402421
403422 os_profile {
404- computer_name = " ${ local . hostname } ${ format ( " %02d " , count. index + 1 ) } "
423+ computer_name = element ( local. hostnames_hana , count. index )
405424 admin_username = var. common_variables [" authorized_user" ]
406425 }
407426
@@ -419,15 +438,48 @@ resource "azurerm_virtual_machine" "hana" {
419438 storage_uri = var. storage_account
420439 }
421440
441+ identity {
442+ type = " SystemAssigned"
443+ }
444+
422445 tags = {
423446 workspace = var.common_variables[" deployment_name" ]
424447 }
425448}
426449
450+ resource "azurerm_role_definition" "fence" {
451+ count = var. common_variables [" hana" ][" ha_enabled" ] && var. common_variables [" hana" ][" fencing_mechanism" ] == " native" ? 1 : 0
452+ name = " role-fence-${ local . hostname } "
453+ # It is recommended to use the first entry of the assignable_scopes.
454+ # https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_definition#scope
455+ scope = format (" /subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s%02d" , data. azurerm_subscription . current . subscription_id , var. resource_group_name , local. hostname , count. index + 1 )
456+
457+ permissions {
458+ actions = [
459+ " Microsoft.Compute/*/read" ,
460+ " Microsoft.Compute/virtualMachines/powerOff/action" ,
461+ " Microsoft.Compute/virtualMachines/restart/action" ,
462+ " Microsoft.Compute/virtualMachines/start/action"
463+ ]
464+ not_actions = []
465+ }
466+
467+ assignable_scopes = [
468+ for h in local . hostnames : format (" /subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s" , data. azurerm_subscription . current . subscription_id , var. resource_group_name , h)
469+ ]
470+ }
471+
472+ resource "azurerm_role_assignment" "host" {
473+ for_each = { for r in local . fence_scopes : " ${ r . node } _${ r . scope } " => r if var . common_variables [" hana" ][" ha_enabled" ] && var . common_variables [" hana" ][" fencing_mechanism" ] == " native" }
474+ scope = each. value . scope
475+ role_definition_id = element (azurerm_role_definition. fence . * . role_definition_resource_id , 0 )
476+ principal_id = each. value . principal
477+ }
478+
427479module "hana_majority_maker" {
428480 source = " ../majority_maker_node"
429481 node_count = local. create_scale_out
430- name = var . name
482+ name = local . hostname
431483 common_variables = var. common_variables
432484 bastion_host = var. bastion_host
433485 az_region = var. az_region
@@ -446,11 +498,6 @@ module "hana_majority_maker" {
446498 cluster_ssh_key = var. cluster_ssh_key
447499 os_image = var. os_image
448500 iscsi_srv_ip = var. iscsi_srv_ip
449- # only used by azure fence agent (native fencing)
450- subscription_id = var. subscription_id
451- tenant_id = var. tenant_id
452- fence_agent_app_id = var. fence_agent_app_id
453- fence_agent_client_secret = var. fence_agent_client_secret
454501}
455502
456503module "hana_on_destroy" {
0 commit comments