All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.azure.management.keyvault.implementation.SecretImpl Maven / Gradle / Ivy

Go to download

This package contains Microsoft Azure Key Vault Management SDK. This package has been deprecated. A replacement package com.azure.resourcemanager:azure-resourcemanager-keyvault is available as of 31-March-2022. We strongly encourage you to upgrade to continue receiving updates. See Migration Guide https://aka.ms/java-track2-migration-guide for guidance on upgrading. Refer to our deprecation policy https://azure.github.io/azure-sdk/policies_support.html for more details.

There is a newer version: 1.41.4
Show newest version
/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 */

package com.microsoft.azure.management.keyvault.implementation;

import com.microsoft.azure.ListOperationCallback;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.keyvault.models.Attributes;
import com.microsoft.azure.keyvault.models.SecretAttributes;
import com.microsoft.azure.keyvault.models.SecretBundle;
import com.microsoft.azure.keyvault.models.SecretItem;
import com.microsoft.azure.keyvault.requests.SetSecretRequest;
import com.microsoft.azure.keyvault.requests.UpdateSecretRequest;
import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.keyvault.Secret;
import com.microsoft.azure.management.keyvault.Vault;
import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableUpdatableImpl;
import com.microsoft.azure.management.resources.fluentcore.utils.PagedListConverter;
import com.microsoft.azure.management.resources.fluentcore.utils.Utils;
import com.microsoft.rest.ServiceFuture;
import rx.Observable;
import rx.functions.Action0;
import rx.functions.Func1;

import java.util.Map;

/**
 * Implementation for Vault and its parent interfaces.
 */
@LangDefinition
class SecretImpl
        extends CreatableUpdatableImpl<
                Secret,
                SecretBundle,
                SecretImpl>
        implements
        Secret,
        Secret.Definition,
        Secret.Update {

    private final Vault vault;
    private SetSecretRequest.Builder setSecretRequest;
    private UpdateSecretRequest.Builder updateSecretRequest;

    SecretImpl(String name, SecretBundle innerObject, Vault vault) {
        super(name, innerObject);
        this.vault = vault;
        this.updateSecretRequest = new UpdateSecretRequest.Builder(vault.vaultUri(), name);
    }

    @Override
    public String id() {
        return inner().id();
    }

    @Override
    public String value() {
        return inner().value();
    }

    @Override
    public SecretAttributes attributes() {
        return inner().attributes();
    }

    @Override
    public Map tags() {
        return inner().tags();
    }

    @Override
    public String contentType() {
        return inner().contentType();
    }

    @Override
    public String kid() {
        return inner().kid();
    }

    @Override
    public boolean managed() {
        return Utils.toPrimitiveBoolean(inner().managed());
    }

    @Override
    public PagedList listVersions() {
        return new PagedListConverter() {

            @Override
            public Observable typeConvertAsync(final SecretItem secretItem) {
                return new KeyVaultFutures.ServiceFutureConverter() {

                    @Override
                    protected ServiceFuture callAsync() {
                        if (secretItem.attributes().enabled()) {
                            return vault.client().getSecretAsync(secretItem.identifier().identifier(), null);
                        } else {
                            SecretBundle secretBundle = new SecretBundle()
                                    .withId(secretItem.id())
                                    .withAttributes(secretItem.attributes())
                                    .withContentType(secretItem.contentType())
                                    .withTags(secretItem.tags());
                            return ServiceFuture.fromBody(Observable.just(secretBundle), null);
                        }
                    }

                    @Override
                    protected Secret wrapModel(SecretBundle secretBundle) {
                        return new SecretImpl(secretBundle.secretIdentifier().name(), secretBundle, vault);
                    }
                }.toObservable();
            }
        }.convert(vault.client().listSecretVersions(vault.vaultUri(), name()));
    }

    @Override
    public Observable listVersionsAsync() {
        return new KeyVaultFutures.ListCallbackObserver() {

            @Override
            protected void list(ListOperationCallback callback) {
                vault.client().listSecretVersionsAsync(vault.vaultUri(), name(), callback);
            }

            @Override
            protected Observable typeConvertAsync(SecretItem o) {
                return Observable.just(o);
            }
        }.toObservable()
                .flatMap(new Func1>() {
                    @Override
                    public Observable call(final SecretItem secretItem) {
                        return new KeyVaultFutures.ServiceFutureConverter() {

                            @Override
                            protected ServiceFuture callAsync() {
                                if (secretItem.attributes().enabled()) {
                                    return vault.client().getSecretAsync(secretItem.identifier().identifier(), null);
                                } else {
                                    SecretBundle secretBundle = new SecretBundle()
                                            .withId(secretItem.id())
                                            .withAttributes(secretItem.attributes())
                                            .withContentType(secretItem.contentType())
                                            .withTags(secretItem.tags());
                                    return ServiceFuture.fromBody(Observable.just(secretBundle), null);
                                }
                            }

                            @Override
                            protected Secret wrapModel(SecretBundle secretBundle) {
                                return new SecretImpl(secretItem.identifier().name(), secretBundle, vault);
                            }
                        }.toObservable();
                    }
                });
    }

    @Override
    protected Observable getInnerAsync() {
        return Observable.from(vault.client().getSecretAsync(id(), null));
    }

    @Override
    public SecretImpl withTags(Map tags) {
        setSecretRequest.withTags(tags);
        updateSecretRequest.withTags(tags);
        return this;
    }

    @Override
    public boolean isInCreateMode() {
        return id() == null;
    }

    @Override
    public Observable createResourceAsync() {
        return Observable.from(vault.client().setSecretAsync(setSecretRequest.build(), null))
                .map(innerToFluentMap(this))
                .doOnCompleted(new Action0() {
                    @Override
                    public void call() {
                        setSecretRequest = null;
                        updateSecretRequest = new UpdateSecretRequest.Builder(vault.vaultUri(), name());
                    }
                });
    }

    @Override
    public Observable updateResourceAsync() {
        Observable set = Observable.just((Secret) this);
        if (setSecretRequest != null) {
            set = createResourceAsync();
        }
        return set.flatMap(new Func1>() {
            @Override
            public Observable call(Secret secret) {
                return Observable.from(vault.client().updateSecretAsync(updateSecretRequest.build(), null));
            }
        }).flatMap(new Func1>() {
            @Override
            public Observable call(SecretBundle secretBundle) {
                return refreshAsync();
            }
        }).doOnCompleted(new Action0() {
            @Override
            public void call() {
                setSecretRequest = null;
                updateSecretRequest = new UpdateSecretRequest.Builder(vault.vaultUri(), name());
            }
        });
    }

    @Override
    public SecretImpl withAttributes(Attributes attributes) {
        setSecretRequest.withAttributes(attributes);
        updateSecretRequest.withAttributes(attributes);
        return this;
    }

    @Override
    public SecretImpl withVersion(String version) {
        updateSecretRequest.withVersion(version);
        return this;
    }

    @Override
    public SecretImpl withValue(String value) {
        setSecretRequest = new SetSecretRequest.Builder(vault.vaultUri(), name(), value);
        return this;
    }

    @Override
    public SecretImpl withContentType(String contentType) {
        setSecretRequest.withContentType(contentType);
        updateSecretRequest.withContentType(contentType);
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy