Azure 資料總管是一項快速又可高度調整的資料探索服務,可用於處理記錄和遙測資料。 在本文中,您將瞭解如何使用 C#、Python 或 Azure Resource Manager (ARM) 範本新增 Azure Data Explorer資料庫主體。
執行下列程式碼以新增資料庫主體:
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 |
您的租用戶識別碼。 也稱為目錄識別碼。 |
subscriptionId |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
您用來建立資源的訂用帳戶識別碼。 |
clientId |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
應用程式的用戶端識別碼,可存取您租用戶中的資源。 |
clientSecret |
PlaceholderClientSecret |
應用程式的用戶端密碼,可以存取您租用戶中的資源。 |
resourceGroupName |
testrg |
包含叢集的資源群組名稱。 |
clusterName |
mykustocluster |
您叢集的名稱。 |
databaseName |
mykustodatabase |
您的資料庫名稱。 |
databasePrincipalAssignmentName |
mykustodatabaseprincipalassignment |
資料庫主體資源的名稱。 |
principalId |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
主體識別碼,可以是使用者電子郵件、應用程式識別碼或安全性群組名稱。 |
角色 (role) |
管理員 |
資料庫主體的角色,可以是 '管理員'、'Ingestor'、'Monitor'、'User'、'UnrestrictedViewers'、'Viewer'。 |
tenantIdForPrincipal |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
主體的租使用者識別碼。 |
principalType |
App |
主體的類型,可以是 '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 |
您的租用戶識別碼。 也稱為目錄識別碼。 |
subscription_id |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
您用來建立資源的訂用帳戶識別碼。 |
client_id |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
應用程式的用戶端識別碼,可存取您租用戶中的資源。 |
client_secret |
xxxxxxxxxxxxxx |
應用程式的用戶端密碼,可以存取您租用戶中的資源。 |
resource_group_name |
testrg |
包含叢集的資源群組名稱。 |
cluster_name |
mykustocluster |
您叢集的名稱。 |
database_name |
mykustodatabase |
您的資料庫名稱。 |
principal_assignment_name |
databasePrincipalAssignment1 |
資料庫主體資源的名稱。 |
principal_id |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
主體識別碼,可以是使用者電子郵件、應用程式識別碼或安全性群組名稱。 |
角色 (role) |
管理員 |
資料庫主體的角色,可以是 '管理員'、'Ingestor'、'Monitor'、'User'、'UnrestrictedViewers'、'Viewer'。 |
tenant_id_for_principal |
xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx |
主體的租使用者識別碼。 |
principal_type |
App |
主體的類型,可以是 'User'、'App' 或 'Group' |
下列範例顯示用於新增資料庫主體的 Azure Resource Manager 範本。 您可以使用表單,在 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')]"
}
}
]
}