use of com.microsoft.azure.management.dns.DnsZone in project azure-sdk-for-java by Azure.
the class TestDns method createResource.
@Override
public DnsZone createResource(DnsZones dnsZones) throws Exception {
final Region region = Region.US_EAST;
final String groupName = "rg" + this.testId;
final String topLevelDomain = "www.contoso" + this.testId + ".com";
DnsZone dnsZone = dnsZones.define(topLevelDomain).withNewResourceGroup(groupName, region).defineARecordSet("www").withIPv4Address("23.96.104.40").withIPv4Address("24.97.105.41").withTimeToLive(// Overwrite default 3600 seconds
7200).attach().defineAaaaRecordSet("www").withIPv6Address("2001:0db8:85a3:0000:0000:8a2e:0370:7334").withIPv6Address("2002:0db9:85a4:0000:0000:8a2e:0371:7335").attach().defineMXRecordSet("email").withMailExchange("mail.contoso-mail-exchange1.com", 1).withMailExchange("mail.contoso-mail-exchange2.com", 2).withMetadata("mxa", "mxaa").withMetadata("mxb", "mxbb").attach().defineNSRecordSet("partners").withNameServer("ns1-05.azure-dns.com").withNameServer("ns2-05.azure-dns.net").withNameServer("ns3-05.azure-dns.org").withNameServer("ns4-05.azure-dns.info").attach().defineTxtRecordSet("@").withText("windows-apps-verification=2ZzjfideIJFLFje83").attach().defineTxtRecordSet("www").withText("some info about www.contoso.com").attach().defineSrvRecordSet("_sip._tcp").withRecord("bigbox.contoso-service.com", 5060, 10, 60).withRecord("smallbox1.contoso-service.com", 5060, 10, 20).withRecord("smallbox2.contoso-service.com", 5060, 10, 20).withRecord("backupbox.contoso-service.com", 5060, 10, 0).attach().definePtrRecordSet("40").withTargetDomainName("www.contoso.com").withTargetDomainName("mail.contoso.com").attach().definePtrRecordSet("41").withTargetDomainName("www.contoso.com").withTargetDomainName("mail.contoso.com").attach().withCNameRecordSet("documents", "doc.contoso.com").withCNameRecordSet("userguide", "doc.contoso.com").withTag("a", "aa").withTag("b", "bb").create();
// Check Dns zone properties
Assert.assertTrue(dnsZone.name().startsWith(topLevelDomain));
// Default '@' name servers
Assert.assertTrue(dnsZone.nameServers().size() > 0);
Assert.assertTrue(dnsZone.tags().size() == 2);
// Check SOA record - external child resource (created by default)
SoaRecordSet soaRecordSet = dnsZone.getSoaRecordSet();
Assert.assertTrue(soaRecordSet.name().startsWith("@"));
SoaRecord soaRecord = soaRecordSet.record();
Assert.assertNotNull(soaRecord);
// Check explicitly created external child resources [A, AAAA, MX, NS, TXT, SRV, PTR, CNAME]
//
// Check A records
PagedList<ARecordSet> aRecordSets = dnsZone.aRecordSets().list();
Assert.assertTrue(aRecordSets.size() == 1);
Assert.assertTrue(aRecordSets.get(0).timeToLive() == 7200);
// Check AAAA records
PagedList<AaaaRecordSet> aaaaRecordSets = dnsZone.aaaaRecordSets().list();
Assert.assertTrue(aaaaRecordSets.size() == 1);
Assert.assertTrue(aaaaRecordSets.get(0).name().startsWith("www"));
Assert.assertTrue(aaaaRecordSets.get(0).ipv6Addresses().size() == 2);
// Check MX records
PagedList<MXRecordSet> mxRecordSets = dnsZone.mxRecordSets().list();
Assert.assertTrue(mxRecordSets.size() == 1);
MXRecordSet mxRecordSet = mxRecordSets.get(0);
Assert.assertNotNull(mxRecordSet);
Assert.assertTrue(mxRecordSet.name().startsWith("email"));
Assert.assertTrue(mxRecordSet.metadata().size() == 2);
Assert.assertTrue(mxRecordSet.records().size() == 2);
for (MxRecord mxRecord : mxRecordSet.records()) {
Assert.assertTrue(mxRecord.exchange().startsWith("mail.contoso-mail-exchange1.com") || mxRecord.exchange().startsWith("mail.contoso-mail-exchange2.com"));
Assert.assertTrue(mxRecord.preference() == 1 || mxRecord.preference() == 2);
}
// Check NS records
PagedList<NSRecordSet> nsRecordSets = dnsZone.nsRecordSets().list();
// One created above with name 'partners' + the default '@'
Assert.assertTrue(nsRecordSets.size() == 2);
// Check TXT records
PagedList<TxtRecordSet> txtRecordSets = dnsZone.txtRecordSets().list();
Assert.assertTrue(txtRecordSets.size() == 2);
// Check SRV records
PagedList<SrvRecordSet> srvRecordSets = dnsZone.srvRecordSets().list();
Assert.assertTrue(srvRecordSets.size() == 1);
// Check PTR records
PagedList<PtrRecordSet> ptrRecordSets = dnsZone.ptrRecordSets().list();
Assert.assertTrue(ptrRecordSets.size() == 2);
// Check CNAME records
PagedList<CNameRecordSet> cnameRecordSets = dnsZone.cNameRecordSets().list();
Assert.assertTrue(cnameRecordSets.size() == 2);
return dnsZone;
}
use of com.microsoft.azure.management.dns.DnsZone in project azure-sdk-for-java by Azure.
the class ManageDns method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final String customDomainName = "THE CUSTOM DOMAIN THAT YOU OWN (e.g. contoso.com)";
final String rgName = SdkContext.randomResourceName("rgNEMV_", 24);
final String appServicePlanName = SdkContext.randomResourceName("jplan1_", 15);
final String webAppName = SdkContext.randomResourceName("webapp1-", 20);
try {
ResourceGroup resourceGroup = azure.resourceGroups().define(rgName).withRegion(Region.US_EAST2).create();
//============================================================
// Creates root DNS Zone
System.out.println("Creating root DNS zone " + customDomainName + "...");
DnsZone rootDnsZone = azure.dnsZones().define(customDomainName).withExistingResourceGroup(resourceGroup).create();
System.out.println("Created root DNS zone " + rootDnsZone.name());
Utils.print(rootDnsZone);
//============================================================
// Sets NS records in the parent zone (hosting custom domain) to make Azure DNS the authoritative
// source for name resolution for the zone
System.out.println("Go to your registrar portal and configure your domain " + customDomainName + " with following name server addresses");
for (String nameServer : rootDnsZone.nameServers()) {
System.out.println(" " + nameServer);
}
System.out.println("Press a key after finishing above step");
System.in.read();
//============================================================
// Creates a web App
System.out.println("Creating Web App " + webAppName + "...");
WebApp webApp = azure.webApps().define(webAppName).withRegion(Region.US_EAST2).withExistingResourceGroup(rgName).withNewWindowsPlan(PricingTier.BASIC_B1).defineSourceControl().withPublicGitRepository("https://github.com/jianghaolu/azure-site-test").withBranch("master").attach().create();
System.out.println("Created web app " + webAppName);
Utils.print(webApp);
//============================================================
// Creates a CName record and bind it with the web app
// Step 1: Adds CName Dns record to root DNS zone that specify web app host domain as an
// alias for www.[customDomainName]
System.out.println("Updating DNS zone by adding a CName record...");
rootDnsZone = rootDnsZone.update().withCNameRecordSet("www", webApp.defaultHostName()).apply();
System.out.println("DNS zone updated");
Utils.print(rootDnsZone);
// Waiting for a minute for DNS CName entry to propagate
System.out.println("Waiting a minute for CName record entry to propagate...");
Thread.sleep(60 * 1000);
// Step 2: Adds a web app host name binding for www.[customDomainName]
// This binding action will fail if the CName record propagation is not yet completed
System.out.println("Updating Web app with host name binding...");
webApp.update().defineHostnameBinding().withThirdPartyDomain(customDomainName).withSubDomain("www").withDnsRecordType(CustomHostNameDnsRecordType.CNAME).attach().apply();
System.out.println("Web app updated");
Utils.print(webApp);
//============================================================
// Creates a virtual machine with public IP
System.out.println("Creating a virtual machine with public IP...");
VirtualMachine virtualMachine1 = azure.virtualMachines().define(SdkContext.randomResourceName("employeesvm-", 20)).withRegion(Region.US_EAST).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(SdkContext.randomResourceName("empip-", 20)).withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_R2_DATACENTER).withAdminUsername("testuser").withAdminPassword("12NewPA$$w0rd!").withSize(VirtualMachineSizeTypes.STANDARD_D1_V2).create();
System.out.println("Virtual machine created");
//============================================================
// Update DNS zone by adding a A record in root DNS zone pointing to virtual machine IPv4 address
PublicIPAddress vm1PublicIPAddress = virtualMachine1.getPrimaryPublicIPAddress();
System.out.println("Updating root DNS zone " + customDomainName + "...");
rootDnsZone = rootDnsZone.update().defineARecordSet("employees").withIPv4Address(vm1PublicIPAddress.ipAddress()).attach().apply();
System.out.println("Updated root DNS zone " + rootDnsZone.name());
Utils.print(rootDnsZone);
// Prints the CName and A Records in the root DNS zone
//
System.out.println("Getting CName record set in the root DNS zone " + customDomainName + "...");
PagedList<CNameRecordSet> cnameRecordSets = rootDnsZone.cNameRecordSets().list();
for (CNameRecordSet cnameRecordSet : cnameRecordSets) {
System.out.println("Name: " + cnameRecordSet.name() + " Canonical Name: " + cnameRecordSet.canonicalName());
}
System.out.println("Getting ARecord record set in the root DNS zone " + customDomainName + "...");
PagedList<ARecordSet> aRecordSets = rootDnsZone.aRecordSets().list();
for (ARecordSet aRecordSet : aRecordSets) {
System.out.println("Name: " + aRecordSet.name());
for (String ipv4Address : aRecordSet.ipv4Addresses()) {
System.out.println(" " + ipv4Address);
}
}
//============================================================
// Creates a child DNS zone
String partnerSubDomainName = "partners." + customDomainName;
System.out.println("Creating child DNS zone " + partnerSubDomainName + "...");
DnsZone partnersDnsZone = azure.dnsZones().define(partnerSubDomainName).withExistingResourceGroup(resourceGroup).create();
System.out.println("Created child DNS zone " + partnersDnsZone.name());
Utils.print(partnersDnsZone);
//============================================================
// Adds NS records in the root dns zone to delegate partners.[customDomainName] to child dns zone
System.out.println("Updating root DNS zone " + rootDnsZone + "...");
DnsRecordSet.UpdateDefinitionStages.WithNSRecordNameServerOrAttachable<DnsZone.Update> nsRecordStage = rootDnsZone.update().defineNSRecordSet("partners").withNameServer(partnersDnsZone.nameServers().get(0));
for (int i = 1; i < partnersDnsZone.nameServers().size(); i++) {
nsRecordStage = nsRecordStage.withNameServer(partnersDnsZone.nameServers().get(i));
}
nsRecordStage.attach().apply();
System.out.println("Root DNS zone updated");
Utils.print(rootDnsZone);
//============================================================
// Creates a virtual machine with public IP
System.out.println("Creating a virtual machine with public IP...");
VirtualMachine virtualMachine2 = azure.virtualMachines().define(SdkContext.randomResourceName("partnersvm-", 20)).withRegion(Region.US_EAST).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(SdkContext.randomResourceName("ptnerpip-", 20)).withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_R2_DATACENTER).withAdminUsername("testuser").withAdminPassword("12NewPA$$w0rd!").withSize(VirtualMachineSizeTypes.STANDARD_D1_V2).create();
System.out.println("Virtual machine created");
//============================================================
// Update child DNS zone by adding a A record pointing to virtual machine IPv4 address
PublicIPAddress vm2PublicIPAddress = virtualMachine2.getPrimaryPublicIPAddress();
System.out.println("Updating child DNS zone " + partnerSubDomainName + "...");
partnersDnsZone = partnersDnsZone.update().defineARecordSet("@").withIPv4Address(vm2PublicIPAddress.ipAddress()).attach().apply();
System.out.println("Updated child DNS zone " + partnersDnsZone.name());
Utils.print(partnersDnsZone);
//============================================================
// Removes A record entry from the root DNS zone
System.out.println("Removing A Record from root DNS zone " + rootDnsZone.name() + "...");
rootDnsZone = rootDnsZone.update().withoutARecordSet("employees").apply();
System.out.println("Removed A Record from root DNS zone");
Utils.print(rootDnsZone);
//============================================================
// Deletes the DNS zone
System.out.println("Deleting child DNS zone " + partnersDnsZone.name() + "...");
azure.dnsZones().deleteById(partnersDnsZone.id());
System.out.println("Deleted child DNS zone " + partnersDnsZone.name());
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().beginDeleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
Aggregations