Search in sources :

Example 11 with ResourceGroup

use of com.microsoft.azure.management.resources.ResourceGroup in project azure-tools-for-java by Microsoft.

the class AzureModelController method updateResourceGroupMaps.

public static synchronized void updateResourceGroupMaps(IProgressIndicator progressIndicator) throws IOException, CanceledByUserException, AuthException {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    // not signed in
    if (azureManager == null) {
        return;
    }
    updateSubscriptionMaps(progressIndicator);
    AzureModel azureModel = AzureModel.getInstance();
    Map<ResourceGroup, List<WebApp>> rgwaMap = azureModel.createResourceGroupToWebAppMap();
    Map<ResourceGroup, List<AppServicePlan>> rgspMap = azureModel.createResourceGroupToAppServicePlanMap();
    for (SubscriptionDetail sd : azureModel.getSubscriptionToResourceGroupMap().keySet()) {
        if (progressIndicator != null && progressIndicator.isCanceled()) {
            clearAll();
            throw new CanceledByUserException();
        }
        List<ResourceGroup> rgList = azureModel.getSubscriptionToResourceGroupMap().get(sd);
        if (rgList.size() == 0) {
            System.out.println("no resource groups found!");
            continue;
        }
        Azure azure = azureManager.getAzure(sd.getSubscriptionId());
        updateResGrDependency(azure, rgList, progressIndicator, rgwaMap, rgspMap);
    }
    azureModel.setResourceGroupToWebAppMap(rgwaMap);
    azureModel.setResourceGroupToAppServicePlanMap(rgspMap);
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup)

Example 12 with ResourceGroup

use of com.microsoft.azure.management.resources.ResourceGroup in project azure-tools-for-java by Microsoft.

the class AzureSDKManager method createVirtualMachine.

public static VirtualMachine createVirtualMachine(String subscriptionId, @NotNull String name, @NotNull String resourceGroup, boolean withNewResourceGroup, @NotNull String size, @NotNull String region, final VirtualMachineImage vmImage, Object knownImage, boolean isKnownImage, final StorageAccount storageAccount, com.microsoft.tooling.msservices.model.storage.StorageAccount newStorageAccount, boolean withNewStorageAccount, final Network network, VirtualNetwork newNetwork, boolean withNewNetwork, @NotNull String subnet, @Nullable PublicIPAddress pip, boolean withNewPip, @Nullable AvailabilitySet availabilitySet, boolean withNewAvailabilitySet, @NotNull final String username, @Nullable final String password, @Nullable String publicKey) throws Exception {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    Azure azure = azureManager.getAzure(subscriptionId);
    boolean isWindows;
    if (isKnownImage) {
        isWindows = knownImage instanceof KnownWindowsVirtualMachineImage;
    } else {
        isWindows = vmImage.osDiskImage().operatingSystem().equals(OperatingSystemTypes.WINDOWS);
    }
    // ------ Resource Group ------
    VirtualMachine.DefinitionStages.WithGroup withGroup = azure.virtualMachines().define(name).withRegion(region);
    Creatable<ResourceGroup> newResourceGroup = null;
    VirtualMachine.DefinitionStages.WithNetwork withNetwork;
    if (withNewResourceGroup) {
        newResourceGroup = azure.resourceGroups().define(resourceGroup).withRegion(region);
        withNetwork = withGroup.withNewResourceGroup(newResourceGroup);
    } else {
        withNetwork = withGroup.withExistingResourceGroup(resourceGroup);
    }
    // ------ Virtual Network -----
    VirtualMachine.DefinitionStages.WithPublicIPAddress withPublicIpAddress;
    if (withNewNetwork) {
        Network.DefinitionStages.WithGroup networkWithGroup = azure.networks().define(newNetwork.name).withRegion(region);
        Creatable<Network> newVirtualNetwork;
        if (withNewResourceGroup) {
            newVirtualNetwork = networkWithGroup.withNewResourceGroup(newResourceGroup).withAddressSpace(newNetwork.addressSpace).withSubnet(newNetwork.subnet.name, newNetwork.subnet.addressSpace);
        } else {
            newVirtualNetwork = networkWithGroup.withExistingResourceGroup(resourceGroup).withAddressSpace(newNetwork.addressSpace).withSubnet(newNetwork.subnet.name, newNetwork.subnet.addressSpace);
        }
        withPublicIpAddress = withNetwork.withNewPrimaryNetwork(newVirtualNetwork).withPrimaryPrivateIPAddressDynamic();
    //            withPublicIpAddress = withNetwork.withNewPrimaryNetwork("10.0.0.0/28").
    //                    .withPrimaryPrivateIpAddressDynamic();
    } else {
        withPublicIpAddress = withNetwork.withExistingPrimaryNetwork(network).withSubnet(subnet).withPrimaryPrivateIPAddressDynamic();
    }
    // ------ Public IP Address------
    VirtualMachine.DefinitionStages.WithOS withOS;
    if (pip == null) {
        if (withNewPip) {
            withOS = withPublicIpAddress.withNewPrimaryPublicIPAddress(name + "pip");
        } else {
            withOS = withPublicIpAddress.withoutPrimaryPublicIPAddress();
        }
    } else {
        withOS = withPublicIpAddress.withExistingPrimaryPublicIPAddress(pip);
    }
    // ------ OS and credentials -----
    VirtualMachine.DefinitionStages.WithCreate withCreate;
    if (isWindows) {
        VirtualMachine.DefinitionStages.WithWindowsAdminUsernameManagedOrUnmanaged withWindowsAdminUsername;
        if (isKnownImage) {
            withWindowsAdminUsername = withOS.withPopularWindowsImage((KnownWindowsVirtualMachineImage) knownImage);
        } else {
            withWindowsAdminUsername = withOS.withSpecificWindowsImageVersion(vmImage.imageReference());
        }
        withCreate = withWindowsAdminUsername.withAdminUsername(username).withAdminPassword(password).withUnmanagedDisks();
    } else {
        VirtualMachine.DefinitionStages.WithLinuxRootPasswordOrPublicKeyManagedOrUnmanaged withLinuxRootPasswordOrPublicKey;
        if (isKnownImage) {
            withLinuxRootPasswordOrPublicKey = withOS.withPopularLinuxImage((KnownLinuxVirtualMachineImage) knownImage).withRootUsername(username);
        } else {
            withLinuxRootPasswordOrPublicKey = withOS.withSpecificLinuxImageVersion(vmImage.imageReference()).withRootUsername(username);
        }
        VirtualMachine.DefinitionStages.WithLinuxCreateManagedOrUnmanaged withLinuxCreate;
        // we assume either password or public key is not empty
        if (password != null && !password.isEmpty()) {
            withLinuxCreate = withLinuxRootPasswordOrPublicKey.withRootPassword(password);
            if (publicKey != null) {
                withLinuxCreate = withLinuxCreate.withSsh(publicKey);
            }
        } else {
            withLinuxCreate = withLinuxRootPasswordOrPublicKey.withSsh(publicKey);
        }
        withCreate = withLinuxCreate.withUnmanagedDisks();
    }
    withCreate = withCreate.withSize(size);
    // ---- Storage Account --------
    if (withNewStorageAccount) {
        StorageAccount.DefinitionStages.WithCreate newAccount;
        StorageAccount.DefinitionStages.WithGroup withGroupAccount = azure.storageAccounts().define(newStorageAccount.getName()).withRegion(newStorageAccount.getLocation());
        if (newStorageAccount.isNewResourceGroup()) {
            newAccount = withGroupAccount.withNewResourceGroup(newStorageAccount.getResourceGroupName());
        } else {
            newAccount = withGroupAccount.withExistingResourceGroup(newStorageAccount.getResourceGroupName());
        }
        // only general purpose accounts used to create vm
        newAccount.withGeneralPurposeAccountKind().withSku(SkuName.fromString(newStorageAccount.getType()));
        withCreate = withCreate.withNewStorageAccount(newAccount);
    } else {
        withCreate = withCreate.withExistingStorageAccount(storageAccount);
    }
    if (withNewAvailabilitySet) {
        withCreate = withCreate.withNewAvailabilitySet(name + "as");
    } else if (availabilitySet != null) {
        withCreate = withCreate.withExistingAvailabilitySet(availabilitySet);
    }
    return withCreate.create();
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) KnownWindowsVirtualMachineImage(com.microsoft.azure.management.compute.KnownWindowsVirtualMachineImage) Network(com.microsoft.azure.management.network.Network) VirtualNetwork(com.microsoft.tooling.msservices.model.vm.VirtualNetwork) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup)

Example 13 with ResourceGroup

use of com.microsoft.azure.management.resources.ResourceGroup in project azure-sdk-for-java by Azure.

the class RedisCacheOperationsTests method canCRUDRedisCache.

@Test
public void canCRUDRedisCache() throws Exception {
    // Create
    Creatable<ResourceGroup> resourceGroups = resourceManager.resourceGroups().define(RG_NAME_SECOND).withRegion(Region.US_CENTRAL);
    Creatable<RedisCache> redisCacheDefinition1 = redisManager.redisCaches().define(RR_NAME).withRegion(Region.ASIA_EAST).withNewResourceGroup(RG_NAME).withBasicSku();
    Creatable<RedisCache> redisCacheDefinition2 = redisManager.redisCaches().define(RR_NAME_SECOND).withRegion(Region.US_CENTRAL).withNewResourceGroup(resourceGroups).withPremiumSku().withShardCount(10).withPatchSchedule(DayOfWeek.SUNDAY, 10, Period.minutes(302));
    Creatable<RedisCache> redisCacheDefinition3 = redisManager.redisCaches().define(RR_NAME_THIRD).withRegion(Region.US_CENTRAL).withNewResourceGroup(resourceGroups).withPremiumSku(2).withRedisConfiguration("maxclients", "2").withNonSslPort();
    CreatedResources<RedisCache> batchRedisCaches = redisManager.redisCaches().create(redisCacheDefinition1, redisCacheDefinition2, redisCacheDefinition3);
    StorageAccount storageAccount = storageManager.storageAccounts().define(SA_NAME).withRegion(Region.US_CENTRAL).withExistingResourceGroup(RG_NAME_SECOND).create();
    RedisCache redisCache = batchRedisCaches.get(redisCacheDefinition1.key());
    RedisCache redisCachePremium = batchRedisCaches.get(redisCacheDefinition3.key());
    Assert.assertEquals(RG_NAME, redisCache.resourceGroupName());
    Assert.assertEquals(SkuName.BASIC, redisCache.sku().name());
    // List by Resource Group
    List<RedisCache> redisCaches = redisManager.redisCaches().listByResourceGroup(RG_NAME);
    boolean found = false;
    for (RedisCache existingRedisCache : redisCaches) {
        if (existingRedisCache.name().equals(RR_NAME)) {
            found = true;
        }
    }
    Assert.assertTrue(found);
    Assert.assertEquals(1, redisCaches.size());
    // List all Redis resources
    redisCaches = redisManager.redisCaches().list();
    found = false;
    for (RedisCache existingRedisCache : redisCaches) {
        if (existingRedisCache.name().equals(RR_NAME)) {
            found = true;
        }
    }
    Assert.assertTrue(found);
    Assert.assertTrue(redisCaches.size() >= 3);
    // Get
    RedisCache redisCacheGet = redisManager.redisCaches().getByResourceGroup(RG_NAME, RR_NAME);
    Assert.assertNotNull(redisCacheGet);
    Assert.assertEquals(redisCache.id(), redisCacheGet.id());
    Assert.assertEquals(redisCache.provisioningState(), redisCacheGet.provisioningState());
    // Get Keys
    RedisAccessKeys redisKeys = redisCache.keys();
    Assert.assertNotNull(redisKeys);
    Assert.assertNotNull(redisKeys.primaryKey());
    Assert.assertNotNull(redisKeys.secondaryKey());
    // Regen key
    RedisAccessKeys oldKeys = redisCache.refreshKeys();
    RedisAccessKeys updatedPrimaryKey = redisCache.regenerateKey(RedisKeyType.PRIMARY);
    RedisAccessKeys updatedSecondaryKey = redisCache.regenerateKey(RedisKeyType.SECONDARY);
    Assert.assertNotNull(oldKeys);
    Assert.assertNotNull(updatedPrimaryKey);
    Assert.assertNotNull(updatedSecondaryKey);
    Assert.assertNotEquals(oldKeys.primaryKey(), updatedPrimaryKey.primaryKey());
    Assert.assertEquals(oldKeys.secondaryKey(), updatedPrimaryKey.secondaryKey());
    Assert.assertNotEquals(oldKeys.secondaryKey(), updatedSecondaryKey.secondaryKey());
    Assert.assertNotEquals(updatedPrimaryKey.secondaryKey(), updatedSecondaryKey.secondaryKey());
    Assert.assertEquals(updatedPrimaryKey.primaryKey(), updatedSecondaryKey.primaryKey());
    // Update to STANDARD Sku from BASIC SKU
    redisCache = redisCache.update().withStandardSku().apply();
    Assert.assertEquals(SkuName.STANDARD, redisCache.sku().name());
    Assert.assertEquals(SkuFamily.C, redisCache.sku().family());
    try {
        redisCache.update().withBasicSku(1).apply();
        fail();
    } catch (CloudException e) {
    // expected since Sku downgrade is not supported
    }
    // Refresh
    redisCache.refresh();
    // delete
    redisManager.redisCaches().deleteById(redisCache.id());
    // Premium SKU Functionality
    RedisCachePremium premiumCache = redisCachePremium.asPremium();
    Assert.assertEquals(SkuFamily.P, premiumCache.sku().family());
    // Redis configuration update
    premiumCache.update().withRedisConfiguration("maxclients", "3").apply();
    premiumCache.update().withoutRedisConfiguration("maxclients").apply();
    premiumCache.update().withoutRedisConfiguration().apply();
    premiumCache.update().withPatchSchedule(DayOfWeek.MONDAY, 1).withPatchSchedule(DayOfWeek.TUESDAY, 5).apply();
    // Reboot
    premiumCache.forceReboot(RebootType.ALL_NODES);
    // Patch Schedule
    List<ScheduleEntry> patchSchedule = premiumCache.listPatchSchedules();
    Assert.assertEquals(2, patchSchedule.size());
    premiumCache.deletePatchSchedule();
    patchSchedule = redisManager.redisCaches().getById(premiumCache.id()).asPremium().listPatchSchedules();
    Assert.assertNull(patchSchedule);
// currently throws because SAS url of the container should be provided as
// {"error":{
//      "code":"InvalidRequestBody",
//      "message": "One of the SAS URIs provided could not be used for the following reason:
//                  The SAS token is poorly formatted.\r\nRequestID=ed105089-b93b-427e-9cbb-d78ed80d23b0",
//      "target":null}}
// com.microsoft.azure.CloudException: One of the SAS URIs provided could not be used for the following reason: The SAS token is poorly formatted.
/*premiumCache.exportData(storageAccount.name(),"snapshot1");

        premiumCache.importData(Arrays.asList("snapshot1"));*/
}
Also used : CloudException(com.microsoft.azure.CloudException) StorageAccount(com.microsoft.azure.management.storage.StorageAccount) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Test(org.junit.Test)

Example 14 with ResourceGroup

use of com.microsoft.azure.management.resources.ResourceGroup in project azure-sdk-for-java by Azure.

the class GroupPagedList method nextPage.

@Override
public Page<E> nextPage(String s) {
    if (resourceGroupItr.hasNext()) {
        ResourceGroup resourceGroup = resourceGroupItr.next();
        PageImpl<E> page = new PageImpl<>();
        page.setItems(listNextGroup(resourceGroup.name()));
        page.setNextPageLink(s);
        return page;
    } else {
        // return an empty page without next link so that iteration will terminate
        PageImpl<E> page = new PageImpl<>();
        page.setItems(new ArrayList<E>());
        return page;
    }
}
Also used : PageImpl(com.microsoft.azure.management.resources.implementation.PageImpl) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup)

Example 15 with ResourceGroup

use of com.microsoft.azure.management.resources.ResourceGroup in project azure-sdk-for-java by Azure.

the class NetworkInterfaceOperationsTests method canCreateBatchOfNetworkInterfaces.

@Test
public void canCreateBatchOfNetworkInterfaces() throws Exception {
    ResourceGroups resourceGroups = resourceManager.resourceGroups();
    Networks networks = networkManager.networks();
    NetworkInterfaces networkInterfaces = networkManager.networkInterfaces();
    Creatable<ResourceGroup> resourceGroupCreatable = resourceGroups.define(RG_NAME).withRegion(Region.US_EAST);
    final String vnetName = "vnet1212";
    Creatable<Network> networkCreatable = networks.define(vnetName).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withAddressSpace("10.0.0.0/28");
    // Prepare a batch of nics
    //
    final String nic1Name = "nic1";
    Creatable<NetworkInterface> networkInterface1Creatable = networkInterfaces.define(nic1Name).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.5");
    final String nic2Name = "nic2";
    Creatable<NetworkInterface> networkInterface2Creatable = networkInterfaces.define(nic2Name).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.6");
    final String nic3Name = "nic3";
    Creatable<NetworkInterface> networkInterface3Creatable = networkInterfaces.define(nic3Name).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.7");
    final String nic4Name = "nic4";
    Creatable<NetworkInterface> networkInterface4Creatable = networkInterfaces.define(nic4Name).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.8");
    @SuppressWarnings("unchecked") Collection<NetworkInterface> batchNics = networkInterfaces.create(networkInterface1Creatable, networkInterface2Creatable, networkInterface3Creatable, networkInterface4Creatable).values();
    Assert.assertTrue(batchNics.size() == 4);
    HashMap<String, Boolean> found = new LinkedHashMap<>();
    for (NetworkInterface nic : batchNics) {
        if (nic.name().equalsIgnoreCase(nic1Name)) {
            found.put(nic1Name, true);
        }
        if (nic.name().equalsIgnoreCase(nic2Name)) {
            found.put(nic2Name, true);
        }
        if (nic.name().equalsIgnoreCase(nic3Name)) {
            found.put(nic3Name, true);
        }
        if (nic.name().equalsIgnoreCase(nic4Name)) {
            found.put(nic4Name, true);
        }
    }
    Assert.assertTrue(found.size() == 4);
}
Also used : ResourceGroups(com.microsoft.azure.management.resources.ResourceGroups) LinkedHashMap(java.util.LinkedHashMap) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Test(org.junit.Test)

Aggregations

ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)53 Test (org.junit.Test)22 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)13 SubscriptionDetail (com.microsoft.azuretools.authmanage.models.SubscriptionDetail)12 ArrayList (java.util.ArrayList)10 Network (com.microsoft.azure.management.network.Network)9 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)9 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)8 Creatable (com.microsoft.azure.management.resources.fluentcore.model.Creatable)7 AzureManager (com.microsoft.azuretools.sdkmanage.AzureManager)7 Azure (com.microsoft.azure.management.Azure)6 AppServicePlan (com.microsoft.azure.management.appservice.AppServicePlan)6 WebApp (com.microsoft.azure.management.appservice.WebApp)6 HashMap (java.util.HashMap)5 StopWatch (org.apache.commons.lang3.time.StopWatch)5 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)4 Location (com.microsoft.azure.management.resources.Location)4 CloudException (com.microsoft.azure.CloudException)3 Period (org.joda.time.Period)3 ApplicationGateway (com.microsoft.azure.management.network.ApplicationGateway)2