😀

Azure SDK for Node.jsでSQL Databaseの情報取得

2022/08/07に公開

Sample Script

npm install @azure/arm-sql
npm install @azure/identity
const { SqlManagementClient } = require("@azure/arm-sql");
const { AzureCliCredential } = require("@azure/identity");

async function main() {
    const credential = new AzureCliCredential();
    const subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
    const client = new SqlManagementClient(credential, subscriptionId);

    const resourceGoupName = '<resource group>';
    const serverName = '<sql server>';

    const resArray = new Array();
    for await (let item of client.databases.listByServer(resourceGoupName, serverName)) {
      resArray.push(item);
    }
    console.log(resArray);
}

main();

実行

※ 事前に az login で認証しておく

$ node sql_db_list.js
[
  {
    id: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/<resource group>/providers/Microsoft.Sql/servers/tsaeki0807/databases/tsaeki0807',
    name: 'tsaeki0807',
    type: 'Microsoft.Sql/servers/databases',
    location: 'japaneast',
    sku: { name: 'Basic', tier: 'Basic', capacity: 5 },
    kind: 'v12.0,user',
    collation: 'SQL_Latin1_General_CP1_CI_AS',
    maxSizeBytes: 2147483648,
    status: 'Online',
    databaseId: '',
    creationDate: 2022-08-07T05:08:39.437Z,
    currentServiceObjectiveName: 'Basic',
    requestedServiceObjectiveName: 'Basic',
    defaultSecondaryLocation: 'japanwest',
    catalogCollation: 'SQL_Latin1_General_CP1_CI_AS',
    zoneRedundant: false,
    readScale: 'Disabled',
    currentSku: { name: 'Basic', tier: 'Basic', capacity: 5 },
    currentBackupStorageRedundancy: 'Geo',
    requestedBackupStorageRedundancy: 'Geo',
    maintenanceConfigurationId: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Maintenance/publicMaintenanceConfigurations/SQL_Default',
    isLedgerOn: false,
    isInfraEncryptionEnabled: false
  },
  {
    id: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/<resource group>/providers/Microsoft.Sql/servers/tsaeki0807/databases/master',
    name: 'master',
    type: 'Microsoft.Sql/servers/databases',
    location: 'japaneast',
    sku: { name: 'Basic', tier: 'Basic', capacity: 5 },
    kind: 'v12.0,system',
    managedBy: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/<resource group>/providers/Microsoft.Sql/servers/tsaeki0807',
    collation: 'SQL_Latin1_General_CP1_CI_AS',
    maxSizeBytes: 53687091200,
    status: 'Online',
    databaseId: '',
    creationDate: 2022-08-07T05:06:29.180Z,
    currentServiceObjectiveName: 'Basic',
    requestedServiceObjectiveName: 'Basic',
    defaultSecondaryLocation: 'japanwest',
    catalogCollation: 'SQL_Latin1_General_CP1_CI_AS',
    zoneRedundant: false,
    readScale: 'Disabled',
    currentSku: { name: 'Basic', tier: 'Basic', capacity: 5 },
    currentBackupStorageRedundancy: 'Local',
    requestedBackupStorageRedundancy: 'Local',
    maintenanceConfigurationId: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Maintenance/publicMaintenanceConfigurations/SQL_Default',
    isLedgerOn: false,
    isInfraEncryptionEnabled: false
  }
]

メモ

参考

https://www.npmjs.com/package/@azure/arm-sql
https://www.npmjs.com/package/@azure/identity
https://docs.microsoft.com/en-us/azure/developer/javascript/azure-sdk-library-package-index
https://docs.microsoft.com/en-us/rest/api/sql/2021-11-01-preview/databases/list-by-server?tabs=HTTP

Discussion