Search in sources :

Example 1 with Subscription

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

the class AzureTests method listSubscriptions.

/**
     * Tests subscription listing.
     * @throws Exception
     */
@Test
public void listSubscriptions() throws Exception {
    Assert.assertTrue(0 < azure.subscriptions().list().size());
    Subscription subscription = azure.getCurrentSubscription();
    Assert.assertNotNull(subscription);
    Assert.assertTrue(azure.subscriptionId().equalsIgnoreCase(subscription.subscriptionId()));
}
Also used : Subscription(com.microsoft.azure.management.resources.Subscription) Test(org.junit.Test)

Example 2 with Subscription

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

the class AzureDockerUtils method refreshDockerSubscriptions.

public static Map<String, AzureDockerSubscription> refreshDockerSubscriptions(AzureManager azureAuthManager) {
    Map<String, AzureDockerSubscription> subsMap = new HashMap<>();
    try {
        if (DEBUG)
            System.out.format("Get AzureDockerHostsManage subscription details: %s\n", new Date().toString());
        SubscriptionManager subscriptionManager = azureAuthManager.getSubscriptionManager();
        List<SubscriptionDetail> subscriptions = subscriptionManager.getSubscriptionDetails();
        if (subscriptions != null) {
            if (DEBUG)
                System.out.format("Get AzureDockerHostsManage Docker subscription details: %s\n", new Date().toString());
            Observable.from(subscriptions).flatMap(subscriptionDetail -> {
                return Observable.create(new Observable.OnSubscribe<AzureDockerSubscription>() {

                    @Override
                    public void call(Subscriber<? super AzureDockerSubscription> dockerSubscriptionSubscriber) {
                        if (subscriptionDetail.isSelected()) {
                            AzureDockerSubscription dockerSubscription = new AzureDockerSubscription();
                            dockerSubscription.id = subscriptionDetail.getSubscriptionId();
                            try {
                                if (DEBUG)
                                    System.out.format("\tGet AzureDockerHostsManage Docker subscription: %s at %s\n", dockerSubscription.id, new Date().toString());
                                dockerSubscription.tid = subscriptionDetail.getTenantId();
                                dockerSubscription.name = subscriptionDetail.getSubscriptionName();
                                dockerSubscription.azureClient = azureAuthManager.getAzure(dockerSubscription.id);
                                dockerSubscription.keyVaultClient = azureAuthManager.getKeyVaultClient(subscriptionDetail.getTenantId());
                                dockerSubscription.isSelected = true;
                                if (AzureDockerUtils.hasServicePrincipalAzureManager(azureAuthManager)) {
                                    dockerSubscription.userId = null;
                                    dockerSubscription.servicePrincipalId = azureAuthManager.getCurrentUserId();
                                } else {
                                    dockerSubscription.userId = azureAuthManager.getCurrentUserId();
                                    dockerSubscription.servicePrincipalId = null;
                                }
                                dockerSubscriptionSubscriber.onNext(dockerSubscription);
                            } catch (Exception e) {
                                e.printStackTrace();
                                DefaultLoader.getUIHelper().showError(e.getMessage(), "Error Loading Subscription Details for " + dockerSubscription.id);
                            }
                        }
                        dockerSubscriptionSubscriber.onCompleted();
                    }
                }).subscribeOn(Schedulers.io());
            }).subscribeOn(Schedulers.io()).toBlocking().subscribe(new Action1<AzureDockerSubscription>() {

                @Override
                public void call(AzureDockerSubscription dockerSubscription) {
                    subsMap.put(dockerSubscription.id, dockerSubscription);
                }
            });
        }
        if (DEBUG)
            System.out.format("Get AzureDockerHostsManage locations: %s\n", new Date().toString());
        List<Subscription> azureSubscriptionList = azureAuthManager.getSubscriptions();
        for (Subscription subscription : azureSubscriptionList) {
            AzureDockerSubscription dockerSubscription = subsMap.get(subscription.subscriptionId());
            if (dockerSubscription != null) {
                List<String> locations = subscription.listLocations().stream().sorted(Comparator.comparing(Location::displayName)).map(o -> o.name().toLowerCase()).collect(Collectors.toList());
                dockerSubscription.locations = locations;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        DefaultLoader.getUIHelper().showError(e.getMessage(), "Error loading subscription details");
    }
    return subsMap;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) java.util(java.util) StorageAccount(com.microsoft.azure.management.storage.StorageAccount) URL(java.net.URL) Subnet(com.microsoft.azure.management.network.Subnet) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) Callable(java.util.concurrent.Callable) Action1(rx.functions.Action1) Location(com.microsoft.azure.management.resources.Location) Observable(rx.Observable) Azure(com.microsoft.azure.management.Azure) Network(com.microsoft.azure.management.network.Network) Func1(rx.functions.Func1) ServicePrincipalAzureManager(com.microsoft.azuretools.sdkmanage.ServicePrincipalAzureManager) Schedulers(rx.schedulers.Schedulers) DefaultLoader(com.microsoft.tooling.msservices.components.DefaultLoader) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) Vault(com.microsoft.azure.management.keyvault.Vault) Subscription(com.microsoft.azure.management.resources.Subscription) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) Subscriber(rx.Subscriber) AzureDockerCertVaultOps(com.microsoft.azure.docker.ops.AzureDockerCertVaultOps) AzureRegisterProviderNamespaces(com.microsoft.azuretools.utils.AzureRegisterProviderNamespaces) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) Pair(com.microsoft.azuretools.utils.Pair) CountDownLatch(java.util.concurrent.CountDownLatch) Action0(rx.functions.Action0) com.microsoft.azure.docker.model(com.microsoft.azure.docker.model) AzureDockerVMOps(com.microsoft.azure.docker.ops.AzureDockerVMOps) KeyVaultClient(com.microsoft.azure.keyvault.KeyVaultClient) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) Observable(rx.Observable) UnknownHostException(java.net.UnknownHostException) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) Subscription(com.microsoft.azure.management.resources.Subscription)

Example 3 with Subscription

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

the class AzureTests method listLocations.

/**
     * Tests location listing.
     * @throws Exception
     */
@Test
public void listLocations() throws Exception {
    Subscription subscription = azure.getCurrentSubscription();
    Assert.assertNotNull(subscription);
    for (Location location : subscription.listLocations()) {
        Region region = Region.findByLabelOrName(location.name());
        Assert.assertNotNull(region);
        Assert.assertEquals(region, location.region());
        Assert.assertEquals(region.name().toLowerCase(), location.name().toLowerCase());
    }
    Location location = subscription.getLocationByRegion(Region.US_WEST);
    Assert.assertNotNull(location);
    Assert.assertTrue(Region.US_WEST.name().equalsIgnoreCase(location.name()));
}
Also used : Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) Subscription(com.microsoft.azure.management.resources.Subscription) Location(com.microsoft.azure.management.resources.Location) Test(org.junit.Test)

Example 4 with Subscription

use of com.microsoft.azure.management.resources.Subscription 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 5 with Subscription

use of com.microsoft.azure.management.resources.Subscription 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)

Aggregations

Subscription (com.microsoft.azure.management.resources.Subscription)8 Location (com.microsoft.azure.management.resources.Location)4 SubscriptionDetail (com.microsoft.azuretools.authmanage.models.SubscriptionDetail)4 Azure (com.microsoft.azure.management.Azure)3 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)3 Tenant (com.microsoft.azure.management.resources.Tenant)3 SubscriptionManager (com.microsoft.azuretools.authmanage.SubscriptionManager)3 AzureManager (com.microsoft.azuretools.sdkmanage.AzureManager)3 Pair (com.microsoft.azuretools.utils.Pair)2 LinkedList (java.util.LinkedList)2 Test (org.junit.Test)2 com.microsoft.azure.docker.model (com.microsoft.azure.docker.model)1 AzureDockerCertVaultOps (com.microsoft.azure.docker.ops.AzureDockerCertVaultOps)1 AzureDockerVMOps (com.microsoft.azure.docker.ops.AzureDockerVMOps)1 KeyVaultClient (com.microsoft.azure.keyvault.KeyVaultClient)1 Vault (com.microsoft.azure.management.keyvault.Vault)1 Network (com.microsoft.azure.management.network.Network)1 Subnet (com.microsoft.azure.management.network.Subnet)1 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)1 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)1