Search in sources :

Example 1 with Resource

use of com.microsoft.azure.management.resources.fluentcore.arm.models.Resource in project azure-sdk-for-java by Azure.

the class TestResourceStreaming method createResource.

@Override
public VirtualMachine createResource(VirtualMachines virtualMachines) throws Exception {
    final String vmName = "vm" + this.testId;
    System.out.println("In createResource \n\n\n");
    Creatable<ResourceGroup> rgCreatable = virtualMachines.manager().resourceManager().resourceGroups().define(SdkContext.randomResourceName("rg" + vmName, 20)).withRegion(Region.US_EAST);
    Creatable<StorageAccount> storageCreatable = this.storageAccounts.define(SdkContext.randomResourceName("stg", 20)).withRegion(Region.US_EAST).withNewResourceGroup(rgCreatable);
    final AtomicInteger resourceCount = new AtomicInteger(0);
    VirtualMachine virtualMachine = (VirtualMachine) virtualMachines.define(vmName).withRegion(Region.US_EAST).withNewResourceGroup(rgCreatable).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(SdkContext.randomResourceName("pip", 20)).withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_R2_DATACENTER).withAdminUsername("testuser").withAdminPassword("12NewPA$$w0rd!").withSize(VirtualMachineSizeTypes.STANDARD_D1_V2).withNewStorageAccount(storageCreatable).withNewAvailabilitySet(SdkContext.randomResourceName("avset", 10)).createAsync().map(new Func1<Indexable, Resource>() {

        @Override
        public Resource call(Indexable resource) {
            resourceCount.incrementAndGet();
            Resource createdResource = (Resource) resource;
            System.out.println("Created :" + createdResource.id());
            return createdResource;
        }
    }).toBlocking().last();
    Assert.assertTrue(resourceCount.get() == 7);
    return virtualMachine;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StorageAccount(com.microsoft.azure.management.storage.StorageAccount) Resource(com.microsoft.azure.management.resources.fluentcore.arm.models.Resource) Indexable(com.microsoft.azure.management.resources.fluentcore.model.Indexable) Func1(rx.functions.Func1) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 2 with Resource

use of com.microsoft.azure.management.resources.fluentcore.arm.models.Resource in project azure-sdk-for-java by Azure.

the class VirtualMachineOperationsTests method canStreamParallelCreatedVirtualMachinesAndRelatedResources.

@Test
public void canStreamParallelCreatedVirtualMachinesAndRelatedResources() throws Exception {
    String vmNamePrefix = "vmz";
    String publicIpNamePrefix = generateRandomResourceName("pip-", 15);
    String networkNamePrefix = generateRandomResourceName("vnet-", 15);
    int count = 5;
    final Set<String> virtualMachineNames = new HashSet<>();
    for (int i = 0; i < count; i++) {
        virtualMachineNames.add(String.format("%s-%d", vmNamePrefix, i));
    }
    final Set<String> networkNames = new HashSet<>();
    for (int i = 0; i < count; i++) {
        networkNames.add(String.format("%s-%d", networkNamePrefix, i));
    }
    final Set<String> publicIPAddressNames = new HashSet<>();
    for (int i = 0; i < count; i++) {
        publicIPAddressNames.add(String.format("%s-%d", publicIpNamePrefix, i));
    }
    final CreatablesInfo creatablesInfo = prepareCreatableVirtualMachines(REGION, vmNamePrefix, networkNamePrefix, publicIpNamePrefix, count);
    final AtomicInteger resourceCount = new AtomicInteger(0);
    List<Creatable<VirtualMachine>> virtualMachineCreatables = creatablesInfo.virtualMachineCreatables;
    computeManager.virtualMachines().createAsync(virtualMachineCreatables).map(new Func1<Indexable, Indexable>() {

        @Override
        public Indexable call(Indexable createdResource) {
            if (createdResource instanceof Resource) {
                Resource resource = (Resource) createdResource;
                System.out.println("Created: " + resource.id());
                if (resource instanceof VirtualMachine) {
                    VirtualMachine virtualMachine = (VirtualMachine) resource;
                    Assert.assertTrue(virtualMachineNames.contains(virtualMachine.name()));
                    Assert.assertNotNull(virtualMachine.id());
                } else if (resource instanceof Network) {
                    Network network = (Network) resource;
                    Assert.assertTrue(networkNames.contains(network.name()));
                    Assert.assertNotNull(network.id());
                } else if (resource instanceof PublicIPAddress) {
                    PublicIPAddress publicIPAddress = (PublicIPAddress) resource;
                    Assert.assertTrue(publicIPAddressNames.contains(publicIPAddress.name()));
                    Assert.assertNotNull(publicIPAddress.id());
                }
            }
            resourceCount.incrementAndGet();
            return createdResource;
        }
    }).toBlocking().last();
    // 1 resource group, 1 storage, 5 network, 5 publicIp, 5 nic, 5 virtual machines
    // Additional one for CreatableUpdatableResourceRoot.
    // TODO - ans - We should not emit CreatableUpdatableResourceRoot.
    Assert.assertEquals(resourceCount.get(), 23);
}
Also used : Resource(com.microsoft.azure.management.resources.fluentcore.arm.models.Resource) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Network(com.microsoft.azure.management.network.Network) Creatable(com.microsoft.azure.management.resources.fluentcore.model.Creatable) Indexable(com.microsoft.azure.management.resources.fluentcore.model.Indexable) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Resource

use of com.microsoft.azure.management.resources.fluentcore.arm.models.Resource in project azure-sdk-for-java by Azure.

the class ApplicationGatewayImpl method beforeCreating.

@Override
protected void beforeCreating() {
    // Process created PIPs
    for (Entry<String, String> frontendPipPair : this.creatablePipsByFrontend.entrySet()) {
        Resource createdPip = this.createdResource(frontendPipPair.getValue());
        this.updateFrontend(frontendPipPair.getKey()).withExistingPublicIPAddress(createdPip.id());
    }
    this.creatablePipsByFrontend.clear();
    // Reset and update IP configs
    ensureDefaultIPConfig();
    this.inner().withGatewayIPConfigurations(innersFromWrappers(this.ipConfigs.values()));
    // Reset and update frontends
    this.inner().withFrontendIPConfigurations(innersFromWrappers(this.frontends.values()));
    // Reset and update probes
    this.inner().withProbes(innersFromWrappers(this.probes.values()));
    // Reset and update backends
    this.inner().withBackendAddressPools(innersFromWrappers(this.backends.values()));
    // Reset and update backend HTTP settings configs
    this.inner().withBackendHttpSettingsCollection(innersFromWrappers(this.backendHttpConfigs.values()));
    for (ApplicationGatewayBackendHttpConfiguration config : this.backendHttpConfigs.values()) {
        SubResource ref;
        // Clear deleted probe references
        ref = config.inner().probe();
        if (ref != null && !this.probes().containsKey(ResourceUtils.nameFromResourceId(ref.id()))) {
            config.inner().withProbe(null);
        }
    }
    // Reset and update HTTP listeners
    this.inner().withHttpListeners(innersFromWrappers(this.listeners.values()));
    for (ApplicationGatewayListener listener : this.listeners.values()) {
        SubResource ref;
        // Clear deleted frontend references
        ref = listener.inner().frontendIPConfiguration();
        if (ref != null && !this.frontends().containsKey(ResourceUtils.nameFromResourceId(ref.id()))) {
            listener.inner().withFrontendIPConfiguration(null);
        }
        // Clear deleted frontend port references
        ref = listener.inner().frontendPort();
        if (ref != null && !this.frontendPorts().containsKey(ResourceUtils.nameFromResourceId(ref.id()))) {
            listener.inner().withFrontendPort(null);
        }
        // Clear deleted SSL certificate references
        ref = listener.inner().sslCertificate();
        if (ref != null && !this.sslCertificates().containsKey(ResourceUtils.nameFromResourceId(ref.id()))) {
            listener.inner().withSslCertificate(null);
        }
    }
    // Reset and update request routing rules
    this.inner().withRequestRoutingRules(innersFromWrappers(this.rules.values()));
    for (ApplicationGatewayRequestRoutingRule rule : this.rules.values()) {
        SubResource ref;
        // Clear deleted backends
        ref = rule.inner().backendAddressPool();
        if (ref != null && !this.backends().containsKey(ResourceUtils.nameFromResourceId(ref.id()))) {
            rule.inner().withBackendAddressPool(null);
        }
        // Clear deleted backend HTTP configs
        ref = rule.inner().backendHttpSettings();
        if (ref != null && !this.backendHttpConfigurations().containsKey(ResourceUtils.nameFromResourceId(ref.id()))) {
            rule.inner().withBackendHttpSettings(null);
        }
        // Clear deleted frontend HTTP listeners
        ref = rule.inner().httpListener();
        if (ref != null && !this.listeners().containsKey(ResourceUtils.nameFromResourceId(ref.id()))) {
            rule.inner().withHttpListener(null);
        }
    }
    // Reset and update SSL certs
    this.inner().withSslCertificates(innersFromWrappers(this.sslCerts.values()));
}
Also used : SubResource(com.microsoft.azure.SubResource) SubResource(com.microsoft.azure.SubResource) Resource(com.microsoft.azure.management.resources.fluentcore.arm.models.Resource) ApplicationGatewayRequestRoutingRule(com.microsoft.azure.management.network.ApplicationGatewayRequestRoutingRule) ApplicationGatewayBackendHttpConfiguration(com.microsoft.azure.management.network.ApplicationGatewayBackendHttpConfiguration) ApplicationGatewayListener(com.microsoft.azure.management.network.ApplicationGatewayListener)

Example 4 with Resource

use of com.microsoft.azure.management.resources.fluentcore.arm.models.Resource in project azure-sdk-for-java by Azure.

the class VirtualMachineOperationsTests method canDeleteRelatedResourcesFromFailedParallelVMCreations.

@Test
@Ignore("Can't be played from recording for some reason...")
public void canDeleteRelatedResourcesFromFailedParallelVMCreations() {
    final int desiredVMCount = 40;
    final Region region = Region.US_EAST;
    final String resourceGroupName = RG_NAME;
    // Create one resource group for everything, to ensure no reliance on resource groups
    ResourceGroup resourceGroup = resourceManager.resourceGroups().define(resourceGroupName).withRegion(region).create();
    // Needed for tracking related resources
    final Map<String, Collection<Creatable<? extends Resource>>> vmNonNicResourceDefinitions = new HashMap<>();
    // Tracking NICs separately because they have to be deleted first
    final Map<String, Creatable<NetworkInterface>> nicDefinitions = new HashMap<>();
    final Map<String, Creatable<VirtualMachine>> vmDefinitions = new HashMap<>();
    final Map<String, String> createdResourceIds = new HashMap<>();
    final List<Throwable> errors = new ArrayList<>();
    // Prepare a number of VM definitions along with their related resource definitions
    for (int i = 0; i < desiredVMCount; i++) {
        Collection<Creatable<? extends Resource>> relatedDefinitions = new ArrayList<>();
        // Define a network for each VM
        String networkName = SdkContext.randomResourceName("net", 14);
        Creatable<Network> networkDefinition = networkManager.networks().define(networkName).withRegion(region).withExistingResourceGroup(resourceGroup).withAddressSpace("10.0." + i + ".0/29");
        relatedDefinitions.add(networkDefinition);
        // Define a PIP for each VM
        String pipName = SdkContext.randomResourceName("pip", 14);
        PublicIPAddress.DefinitionStages.WithCreate pipDefinition = this.networkManager.publicIPAddresses().define(pipName).withRegion(region).withExistingResourceGroup(resourceGroup);
        relatedDefinitions.add(pipDefinition);
        // Define a NIC for each VM
        String nicName = SdkContext.randomResourceName("nic", 14);
        Creatable<NetworkInterface> nicDefinition = networkManager.networkInterfaces().define(nicName).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork(networkDefinition).withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(pipDefinition);
        // Define a storage account for each VM
        String storageAccountName = SdkContext.randomResourceName("st", 14);
        Creatable<StorageAccount> storageAccountDefinition = storageManager.storageAccounts().define(storageAccountName).withRegion(region).withExistingResourceGroup(resourceGroup);
        relatedDefinitions.add(storageAccountDefinition);
        // Define an availability set for each VM
        String availabilitySetName = SdkContext.randomResourceName("as", 14);
        Creatable<AvailabilitySet> availabilitySetDefinition = computeManager.availabilitySets().define(availabilitySetName).withRegion(region).withExistingResourceGroup(resourceGroup);
        relatedDefinitions.add(availabilitySetDefinition);
        String vmName = SdkContext.randomResourceName("vm", 14);
        // Define a VM
        String userName;
        if (i == desiredVMCount / 2) {
            // Intentionally cause a failure in one of the VMs
            userName = "";
        } else {
            userName = "tester";
        }
        Creatable<VirtualMachine> vmDefinition = computeManager.virtualMachines().define(vmName).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetworkInterface(nicDefinition).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword("Abcdef.123456!").withNewStorageAccount(storageAccountDefinition).withSize(VirtualMachineSizeTypes.STANDARD_DS1_V2).withNewAvailabilitySet(availabilitySetDefinition);
        // Keep track of all the related resource definitions based on the VM definition
        vmNonNicResourceDefinitions.put(vmDefinition.key(), relatedDefinitions);
        nicDefinitions.put(vmDefinition.key(), nicDefinition);
        vmDefinitions.put(vmDefinition.key(), vmDefinition);
    }
    // Start the parallel creation of everything
    computeManager.virtualMachines().createAsync(new ArrayList<>(vmDefinitions.values())).map(new Func1<Indexable, Indexable>() {

        @Override
        public Indexable call(Indexable createdResource) {
            if (createdResource instanceof Resource) {
                Resource resource = (Resource) createdResource;
                System.out.println("Created: " + resource.id());
                if (resource instanceof VirtualMachine) {
                    VirtualMachine virtualMachine = (VirtualMachine) resource;
                    // Record that this VM was created successfully
                    vmDefinitions.remove(virtualMachine.key());
                    // Remove the associated resources from cleanup list
                    vmNonNicResourceDefinitions.remove(virtualMachine.key());
                    // Remove the associated NIC from cleanup list
                    nicDefinitions.remove(virtualMachine.key());
                } else {
                    // Add this related resource to potential cleanup list
                    createdResourceIds.put(resource.key(), resource.id());
                }
            }
            return createdResource;
        }
    }).onErrorReturn(new Func1<Throwable, Indexable>() {

        @Override
        public Indexable call(Throwable throwable) {
            errors.add(throwable);
            return null;
        }
    }).toBlocking().last();
    // Delete remaining successfully created NICs of failed VM creations
    Collection<String> nicIdsToDelete = new ArrayList<>();
    for (Creatable<NetworkInterface> nicDefinition : nicDefinitions.values()) {
        String nicId = createdResourceIds.get(nicDefinition.key());
        if (nicId != null) {
            nicIdsToDelete.add(nicId);
        }
    }
    if (!nicIdsToDelete.isEmpty()) {
        networkManager.networkInterfaces().deleteByIds(nicIdsToDelete);
    }
    // Delete remaining successfully created resources of failed VM creations
    Collection<Completable> deleteObservables = new ArrayList<>();
    for (Collection<Creatable<? extends Resource>> relatedResources : vmNonNicResourceDefinitions.values()) {
        for (Creatable<? extends Resource> resource : relatedResources) {
            String createdResourceId = createdResourceIds.get(resource.key());
            if (createdResourceId != null) {
                deleteObservables.add(resourceManager.genericResources().deleteByIdAsync(createdResourceId));
            }
        }
    }
    // Delete as much as possible, postponing the errors till the end
    Completable.mergeDelayError(deleteObservables).await();
    // Show any errors
    for (Throwable error : errors) {
        System.out.println("\n### ERROR ###\n");
        if (error instanceof CloudException) {
            CloudException ce = (CloudException) error;
            System.out.println("CLOUD EXCEPTION: " + ce.getMessage());
        } else {
            error.printStackTrace();
        }
    }
    System.out.println("Number of failed/cleaned up VM creations: " + vmNonNicResourceDefinitions.size());
    // Verifications
    final int successfulVMCount = desiredVMCount - vmNonNicResourceDefinitions.size();
    final int actualVMCount = computeManager.virtualMachines().listByResourceGroup(resourceGroupName).size();
    System.out.println("Number of actual successful VMs: " + actualVMCount);
    Assert.assertEquals(successfulVMCount, actualVMCount);
    final int actualNicCount = networkManager.networkInterfaces().listByResourceGroup(resourceGroupName).size();
    Assert.assertEquals(successfulVMCount, actualNicCount);
    final int actualNetworkCount = networkManager.networks().listByResourceGroup(resourceGroupName).size();
    Assert.assertEquals(successfulVMCount, actualNetworkCount);
    final int actualPipCount = networkManager.publicIPAddresses().listByResourceGroup(resourceGroupName).size();
    Assert.assertEquals(successfulVMCount, actualPipCount);
    final int actualAvailabilitySetCount = computeManager.availabilitySets().listByResourceGroup(resourceGroupName).size();
    Assert.assertEquals(successfulVMCount, actualAvailabilitySetCount);
    final int actualStorageAccountCount = storageManager.storageAccounts().listByResourceGroup(resourceGroupName).size();
    Assert.assertEquals(successfulVMCount, actualStorageAccountCount);
    // Verify that at least one VM failed.
    // TODO: Ideally only one, but today the internal RX logic terminates eagerly -- need to change that for parallel creation to terminate more "lazily" in the future
    Assert.assertTrue(successfulVMCount < desiredVMCount);
}
Also used : Completable(rx.Completable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Network(com.microsoft.azure.management.network.Network) Creatable(com.microsoft.azure.management.resources.fluentcore.model.Creatable) Indexable(com.microsoft.azure.management.resources.fluentcore.model.Indexable) Func1(rx.functions.Func1) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Resource(com.microsoft.azure.management.resources.fluentcore.arm.models.Resource) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) CloudException(com.microsoft.azure.CloudException) StorageAccount(com.microsoft.azure.management.storage.StorageAccount) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) Collection(java.util.Collection) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Resource (com.microsoft.azure.management.resources.fluentcore.arm.models.Resource)4 Indexable (com.microsoft.azure.management.resources.fluentcore.model.Indexable)3 Network (com.microsoft.azure.management.network.Network)2 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)2 Creatable (com.microsoft.azure.management.resources.fluentcore.model.Creatable)2 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 Func1 (rx.functions.Func1)2 CloudException (com.microsoft.azure.CloudException)1 SubResource (com.microsoft.azure.SubResource)1 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)1 ApplicationGatewayBackendHttpConfiguration (com.microsoft.azure.management.network.ApplicationGatewayBackendHttpConfiguration)1 ApplicationGatewayListener (com.microsoft.azure.management.network.ApplicationGatewayListener)1 ApplicationGatewayRequestRoutingRule (com.microsoft.azure.management.network.ApplicationGatewayRequestRoutingRule)1 NetworkInterface (com.microsoft.azure.management.network.NetworkInterface)1 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)1 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1