Search in sources :

Example 1 with ServicePrincipal

use of com.microsoft.azure.management.graphrbac.ServicePrincipal in project azure-sdk-for-java by Azure.

the class ServicePrincipalImpl method createResourceAsync.

@Override
public Observable<ServicePrincipal> createResourceAsync() {
    ActiveDirectoryApplication application = (ActiveDirectoryApplication) ((Object) super.createdModel(applicationCreatable.key()));
    createParameters.withAppId(application.applicationId());
    Observable<ServicePrincipal> sp = manager.inner().servicePrincipals().createAsync(createParameters).map(innerToFluentMap(this)).flatMap(new Func1<ServicePrincipal, Observable<ServicePrincipal>>() {

        @Override
        public Observable<ServicePrincipal> call(ServicePrincipal servicePrincipal) {
            return refreshCredentialsAsync();
        }
    });
    if (roles == null || roles.isEmpty()) {
        return sp;
    }
    return sp.flatMap(new Func1<ServicePrincipal, Observable<ServicePrincipal>>() {

        @Override
        public Observable<ServicePrincipal> call(final ServicePrincipal servicePrincipal) {
            return Observable.from(roles.entrySet()).flatMap(new Func1<Map.Entry<String, BuiltInRole>, Observable<?>>() {

                @Override
                public Observable<?> call(Map.Entry<String, BuiltInRole> role) {
                    return manager().roleAssignments().define(UUID.randomUUID().toString()).forServicePrincipal(servicePrincipal).withBuiltInRole(role.getValue()).withScope(role.getKey()).createAsync().retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {

                        @Override
                        public Observable<?> call(Observable<? extends Throwable> observable) {
                            return observable.zipWith(Observable.range(1, 30), new Func2<Throwable, Integer, Integer>() {

                                @Override
                                public Integer call(Throwable throwable, Integer integer) {
                                    if (throwable instanceof CloudException && ((CloudException) throwable).body().code().equalsIgnoreCase("PrincipalNotFound")) {
                                        return integer;
                                    } else {
                                        throw Exceptions.propagate(throwable);
                                    }
                                }
                            }).flatMap(new Func1<Integer, Observable<?>>() {

                                @Override
                                public Observable<?> call(Integer i) {
                                    return Observable.timer(i, TimeUnit.SECONDS);
                                }
                            });
                        }
                    });
                }
            }).last().map(new Func1<Object, ServicePrincipal>() {

                @Override
                public ServicePrincipal call(Object o) {
                    return servicePrincipal;
                }
            });
        }
    }).map(new Func1<ServicePrincipal, ServicePrincipal>() {

        @Override
        public ServicePrincipal call(ServicePrincipal servicePrincipal) {
            for (PasswordCredentialImpl<?> passwordCredential : passwordCredentials) {
                passwordCredential.exportAuthFile((ServicePrincipalImpl) servicePrincipal);
            }
            for (CertificateCredentialImpl<?> certificateCredential : certificateCredentials) {
                certificateCredential.exportAuthFile((ServicePrincipalImpl) servicePrincipal);
            }
            return servicePrincipal;
        }
    });
}
Also used : ServicePrincipal(com.microsoft.azure.management.graphrbac.ServicePrincipal) BuiltInRole(com.microsoft.azure.management.graphrbac.BuiltInRole) CloudException(com.microsoft.azure.CloudException) Observable(rx.Observable) ActiveDirectoryApplication(com.microsoft.azure.management.graphrbac.ActiveDirectoryApplication) Func1(rx.functions.Func1) HashMap(java.util.HashMap) Map(java.util.Map) Func2(rx.functions.Func2)

Example 2 with ServicePrincipal

use of com.microsoft.azure.management.graphrbac.ServicePrincipal in project azure-sdk-for-java by Azure.

the class ServicePrincipalImpl method refreshCredentialsAsync.

Observable<ServicePrincipal> refreshCredentialsAsync() {
    final Observable<ServicePrincipal> keyCredentials = manager.inner().servicePrincipals().listKeyCredentialsAsync(id()).map(new Func1<List<KeyCredentialInner>, Map<String, CertificateCredential>>() {

        @Override
        public Map<String, CertificateCredential> call(List<KeyCredentialInner> keyCredentialInners) {
            if (keyCredentialInners == null || keyCredentialInners.isEmpty()) {
                return null;
            }
            Map<String, CertificateCredential> certificateCredentialMap = new HashMap<String, CertificateCredential>();
            for (KeyCredentialInner inner : keyCredentialInners) {
                CertificateCredential credential = new CertificateCredentialImpl<>(inner);
                certificateCredentialMap.put(credential.name(), credential);
            }
            return certificateCredentialMap;
        }
    }).map(new Func1<Map<String, CertificateCredential>, ServicePrincipal>() {

        @Override
        public ServicePrincipal call(Map<String, CertificateCredential> stringCertificateCredentialMap) {
            ServicePrincipalImpl.this.cachedCertificateCredentials = stringCertificateCredentialMap;
            return ServicePrincipalImpl.this;
        }
    });
    final Observable<ServicePrincipal> passwordCredentials = manager.inner().servicePrincipals().listPasswordCredentialsAsync(id()).map(new Func1<List<PasswordCredentialInner>, Map<String, PasswordCredential>>() {

        @Override
        public Map<String, PasswordCredential> call(List<PasswordCredentialInner> passwordCredentialInners) {
            if (passwordCredentialInners == null || passwordCredentialInners.isEmpty()) {
                return null;
            }
            Map<String, PasswordCredential> passwordCredentialMap = new HashMap<String, PasswordCredential>();
            for (PasswordCredentialInner inner : passwordCredentialInners) {
                PasswordCredential credential = new PasswordCredentialImpl<>(inner);
                passwordCredentialMap.put(credential.name(), credential);
            }
            return passwordCredentialMap;
        }
    }).map(new Func1<Map<String, PasswordCredential>, ServicePrincipal>() {

        @Override
        public ServicePrincipal call(Map<String, PasswordCredential> stringPasswordCredentialMap) {
            ServicePrincipalImpl.this.cachedPasswordCredentials = stringPasswordCredentialMap;
            return ServicePrincipalImpl.this;
        }
    });
    return keyCredentials.mergeWith(passwordCredentials).last();
}
Also used : HashMap(java.util.HashMap) ServicePrincipal(com.microsoft.azure.management.graphrbac.ServicePrincipal) PasswordCredential(com.microsoft.azure.management.graphrbac.PasswordCredential) CertificateCredential(com.microsoft.azure.management.graphrbac.CertificateCredential) ArrayList(java.util.ArrayList) List(java.util.List) Func1(rx.functions.Func1) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ServicePrincipal (com.microsoft.azure.management.graphrbac.ServicePrincipal)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Func1 (rx.functions.Func1)2 CloudException (com.microsoft.azure.CloudException)1 ActiveDirectoryApplication (com.microsoft.azure.management.graphrbac.ActiveDirectoryApplication)1 BuiltInRole (com.microsoft.azure.management.graphrbac.BuiltInRole)1 CertificateCredential (com.microsoft.azure.management.graphrbac.CertificateCredential)1 PasswordCredential (com.microsoft.azure.management.graphrbac.PasswordCredential)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Observable (rx.Observable)1 Func2 (rx.functions.Func2)1