Azure 数据资源管理器是一项快速且高度可缩放的数据探索服务,适用于日志和遥测数据。 本文介绍如何使用 C#、Python 或 Azure 资源管理器 (ARM) 模板向 Azure 数据资源管理器添加数据库主体。
运行以下代码以添加数据库主体:
var tenantId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; //Directory (tenant) ID
var clientId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; //Application ID
var clientSecret = "PlaceholderClientSecret"; //Client Secret
var subscriptionId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";
var credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);
var resourceManagementClient = new ArmClient(credentials, subscriptionId);
var resourceGroupName = "testrg";
//The cluster that is created as part of the Prerequisites
var clusterName = "mykustocluster";
var databaseName = "mykustodatabase";
var subscription = await resourceManagementClient.GetDefaultSubscriptionAsync();
var resourceGroup = (await subscription.GetResourceGroupAsync(resourceGroupName)).Value;
var cluster = (await resourceGroup.GetKustoClusterAsync(clusterName)).Value;
var database = (await cluster.GetKustoDatabaseAsync(databaseName)).Value;
var databasePrincipalAssignments = database.GetKustoDatabasePrincipalAssignments();
var databasePrincipalAssignmentName = "mykustodatabaseprincipalassignment";
var principalId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; //User email, application ID, or security group name
var role = KustoDatabasePrincipalRole.Admin; //Admin, Ingestor, Monitor, User, UnrestrictedViewers, Viewer
var tenantIdForPrincipal = new Guid("xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx");
var principalType = KustoPrincipalAssignmentType.App; //User, App, or Group
var databasePrincipalAssignmentData = new KustoDatabasePrincipalAssignmentData
{
DatabasePrincipalId = principalId, Role = role, PrincipalType = principalType, TenantId = tenantIdForPrincipal
};
await databasePrincipalAssignments.CreateOrUpdateAsync(
WaitUntil.Completed, databasePrincipalAssignmentName, databasePrincipalAssignmentData
);
设置 |
建议的值 |
字段说明 |
tenantId |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
租户 ID。 也称为目录 ID。 |
subscriptionId |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
用于创建资源的订阅 ID。 |
clientId |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
可以访问租户中资源的应用程序的客户端 ID。 |
clientSecret |
PlaceholderClientSecret |
可以访问租户中资源的应用程序的客户端密码。 |
resourceGroupName |
testrg |
包含群集的资源组的名称。 |
clusterName |
mykustocluster |
群集的名称。 |
databaseName |
mykustodatabase |
数据库名称。 |
databasePrincipalAssignmentName |
mykustodatabaseprincipalassignment |
数据库主体资源的名称。 |
principalId |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
主体 ID,可以是用户电子邮件、应用程序 ID 或安全组名称。 |
角色 (role) |
管理员 |
数据库主体的角色,可以是“Admin”、“Ingestor”、“Monitor”、“User”、“UnrestrictedViewers”和“Viewer”。 |
tenantIdForPrincipal |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
主体的租户 ID。 |
principalType |
应用 |
主体的类型,可以是“User”、“App”或“Group” |
以下示例演示如何以编程方式添加数据库主体。
from azure.mgmt.kusto import KustoManagementClient
from azure.mgmt.kusto.models import DatabasePrincipalAssignment
from azure.common.credentials import ServicePrincipalCredentials
#Directory (tenant) ID
tenant_id = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"
#Application ID
client_id = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"
#Client Secret
client_secret = "xxxxxxxxxxxxxx"
subscription_id = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"
credentials = ServicePrincipalCredentials(
client_id=client_id,
secret=client_secret,
tenant=tenant_id
)
kusto_management_client = KustoManagementClient(credentials, subscription_id)
resource_group_name = "testrg"
#The cluster and database that is created as part of the Prerequisites
cluster_name = "mykustocluster"
database_name = "mykustodatabase"
principal_assignment_name = "clusterPrincipalAssignment1"
#User email, application ID, or security group name
principal_id = "xxxxxxxx"
#AllDatabasesAdmin, AllDatabasesMonitor or AllDatabasesViewer
role = "AllDatabasesAdmin"
tenant_id_for_principal = tenantId
#User, App, or Group
principal_type = "App"
#Returns an instance of LROPoller, check https://learn.microsoft.com/python/api/msrest/msrest.polling.lropoller?view=azure-python
poller = kusto_management_client.database_principal_assignments.create_or_update(resource_group_name=resource_group_name, cluster_name=cluster_name, database_name=database_name, principal_assignment_name= principal_assignment_name, parameters=DatabasePrincipalAssignment(principal_id=principal_id, role=role, tenant_id=tenant_id_for_principal, principal_type=principal_type))
设置 |
建议的值 |
字段说明 |
tenant_id |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
租户 ID。 也称为目录 ID。 |
subscription_id |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
用于创建资源的订阅 ID。 |
client_id |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
可以访问租户中资源的应用程序的客户端 ID。 |
client_secret |
xxxxxxxxxxxxxx |
可以访问租户中资源的应用程序的客户端密码。 |
resource_group_name |
testrg |
包含群集的资源组的名称。 |
cluster_name |
mykustocluster |
群集的名称。 |
database_name |
mykustodatabase |
数据库名称。 |
principal_assignment_name |
databasePrincipalAssignment1 |
数据库主体资源的名称。 |
principal_id |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
主体 ID,可以是用户电子邮件、应用程序 ID 或安全组名称。 |
角色 (role) |
管理员 |
数据库主体的角色,可以是“Admin”、“Ingestor”、“Monitor”、“User”、“UnrestrictedViewers”和“Viewer”。 |
tenant_id_for_principal |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
主体的租户 ID。 |
principal_type |
应用 |
主体的类型,可以是“User”、“App”或“Group” |
以下示例演示了用于添加数据库主体的 Azure 资源管理器模板。 可以使用此窗体在 Azure 门户中编辑和部署模板。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"principalAssignmentName": {
"type": "string",
"defaultValue": "principalAssignment1",
"metadata": {
"description": "Specifies the name of the principal assignment"
}
},
"clusterName": {
"type": "string",
"defaultValue": "mykustocluster",
"metadata": {
"description": "Specifies the name of the cluster"
}
},
"databaseName": {
"type": "string",
"defaultValue": "mykustodatabase",
"metadata": {
"description": "Specifies the name of the database"
}
},
"principalIdForDatabase": {
"type": "string",
"metadata": {
"description": "Specifies the principal id. It can be user email, application (client) ID, security group name"
}
},
"roleForDatabasePrincipal": {
"type": "string",
"defaultValue": "Admin",
"metadata": {
"description": "Specifies the database principal role. It can be 'Admin', 'Ingestor', 'Monitor', 'User', 'UnrestrictedViewers', 'Viewer'"
}
},
"tenantIdForDatabasePrincipal": {
"type": "string",
"metadata": {
"description": "Specifies the tenantId of the database principal"
}
},
"principalTypeForDatabase": {
"type": "string",
"defaultValue": "App",
"metadata": {
"description": "Specifies the principal type. It can be 'User', 'App', 'Group'"
}
}
},
"variables": {
},
"resources": [{
"type": "Microsoft.Kusto/Clusters/Databases/principalAssignments",
"apiVersion": "2019-11-09",
"name": "[concat(parameters('clusterName'), '/', parameters('databaseName'), '/', parameters('principalAssignmentName'))]",
"properties": {
"principalId": "[parameters('principalIdForDatabase')]",
"role": "[parameters('roleForDatabasePrincipal')]",
"tenantId": "[parameters('tenantIdForDatabasePrincipal')]",
"principalType": "[parameters('principalTypeForDatabase')]"
}
}
]
}