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

com.microsoft.azure.management.graphrbac.implementation.ActiveDirectoryGroupImpl Maven / Gradle / Ivy

Go to download

This package contains Microsoft Azure Graph RBAC Management SDK. This package has been deprecated. A replacement package com.azure.resourcemanager:azure-resourcemanager-authorization 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.graphrbac.implementation;

import com.microsoft.azure.Page;
import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.graphrbac.ActiveDirectoryGroup;
import com.microsoft.azure.management.graphrbac.ActiveDirectoryObject;
import com.microsoft.azure.management.graphrbac.ActiveDirectoryUser;
import com.microsoft.azure.management.graphrbac.ServicePrincipal;
import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableUpdatableImpl;
import com.microsoft.azure.management.resources.fluentcore.utils.Utils;
import com.microsoft.rest.protocol.SerializerAdapter;
import rx.Observable;
import rx.functions.Action0;
import rx.functions.Func1;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Implementation for Group and its parent interfaces.
 */
@LangDefinition(ContainerName = "/Microsoft.Azure.Management.Graph.RBAC.Fluent")
class ActiveDirectoryGroupImpl
        extends CreatableUpdatableImpl
        implements ActiveDirectoryGroup,
            ActiveDirectoryGroup.Definition,
            ActiveDirectoryGroup.Update {

    private final GraphRbacManager manager;
    private GroupCreateParametersInner createParameters;
    private String domainName;
    private Set membersToAdd;
    private Set membersToRemove;

    ActiveDirectoryGroupImpl(ADGroupInner innerModel, GraphRbacManager manager) {
        super(innerModel.displayName(), innerModel);
        this.manager = manager;
        this.createParameters = new GroupCreateParametersInner()
                .withDisplayName(innerModel.displayName())
                .withMailEnabled(false)
                .withSecurityEnabled(true);
        membersToAdd = new HashSet<>();
        membersToRemove = new HashSet<>();
    }

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

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

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

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

    @Override
    public Set listMembers() {
        return Collections.unmodifiableSet(new HashSet<>(listMembersAsync().toList().toBlocking().single()));
    }

    @Override
    public Observable listMembersAsync() {
        return manager().inner().groups().getGroupMembersAsync(id())
                .flatMap(new Func1, Observable>() {
                    @Override
                    public Observable call(Page aadObjectInnerPage) {
                        return Observable.from(aadObjectInnerPage.items());
                    }
                }).map(new Func1() {
                    @Override
                    public ActiveDirectoryObject call(AADObjectInner aadObjectInner) {
                        SerializerAdapter adapter = manager().inner().restClient().serializerAdapter();
                        try {
                            String serialized = adapter.serialize(aadObjectInner);
                            switch (aadObjectInner.objectType()) {
                                case "User":
                                    return new ActiveDirectoryUserImpl(adapter.deserialize(serialized, UserInner.class), manager());
                                case "Group":
                                    return new ActiveDirectoryGroupImpl(adapter.deserialize(serialized, ADGroupInner.class), manager());
                                case "ServicePrincipal":
                                    return new ServicePrincipalImpl(adapter.deserialize(serialized, ServicePrincipalInner.class), manager());
                                case "Application":
                                    return new ActiveDirectoryApplicationImpl(adapter.deserialize(serialized, ApplicationInner.class), manager());
                                default:
                                    return null;
                            }
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
    }

    @Override
    public GraphRbacManager manager() {
        return manager;
    }

    @Override
    protected Observable getInnerAsync() {
        return manager().inner().groups().getAsync(id());
    }

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

    @Override
    public Observable createResourceAsync() {
        Observable group = Observable.just(this);
        if (isInCreateMode()) {
            group = manager().inner().groups().createAsync(createParameters)
                    .map(innerToFluentMap(this));
        }
        if (!membersToRemove.isEmpty()) {
            group = group.flatMap(new Func1>() {
                @Override
                public Observable call(Object o) {
                    return Observable.from(membersToRemove)
                            .flatMap(new Func1>() {
                                @Override
                                public Observable call(String s) {
                                    return manager().inner().groups().removeMemberAsync(id(), s);
                                }
                            }).last().doOnCompleted(new Action0() {
                                @Override
                                public void call() {
                                    membersToRemove.clear();
                                }
                            });
                }
            });
        }
        if (!membersToAdd.isEmpty()) {
            group = group.flatMap(new Func1>() {
                @Override
                public Observable call(Object o) {
                    return Observable.from(membersToAdd)
                            .flatMap(new Func1>() {
                                @Override
                                public Observable call(String s) {
                                    return manager().inner().groups().addMemberAsync(id(), new GroupAddMemberParametersInner().withUrl(s));
                                }
                            }).last().doOnCompleted(new Action0() {
                                @Override
                                public void call() {
                                    membersToAdd.clear();
                                }
                            });
                }
            });
        }
        return group.map(new Func1() {
            @Override
            public ActiveDirectoryGroup call(Object o) {
                return ActiveDirectoryGroupImpl.this;
            }
        });
    }

    @Override
    public ActiveDirectoryGroupImpl withEmailAlias(String mailNickname) {
        // User providing domain
        if (mailNickname.contains("@")) {
            String[] parts = mailNickname.split("@");
            domainName = parts[1];
            mailNickname = parts[0];
        }
        createParameters.withMailNickname(mailNickname);
        return this;
    }

    @Override
    public ActiveDirectoryGroupImpl withMember(String objectId) {
        membersToAdd.add(String.format("https://%s/%s/directoryObjects/%s",
                manager().inner().retrofit().baseUrl().host(), manager().tenantId(), objectId));
        return this;
    }

    @Override
    public ActiveDirectoryGroupImpl withMember(ActiveDirectoryUser user) {
        return withMember(user.id());
    }

    @Override
    public ActiveDirectoryGroupImpl withMember(ActiveDirectoryGroup group) {
        return withMember(group.id());
    }

    @Override
    public ActiveDirectoryGroupImpl withMember(ServicePrincipal servicePrincipal) {
        return withMember(servicePrincipal.id());
    }

    @Override
    public ActiveDirectoryGroupImpl withoutMember(String objectId) {
        membersToRemove.add(objectId);
        return this;
    }

    @Override
    public ActiveDirectoryGroupImpl withoutMember(ActiveDirectoryUser user) {
        return withoutMember(user.id());
    }

    @Override
    public ActiveDirectoryGroupImpl withoutMember(ActiveDirectoryGroup group) {
        return withoutMember(group.id());
    }

    @Override
    public ActiveDirectoryGroupImpl withoutMember(ServicePrincipal servicePrincipal) {
        return withoutMember(servicePrincipal.id());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy