Search in sources :

Example 16 with AzureManager

use of com.microsoft.azuretools.sdkmanage.AzureManager in project azure-tools-for-java by Microsoft.

the class WebAppUtils method deleteAppService.

public static void deleteAppService(WebAppDetails webAppDetails) throws IOException {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    Azure azure = azureManager.getAzure(webAppDetails.subscriptionDetail.getSubscriptionId());
    azure.webApps().deleteById(webAppDetails.webApp.id());
    // check asp still exists
    AppServicePlan asp = azure.appServices().appServicePlans().getById(webAppDetails.appServicePlan.id());
    System.out.println("asp is " + (asp == null ? "null -> removing form cache" : asp.name()));
    // update cache
    AzureModelController.removeWebAppFromResourceGroup(webAppDetails.resourceGroup, webAppDetails.webApp);
    if (asp == null) {
        AzureModelController.removeAppServicePlanFromResourceGroup(webAppDetails.appServicePlanResourceGroup, webAppDetails.appServicePlan);
    }
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager)

Example 17 with AzureManager

use of com.microsoft.azuretools.sdkmanage.AzureManager 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 18 with AzureManager

use of com.microsoft.azuretools.sdkmanage.AzureManager in project azure-tools-for-java by Microsoft.

the class AzureSDKManager method createStorageAccount.

public static StorageAccount createStorageAccount(String subscriptionId, String name, String region, boolean newResourceGroup, String resourceGroup, Kind kind, AccessTier accessTier, boolean enableEncription, String skuName) throws Exception {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    Azure azure = azureManager.getAzure(subscriptionId);
    StorageAccount.DefinitionStages.WithGroup newStorageAccountBlank = azure.storageAccounts().define(name).withRegion(region);
    StorageAccount.DefinitionStages.WithCreate newStorageAccountWithGroup;
    if (newResourceGroup) {
        newStorageAccountWithGroup = newStorageAccountBlank.withNewResourceGroup(resourceGroup);
    } else {
        newStorageAccountWithGroup = newStorageAccountBlank.withExistingResourceGroup(resourceGroup);
    }
    if (kind == Kind.BLOB_STORAGE) {
        newStorageAccountWithGroup = newStorageAccountWithGroup.withBlobStorageAccountKind().withAccessTier(accessTier);
    } else {
        newStorageAccountWithGroup = newStorageAccountWithGroup.withGeneralPurposeAccountKind();
    }
    if (enableEncription) {
        newStorageAccountWithGroup = newStorageAccountWithGroup.withEncryption(new Encryption());
    }
    return newStorageAccountWithGroup.withSku(SkuName.fromString(skuName)).create();
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) Encryption(com.microsoft.azure.management.storage.Encryption)

Example 19 with AzureManager

use of com.microsoft.azuretools.sdkmanage.AzureManager in project azure-tools-for-java by Microsoft.

the class AzureSDKManager method getLocationsForApplicationInsights.

public static List<String> getLocationsForApplicationInsights(SubscriptionDetail subscription) throws Exception {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    if (azureManager == null) {
        // not signed in
        return null;
    }
    String tenantId = subscription.getTenantId();
    return getApplicationManagementClient(tenantId, azureManager.getAccessToken(tenantId)).getAvailableGeoLocations();
}
Also used : AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager)

Example 20 with AzureManager

use of com.microsoft.azuretools.sdkmanage.AzureManager in project azure-tools-for-java by Microsoft.

the class AzureSDKManager method createApplicationInsightsResource.

public static Resource createApplicationInsightsResource(@NotNull SubscriptionDetail subscription, @NotNull String resourceGroupName, boolean isNewGroup, @NotNull String resourceName, @NotNull String location) throws Exception {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    if (azureManager == null) {
        // not signed in
        return null;
    }
    String tenantId = subscription.getTenantId();
    ApplicationInsightsManagementClient client = getApplicationManagementClient(tenantId, azureManager.getAccessToken(tenantId));
    if (isNewGroup) {
        client.createResourceGroup(subscription.getSubscriptionId(), resourceGroupName, location);
    }
    return client.createResource(subscription.getSubscriptionId(), resourceGroupName, resourceName, location);
}
Also used : AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) ApplicationInsightsManagementClient(com.microsoft.applicationinsights.management.rest.ApplicationInsightsManagementClient)

Aggregations

AzureManager (com.microsoft.azuretools.sdkmanage.AzureManager)48 SubscriptionDetail (com.microsoft.azuretools.authmanage.models.SubscriptionDetail)17 Azure (com.microsoft.azure.management.Azure)15 SubscriptionManager (com.microsoft.azuretools.authmanage.SubscriptionManager)15 AzureCmdException (com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)12 AzureDockerHostsManager (com.microsoft.azure.docker.AzureDockerHostsManager)8 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)8 DockerHost (com.microsoft.azure.docker.model.DockerHost)6 Location (com.microsoft.azure.management.resources.Location)4 AzureDockerImageInstance (com.microsoft.azure.docker.model.AzureDockerImageInstance)3 EditableDockerHost (com.microsoft.azure.docker.model.EditableDockerHost)3 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)3 Subscription (com.microsoft.azure.management.resources.Subscription)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)3 KeyVaultClient (com.microsoft.azure.keyvault.KeyVaultClient)2 Network (com.microsoft.azure.management.network.Network)2 Kind (com.microsoft.azure.management.storage.Kind)2