Search in sources :

Example 1 with Pair

use of com.microsoft.azuretools.utils.Pair in project azure-tools-for-java by Microsoft.

the class AzureDockerCertVaultOps method getVault.

public static AzureDockerCertVault getVault(AzureDockerCertVault certVault, KeyVaultClient keyVaultClient) throws AzureDockerException {
    if (certVault == null || keyVaultClient == null || certVault.uri == null) {
        throw new AzureDockerException("Unexpected argument values; azureClient, vault name and resourceGroupName cannot be null");
    }
    String vaultUri = certVault.uri;
    try {
        SecretBundle secret = keyVaultClient.getSecret(vaultUri, SECRETENTRY_DOCKERHOSTNAMES);
        if (secret != null) {
            certVault.hostName = secret.value();
        } else {
            certVault.hostName = null;
            return null;
        }
    } catch (Exception e) {
        return null;
    }
    //Execute Key Vault Secret read in parallel
    Map<String, String> secretNamesAndValueMap = new HashMap<>();
    Observable.from(DOCKERHOST_SECRETS).flatMap(secretName -> {
        return Observable.create(new Observable.OnSubscribe<Pair<String, String>>() {

            @Override
            public void call(Subscriber<? super Pair<String, String>> subscriber) {
                keyVaultClient.getSecretAsync(vaultUri, secretName, new ServiceCallback<SecretBundle>() {

                    @Override
                    public void failure(Throwable throwable) {
                        subscriber.onCompleted();
                    }

                    @Override
                    public void success(SecretBundle secretBundle) {
                        if (secretBundle != null) {
                            subscriber.onNext(new Pair<>(secretName, secretBundle.value()));
                        }
                        subscriber.onCompleted();
                    }
                });
            }
        }).subscribeOn(Schedulers.io());
    }, 5).subscribeOn(Schedulers.io()).toBlocking().subscribe(new Action1<Pair<String, String>>() {

        @Override
        public void call(Pair<String, String> secretNameAndValue) {
            secretNamesAndValueMap.put(secretNameAndValue.first(), secretNameAndValue.second());
        }
    });
    String currentSecretValue;
    currentSecretValue = secretNamesAndValueMap.get("vmUsername");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.vmUsername = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("vmPwd");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.vmPwd = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("sshKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.sshKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("sshPubKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.sshPubKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsCACert");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsCACert = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsCAKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsCAKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsClientCert");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsClientCert = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsClientKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsClientKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsServerCert");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsServerCert = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsServerKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsServerKey = currentSecretValue;
    }
    return certVault;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) JSch(com.jcraft.jsch.JSch) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Action1(rx.functions.Action1) DEBUG(com.microsoft.azure.docker.ops.utils.AzureDockerUtils.DEBUG) SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) Observable(rx.Observable) Azure(com.microsoft.azure.management.Azure) Map(java.util.Map) Schedulers(rx.schedulers.Schedulers) DefaultLoader(com.microsoft.tooling.msservices.components.DefaultLoader) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Vault(com.microsoft.azure.management.keyvault.Vault) Subscriber(rx.Subscriber) ServiceCallback(com.microsoft.rest.ServiceCallback) Files(java.nio.file.Files) AzureDockerUtils(com.microsoft.azure.docker.ops.utils.AzureDockerUtils) FileWriter(java.io.FileWriter) Pair(com.microsoft.azuretools.utils.Pair) SetSecretRequest(com.microsoft.azure.keyvault.requests.SetSecretRequest) SecretPermissions(com.microsoft.azure.management.keyvault.SecretPermissions) List(java.util.List) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) AzureDockerCertVault(com.microsoft.azure.docker.model.AzureDockerCertVault) Paths(java.nio.file.Paths) CloudException(com.microsoft.azure.CloudException) KeyVaultClient(com.microsoft.azure.keyvault.KeyVaultClient) HashMap(java.util.HashMap) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) CloudException(com.microsoft.azure.CloudException) Observable(rx.Observable) SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) ServiceCallback(com.microsoft.rest.ServiceCallback) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) KeyPair(com.jcraft.jsch.KeyPair) Pair(com.microsoft.azuretools.utils.Pair)

Example 2 with Pair

use of com.microsoft.azuretools.utils.Pair in project azure-tools-for-java by Microsoft.

the class AzureDockerUtils method getVirtualNetworks.

public static Map<String, Pair<String, List<String>>> getVirtualNetworks(Azure azureClient, String region) {
    Map<String, Pair<String, List<String>>> result = new HashMap<>();
    if (azureClient != null) {
        for (Network vnet : azureClient.networks().list()) {
            if (vnet.regionName().toLowerCase().equals(region) || vnet.regionName().toLowerCase().equals(region)) {
                List<String> subnets = new ArrayList<>();
                subnets.addAll(vnet.subnets().keySet());
                result.put(vnet.id(), new Pair<>(vnet.name(), subnets));
            }
        }
    }
    return result;
}
Also used : Network(com.microsoft.azure.management.network.Network) Pair(com.microsoft.azuretools.utils.Pair)

Example 3 with Pair

use of com.microsoft.azuretools.utils.Pair in project azure-tools-for-java by Microsoft.

the class RemoteDebugRunConfiguration method readExternal.

@Override
public void readExternal(Element rootElement) throws InvalidDataException {
    super.readExternal(rootElement);
    SparkSubmitModel model = getSubmitModel();
    SparkSubmissionParameter parameter = Optional.ofNullable(model.getSubmissionParameter()).orElse(new SparkSubmissionParameter("", false, "", "", "", "", new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(), Arrays.stream(SparkSubmissionParameter.defaultParameters).collect(Collectors.toMap(Pair::first, Pair::second))));
    Optional.ofNullable(rootElement.getChild(SUBMISSION_CONTENT_NAME)).ifPresent((element -> {
        Optional.ofNullable(element.getAttribute(SUBMISSION_ATTRIBUTE_CLUSTER_NAME)).ifPresent(attribute -> parameter.setClusterName(attribute.getValue()));
        Optional.ofNullable(element.getAttribute(SUBMISSION_ATTRIBUTE_IS_LOCAL_ARTIFACT)).ifPresent(attribute -> parameter.setLocalArtifact(attribute.getValue().toLowerCase().equals("true")));
        Optional.ofNullable(element.getAttribute(SUBMISSION_ATTRIBUTE_ARTIFACT_NAME)).ifPresent(attribute -> parameter.setArtifactName(attribute.getValue()));
        Optional.ofNullable(element.getAttribute(SUBMISSION_ATTRIBUTE_CLASSNAME)).ifPresent(attribute -> parameter.setClassName(attribute.getValue()));
        model.setSubmissionParameters(parameter);
    }));
}
Also used : SparkSubmissionParameter(com.microsoft.azure.hdinsight.spark.common.SparkSubmissionParameter) Arrays(java.util.Arrays) ExecutionException(com.intellij.execution.ExecutionException) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SparkSubmissionParameter(com.microsoft.azure.hdinsight.spark.common.SparkSubmissionParameter) Executor(com.intellij.execution.Executor) InvalidDataException(com.intellij.openapi.util.InvalidDataException) Collectors(java.util.stream.Collectors) com.intellij.execution.configurations(com.intellij.execution.configurations) ArrayList(java.util.ArrayList) Pair(com.microsoft.azuretools.utils.Pair) Nullable(org.jetbrains.annotations.Nullable) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) SparkSubmissionContentPanel(com.microsoft.azure.hdinsight.spark.ui.SparkSubmissionContentPanel) Project(com.intellij.openapi.project.Project) Optional(java.util.Optional) SparkSubmitModel(com.microsoft.azure.hdinsight.spark.common.SparkSubmitModel) Module(com.intellij.openapi.module.Module) WriteExternalException(com.intellij.openapi.util.WriteExternalException) SparkBatchJobSubmissionState(com.microsoft.azure.hdinsight.spark.run.SparkBatchJobSubmissionState) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) SettingsEditor(com.intellij.openapi.options.SettingsEditor) SparkSubmitModel(com.microsoft.azure.hdinsight.spark.common.SparkSubmitModel) ArrayList(java.util.ArrayList)

Example 4 with Pair

use of com.microsoft.azuretools.utils.Pair in project azure-tools-for-java by Microsoft.

the class SubscriptionManager method updateAccountSubscriptionList.

protected List<SubscriptionDetail> updateAccountSubscriptionList() throws IOException {
    System.out.println(Thread.currentThread().getId() + " SubscriptionManager.updateAccountSubscriptionList()");
    if (azureManager == null) {
        throw new IllegalArgumentException("azureManager is null");
    }
    System.out.println("Getting subscription list from Azure");
    List<SubscriptionDetail> sdl = new ArrayList<>();
    List<Pair<Subscription, Tenant>> stpl = azureManager.getSubscriptionsWithTenant();
    for (Pair<Subscription, Tenant> stp : stpl) {
        sdl.add(new SubscriptionDetail(stp.first().subscriptionId(), stp.first().displayName(), stp.second().tenantId(), true));
    }
    return sdl;
}
Also used : Tenant(com.microsoft.azure.management.resources.Tenant) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) Subscription(com.microsoft.azure.management.resources.Subscription) Pair(com.microsoft.azuretools.utils.Pair)

Example 5 with Pair

use of com.microsoft.azuretools.utils.Pair in project azure-tools-for-java by Microsoft.

the class AzureManagerBase method getSubscriptionsWithTenant.

@Override
@AzureOperation(name = "account|subscription.list.tenant|authorized", type = AzureOperation.Type.SERVICE)
public List<Pair<Subscription, Tenant>> getSubscriptionsWithTenant() {
    final List<Pair<Subscription, Tenant>> subscriptions = new LinkedList<>();
    final Azure.Authenticated authentication = authTenant(getCurrentTenantId());
    // could be multi tenant - return all subscriptions for the current account
    final List<Tenant> tenants = getTenants(authentication);
    final List<String> failedTenantIds = new ArrayList<>();
    for (final Tenant tenant : tenants) {
        try {
            final Azure.Authenticated tenantAuthentication = authTenant(tenant.tenantId());
            final List<Subscription> tenantSubscriptions = getSubscriptions(tenantAuthentication);
            for (final Subscription subscription : tenantSubscriptions) {
                subscriptions.add(new Pair<>(subscription, tenant));
            }
        } catch (final Exception e) {
            // just skip for cases user failing to get subscriptions of tenants he/she has no permission to get access token.
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            failedTenantIds.add(tenant.tenantId());
        }
    }
    if (!failedTenantIds.isEmpty()) {
        final INotification nw = CommonSettings.getUiFactory().getNotificationWindow();
        nw.deliver("Lack permission for some tenants", "You don't have permission on the tenant(s): " + StringUtils.join(failedTenantIds, ","));
    }
    return subscriptions;
}
Also used : Azure(com.microsoft.azure.management.Azure) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Tenant(com.microsoft.azure.management.resources.Tenant) INotification(com.microsoft.azuretools.authmanage.interact.INotification) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) Pair(com.microsoft.azuretools.utils.Pair) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Aggregations

Pair (com.microsoft.azuretools.utils.Pair)5 Azure (com.microsoft.azure.management.Azure)2 Tenant (com.microsoft.azure.management.resources.Tenant)2 ArrayList (java.util.ArrayList)2 ExecutionException (com.intellij.execution.ExecutionException)1 Executor (com.intellij.execution.Executor)1 com.intellij.execution.configurations (com.intellij.execution.configurations)1 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)1 Module (com.intellij.openapi.module.Module)1 SettingsEditor (com.intellij.openapi.options.SettingsEditor)1 Project (com.intellij.openapi.project.Project)1 InvalidDataException (com.intellij.openapi.util.InvalidDataException)1 WriteExternalException (com.intellij.openapi.util.WriteExternalException)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 JSch (com.jcraft.jsch.JSch)1 KeyPair (com.jcraft.jsch.KeyPair)1 CloudException (com.microsoft.azure.CloudException)1 AzureDockerCertVault (com.microsoft.azure.docker.model.AzureDockerCertVault)1 AzureDockerException (com.microsoft.azure.docker.model.AzureDockerException)1 AzureDockerUtils (com.microsoft.azure.docker.ops.utils.AzureDockerUtils)1