-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also extended example doc and added links. Refactored zone print.
- Loading branch information
Showing
6 changed files
with
284 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...amples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListDnsRecords.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* EDITING INSTRUCTIONS | ||
* This file is referenced in README's and javadoc. Any change to this file should be reflected in | ||
* the project's README's and package-info.java. | ||
*/ | ||
|
||
package com.google.gcloud.examples.dns.snippets; | ||
|
||
import com.google.gcloud.dns.ChangeRequest; | ||
import com.google.gcloud.dns.Dns; | ||
import com.google.gcloud.dns.DnsOptions; | ||
import com.google.gcloud.dns.DnsRecord; | ||
import com.google.gcloud.dns.Zone; | ||
|
||
import java.util.Iterator; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* A snippet for Google Cloud DNS showing how to create a DNS records. | ||
*/ | ||
public class CreateAndListDnsRecords { | ||
|
||
public static void main(String... args) { | ||
// Create a service object. | ||
// The project ID and credentials will be inferred from the environment. | ||
Dns dns = DnsOptions.defaultInstance().service(); | ||
|
||
// Change this to a zone name that exists within your project | ||
String zoneName = "some-sample-zone"; | ||
|
||
// Get zone from the service | ||
Zone zone = dns.getZone(zoneName); | ||
|
||
// Prepare a <i>www.<zone-domain>.</i> type A record with ttl of 24 hours | ||
String ip = "12.13.14.15"; | ||
DnsRecord toCreate = DnsRecord.builder("www." + zone.dnsName(), DnsRecord.Type.A) | ||
.ttl(24, TimeUnit.HOURS) | ||
.addRecord(ip) | ||
.build(); | ||
|
||
// Make a change | ||
ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); | ||
|
||
// Verify a www.<zone-domain>. type A record does not exist yet. | ||
// If it does exist, we will overwrite it with our prepared record. | ||
Iterator<DnsRecord> recordIterator = zone.listDnsRecords().iterateAll(); | ||
while (recordIterator.hasNext()) { | ||
DnsRecord current = recordIterator.next(); | ||
if (toCreate.name().equals(current.name()) && toCreate.type().equals(current.type())) { | ||
changeBuilder.delete(current); | ||
} | ||
} | ||
|
||
// Build and apply the change request to our zone | ||
zone.applyChangeRequest(changeBuilder.build()); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...va-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListZones.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* EDITING INSTRUCTIONS | ||
* This file is referenced in README's and javadoc. Any change to this file should be reflected in | ||
* the project's README's and package-info.java. | ||
*/ | ||
|
||
package com.google.gcloud.examples.dns.snippets; | ||
|
||
import com.google.gcloud.dns.Dns; | ||
import com.google.gcloud.dns.DnsOptions; | ||
import com.google.gcloud.dns.Zone; | ||
import com.google.gcloud.dns.ZoneInfo; | ||
|
||
import java.util.Iterator; | ||
|
||
/** | ||
* A snippet for Google Cloud DNS showing how to create a zone and list all zones in the project. | ||
* You will need to change the {@code domainName} to a domain name, the ownership of which you | ||
* should verify with Google. | ||
*/ | ||
public class CreateAndListZones { | ||
|
||
public static void main(String... args) { | ||
// Create a service object | ||
// The project ID and credentials will be inferred from the environment. | ||
Dns dns = DnsOptions.defaultInstance().service(); | ||
|
||
// Create a zone metadata object | ||
String zoneName = "my_unique_zone"; // Change this zone name which is unique within your project | ||
String domainName = "someexampledomain.com."; // Change this to a domain which you own | ||
String description = "This is a gcloud-java-dns sample zone."; | ||
ZoneInfo zoneInfo = ZoneInfo.of(zoneName, domainName, description); | ||
|
||
// Create zone in Google Cloud DNS | ||
Zone createdZone = dns.create(zoneInfo); | ||
System.out.printf("Zone was created and assigned ID %s.%n", createdZone.id()); | ||
|
||
// Now list all the zones within this project | ||
Iterator<Zone> zoneIterator = dns.listZones().iterateAll(); | ||
int counter = 1; | ||
while (zoneIterator.hasNext()) { | ||
System.out.printf("#%d.: %s%n%n", counter, zoneIterator.next().toString()); | ||
counter++; | ||
} | ||
} | ||
} |
Oops, something went wrong.