如何使用 python 脚本在 GCP 中创建 DNS 记录集
How to create DNS record-set in GCP using python script
我正在尝试开发一个 Python 自动化脚本,将“A”类型的 DNS 记录集添加到我现有的 GCP DNS 托管区域“my-sites”中
import json
from google.oauth2 import service_account
from google.cloud import dns
from google.cloud.exceptions import NotFound
gcp_dns_credentials={
"type": "service_account",
"project_id": "mygcpprojectid-1122",
"private_key_id": "myprivkeyid",
"private_key": "-----BEGIN PRIVATE KEY-----\nmyprivatekey\n-----END PRIVATE KEY-----\n",
"client_email": "client-mail@mygcpprojectid-1122.iam.gserviceaccount.com",
"client_id": "myclientid",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/client-mail%40mygcpprojectid-1122.iam.gserviceaccount.com"
}
project_id="mygcpprojectid-1122"
zone_name="my-sites"
dns_credentials = service_account.Credentials.from_service_account_info(gcp_dns_credentials)
client = dns.Client(project=project_id,credentials=dns_credentials)
zone = client.zone(zone_name)
create_records=dns.resource_record_set.ResourceRecordSet(name="mydnsrecord2.mygcpproject.com",record_type="A",ttl=300,rrdatas=["13.66.xx.xx"],zone=zone)
此脚本执行既不会抛出错误也不会创建 DNS 记录集。
我提到了这个文档 - https://cloud.google.com/python/docs/reference/dns/latest/resource-record-set
谁能帮帮我:)
没有报错,因为在Google Cloud DNS端还没有做任何事情。
DNS 更改是自动进行的,这意味着您可以进行多项更改(添加、删除等)并一次应用所有更改。所有更改生效或 none 执行(回滚)。
DNS 操作是通过更改集执行的。这意味着创建更改列表(例如创建/修改/删除资源记录)。
add_record_set() 方法追加到变更集 link.
create() 方法应用变更集 link。此方法实际上是修改您的 DNS 服务器资源记录。
只是用 python 代码重申@JohnHanley 解决方案
from google.oauth2 import service_account
from googleapiclient import discovery
gcp_dns_credentials={
"blah blah": "all dummy credentials in json format already mentioned in the question "
}
project_id="mygcpprojectid-1122"
zone_name="my-sites"
credentials = service_account.Credentials.from_service_account_info(gcp_dns_credentials)
service = discovery.build('dns', 'v1', credentials=credentials)
change_body = {
"additions": [
{
"name": "mydnsrecord2.mygcpproject.com.",
"type": "A",
"ttl": 300,
"rrdata": ["13.66.xx.xx"]
}
]
}
request = service.changes().create(project=project_id, managedZone=zone_name, body=change_body)
response = request.execute()
此脚本执行将创建 mydnsrecord2.mygcpproject.com
record-set
引用了此文档https://cloud.google.com/dns/docs/reference/v1/changes/create#python
我正在尝试开发一个 Python 自动化脚本,将“A”类型的 DNS 记录集添加到我现有的 GCP DNS 托管区域“my-sites”中
import json
from google.oauth2 import service_account
from google.cloud import dns
from google.cloud.exceptions import NotFound
gcp_dns_credentials={
"type": "service_account",
"project_id": "mygcpprojectid-1122",
"private_key_id": "myprivkeyid",
"private_key": "-----BEGIN PRIVATE KEY-----\nmyprivatekey\n-----END PRIVATE KEY-----\n",
"client_email": "client-mail@mygcpprojectid-1122.iam.gserviceaccount.com",
"client_id": "myclientid",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/client-mail%40mygcpprojectid-1122.iam.gserviceaccount.com"
}
project_id="mygcpprojectid-1122"
zone_name="my-sites"
dns_credentials = service_account.Credentials.from_service_account_info(gcp_dns_credentials)
client = dns.Client(project=project_id,credentials=dns_credentials)
zone = client.zone(zone_name)
create_records=dns.resource_record_set.ResourceRecordSet(name="mydnsrecord2.mygcpproject.com",record_type="A",ttl=300,rrdatas=["13.66.xx.xx"],zone=zone)
此脚本执行既不会抛出错误也不会创建 DNS 记录集。 我提到了这个文档 - https://cloud.google.com/python/docs/reference/dns/latest/resource-record-set
谁能帮帮我:)
没有报错,因为在Google Cloud DNS端还没有做任何事情。
DNS 更改是自动进行的,这意味着您可以进行多项更改(添加、删除等)并一次应用所有更改。所有更改生效或 none 执行(回滚)。
DNS 操作是通过更改集执行的。这意味着创建更改列表(例如创建/修改/删除资源记录)。
add_record_set() 方法追加到变更集 link.
create() 方法应用变更集 link。此方法实际上是修改您的 DNS 服务器资源记录。
只是用 python 代码重申@JohnHanley 解决方案
from google.oauth2 import service_account
from googleapiclient import discovery
gcp_dns_credentials={
"blah blah": "all dummy credentials in json format already mentioned in the question "
}
project_id="mygcpprojectid-1122"
zone_name="my-sites"
credentials = service_account.Credentials.from_service_account_info(gcp_dns_credentials)
service = discovery.build('dns', 'v1', credentials=credentials)
change_body = {
"additions": [
{
"name": "mydnsrecord2.mygcpproject.com.",
"type": "A",
"ttl": 300,
"rrdata": ["13.66.xx.xx"]
}
]
}
request = service.changes().create(project=project_id, managedZone=zone_name, body=change_body)
response = request.execute()
此脚本执行将创建 mydnsrecord2.mygcpproject.com
record-set
引用了此文档https://cloud.google.com/dns/docs/reference/v1/changes/create#python