使用Bicep的Azure SQL故障转移组

x0fgdtte  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(120)

我正在尝试使用Bicep模板实现Azure SQL故障转移组。我遇到的问题是,我无法弄清楚如何引用作为故障转移组的数据库属性中的参数传递的数据库数组。当将数组值赋给数据库属性时,它给了我一个错误,即期望字符串值,而赋值的是数组,这当然是不正确的。

resource symbolicname 'Microsoft.Sql/servers/failoverGroups@2021-11-01-preview' = {
  name: 'string'
  tags: {
    tagName1: 'tagValue1'
    tagName2: 'tagValue2'
  }
  parent: resourceSymbolicName
  properties: {
    databases: [
      'string'
    ]
    partnerServers: [
      {
        id: 'string'
      }
    ]
    readWriteEndpoint: {
      failoverPolicy: 'string'
      failoverWithDataLossGracePeriodMinutes: int
    }
  }
}

有没有人有任何使用Bicep模板的Azure SQL故障转移组的工作示例,或者在解决方案上为我指出正确的方向?
编辑1 -主要。二头肌:这是我在添加托马斯的更改后尝试的代码:

param databases array
param primarySqlServerName string
param drSqlServerName string
param failovergroupName string

module primarySql '***************' = {
  Deploy primary SQL Server and database
}
module drSql '****************' = {
  Deploy secondary SQL Server
}

resource sqlServerFailoverGroup 'Microsoft.Sql/servers/failoverGroups@2020-11-01-preview' = {
  name: '${primarySqlServerName}/${failovergroupName}'
  properties: {
    databases: [for database in databases: resourceId('Microsoft.Sql/servers/databases', primarySqlServerName, database)]
    readWriteEndpoint: {
      failoverPolicy: 'Automatic'
      failoverWithDataLossGracePeriodMinutes: 60
    }
    readOnlyEndpoint: {
      failoverPolicy: 'Enabled'
    }
    partnerServers: [
      {
        id: resourceId('Microsoft.Sql/servers', drSqlServerName)
      }
    ]
  }
  dependsOn: [
    primarySql
    drSql
  ]
}

在上面部署时获取下面的错误:
无法处理行“% 1”和列“1289”处的资源的模板语言表达式。'无法计算模板语言函数' resourceId ':所有函数参数必须是字符串文字。请访问aka.ms/arm-template-expressions/#resourceid了解使用详情。

axr492tv

axr492tv1#

databases属性是一个of字符串。partnerServers属性是一个对象数组。
两者都需要数据库和伙伴服务器的资源ID。
下面是一个使用故障转移组部署2SQLServer和数据库的简单示例
sql.bicep:

param location string = resourceGroup().location
param sqlServerName string
param sqlServerPrincipalType string
param sqlServerAadAdminName string
param sqlServerAadAdminId string
param databaseNames array

// Create the Server
resource sqlServer 'Microsoft.Sql/servers@2020-11-01-preview' = {
  name: sqlServerName
  location: location
  tags: {}
  properties: {
    administrators: {
      administratorType: 'ActiveDirectory'
      principalType: sqlServerPrincipalType
      login: sqlServerAadAdminName
      sid: sqlServerAadAdminId
      azureADOnlyAuthentication: true
    }
  }
}

// We create the database only in the primary region
resource database 'Microsoft.Sql/servers/databases@2020-08-01-preview' = [for databaseName in databaseNames: if (!empty(databaseNames)) {
  name: empty(databaseNames) ? 'placeholder' : databaseName
  parent: sqlServer
  location: location
  sku: {
    name: 'S0'
    tier: 'Standard'
  }
  properties: {
    sourceDatabaseId: sqlServer.id
  }
}]

和主.bicep文件:

param failoverGroupName string
param primarySqlServerName string
param primaryLocation string
param secondarySqlServerName string
param secondaryLocation string
param sqlServerPrincipalType string
param sqlServerAadAdminName string
param sqlServerAadAdminId string

param databaseNames array

module primarySql 'sql.bicep' = {
  name: 'primarySql'
  params: {
    location: primaryLocation
    sqlServerName: primarySqlServerName
    sqlServerPrincipalType: sqlServerPrincipalType
    sqlServerAadAdminName: sqlServerAadAdminName
    sqlServerAadAdminId: sqlServerAadAdminId
    databaseNames: databaseNames
  }
}

module secondarySql 'sql.bicep' = {
  name: 'secondarySql'
  params: {
    location: secondaryLocation
    sqlServerName: secondarySqlServerName
    sqlServerPrincipalType: sqlServerPrincipalType
    sqlServerAadAdminName: sqlServerAadAdminName
    sqlServerAadAdminId: sqlServerAadAdminId
    databaseNames: []
  }
}

resource sqlServerFailoverGroup 'Microsoft.Sql/servers/failoverGroups@2020-11-01-preview' = {
  name: '${primarySqlServerName}/${failoverGroupName}'
  dependsOn: [ primarySql, secondarySql ]
  properties: {
    databases: [for dataBaseName in databaseNames: resourceId('Microsoft.Sql/servers/databases', primarySqlServerName, dataBaseName)]
    readWriteEndpoint: {
      failoverPolicy: 'Automatic'
      failoverWithDataLossGracePeriodMinutes: 60
    }
    readOnlyEndpoint: {
      failoverPolicy: 'Enabled'
    }
    partnerServers: [
      {
        id: resourceId(resourceGroup().name, 'Microsoft.Sql/servers', secondarySqlServerName)
      }
    ]
  }
}

相关问题