Search in sources :

Example 31 with AzureOperation

use of com.microsoft.azure.toolkit.lib.common.operation.AzureOperation in project azure-tools-for-java by Microsoft.

the class AuthMethodManager method loadSettings.

@AzureOperation(name = "account|auth_setting.load", type = AzureOperation.Type.TASK)
private static AuthMethodDetails loadSettings() {
    System.out.println("loading authMethodDetails...");
    try {
        String json = AzureStoreManager.getInstance().getIdeStore().getProperty(ACCOUNT, AUTH_METHOD_DETAIL, "");
        if (StringUtils.isBlank(json)) {
            FileStorage fs = new FileStorage(FILE_NAME_AUTH_METHOD_DETAILS, CommonSettings.getSettingsBaseDir());
            byte[] data = fs.read();
            json = new String(data);
            AzureStoreManager.getInstance().getIdeStore().setProperty(ACCOUNT, AUTH_METHOD_DETAIL, json);
            fs.removeFile();
        }
        if (StringUtils.isBlank(json)) {
            System.out.println("No auth method details are saved.");
            return new AuthMethodDetails();
        }
        return JsonHelper.deserialize(AuthMethodDetails.class, json);
    } catch (IOException ignored) {
        System.out.println("Failed to loading authMethodDetails settings. Use defaults.");
        return new AuthMethodDetails();
    }
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) IOException(java.io.IOException) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 32 with AzureOperation

use of com.microsoft.azure.toolkit.lib.common.operation.AzureOperation in project azure-tools-for-java by Microsoft.

the class AuthMethodManager method getAzureClient.

@NotNull
@AzureOperation(name = "common|rest_client.create", params = { "sid" }, type = AzureOperation.Type.TASK)
public Azure getAzureClient(String sid) {
    final AzureManager manager = getAzureManager();
    if (manager != null) {
        final Azure azure = manager.getAzure(sid);
        if (azure != null) {
            return azure;
        }
    }
    final String error = "Failed to connect Azure service with current account";
    final String action = "Confirm you have already signed in with subscription: " + sid;
    final String errorCode = "001";
    throw new AzureToolkitRuntimeException(error, null, action, errorCode);
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) IdentityAzureManager(com.microsoft.azuretools.sdkmanage.IdentityAzureManager) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) NotNull(com.microsoft.azuretools.azurecommons.helpers.NotNull)

Example 33 with AzureOperation

use of com.microsoft.azure.toolkit.lib.common.operation.AzureOperation in project azure-tools-for-java by Microsoft.

the class FileUtil method zipFiles.

/**
 * Utility method to zip the given collection source files to the destination file.
 * @param sourceFiles source files array
 * @param targetZipFile ZIP file that will be created or overwritten
 */
@AzureOperation(name = "common|artifact.zip_files", params = { "targetZipFile.getName()" }, type = AzureOperation.Type.TASK)
public static void zipFiles(@NotNull final File[] sourceFiles, @NotNull final File targetZipFile) throws Exception {
    ensureValidZipSourceAndTarget(sourceFiles, targetZipFile);
    final FileOutputStream fos = new FileOutputStream(targetZipFile);
    final ZipOutputStream zipOut = new ZipOutputStream(fos);
    try {
        for (final File file : sourceFiles) {
            addToZipFile(file, zipOut);
        }
    } finally {
        if (zipOut != null) {
            zipOut.close();
        }
        if (fos != null) {
            fos.close();
        }
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipFile(java.util.zip.ZipFile) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 34 with AzureOperation

use of com.microsoft.azure.toolkit.lib.common.operation.AzureOperation in project azure-tools-for-java by Microsoft.

the class SubscriptionManager method loadSubscriptions.

@AzureOperation(name = "account|subscription.load_cache", type = AzureOperation.Type.TASK)
public static List<SubscriptionDetail> loadSubscriptions() {
    System.out.println("SubscriptionManager.loadSubscriptions()");
    try {
        String json = AzureStoreManager.getInstance().getIdeStore().getProperty(TelemetryConstants.ACCOUNT, "subscription_details");
        if (StringUtils.isBlank(json)) {
            final FileStorage file = new FileStorage(FILE_NAME_SUBSCRIPTIONS_DETAILS, CommonSettings.getSettingsBaseDir());
            final byte[] data = file.read();
            json = new String(data, StandardCharsets.UTF_8);
            file.removeFile();
            AzureStoreManager.getInstance().getIdeStore().setProperty(TelemetryConstants.ACCOUNT, "subscriptions_json", json);
        }
        if (json.isEmpty()) {
            System.out.println("subscription details is empty");
            return Collections.emptyList();
        }
        final SubscriptionDetail[] sda = JsonHelper.deserialize(SubscriptionDetail[].class, json);
        return new ArrayList<>(Arrays.asList(sda));
    } catch (final IOException e) {
        final String error = "Failed to load local cached subscriptions";
        final String action = "Retry later or logout to clear local cached subscriptions";
        throw new AzureToolkitRuntimeException(error, e);
    }
}
Also used : ArrayList(java.util.ArrayList) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) IOException(java.io.IOException) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 35 with AzureOperation

use of com.microsoft.azure.toolkit.lib.common.operation.AzureOperation in project azure-tools-for-java by Microsoft.

the class AzureMvpModel method listLocationsBySubscriptionId.

/**
 * List Location by Subscription ID.
 *
 * @param sid subscription Id
 * @return List of Location instances
 */
@AzureOperation(name = "common|region.list.subscription", params = { "sid" }, type = AzureOperation.Type.SERVICE)
public List<Region> listLocationsBySubscriptionId(String sid) {
    List<Region> locations = new ArrayList<>();
    Subscription subscription = getSubscriptionById(sid);
    try {
        locations.addAll(az(AzureAccount.class).listRegions(subscription.getId()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    Collections.sort(locations, getComparator(Region::getName));
    return locations;
}
Also used : ArrayList(java.util.ArrayList) Region(com.microsoft.azure.toolkit.lib.common.model.Region) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Aggregations

AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)64 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)11 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)11 IOException (java.io.IOException)10 Project (com.intellij.openapi.project.Project)9 ArrayList (java.util.ArrayList)9 AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)8 Operation (com.microsoft.azuretools.telemetrywrapper.Operation)6 Path (java.nio.file.Path)6 Module (com.intellij.openapi.module.Module)5 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 IWebApp (com.microsoft.azure.toolkit.lib.appservice.service.IWebApp)5 Azure (com.microsoft.azure.management.Azure)4 IFunctionApp (com.microsoft.azure.toolkit.lib.appservice.service.IFunctionApp)4 AzureUIRefreshEvent (com.microsoft.azuretools.utils.AzureUIRefreshEvent)4 Action (com.microsoft.azure.toolkit.lib.common.action.Action)3 ResourceGroup (com.microsoft.azure.toolkit.lib.common.model.ResourceGroup)3 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)3 AzureTaskManager (com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager)3