com.microsoft.azure.management.graphrbac.implementation.ServicePrincipalImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-mgmt-graph-rbac Show documentation
Show all versions of azure-mgmt-graph-rbac Show documentation
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.
/**
* 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.google.common.base.Function;
import com.google.common.collect.Lists;
import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.graphrbac.ActiveDirectoryApplication;
import com.microsoft.azure.management.graphrbac.BuiltInRole;
import com.microsoft.azure.management.graphrbac.CertificateCredential;
import com.microsoft.azure.management.graphrbac.PasswordCredential;
import com.microsoft.azure.management.graphrbac.RoleAssignment;
import com.microsoft.azure.management.graphrbac.ServicePrincipal;
import com.microsoft.azure.management.resources.ResourceGroup;
import com.microsoft.azure.management.resources.fluentcore.model.Creatable;
import com.microsoft.azure.management.resources.fluentcore.model.Indexable;
import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableUpdatableImpl;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Implementation for ServicePrincipal and its parent interfaces.
*/
@LangDefinition(ContainerName = "/Microsoft.Azure.Management.Graph.RBAC.Fluent")
class ServicePrincipalImpl
extends CreatableUpdatableImpl
implements
ServicePrincipal,
ServicePrincipal.Definition,
ServicePrincipal.Update,
HasCredential {
private GraphRbacManager manager;
private Map cachedPasswordCredentials;
private Map cachedCertificateCredentials;
private Map cachedRoleAssignments;
private ServicePrincipalCreateParametersInner createParameters;
private Creatable applicationCreatable;
private Map rolesToCreate;
private Set rolesToDelete;
String assignedSubscription;
private List> certificateCredentialsToCreate;
private List> passwordCredentialsToCreate;
private Set certificateCredentialsToDelete;
private Set passwordCredentialsToDelete;
ServicePrincipalImpl(ServicePrincipalInner innerObject, GraphRbacManager manager) {
super(innerObject.displayName(), innerObject);
this.manager = manager;
this.createParameters = new ServicePrincipalCreateParametersInner().withAccountEnabled(true);
this.cachedRoleAssignments = new HashMap<>();
this.rolesToCreate = new HashMap<>();
this.rolesToDelete = new HashSet<>();
this.cachedCertificateCredentials = new HashMap<>();
this.certificateCredentialsToCreate = new ArrayList<>();
this.certificateCredentialsToDelete = new HashSet<>();
this.cachedPasswordCredentials = new HashMap<>();
this.passwordCredentialsToCreate = new ArrayList<>();
this.passwordCredentialsToDelete = new HashSet<>();
}
@Override
public String id() {
return inner().objectId();
}
@Override
public String applicationId() {
return inner().appId();
}
@Override
public List servicePrincipalNames() {
return inner().servicePrincipalNames();
}
@Override
public Map passwordCredentials() {
return Collections.unmodifiableMap(cachedPasswordCredentials);
}
@Override
public Map certificateCredentials() {
return Collections.unmodifiableMap(cachedCertificateCredentials);
}
@Override
public Set roleAssignments() {
return Collections.unmodifiableSet(new HashSet<>(cachedRoleAssignments.values()));
}
@Override
protected Observable getInnerAsync() {
return manager.inner().servicePrincipals().getAsync(id());
}
@Override
public Observable createResourceAsync() {
Observable sp = Observable.just((ServicePrincipal) this);
if (isInCreateMode()) {
if (applicationCreatable != null) {
ActiveDirectoryApplication application = this.taskResult(applicationCreatable.key());
createParameters.withAppId(application.applicationId());
}
sp = manager.inner().servicePrincipals().createAsync(createParameters)
.map(innerToFluentMap(this));
}
return sp.flatMap(new Func1>() {
@Override
public Observable call(ServicePrincipal servicePrincipal) {
return submitCredentialsAsync(servicePrincipal).mergeWith(submitRolesAsync(servicePrincipal));
}
}).map(new Func1() {
@Override
public ServicePrincipal call(ServicePrincipal servicePrincipal) {
for (PasswordCredentialImpl> passwordCredential : passwordCredentialsToCreate) {
passwordCredential.exportAuthFile((ServicePrincipalImpl) servicePrincipal);
}
for (CertificateCredentialImpl> certificateCredential : certificateCredentialsToCreate) {
certificateCredential.exportAuthFile((ServicePrincipalImpl) servicePrincipal);
}
passwordCredentialsToCreate.clear();
certificateCredentialsToCreate.clear();
return servicePrincipal;
}
});
}
private Observable submitCredentialsAsync(final ServicePrincipal sp) {
Observable observable = Observable.just(null);
if (!certificateCredentialsToCreate.isEmpty() || !certificateCredentialsToDelete.isEmpty()) {
Map newCerts = new HashMap<>(cachedCertificateCredentials);
for (String delete : certificateCredentialsToDelete) {
newCerts.remove(delete);
}
for (CertificateCredential create : certificateCredentialsToCreate) {
newCerts.put(create.name(), create);
}
observable = observable.mergeWith(manager().inner().servicePrincipals().updateKeyCredentialsAsync(sp.id(),
Lists.transform(new ArrayList<>(newCerts.values()), new Function() {
@Override
public KeyCredentialInner apply(CertificateCredential input) {
return input.inner();
}
})));
}
if (!passwordCredentialsToCreate.isEmpty() || !passwordCredentialsToDelete.isEmpty()) {
Map newPasses = new HashMap<>(cachedPasswordCredentials);
for (String delete : passwordCredentialsToDelete) {
newPasses.remove(delete);
}
for (PasswordCredential create : passwordCredentialsToCreate) {
newPasses.put(create.name(), create);
}
observable = observable.mergeWith(manager().inner().servicePrincipals().updatePasswordCredentialsAsync(sp.id(),
Lists.transform(new ArrayList<>(newPasses.values()), new Function() {
@Override
public PasswordCredentialInner apply(PasswordCredential input) {
return input.inner();
}
})));
}
return observable.last().flatMap(new Func1>() {
@Override
public Observable call(Void aVoid) {
passwordCredentialsToDelete.clear();
certificateCredentialsToDelete.clear();
return refreshCredentialsAsync();
}
});
}
private Observable submitRolesAsync(final ServicePrincipal servicePrincipal) {
Observable create;
if (rolesToCreate.isEmpty()) {
create = Observable.just(servicePrincipal);
} else {
create = Observable.from(rolesToCreate.entrySet())
.flatMap(new Func1, Observable>() {
@Override
public Observable call(Map.Entry role) {
return manager().roleAssignments().define(SdkContext.randomUuid())
.forServicePrincipal(servicePrincipal)
.withBuiltInRole(role.getValue())
.withScope(role.getKey())
.createAsync();
}
})
.doOnNext(new Action1() {
@Override
public void call(Indexable o) {
cachedRoleAssignments.put(((RoleAssignment) o).id(), (RoleAssignment) o);
}
})
.last()
.map(new Func1() {
@Override
public ServicePrincipal call(Indexable o) {
rolesToCreate.clear();
return servicePrincipal;
}
});
}
Observable delete;
if (rolesToDelete.isEmpty()) {
delete = Observable.just(servicePrincipal);
} else {
delete = Observable.from(rolesToDelete)
.flatMap(new Func1>() {
@Override
public Observable call(final String s) {
return manager().roleAssignments().deleteByIdAsync(cachedRoleAssignments.get(s).id())
.toSingle(new Func0() {
@Override
public String call() {
return s;
}
}).toObservable();
}
})
.doOnNext(new Action1() {
@Override
public void call(String s) {
cachedRoleAssignments.remove(s);
}
})
.last()
.map(new Func1