use of com.microsoft.azure.management.resources.ResourceGroup in project azure-sdk-for-java by Azure.
the class VirtualMachineManagedDiskOperationsTests method canCreateUpdateVirtualMachineWithEmptyManagedDataDisks.
@Test
public void canCreateUpdateVirtualMachineWithEmptyManagedDataDisks() {
final String publicIpDnsLabel = generateRandomResourceName("pip", 20);
final String uname = "juser";
final String password = "123tEst!@|ac";
// Create with implicit + explicit empty disks, check default and override
//
final String vmName1 = "myvm1";
final String explicitlyCreatedEmptyDiskName1 = generateRandomResourceName(vmName1 + "_mdisk_", 25);
final String explicitlyCreatedEmptyDiskName2 = generateRandomResourceName(vmName1 + "_mdisk_", 25);
final String explicitlyCreatedEmptyDiskName3 = generateRandomResourceName(vmName1 + "_mdisk_", 25);
ResourceGroup resourceGroup = resourceManager.resourceGroups().define(RG_NAME).withRegion(region).create();
Creatable<Disk> creatableEmptyDisk1 = computeManager.disks().define(explicitlyCreatedEmptyDiskName1).withRegion(region).withExistingResourceGroup(resourceGroup).withData().withSizeInGB(150);
Creatable<Disk> creatableEmptyDisk2 = computeManager.disks().define(explicitlyCreatedEmptyDiskName2).withRegion(region).withExistingResourceGroup(resourceGroup).withData().withSizeInGB(150);
Creatable<Disk> creatableEmptyDisk3 = computeManager.disks().define(explicitlyCreatedEmptyDiskName3).withRegion(region).withExistingResourceGroup(resourceGroup).withData().withSizeInGB(150);
VirtualMachine virtualMachine = computeManager.virtualMachines().define(vmName1).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIpDnsLabel).withPopularLinuxImage(linuxImage).withRootUsername(uname).withRootPassword(password).withNewDataDisk(// CreateOption: EMPTY
100).withNewDataDisk(100, 1, // CreateOption: EMPTY
CachingTypes.READ_ONLY).withNewDataDisk(// CreateOption: ATTACH
creatableEmptyDisk1).withNewDataDisk(creatableEmptyDisk2, 2, // CreateOption: ATTACH
CachingTypes.NONE).withNewDataDisk(creatableEmptyDisk3, 3, // CreateOption: ATTACH
CachingTypes.NONE).withSize(VirtualMachineSizeTypes.STANDARD_D5_V2).withOSDiskCaching(CachingTypes.READ_WRITE).create();
Assert.assertTrue(virtualMachine.isManagedDiskEnabled());
// There should not be any un-managed data disks
//
Assert.assertNotNull(virtualMachine.unmanagedDataDisks());
Assert.assertEquals(virtualMachine.unmanagedDataDisks().size(), 0);
// Validate the managed data disks
//
Map<Integer, VirtualMachineDataDisk> dataDisks = virtualMachine.dataDisks();
Assert.assertNotNull(dataDisks);
Assert.assertTrue(dataDisks.size() == 5);
Assert.assertTrue(dataDisks.containsKey(1));
VirtualMachineDataDisk dataDiskLun1 = dataDisks.get(1);
Assert.assertNotNull(dataDiskLun1.id());
Assert.assertEquals(dataDiskLun1.cachingType(), CachingTypes.READ_ONLY);
Assert.assertEquals(dataDiskLun1.size(), 100);
Assert.assertTrue(dataDisks.containsKey(2));
VirtualMachineDataDisk dataDiskLun2 = dataDisks.get(2);
Assert.assertNotNull(dataDiskLun2.id());
Assert.assertEquals(dataDiskLun2.cachingType(), CachingTypes.NONE);
Assert.assertEquals(dataDiskLun2.size(), 150);
Assert.assertTrue(dataDisks.containsKey(3));
VirtualMachineDataDisk dataDiskLun3 = dataDisks.get(3);
Assert.assertNotNull(dataDiskLun3.id());
Assert.assertEquals(dataDiskLun3.cachingType(), CachingTypes.NONE);
Assert.assertEquals(dataDiskLun3.size(), 150);
//
for (VirtualMachineDataDisk dataDisk : dataDisks.values()) {
if (dataDisk.lun() != 1 && dataDisk.lun() != 2 && dataDisk.lun() != 3) {
Assert.assertEquals(dataDisk.cachingType(), CachingTypes.READ_WRITE);
Assert.assertEquals(dataDisk.storageAccountType(), StorageAccountTypes.STANDARD_LRS);
}
}
// Updating and adding disk as part of VM Update seems consistency failing, CRP is aware of
// this, hence until it is fixed comment-out the test
//
// {
// "startTime": "2017-01-26T05:48:59.9290573+00:00",
// "endTime": "2017-01-26T05:49:02.2884052+00:00",
// "status": "Failed",
// "error": {
// "code": "InternalExecutionError",
// "message": "An internal execution error occurred."
// },
// "name": "bc8072a7-38bb-445b-ae59-f16cf125342c"
// }
//
// virtualMachine.deallocate();
//
// virtualMachine.update()
// .withDataDiskUpdated(1, 200)
// .withDataDiskUpdated(2, 200, CachingTypes.READ_WRITE)
// .withNewDataDisk(60)
// .apply();
//
// Assert.assertTrue(virtualMachine.isManagedDiskEnabled());
// // There should not be any un-managed data disks
// //
// Assert.assertNotNull(virtualMachine.unmanagedDataDisks());
// Assert.assertEquals(virtualMachine.unmanagedDataDisks().size(), 0);
//
// // Validate the managed data disks
// //
// dataDisks = virtualMachine.dataDisks();
// Assert.assertNotNull(dataDisks);
// Assert.assertTrue(dataDisks.size() == 6);
// Assert.assertTrue(dataDisks.containsKey(1));
// dataDiskLun1 = dataDisks.get(1);
// Assert.assertNotNull(dataDiskLun1.id());
// Assert.assertEquals(dataDiskLun1.cachingType(), CachingTypes.READ_ONLY);
// Assert.assertEquals(dataDiskLun1.size(), 200); // 100 -> 200
//
// Assert.assertTrue(dataDisks.containsKey(2));
// dataDiskLun2 = dataDisks.get(2);
// Assert.assertNotNull(dataDiskLun2.id());
// Assert.assertEquals(dataDiskLun2.cachingType(), CachingTypes.READ_WRITE); // NONE -> READ_WRITE
// Assert.assertEquals(dataDiskLun2.size(), 200); // 150 -> 200
//
// Assert.assertTrue(dataDisks.containsKey(3));
// dataDiskLun3 = dataDisks.get(3);
// Assert.assertNotNull(dataDiskLun3.id());
// Assert.assertEquals(dataDiskLun3.cachingType(), CachingTypes.NONE);
// Assert.assertEquals(dataDiskLun3.size(), 150);
//
// // Ensure defaults of other disks are not affected
// for (VirtualMachineDataDisk dataDisk : dataDisks.values()) {
// if (dataDisk.lun() != 1 && dataDisk.lun() != 3) {
// Assert.assertEquals(dataDisk.cachingType(), CachingTypes.READ_WRITE);
// Assert.assertEquals(dataDisk.storageAccountType(), StorageAccountTypes.STANDARD_LRS);
// }
// }
}
use of com.microsoft.azure.management.resources.ResourceGroup in project azure-sdk-for-java by Azure.
the class ManagedDiskOperationsTests method canOperateOnManagedDiskFromDisk.
@Test
public void canOperateOnManagedDiskFromDisk() {
final String diskName1 = generateRandomResourceName("md-1", 20);
final String diskName2 = generateRandomResourceName("md-2", 20);
ResourceGroup resourceGroup = resourceManager.resourceGroups().define(RG_NAME).withRegion(region).create();
// Create an empty managed disk
//
Disk emptyDisk = computeManager.disks().define(diskName1).withRegion(region).withExistingResourceGroup(resourceGroup.name()).withData().withSizeInGB(100).create();
// Create a managed disk from existing managed disk
//
Disk disk = computeManager.disks().define(diskName2).withRegion(region).withExistingResourceGroup(resourceGroup.name()).withData().fromDisk(emptyDisk).withSizeInGB(200).withSku(DiskSkuTypes.STANDARD_LRS).create();
disk = computeManager.disks().getById(disk.id());
Assert.assertNotNull(disk.id());
Assert.assertTrue(disk.name().equalsIgnoreCase(diskName2));
Assert.assertEquals(disk.sku(), DiskSkuTypes.STANDARD_LRS);
Assert.assertEquals(disk.creationMethod(), DiskCreateOption.COPY);
Assert.assertFalse(disk.isAttachedToVirtualMachine());
Assert.assertEquals(disk.sizeInGB(), 200);
Assert.assertNull(disk.osType());
Assert.assertNotNull(disk.source());
Assert.assertEquals(disk.source().type(), CreationSourceType.COPIED_FROM_DISK);
Assert.assertTrue(disk.source().sourceId().equalsIgnoreCase(emptyDisk.id()));
computeManager.disks().deleteById(emptyDisk.id());
computeManager.disks().deleteById(disk.id());
}
use of com.microsoft.azure.management.resources.ResourceGroup in project azure-sdk-for-java by Azure.
the class ManageSimpleTrafficManager 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 rgName = SdkContext.randomResourceName("rgCOPD", 24);
final String userName = "tirekicker";
final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
final int vmCountPerRegion = 2;
Set<Region> regions = new HashSet<>(Arrays.asList(Region.US_EAST, Region.US_WEST));
try {
//=============================================================
// Create a shared resource group for all the resources so they can all be deleted together
//
ResourceGroup resourceGroup = azure.resourceGroups().define(rgName).withRegion(Region.US_EAST).create();
System.out.println("Created a new resource group - " + resourceGroup.id());
// Prepare a batch of creatable VM definitions to put behind the traffic manager
//
List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>();
for (Region region : regions) {
String linuxVMNamePrefix = SdkContext.randomResourceName("vm", 15);
for (int i = 0; i < vmCountPerRegion; i++) {
//=============================================================
// Create a virtual machine in its own virtual network
String vmName = String.format("%s-%d", linuxVMNamePrefix, i);
Creatable<VirtualMachine> vmDefinition = azure.virtualMachines().define(vmName).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork("10.0.0.0/29").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(vmName).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshKey).withSize(VirtualMachineSizeTypes.STANDARD_A1);
creatableVirtualMachines.add(vmDefinition);
}
}
//=============================================================
// Create the VMs !!
StopWatch stopwatch = new StopWatch();
System.out.println("Creating the virtual machines...");
stopwatch.start();
Collection<VirtualMachine> virtualMachines = azure.virtualMachines().create(creatableVirtualMachines).values();
stopwatch.stop();
System.out.println(String.format("Created virtual machines in %d seconds.", stopwatch.getTime() / 1000));
//=============================================================
// Create 1 traffic manager profile
//
String trafficManagerName = SdkContext.randomResourceName("tra", 15);
TrafficManagerProfile.DefinitionStages.WithEndpoint profileWithEndpoint = azure.trafficManagerProfiles().define(trafficManagerName).withExistingResourceGroup(resourceGroup).withLeafDomainLabel(trafficManagerName).withPerformanceBasedRouting();
TrafficManagerProfile.DefinitionStages.WithCreate profileWithCreate = null;
int routingPriority = 1;
for (VirtualMachine vm : virtualMachines) {
String endpointName = SdkContext.randomResourceName("ep", 15);
profileWithCreate = profileWithEndpoint.defineAzureTargetEndpoint(endpointName).toResourceId(vm.getPrimaryPublicIPAddressId()).withRoutingPriority(routingPriority++).attach();
}
stopwatch.reset();
stopwatch.start();
TrafficManagerProfile trafficManagerProfile = profileWithCreate.create();
stopwatch.stop();
System.out.println(String.format("Created a traffic manager profile %s\n in %d seconds.", trafficManagerProfile.id(), stopwatch.getTime() / 1000));
//=============================================================
// Modify the traffic manager to use priority based routing
//
trafficManagerProfile.update().withPriorityBasedRouting().apply();
System.out.println("Modified the traffic manager to use priority-based routing.");
return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().deleteByName(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;
}
use of com.microsoft.azure.management.resources.ResourceGroup in project azure-sdk-for-java by Azure.
the class AzureTests method testApplicationGatewaysInParallel.
@Test
public void testApplicationGatewaysInParallel() throws Exception {
String rgName = SdkContext.randomResourceName("rg", 13);
Region region = Region.US_EAST;
Creatable<ResourceGroup> resourceGroup = azure.resourceGroups().define(rgName).withRegion(region);
List<Creatable<ApplicationGateway>> agCreatables = new ArrayList<>();
agCreatables.add(azure.applicationGateways().define(SdkContext.randomResourceName("ag", 13)).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroup).defineRequestRoutingRule("rule1").fromPrivateFrontend().fromFrontendHttpPort(80).toBackendHttpPort(8080).toBackendIPAddress("10.0.0.1").toBackendIPAddress("10.0.0.2").attach());
agCreatables.add(azure.applicationGateways().define(SdkContext.randomResourceName("ag", 13)).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroup).defineRequestRoutingRule("rule1").fromPrivateFrontend().fromFrontendHttpPort(80).toBackendHttpPort(8080).toBackendIPAddress("10.0.0.3").toBackendIPAddress("10.0.0.4").attach());
CreatedResources<ApplicationGateway> created = azure.applicationGateways().create(agCreatables);
List<ApplicationGateway> ags = new ArrayList<>();
List<String> agIds = new ArrayList<>();
for (Creatable<ApplicationGateway> creatable : agCreatables) {
ApplicationGateway ag = created.get(creatable.key());
Assert.assertNotNull(ag);
ags.add(ag);
agIds.add(ag.id());
}
azure.applicationGateways().stop(agIds);
for (ApplicationGateway ag : ags) {
Assert.assertEquals(ApplicationGatewayOperationalState.STOPPED, ag.refresh().operationalState());
}
azure.applicationGateways().start(agIds);
for (ApplicationGateway ag : ags) {
Assert.assertEquals(ApplicationGatewayOperationalState.RUNNING, ag.refresh().operationalState());
}
azure.applicationGateways().deleteByIds(agIds);
for (String id : agIds) {
Assert.assertNull(azure.applicationGateways().getById(id));
}
azure.resourceGroups().beginDeleteByName(rgName);
}
use of com.microsoft.azure.management.resources.ResourceGroup in project azure-sdk-for-java by Azure.
the class TestVirtualMachineNics method createResource.
@Override
public VirtualMachine createResource(VirtualMachines virtualMachines) throws Exception {
// Prepare the resource group definition
final String rgName = "rg" + this.testId;
Creatable<ResourceGroup> resourceGroupCreatable = virtualMachines.manager().resourceManager().resourceGroups().define(rgName).withRegion(Region.US_EAST);
// Prepare the virtual network definition [shared by primary and secondary network interfaces]
final String vnetName = "vnet" + this.testId;
Creatable<Network> networkCreatable = this.networkManager.networks().define(vnetName).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withAddressSpace("10.0.0.0/28");
// Prepare the secondary network interface definition
final String secondaryNicName = "nic" + this.testId;
Creatable<NetworkInterface> secondaryNetworkInterfaceCreatable = this.networkManager.networkInterfaces().define(secondaryNicName).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.5");
// .withNewPrimaryPublicIPAddress();
// [Secondary NIC cannot have PublicIP - Only primary network interface can reference a public IP address]
// Prepare the secondary network interface definition
final String secondaryNicName2 = "nic2" + this.testId;
Creatable<NetworkInterface> secondaryNetworkInterfaceCreatable2 = this.networkManager.networkInterfaces().define(secondaryNicName2).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.6");
// Create Virtual Machine
final String vmName = "vm" + this.testId;
final String primaryPipName = "pip" + vmName;
VirtualMachine virtualMachine = virtualMachines.define(vmName).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.4").withNewPrimaryPublicIPAddress(primaryPipName).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_14_04_LTS).withRootUsername("testuser").withRootPassword("12NewPA$$w0rd!").withSize(VirtualMachineSizeTypes.STANDARD_A9).withNewSecondaryNetworkInterface(secondaryNetworkInterfaceCreatable).withNewSecondaryNetworkInterface(secondaryNetworkInterfaceCreatable2).create();
Assert.assertTrue(virtualMachine.networkInterfaceIds().size() == 3);
NetworkInterface primaryNetworkInterface = virtualMachine.getPrimaryNetworkInterface();
Assert.assertEquals(primaryNetworkInterface.primaryPrivateIP(), "10.0.0.4");
PublicIPAddress primaryPublicIPAddress = primaryNetworkInterface.primaryIPConfiguration().getPublicIPAddress();
Assert.assertTrue(primaryPublicIPAddress.fqdn().startsWith(primaryPipName));
return virtualMachine;
}
Aggregations