Search in sources :

Example 1 with ApplicationInsightsResource

use of com.microsoft.applicationinsights.preference.ApplicationInsightsResource in project azure-tools-for-java by Microsoft.

the class AppInsightsMngmtPanel method updateApplicationInsightsResourceRegistry.

public static void updateApplicationInsightsResourceRegistry(List<SubscriptionDetail> subList, Project project) throws Exception {
    // remove all resourecs that were not manually added
    keeepManuallyAddedList(project);
    for (SubscriptionDetail sub : subList) {
        if (sub.isSelected()) {
            try {
                // fetch resources available for particular subscription
                List<Resource> resourceList = AzureSDKManager.getApplicationInsightsResources(sub);
                // Removal logic
                List<ApplicationInsightsResource> importedList = ApplicationInsightsResourceRegistry.prepareAppResListFromRes(resourceList, sub);
                // Addition logic
                ApplicationInsightsResourceRegistry.getAppInsightsResrcList().addAll(importedList);
            } catch (Exception ex) {
                AzurePlugin.log("Error loading AppInsights information for subscription '" + sub.getSubscriptionName() + "'");
            }
        }
    }
    AzureSettings.getSafeInstance(project).saveAppInsights();
}
Also used : ApplicationInsightsResource(com.microsoft.applicationinsights.preference.ApplicationInsightsResource) Resource(com.microsoft.applicationinsights.management.rest.model.Resource) ApplicationInsightsResource(com.microsoft.applicationinsights.preference.ApplicationInsightsResource) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail)

Example 2 with ApplicationInsightsResource

use of com.microsoft.applicationinsights.preference.ApplicationInsightsResource in project azure-tools-for-java by Microsoft.

the class ApplicationInsightsResourceRegistryEclipse method updateApplicationInsightsResourceRegistry.

/**
	 * Method updates application insights registry by adding, removing or updating resources.
	 * @param client
	 * @throws IOException
	 * @throws RestOperationException
	 * @throws AzureCmdException 
	 */
public static void updateApplicationInsightsResourceRegistry(List<SubscriptionDetail> subList) throws Exception {
    for (SubscriptionDetail sub : subList) {
        if (sub.isSelected()) {
            try {
                // fetch resources available for particular subscription
                List<Resource> resourceList = AzureSDKManager.getApplicationInsightsResources(sub);
                // Removal logic
                List<ApplicationInsightsResource> registryList = ApplicationInsightsResourceRegistry.getResourceListAsPerSub(sub.getSubscriptionId());
                List<ApplicationInsightsResource> importedList = ApplicationInsightsResourceRegistry.prepareAppResListFromRes(resourceList, sub);
                List<String> inUsekeyList = getInUseInstrumentationKeys();
                for (ApplicationInsightsResource registryRes : registryList) {
                    if (!importedList.contains(registryRes)) {
                        String key = registryRes.getInstrumentationKey();
                        int index = ApplicationInsightsResourceRegistry.getResourceIndexAsPerKey(key);
                        if (inUsekeyList.contains(key)) {
                            // key is used by project but not present in
                            // cloud,
                            // so make it as manually added resource and not
                            // imported.
                            ApplicationInsightsResource resourceToAdd = new ApplicationInsightsResource(key, key, Messages.unknown, Messages.unknown, Messages.unknown, Messages.unknown, false);
                            ApplicationInsightsResourceRegistry.getAppInsightsResrcList().set(index, resourceToAdd);
                        } else {
                            // key is not used by any project then delete
                            // it.
                            ApplicationInsightsResourceRegistry.getAppInsightsResrcList().remove(index);
                        }
                    }
                }
                // Addition logic
                List<ApplicationInsightsResource> list = ApplicationInsightsResourceRegistry.getAppInsightsResrcList();
                for (Resource resource : resourceList) {
                    ApplicationInsightsResource resourceToAdd = new ApplicationInsightsResource(resource.getName(), resource.getInstrumentationKey(), sub.getSubscriptionName(), sub.getSubscriptionId(), resource.getLocation(), resource.getResourceGroup(), true);
                    if (list.contains(resourceToAdd)) {
                        int index = ApplicationInsightsResourceRegistry.getResourceIndexAsPerKey(resource.getInstrumentationKey());
                        ApplicationInsightsResource objectFromRegistry = list.get(index);
                        if (!objectFromRegistry.isImported()) {
                            ApplicationInsightsResourceRegistry.getAppInsightsResrcList().set(index, resourceToAdd);
                        }
                    } else {
                        ApplicationInsightsResourceRegistry.getAppInsightsResrcList().add(resourceToAdd);
                    }
                }
            } catch (Exception e) {
                Activator.getDefault().log(String.format(Messages.aiListErr, sub.getSubscriptionName()), e);
            }
        }
    }
    ApplicationInsightsPreferences.save();
    ApplicationInsightsPreferences.setLoaded(true);
}
Also used : ApplicationInsightsResource(com.microsoft.applicationinsights.preference.ApplicationInsightsResource) Resource(com.microsoft.applicationinsights.management.rest.model.Resource) ApplicationInsightsResource(com.microsoft.applicationinsights.preference.ApplicationInsightsResource) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) IOException(java.io.IOException) RestOperationException(com.microsoft.applicationinsights.management.rest.client.RestOperationException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)

Example 3 with ApplicationInsightsResource

use of com.microsoft.applicationinsights.preference.ApplicationInsightsResource in project azure-tools-for-java by Microsoft.

the class AzurePlugin method initializeAIRegistry.

private void initializeAIRegistry() {
    try {
        AzureSettings.getSafeInstance(myProject).loadAppInsights();
        Module[] modules = ModuleManager.getInstance(myProject).getModules();
        for (Module module : modules) {
            if (module != null && module.isLoaded() && ModuleTypeId.JAVA_MODULE.equals(module.getOptionValue(Module.ELEMENT_TYPE))) {
                String aiXMLPath = String.format("%s%s%s", PluginUtil.getModulePath(module), File.separator, message("aiXMLPath"));
                if (new File(aiXMLPath).exists()) {
                    AILibraryHandler handler = new AILibraryHandler();
                    handler.parseAIConfXmlPath(aiXMLPath);
                    String key = handler.getAIInstrumentationKey();
                    if (key != null && !key.isEmpty()) {
                        String unknown = message("unknown");
                        List<ApplicationInsightsResource> list = ApplicationInsightsResourceRegistry.getAppInsightsResrcList();
                        ApplicationInsightsResource resourceToAdd = new ApplicationInsightsResource(key, key, unknown, unknown, unknown, unknown, false);
                        if (!list.contains(resourceToAdd)) {
                            ApplicationInsightsResourceRegistry.getAppInsightsResrcList().add(resourceToAdd);
                        }
                    }
                }
            }
        }
        AzureSettings.getSafeInstance(myProject).saveAppInsights();
    } catch (Exception ex) {
        AzurePlugin.log(ex.getMessage(), ex);
    }
}
Also used : ApplicationInsightsResource(com.microsoft.applicationinsights.preference.ApplicationInsightsResource) Module(com.intellij.openapi.module.Module) AILibraryHandler(com.microsoft.intellij.ui.libraries.AILibraryHandler)

Example 4 with ApplicationInsightsResource

use of com.microsoft.applicationinsights.preference.ApplicationInsightsResource in project azure-tools-for-java by Microsoft.

the class ApplicationInsightsResourceRegistryEclipse method updateApplicationInsightsResourceRegistry.

/**
 * Method updates application insights registry by adding, removing or updating resources.
 * @param client
 * @throws java.io.IOException
 * @throws RestOperationException
 * @throws AzureCmdException
 */
public static void updateApplicationInsightsResourceRegistry(List<Subscription> subList) throws Exception {
    for (Subscription sub : subList) {
        if (sub.isSelected()) {
            try {
                // fetch resources available for particular subscription
                List<ApplicationInsightsComponent> resourceList = AzureSDKManager.getInsightsResources(sub.getId());
                // Removal logic
                List<ApplicationInsightsResource> registryList = ApplicationInsightsResourceRegistry.getResourceListAsPerSub(sub.getId());
                List<ApplicationInsightsResource> importedList = ApplicationInsightsResourceRegistry.prepareAppResListFromRes(resourceList, sub);
                List<String> inUsekeyList = getInUseInstrumentationKeys();
                for (ApplicationInsightsResource registryRes : registryList) {
                    if (!importedList.contains(registryRes)) {
                        String key = registryRes.getInstrumentationKey();
                        int index = ApplicationInsightsResourceRegistry.getResourceIndexAsPerKey(key);
                        if (inUsekeyList.contains(key)) {
                            // key is used by project but not present in
                            // cloud,
                            // so make it as manually added resource and not
                            // imported.
                            ApplicationInsightsResource resourceToAdd = new ApplicationInsightsResource(key, key, Messages.unknown, Messages.unknown, Messages.unknown, Messages.unknown, false);
                            ApplicationInsightsResourceRegistry.getAppInsightsResrcList().set(index, resourceToAdd);
                        } else {
                            // key is not used by any project then delete
                            // it.
                            ApplicationInsightsResourceRegistry.getAppInsightsResrcList().remove(index);
                        }
                    }
                }
                // Addition logic
                List<ApplicationInsightsResource> list = ApplicationInsightsResourceRegistry.getAppInsightsResrcList();
                for (ApplicationInsightsComponent resource : resourceList) {
                    ApplicationInsightsResource resourceToAdd = new ApplicationInsightsResource(resource, sub, true);
                    if (list.contains(resourceToAdd)) {
                        int index = ApplicationInsightsResourceRegistry.getResourceIndexAsPerKey(resource.instrumentationKey());
                        ApplicationInsightsResource objectFromRegistry = list.get(index);
                        if (!objectFromRegistry.isImported()) {
                            ApplicationInsightsResourceRegistry.getAppInsightsResrcList().set(index, resourceToAdd);
                        }
                    } else {
                        ApplicationInsightsResourceRegistry.getAppInsightsResrcList().add(resourceToAdd);
                    }
                }
            } catch (Exception e) {
                Activator.getDefault().log(String.format(Messages.aiListErr, sub.getName()), e);
            }
        }
    }
    ApplicationInsightsPreferences.save();
    ApplicationInsightsPreferences.setLoaded(true);
}
Also used : ApplicationInsightsResource(com.microsoft.applicationinsights.preference.ApplicationInsightsResource) ApplicationInsightsComponent(com.microsoft.azure.management.applicationinsights.v2015_05_01.ApplicationInsightsComponent) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) IOException(java.io.IOException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)

Example 5 with ApplicationInsightsResource

use of com.microsoft.applicationinsights.preference.ApplicationInsightsResource in project azure-tools-for-java by Microsoft.

the class ApplicationInsightsPreferences method savePreferences.

/**
 * Stores application insights resources list
 * in preference file in the form of byte array.
 */
private void savePreferences() {
    try {
        Preferences prefs = PluginUtil.getPrefs(PREF_FILE);
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput output = new ObjectOutputStream(buffer);
        List<ApplicationInsightsResource> data = ApplicationInsightsResourceRegistry.getAppInsightsResrcList();
        ApplicationInsightsResource[] dataArray = data.stream().filter(a -> !a.isImported()).sorted().toArray(ApplicationInsightsResource[]::new);
        /*
             * Sort list according to application insights resource name.
             * Save only manually added resources
             */
        try {
            output.writeObject(dataArray);
        } finally {
            output.close();
        }
        prefs.putByteArray(PREF_KEY, buffer.toByteArray());
        prefs.flush();
    } catch (BackingStoreException e) {
        Activator.getDefault().log(e.getMessage(), e);
    } catch (IOException e) {
        Activator.getDefault().log(e.getMessage(), e);
    }
}
Also used : ApplicationInsightsResource(com.microsoft.applicationinsights.preference.ApplicationInsightsResource) ObjectOutput(java.io.ObjectOutput) BackingStoreException(org.osgi.service.prefs.BackingStoreException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Preferences(org.osgi.service.prefs.Preferences) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

ApplicationInsightsResource (com.microsoft.applicationinsights.preference.ApplicationInsightsResource)15 IOException (java.io.IOException)5 ApplicationInsightsComponent (com.microsoft.azure.management.applicationinsights.v2015_05_01.ApplicationInsightsComponent)3 Module (com.intellij.openapi.module.Module)2 Resource (com.microsoft.applicationinsights.management.rest.model.Resource)2 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)2 SubscriptionDetail (com.microsoft.azuretools.authmanage.models.SubscriptionDetail)2 AzureCmdException (com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)2 AILibraryHandler (com.microsoft.intellij.ui.libraries.AILibraryHandler)2 ArrayList (java.util.ArrayList)2 Preferences (org.osgi.service.prefs.Preferences)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 RestOperationException (com.microsoft.applicationinsights.management.rest.client.RestOperationException)1 ApplicationInsightsPageTableElement (com.microsoft.applicationinsights.preference.ApplicationInsightsPageTableElement)1 ApplicationInsightsPageTableElements (com.microsoft.applicationinsights.preference.ApplicationInsightsPageTableElements)1 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)1 AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)1 AILibraryHandler (com.microsoft.azuretools.core.applicationinsights.AILibraryHandler)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1