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

com.microsoft.azure.management.locks.implementation.ManagementLocksImpl Maven / Gradle / Ivy

Go to download

This package contains Microsoft Locks Management SDK. This package has been deprecated. A replacement package com.azure.resourcemanager:azure-resourcemanager-resources 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.locks.implementation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import com.microsoft.azure.PagedList;
import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.locks.ManagementLock;
import com.microsoft.azure.management.locks.ManagementLocks;
import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.CreatableResourcesImpl;
import com.microsoft.azure.management.resources.fluentcore.utils.RXMapper;
import com.microsoft.rest.ServiceCallback;
import com.microsoft.rest.ServiceFuture;

import rx.Completable;
import rx.Observable;
import rx.functions.Func1;

/**
 *  Implementation for ManagementLocks.
 */
@LangDefinition
class ManagementLocksImpl
    extends CreatableResourcesImpl
    implements ManagementLocks {

    private final AuthorizationManager manager;

    ManagementLocksImpl(final AuthorizationManager manager) {
        this.manager = manager;
    }

    /**
     * Returns the part of the specified management lock resource ID representing the resource the lock is associated with.
     * @param lockId a lock resource ID
     * @return a resource ID
     */
    public static String resourceIdFromLockId(String lockId) {
        String[] lockIdParts = lockIdParts(lockId);
        if (lockIdParts == null) {
            return null;
        }

        StringBuilder resourceId = new StringBuilder();
        for (int i = 0; i < lockIdParts.length - 4; i++) {
            if (!lockIdParts[i].isEmpty()) {
                resourceId.append("/").append(lockIdParts[i]);
            }
        }

        return resourceId.toString();
    }

    private static String[] lockIdParts(String lockId) {
        if (lockId == null) {
            return null;
        }

        // Format examples:
        // /subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/anu-abc/providers/Microsoft.Authorization/locks/lock-1
        // /subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/anu-abc/providers/Microsoft.Storage/storageAccounts/anustg234/providers/Microsoft.Authorization/locks/lock-2

        String[] parts = lockId.split("/");
        if (parts.length < 4) {
            // ID too short to be possibly a lock ID
            return null;
        }

        if (!parts[parts.length - 2].equalsIgnoreCase("locks")
                || !parts[parts.length - 3].equalsIgnoreCase("Microsoft.Authorization")
                || !parts[parts.length - 4].equalsIgnoreCase("providers")) {
            // Not a lock ID
            return null;
        }

        return parts;
    }

    @Override
    public ManagementLockImpl define(String name) {
        return wrapModel(name);
    }

    // Fluent model create helpers

    @Override
    protected ManagementLockImpl wrapModel(String name) {
        ManagementLockObjectInner inner = new ManagementLockObjectInner();

        return new ManagementLockImpl(name, inner, this.manager());
    }

    @Override
    protected ManagementLockImpl wrapModel(ManagementLockObjectInner inner) {
        if (inner == null) {
            return null;
        }
        return new ManagementLockImpl(inner.id(), inner, this.manager());
    }

    @Override
    public PagedList list() {
        return wrapList(this.inner().list());
    }

    @Override
    public Observable listAsync() {
        return wrapPageAsync(inner().listAsync());
    }

    @Override
    public Completable deleteByIdAsync(String id) {
        String scope = resourceIdFromLockId(id);
        String lockName = ResourceUtils.nameFromResourceId(id);
        if (scope != null && lockName != null) {
            return inner().deleteByScopeAsync(scope, lockName).toCompletable();
        } else {
            return Observable.empty().toCompletable();
        }
    }

    @Override
    public PagedList listByResourceGroup(String resourceGroupName) {
        return wrapList(inner().listByResourceGroup(resourceGroupName));
    }

    @Override
    public Observable listByResourceGroupAsync(String resourceGroupName) {
        return wrapPageAsync(inner().listByResourceGroupAsync(resourceGroupName));
    }

    @Override
    public ManagementLock getByResourceGroup(String resourceGroupName, String name) {
        return this.getByResourceGroupAsync(resourceGroupName, name).toBlocking().last();
    }

    @Override
    public ServiceFuture getByResourceGroupAsync(String resourceGroupName, String name, ServiceCallback callback) {
        return ServiceFuture.fromBody(getByResourceGroupAsync(resourceGroupName, name), callback);
    }

    @Override
    public Observable getByResourceGroupAsync(String resourceGroupName, String name) {
        return this.inner().getByResourceGroupAsync(resourceGroupName, name).map(new Func1() {
            @Override
            public ManagementLock call(ManagementLockObjectInner innerT) {
                return wrapModel(innerT);
            }
        });
    }

    @Override
    public ManagementLock getById(String id) {
        return this.getByIdAsync(id).toBlocking().last();
    }

    @Override
    public Observable getByIdAsync(String id) {
        String resourceId = resourceIdFromLockId(id);
        String lockName = ResourceUtils.nameFromResourceId(id);
        if (resourceId != null && lockName != null) {
            return this.inner().getByScopeAsync(resourceId, lockName).map(new Func1() {

                @Override
                public ManagementLock call(ManagementLockObjectInner inner) {
                    return wrapModel(inner);
                }
            });
        } else {
            return null;
        }
    }

    @Override
    public ServiceFuture getByIdAsync(String id, ServiceCallback callback) {
        return ServiceFuture.fromBody(getByIdAsync(id), callback);
    }

    @Override
    public ServiceFuture deleteByResourceGroupAsync(String resourceGroupName, String name, ServiceCallback callback) {
        return ServiceFuture.fromBody(deleteByResourceGroupAsync(resourceGroupName, name).andThen(Observable.just(null)), callback);
    }

    @Override
    public void deleteByResourceGroup(String resourceGroupName, String name) {
        this.deleteByResourceGroupAsync(resourceGroupName, name).await();
    }

    @Override
    public Completable deleteByResourceGroupAsync(String resourceGroupName, String name) {
        return this.inner().deleteAtResourceGroupLevelAsync(resourceGroupName, name).toCompletable();
    }

    @Override
    public Observable deleteByIdsAsync(Collection ids) {
        if (ids == null || ids.isEmpty()) {
            return Observable.empty();
        }

        Collection> observables = new ArrayList<>();
        for (String id : ids) {
            String lockName = ResourceUtils.nameFromResourceId(id);
            String scopeName = ManagementLocksImpl.resourceIdFromLockId(id);
            if (lockName != null && scopeName != null) {
                Observable o = RXMapper.map(this.inner().deleteByScopeAsync(scopeName, lockName), id);
                observables.add(o);
            }
        }

        return Observable.mergeDelayError(observables);
    }

    @Override
    public Observable deleteByIdsAsync(String... ids) {
        return this.deleteByIdsAsync(new ArrayList(Arrays.asList(ids)));
    }

    @Override
    public void deleteByIds(Collection ids) {
        this.deleteByIdsAsync(ids).toCompletable().await();
    }

    @Override
    public void deleteByIds(String... ids) {
        this.deleteByIdsAsync(ids).toCompletable().await();
    }

    @Override
    public AuthorizationManager manager() {
        return this.manager;
    }

    @Override
    public ManagementLocksInner inner() {
        return this.manager().inner().managementLocks();
    }

    @Override
    public PagedList listForResource(String resourceId) {
        return wrapList(inner().listAtResourceLevel(
                ResourceUtils.groupFromResourceId(resourceId),
                ResourceUtils.resourceProviderFromResourceId(resourceId),
                ResourceUtils.parentRelativePathFromResourceId(resourceId),
                ResourceUtils.resourceTypeFromResourceId(resourceId),
                ResourceUtils.nameFromResourceId(resourceId)));
    }

    @Override
    public Observable listForResourceAsync(String resourceId) {
        return wrapPageAsync(inner().listAtResourceLevelAsync(
                ResourceUtils.groupFromResourceId(resourceId),
                ResourceUtils.resourceProviderFromResourceId(resourceId),
                ResourceUtils.parentRelativePathFromResourceId(resourceId),
                ResourceUtils.resourceTypeFromResourceId(resourceId),
                ResourceUtils.nameFromResourceId(resourceId)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy