Python 用 Azure DNS ライブラリ

概要

Azure DNS は、DNS ドメインのホスティング サービスであり、Azure インフラストラクチャを使用して DNS 解決を実行します。

Azure DNS の概要については、「Azure Portal で Azure DNS の使用を開始する」を参照してください。

管理 API

pip install azure-mgmt-dns

管理クライアントを作成する

管理クライアントのインスタンスは、以下のコードで作成します。

サブスクリプションの一覧から取得できる をsubscription_id指定する必要があります。

Python SDK を使用した Azure Active Directory の認証処理と Credentials インスタンスの作成について詳しくは、「Resource Management Authentication (リソース管理の認証)」を参照してください。

from azure.mgmt.dns import DnsManagementClient
from azure.common.credentials import UserPassCredentials

# Replace this with your subscription id
subscription_id = '33333333-3333-3333-3333-333333333333'

# See above for details on creating different types of AAD credentials
credentials = UserPassCredentials(
	'user@domain.com',  # Your user
	'my_password',      # Your password
)

dns_client = DnsManagementClient(
	credentials,
	subscription_id
)

DNS ゾーンの作成

# The only valid value is 'global', otherwise you will get a:
# The subscription is not registered for the resource type 'dnszones' in the location 'westus'.
zone = dns_client.zones.create_or_update(
	'MyResourceGroup',
	'pydns.com',
	{
	        'zone_type': 'Public', # or Private
		'location': 'global'
	}
)

レコード セットの作成

record_set = dns_client.record_sets.create_or_update(
	'MyResourceGroup',
	'pydns.com',
	'MyRecordSet',
	'A',
	{
			"ttl": 300,
			"arecords": [
				{
				"ipv4_address": "1.2.3.4"
				},
				{
				"ipv4_address": "1.2.3.5"
				}
			]
	}
)