Search in sources :

Example 91 with Azure

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

the class ManageSqlDatabasesAcrossDifferentDataCenters method main.

/**
     * Main entry point.
     * @param args the parameters
     */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder().withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER).withSerializerAdapter(new AzureJacksonAdapter()).withReadTimeout(150, TimeUnit.SECONDS).withLogLevel(LogLevel.BODY).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();
        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());
        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureResponseBuilder(com.microsoft.azure.AzureResponseBuilder) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) File(java.io.File)

Example 92 with Azure

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

the class ManageSqlFirewallRules method main.

/**
     * Main entry point.
     * @param args the parameters
     */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder().withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER).withSerializerAdapter(new AzureJacksonAdapter()).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).withReadTimeout(150, TimeUnit.SECONDS).withLogLevel(LogLevel.BODY).withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();
        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());
        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureResponseBuilder(com.microsoft.azure.AzureResponseBuilder) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) File(java.io.File)

Example 93 with Azure

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

the class AzureDeploymentProgressNotification method deployToDockerContainer.

public void deployToDockerContainer(AzureDockerImageInstance dockerImageInstance, String url) {
    Date startDate = new Date();
    Map<String, String> postEventProperties = new HashMap<String, String>();
    postEventProperties.put("DockerFileOption", dockerImageInstance.predefinedDockerfile);
    String descriptionTask = String.format("Publishing %s into Docker host %s at port(s) %s", new File(dockerImageInstance.artifactPath).getName(), dockerImageInstance.host.name, dockerImageInstance.dockerPortSettings);
    try {
        String msg = String.format("Publishing %s to Docker host %s ...", new File(dockerImageInstance.artifactPath).getName(), dockerImageInstance.host.name);
        notifyProgress(descriptionTask, startDate, null, 5, msg);
        AzureManager azureAuthManager = AuthMethodManager.getInstance().getAzureManager();
        // not signed in
        if (azureAuthManager == null) {
            throw new RuntimeException("User not signed in");
        }
        AzureDockerHostsManager dockerManager = AzureDockerHostsManager.getAzureDockerHostsManagerEmpty(azureAuthManager);
        Azure azureClient = dockerManager.getSubscriptionsMap().get(dockerImageInstance.sid).azureClient;
        KeyVaultClient keyVaultClient = dockerManager.getSubscriptionsMap().get(dockerImageInstance.sid).keyVaultClient;
        if (dockerImageInstance.hasNewDockerHost) {
            msg = String.format("Creating new virtual machine %s ...", dockerImageInstance.host.name);
            notifyProgress(descriptionTask, startDate, null, 10, msg);
            if (AzureDockerUtils.DEBUG)
                System.out.println("Creating new virtual machine: " + new Date().toString());
            AzureDockerVMOps.createDockerHostVM(azureClient, dockerImageInstance.host);
            if (AzureDockerUtils.DEBUG)
                System.out.println("Done creating new virtual machine: " + new Date().toString());
            msg = String.format("Get new VM details...");
            notifyProgress(descriptionTask, startDate, null, 30, msg);
            if (AzureDockerUtils.DEBUG)
                System.out.println("Getting the new Docker host details: " + new Date().toString());
            VirtualMachine vm = azureClient.virtualMachines().getByResourceGroup(dockerImageInstance.host.hostVM.resourceGroupName, dockerImageInstance.host.hostVM.name);
            if (vm != null) {
                DockerHost updatedHost = AzureDockerVMOps.getDockerHost(vm, dockerManager.getDockerVaultsMap());
                if (updatedHost != null) {
                    dockerImageInstance.host.hostVM = updatedHost.hostVM;
                    dockerImageInstance.host.apiUrl = updatedHost.apiUrl;
                }
            }
            if (AzureDockerUtils.DEBUG)
                System.out.println("Done getting the new Docker host details: " + new Date().toString());
            msg = String.format("Waiting for virtual machine to be up %s ...", dockerImageInstance.host.name);
            notifyProgress(descriptionTask, startDate, null, 35, msg);
            if (AzureDockerUtils.DEBUG)
                System.out.println("Waiting for virtual machine to be up: " + new Date().toString());
            AzureDockerVMOps.waitForVirtualMachineStartup(azureClient, dockerImageInstance.host);
            if (AzureDockerUtils.DEBUG)
                System.out.println("Done Waiting for virtual machine to be up: " + new Date().toString());
            msg = String.format("Configuring Docker service for %s ...", dockerImageInstance.host.name);
            notifyProgress(descriptionTask, startDate, null, 45, msg);
            if (AzureDockerUtils.DEBUG)
                System.out.println("Configuring Docker host: " + new Date().toString());
            AzureDockerVMOps.installDocker(dockerImageInstance.host);
            if (AzureDockerUtils.DEBUG)
                System.out.println("Done configuring Docker host: " + new Date().toString());
            msg = String.format("Updating Docker hosts ...");
            notifyProgress(descriptionTask, startDate, null, 50, msg);
            if (AzureDockerUtils.DEBUG)
                System.out.println("Refreshing docker hosts: " + new Date().toString());
            //            dockerManager.refreshDockerHostDetails();
            vm = azureClient.virtualMachines().getByResourceGroup(dockerImageInstance.host.hostVM.resourceGroupName, dockerImageInstance.host.hostVM.name);
            if (vm != null) {
                DockerHost updatedHost = AzureDockerVMOps.getDockerHost(vm, dockerManager.getDockerVaultsMap());
                if (updatedHost != null) {
                    updatedHost.sid = dockerImageInstance.host.sid;
                    updatedHost.hostVM.sid = dockerImageInstance.host.hostVM.sid;
                    if (updatedHost.certVault == null) {
                        updatedHost.certVault = dockerImageInstance.host.certVault;
                        updatedHost.hasPwdLogIn = dockerImageInstance.host.hasPwdLogIn;
                        updatedHost.hasSSHLogIn = dockerImageInstance.host.hasSSHLogIn;
                        updatedHost.isTLSSecured = dockerImageInstance.host.isTLSSecured;
                    }
                    dockerManager.addDockerHostDetails(updatedHost);
                    if (AzureUIRefreshCore.listeners != null) {
                        AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.ADD, updatedHost));
                    }
                }
            }
            if (AzureDockerUtils.DEBUG)
                System.out.println("Done refreshing Docker hosts: " + new Date().toString());
            if (AzureDockerUtils.DEBUG)
                System.out.println("Finished setting up Docker host");
        } else {
            msg = String.format("Using virtual machine %s ...", dockerImageInstance.host.name);
            notifyProgress(descriptionTask, startDate, null, 50, msg);
        }
        if (dockerImageInstance.host.session == null) {
            if (AzureDockerUtils.DEBUG)
                System.out.println("Opening a remote connection to the Docker host: " + new Date().toString());
            dockerImageInstance.host.session = AzureDockerSSHOps.createLoginInstance(dockerImageInstance.host);
            if (AzureDockerUtils.DEBUG)
                System.out.println("Done opening a remote connection to the Docker host: " + new Date().toString());
        }
        if (dockerImageInstance.hasNewDockerHost) {
            if (dockerImageInstance.host.certVault != null && dockerImageInstance.host.certVault.hostName != null) {
                AzureDockerUIResources.createDockerKeyVault(null, dockerImageInstance.host, dockerManager);
            }
        }
        msg = String.format("Uploading Dockerfile and artifact %s on %s ...", dockerImageInstance.artifactName, dockerImageInstance.host.name);
        notifyProgress(descriptionTask, startDate, null, 60, msg);
        if (AzureDockerUtils.DEBUG)
            System.out.println("Uploading Dockerfile and artifact: " + new Date().toString());
        AzureDockerVMOps.uploadDockerfileAndArtifact(dockerImageInstance, dockerImageInstance.host.session);
        if (AzureDockerUtils.DEBUG)
            System.out.println("Uploading Dockerfile and artifact: " + new Date().toString());
        msg = String.format("Creating Docker image %s on %s ...", dockerImageInstance.dockerImageName, dockerImageInstance.host.name);
        notifyProgress(descriptionTask, startDate, null, 80, msg);
        if (AzureDockerUtils.DEBUG)
            System.out.println("Creating a Docker image to the Docker host: " + new Date().toString());
        AzureDockerImageOps.create(dockerImageInstance, dockerImageInstance.host.session);
        if (AzureDockerUtils.DEBUG)
            System.out.println("Done creating a Docker image to the Docker host: " + new Date().toString());
        msg = String.format("Creating Docker container %s for image %s on %s ...", dockerImageInstance.dockerContainerName, dockerImageInstance.dockerImageName, dockerImageInstance.host.name);
        notifyProgress(descriptionTask, startDate, null, 90, msg);
        if (AzureDockerUtils.DEBUG)
            System.out.println("Creating a Docker container to the Docker host: " + new Date().toString());
        AzureDockerContainerOps.create(dockerImageInstance, dockerImageInstance.host.session);
        if (AzureDockerUtils.DEBUG)
            System.out.println("Done creating a Docker container to the Docker host: " + new Date().toString());
        msg = String.format("Starting Docker container %s for image %s on %s ...", dockerImageInstance.dockerContainerName, dockerImageInstance.dockerImageName, dockerImageInstance.host.name);
        notifyProgress(descriptionTask, startDate, null, 95, msg);
        if (AzureDockerUtils.DEBUG)
            System.out.println("Starting a Docker container to the Docker host: " + new Date().toString());
        AzureDockerContainerOps.start(dockerImageInstance, dockerImageInstance.host.session);
        if (AzureDockerUtils.DEBUG)
            System.out.println("Done starting a Docker container to the Docker host: " + new Date().toString());
        notifyProgress(descriptionTask, startDate, url, 100, message("runStatus"), dockerImageInstance.host.name);
    } catch (InterruptedException e) {
        postEventProperties.put("PublishInterruptedError", e.getMessage());
        notifyProgress(descriptionTask, startDate, url, 100, message("runStatus"), dockerImageInstance.host.name);
    } catch (Exception ee) {
        postEventProperties.put("PublishError", ee.getMessage());
        notifyProgress(descriptionTask, startDate, url, 100, "Error: %s", ee.getMessage());
    }
    AppInsightsClient.createByType(AppInsightsClient.EventType.DockerContainer, null, "Deploy", postEventProperties);
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) HashMap(java.util.HashMap) KeyVaultClient(com.microsoft.azure.keyvault.KeyVaultClient) AzureUIRefreshEvent(com.microsoft.azuretools.utils.AzureUIRefreshEvent) Date(java.util.Date) MalformedURLException(java.net.MalformedURLException) DockerHost(com.microsoft.azure.docker.model.DockerHost) AzureDockerHostsManager(com.microsoft.azure.docker.AzureDockerHostsManager) File(java.io.File) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 94 with Azure

use of com.microsoft.azure.management.Azure 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 95 with Azure

use of com.microsoft.azure.management.Azure 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

Azure (com.microsoft.azure.management.Azure)144 File (java.io.File)75 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)20 ExecutorService (java.util.concurrent.ExecutorService)17 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)16 DiskService (com.vmware.photon.controller.model.resources.DiskService)16 ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)15 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)15 AzureUriPaths (com.vmware.photon.controller.model.adapters.azure.AzureUriPaths)15 AzureConstants (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants)15 AzureResourceType (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType)15 AzureConstants.getQueryResultLimit (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.getQueryResultLimit)15 AzureDeferredResultServiceCallback (com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback)15 Default (com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default)15 AzureSdkClients (com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients)15 AzureUtils (com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils)15 AzureUtils.getResourceGroupName (com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getResourceGroupName)15 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)15 ComputeEnumerateAdapterRequest (com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest)15