Managing DNS records for the Google Cloud DNS API involves sending change requests to the API. These changes consist of additions and deletions to your resource record sets collection.
Before you begin
You must have already created a managed zone and completed the prerequisites for that task.
For all the
gcloud
command-line examples below, you can also specify the
--project
parameter for a command to operate against a different project for
that invocation.
Creating, editing, and deleting records
The process for creating, editing, and deleting records is the same, but differs in how you specify the change that you would like to make.
Editing records is performed by sending a change that deletes the existing record that you want to change and then providing an addition in the same request that recreates the record in its new format.
You can specify many changes in the same request and a combination of additions and deletions.
Command line
gcloud dns records --zone="myzonename" edit
An editor will open and you will see an addition and a deletion of the
SOA
record prefilled for you. Increment the serial number in the SOA record with
each change to ensure that your requests can be safely retried in case of an
error. The SOA serial number is the first of five integers in the SOA
rrdatas
value. In the example below, the serial number is incremented by
deleting the old SOA record (serial number
0
) and re-adding the SOA record
with the incremented serial number (
1
).
Add your own additions or deletions into the JSON object, for example:
{
"additions": [
{
"kind": "dns#resourceRecordSet",
"name": "example.com.",
"rrdatas": [
"1.2.3.4 9.8.7.6"
],
"ttl": 21600,
"type": "A"
},
{
"kind": "dns#resourceRecordSet",
"name": "example.com.",
"rrdatas": [
"ns-cloud1.googledomains.com. dns-admin.google.com. 1 21600 3600 1209600 300"
],
"ttl": 21600,
"type": "SOA"
}
],
"deletions": [
{
"kind": "dns#resourceRecordSet",
"name": "example.com.",
"rrdatas": [
"ns-cloud1.googledomains.com. dns-admin.google.com. 0 21600 3600 1209600 300"
],
"ttl": 21600,
"type": "SOA"
}
]
}
This example adds an
A
record and updates the SOA serial number from
0
to
1
.
Python
The following example demonstrates assembling a
changes.create
using the
Python client library
. The sample assumes
your app has already acquired an access token to make API requests on behalf
of a user.
As a best practice, you should change to your SOA record to increment
the SOA serial number to ensure that the changes are picked up and do not
potentially collide with other changes. The SOA serial number is the first
of five integers in the SOA
rrdatas
value. In the example below, the
serial number is incremented by deleting the old SOA record (serial number
0
) and re-adding the SOA record with the incremented serial number (
1
).
In the same request body, an
A
record is added to point to the IP
address
1.2.3.4
:
from apiclient import errors
from apiclient.discovery import build
PROJECT_NAME='<your-project-name>'
ZONE_NAME='<your-zone-name>'
# Two additions in the same request:
BODY = {
'additions' : [{
'name' : 'example.com.',
'type' : 'A',
'ttl' : '3600',
'rrdatas' : [
'1.2.3.4'
]
},{
'kind': 'dns#resourceRecordSet',
'name': 'example.com.',
'rrdatas': [
'ns-cloud1.googledomains.com. dns-admin.google.com. 1 21600 3600 1209600 300'
],
'ttl': 21600,
'type': 'SOA'
}
],
'deletions': [{
'kind': 'dns#resourceRecordSet',
'name': 'example.com.',
'rrdatas': [
'ns-cloud1.googledomains.com. dns-admin.google.com. 0 21600 3600 1209600 300'
],
'ttl': 21600,
'type': 'SOA'
}
]}
try:
service = build('dns', 'v1beta1')
response = service.changes().create(project=PROJECT_NAME,
managedZone=ZONE_NAME,
body=BODY).execute()
except errors.HttpError, error:
print 'An error occurred: %s' % error
Displaying the current record set
To display the current DNS records for your zone:
Command line
gcloud dns records --zone="myzonename" list
The command will output the JSON response for the resource record set for
the first 100 records. You can specify the
--maxresults=<number>
parameter
to increase the number of results that are returned. You might also consider
using the
changes.list
method in the REST API
if you require paging through many sets of results or for specifying query
parameters to filter your results.
Python
from apiclient import errors
from apiclient.discovery import build
PROJECT_NAME='<your-project-name>'
ZONE_NAME='<your-zone-name>'
try:
service = build('dns', 'v1beta1')
response = service.resourcerecordsets().list(project=PROJECT_NAME,
managedZone=ZONE_NAME).execute()
except errors.HttpError, error:
print 'An error occurred: %s' % error
Troubleshooting
cnameResourceRecordSetConflict
The resource record set 'entity.change.additions[ x ]' is invalid because the DNS name 'example.com.' may have either one CNAME resource record set or resource record sets of other types, but not both.
This error can occur when you create two types of resource record sets, such as
both an
A
record and a
CNAME
record for the same DNS name. A common cause of
this error is trying to create a
CNAME
record at the zone apex. This is not
possible because it would conflict with the required
SOA
and
NS
records of
the same name.
invalidRecordCount
The resource record set 'entity.change.additions[ x ]' is only permitted to have one record because it is of type ' <TYPENAME> '.
CNAME
and
SOA
resource record sets are only allowed to have one record.