|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for |
| 4 | + * license information. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.microsoft.azure.management.network.samples; |
| 8 | + |
| 9 | +import com.microsoft.azure.credentials.ApplicationTokenCredentials; |
| 10 | +import com.microsoft.azure.management.Azure; |
| 11 | +import com.microsoft.azure.management.network.Network; |
| 12 | +import com.microsoft.azure.management.privatedns.v2018_09_01.implementation.privatednsManager; |
| 13 | +import com.microsoft.azure.management.resources.fluentcore.arm.Region; |
| 14 | +import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext; |
| 15 | +import com.microsoft.rest.LogLevel; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | + |
| 19 | +public class ManagePrivateDns { |
| 20 | + /** |
| 21 | + * Main function which runs the actual sample. |
| 22 | + * @param azure instance of the azure client |
| 23 | + * @param prDnsManager instance of PrivateDns client |
| 24 | + * @return true if sample runs successfully |
| 25 | + */ |
| 26 | + public static boolean runSample(Azure azure, privatednsManager prDnsManager) { |
| 27 | + final Region region = Region.US_EAST; |
| 28 | + final String rgName = SdkContext.randomResourceName("rg", 24); |
| 29 | + final String nwName = SdkContext.randomResourceName("nw", 24); |
| 30 | + |
| 31 | + try { |
| 32 | + Network network = azure.networks().define(nwName) |
| 33 | + .withRegion(Region.US_EAST) |
| 34 | + .withNewResourceGroup(rgName) |
| 35 | + .withAddressSpace("10.0.0.0/27") |
| 36 | + .withSubnet("subnet1", "10.0.0.0/27") |
| 37 | + .create(); |
| 38 | + // |
| 39 | + // Private DNS hybrid sample |
| 40 | + // |
| 41 | + return true; |
| 42 | + } catch (Exception e) { |
| 43 | + System.err.println(e.getMessage()); |
| 44 | + e.printStackTrace(); |
| 45 | + } finally { |
| 46 | + try { |
| 47 | + System.out.println("Deleting Resource Group: " + rgName); |
| 48 | + azure.resourceGroups().beginDeleteByName(rgName); |
| 49 | + } catch (NullPointerException npe) { |
| 50 | + System.out.println("Did not create any resources in Azure. No clean up is necessary"); |
| 51 | + } catch (Exception g) { |
| 52 | + g.printStackTrace(); |
| 53 | + } |
| 54 | + } |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Main entry point. |
| 60 | + * @param args the parameters |
| 61 | + */ |
| 62 | + public static void main(String[] args) { |
| 63 | + try { |
| 64 | + //============================================================= |
| 65 | + // Authenticate |
| 66 | + |
| 67 | + final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION")); |
| 68 | + final ApplicationTokenCredentials cred = ApplicationTokenCredentials.fromFile(credFile); |
| 69 | + // |
| 70 | + Azure azure = Azure.configure() |
| 71 | + .withLogLevel(LogLevel.BODY) |
| 72 | + .authenticate(credFile) |
| 73 | + .withDefaultSubscription(); |
| 74 | + privatednsManager prDnsManager = privatednsManager.authenticate(cred, cred.defaultSubscriptionId()); |
| 75 | + // Print selected subscription |
| 76 | + // |
| 77 | + System.out.println("Selected subscription: " + azure.subscriptionId()); |
| 78 | + // |
| 79 | + runSample(azure, prDnsManager); |
| 80 | + } catch (Exception e) { |
| 81 | + System.out.println(e.getMessage()); |
| 82 | + e.printStackTrace(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private ManagePrivateDns() { |
| 87 | + } |
| 88 | +} |
0 commit comments