2
votes

i would like to add additonal cname records to google cloud dns using the php api. cname records already exist for sub1.example.com and sub2.example.com added through the web console. but im getting an error

The resource record set 'entity.change.additions[0]' 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.

<?php
require '../autoload.php';


$client_email = CEMAIL;
$private_key = file_get_contents(PKEY);
$scopes = array('https://www.googleapis.com/auth/ndev.clouddns.readwrite');
$project = PNAME;
$managedZone = "example-com";

$creds = new Google_Auth_AssertionCredentials($client_email,$scopes,$private_key);
$client = new Google_Client();
$client->setAssertionCredentials($creds);

$resource = new Google_Service_Dns_ResourceRecordSet();
$resource->kind = "dns#resourceRecordSet";
$resource->name = "example.com.";
$resource->rrdatas[] = "sub3.example.com.";
$resource->ttl = 300;
$resource->type = "CNAME";

$dns = new Google_Service_Dns($client);

$change = new Google_Service_Dns_Change();
$change->kind = "dns#change";
$change->setAdditions([$resource]);

$r=$dns->changes->create($project,$managedZone,$change);
echo '<pre>'.print_r($r,true).'</pre>';

?>

also i could not find a way to list the existsing cname records using the php api as this would have given me a clue to the answer here. Does anyone know how to retreive these, so far the code

<?php
//include details from above

$dns = new Google_Service_Dns($client);

$r=$dns->managedZones->listManagedZones($project);
echo '<pre>'.print_r($r,true).'</pre>';

$r=$dns->managedZones->get($project,$managedZone);
echo '<pre>'.print_r($r,true).'</pre>';

?>

does not list these cname records, although the methods, get and listManagedZones take an additional optional parameter i cannot find documentation on it

source : https://github.com/google/google-api-php-client/blob/master/src/Google/Service/Dns.php line:416, 395

follow on from article : Google Cloud DNS Services - entity.change parameter is required but missing

-

https://developers.google.com/api-client-library/php/

https://developers.google.com/apis-explorer/#p/dns/v1/

https://support.google.com/cloud/answer/6158849?hl=en

https://cloud.google.com/dns/api/v1/resourceRecordSets#resource

https://cloud.google.com/dns/docs

1

1 Answers

1
votes

for those who may find this post later i have answered my own question, as per this post

https://www.zeitgeist.se/2014/05/01/google-cloud-dns-how-to/

i noticed towards the middle of the article, the listing for the mail record had the root domain in the rrdatas field and sub domain in the name property of the resource, switched the two round and voila

$resource = new Google_Service_Dns_ResourceRecordSet();
$resource->kind = "dns#resourceRecordSet";
$resource->name = "sub3.example.com.";       //sub
$resource->rrdatas[] = "example.com.";       //root domain

txt additions:

$del = new Google_Service_Dns_ResourceRecordSet();
$del->kind = "dns#resourceRecordSet";
$del->name = "example.com.";
$del->type = "TXT";
$del->ttl = 300;
$del->rrdatas[] = "\"test=test\"";

$add = new Google_Service_Dns_ResourceRecordSet();
$add->kind = "dns#resourceRecordSet";
$add->name = "example.com.";
$add->type = "TXT";
$add->ttl = 300;
$add->rrdatas[] = "\"test2=test2\"";
$add->rrdatas[] = "\"test3=test3\"";

$change->setAdditions([$add]);
$change->setDeletions([$del]);

$r=$dns->changes->create($project,$managedZone,$change);

https://github.com/google/google-api-php-client/tree/master/examples

llso.

+

Looking up Google Cloud DNS records (nslookup) directly