Search in sources :

Example 51 with ResourceGroup

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

the class AzureModelController method updateSubscriptionMaps.

public static synchronized void updateSubscriptionMaps(IProgressIndicator progressIndicator) throws IOException, CanceledByUserException, AuthException {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    // not signed in
    if (azureManager == null) {
        return;
    }
    if (progressIndicator != null && progressIndicator.isCanceled()) {
        clearAll();
        throw new CanceledByUserException();
    }
    AzureModel azureModel = AzureModel.getInstance();
    // to get regions we nees subscription objects
    if (progressIndicator != null)
        progressIndicator.setText("Reading subscription list...");
    List<Subscription> sl = azureManager.getSubscriptions();
    // convert to map to easier find by sid
    Map<String, Subscription> sidToSubscriptionMap = azureModel.createSidToSubscriptionMap();
    for (Subscription s : sl) {
        sidToSubscriptionMap.put(s.subscriptionId(), s);
    }
    azureModel.setSidToSubscriptionMap(sidToSubscriptionMap);
    Map<SubscriptionDetail, List<Location>> sdlocMap = azureModel.createSubscriptionToRegionMap();
    Map<SubscriptionDetail, List<ResourceGroup>> sdrgMap = azureModel.createSubscriptionToResourceGroupMap();
    SubscriptionManager subscriptionManager = azureManager.getSubscriptionManager();
    subscriptionManager.addListener(subscriptionSelectionListener);
    List<SubscriptionDetail> sdl = subscriptionManager.getSubscriptionDetails();
    for (SubscriptionDetail sd : sdl) {
        if (!sd.isSelected())
            continue;
        if (progressIndicator != null && progressIndicator.isCanceled()) {
            clearAll();
            throw new CanceledByUserException();
        }
        System.out.println("sn : " + sd.getSubscriptionName());
        if (progressIndicator != null)
            progressIndicator.setText("Reading subscription '" + sd.getSubscriptionName() + "'");
        Azure azure = azureManager.getAzure(sd.getSubscriptionId());
        List<ResourceGroup> rgList = azure.resourceGroups().list();
        sdrgMap.put(sd, rgList);
        List<Location> locl = sidToSubscriptionMap.get(sd.getSubscriptionId()).listLocations();
        Collections.sort(locl, new Comparator<Location>() {

            @Override
            public int compare(Location lhs, Location rhs) {
                return lhs.displayName().compareTo(rhs.displayName());
            }
        });
        sdlocMap.put(sd, locl);
    }
    azureModel.setSubscriptionToResourceGroupMap(sdrgMap);
    azureModel.setSubscriptionToLocationMap(sdlocMap);
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) Subscription(com.microsoft.azure.management.resources.Subscription) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Location(com.microsoft.azure.management.resources.Location)

Example 52 with ResourceGroup

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

the class AzureModelController method subscriptionSelectionChanged.

private static synchronized void subscriptionSelectionChanged(IProgressIndicator progressIndicator) throws IOException, AuthException {
    System.out.println("AzureModelController.subscriptionSelectionChanged: starting");
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    // not signed in
    if (azureManager == null) {
        System.out.println("AzureModelController.subscriptionSelectionChanged: azureManager == null -> return");
        return;
    }
    SubscriptionManager subscriptionManager = azureManager.getSubscriptionManager();
    AzureModel azureModel = AzureModel.getInstance();
    Map<SubscriptionDetail, List<ResourceGroup>> srgMap = azureModel.getSubscriptionToResourceGroupMap();
    if (srgMap == null) {
        System.out.println("AzureModelController.subscriptionSelectionChanged: srgMap == null -> return");
        return;
    }
    Map<String, Subscription> sidToSubscriptionMap = azureModel.getSidToSubscriptionMap();
    if (sidToSubscriptionMap == null) {
        System.out.println("AzureModelController.subscriptionSelectionChanged: sidToSubscriptionMap == null -> return");
        return;
    }
    Map<ResourceGroup, List<WebApp>> rgwaMap = azureModel.getResourceGroupToWebAppMap();
    Map<ResourceGroup, List<AppServicePlan>> rgspMap = azureModel.getResourceGroupToAppServicePlanMap();
    System.out.println("AzureModelController.subscriptionSelectionChanged: getting subscription details...");
    List<SubscriptionDetail> sdl = subscriptionManager.getSubscriptionDetails();
    if (sdl == null) {
        System.out.println("AzureModelController.subscriptionSelectionChanged: sdl == null -> return");
        return;
    }
    for (SubscriptionDetail sd : sdl) {
        if (!srgMap.containsKey(sd)) {
            if (!sd.isSelected())
                continue;
            if (progressIndicator != null && progressIndicator.isCanceled()) {
                progressIndicator.setText("Cancelling...");
                clearAll();
                return;
            // FIXME: throw exception?
            }
            Azure azure = azureManager.getAzure(sd.getSubscriptionId());
            // subscription locations
            List<Subscription> sl = azureManager.getSubscriptions();
            System.out.println("Updating subscription locations");
            Subscription subscription = sidToSubscriptionMap.get(sd.getSubscriptionId());
            if (progressIndicator != null)
                progressIndicator.setText(String.format("Updating subscription '%s' locations...", subscription.displayName()));
            List<Location> locl = subscription.listLocations();
            Collections.sort(locl, new Comparator<Location>() {

                @Override
                public int compare(Location lhs, Location rhs) {
                    return lhs.displayName().compareTo(rhs.displayName());
                }
            });
            Map<SubscriptionDetail, List<Location>> sdlocMap = azureModel.getSubscriptionToLocationMap();
            sdlocMap.put(sd, locl);
            // resource group maps
            List<ResourceGroup> rgList = azure.resourceGroups().list();
            srgMap.put(sd, rgList);
            updateResGrDependency(azure, rgList, progressIndicator, rgwaMap, rgspMap);
        } else {
            // find and modify the key
            for (SubscriptionDetail sdk : srgMap.keySet()) {
                if (sdk.equals(sd)) {
                    sdk.setSelected(sd.isSelected());
                }
            }
        }
    }
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) Subscription(com.microsoft.azure.management.resources.Subscription) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Location(com.microsoft.azure.management.resources.Location)

Example 53 with ResourceGroup

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

the class WebAppUtils method createAppService.

public static WebApp createAppService(IProgressIndicator progressIndicator, CreateAppServiceModel model) throws IOException, WebAppException, InterruptedException, AzureCmdException {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    // not signed in
    if (azureManager == null) {
        return null;
    }
    Azure azure = azureManager.getAzure(model.subscriptionDetail.getSubscriptionId());
    AppServicePlan appServicePlan = null;
    if (model.isAppServicePlanCreateNew) {
        AppServicePlan.DefinitionStages.WithGroup ds1 = azure.appServices().appServicePlans().define(model.appServicePlanNameCreateNew).withRegion(model.appServicePlanLocationCreateNew.name());
        AppServicePlan.DefinitionStages.WithPricingTier ds2;
        if (model.isResourceGroupCreateNew) {
            ds2 = ds1.withNewResourceGroup(model.resourceGroupNameCreateNew);
        } else {
            ds2 = ds1.withExistingResourceGroup(model.resourceGroup);
        }
        appServicePlan = ds2.withPricingTier(model.appServicePricingTierCreateNew).withOperatingSystem(OperatingSystem.WINDOWS).create();
    } else {
        appServicePlan = model.appServicePlan;
    }
    WebApp.DefinitionStages.Blank definitionStages = azure.webApps().define(model.webAppName);
    WebAppBase.DefinitionStages.WithCreate<WebApp> withCreate;
    WebApp.DefinitionStages.ExistingWindowsPlanWithGroup ds1 = definitionStages.withExistingWindowsPlan(appServicePlan);
    if (model.isResourceGroupCreateNew) {
        withCreate = ds1.withNewResourceGroup(model.resourceGroupNameCreateNew);
    } else {
        withCreate = ds1.withExistingResourceGroup(model.resourceGroup);
    }
    if (model.jdkDownloadUrl == null) {
        // no custom jdk
        withCreate = withCreate.withJavaVersion(JavaVersion.JAVA_8_NEWEST).withWebContainer(model.webContainer);
    }
    WebApp myWebApp = withCreate.create();
    if (model.jdkDownloadUrl != null) {
        progressIndicator.setText("Deploying custom jdk...");
        WebAppUtils.deployCustomJdk(myWebApp, model.jdkDownloadUrl, model.webContainer, progressIndicator);
    }
    // update cache
    if (model.isResourceGroupCreateNew) {
        ResourceGroup rg = azure.resourceGroups().getByName(model.resourceGroupNameCreateNew);
        if (rg == null) {
            throw new AzureCmdException(String.format("azure.resourceGroups().getByName(%s) returned null"), model.resourceGroupNameCreateNew);
        }
        AzureModelController.addNewResourceGroup(model.subscriptionDetail, rg);
        AzureModelController.addNewWebAppToJustCreatedResourceGroup(rg, myWebApp);
        if (model.isAppServicePlanCreateNew) {
            AzureModelController.addNewAppServicePlanToJustCreatedResourceGroup(rg, appServicePlan);
        } else {
            // add empty list
            AzureModelController.addNewAppServicePlanToJustCreatedResourceGroup(rg, null);
        }
    } else {
        ResourceGroup rg = model.resourceGroup;
        AzureModelController.addNewWebAppToExistingResourceGroup(rg, myWebApp);
        if (model.isAppServicePlanCreateNew) {
            //AppServicePlan asp = azure.appServices().appServicePlans().getById(myWebApp.appServicePlanId());
            AzureModelController.addNewAppServicePlanToExistingResourceGroup(rg, appServicePlan);
        }
    }
    return myWebApp;
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup)

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