Search in sources :

Example 1 with ResourceManager

use of com.microsoft.azure.management.resources.implementation.ResourceManager in project azure-sdk-for-java by Azure.

the class ServicePrincipalsTests method canCRUDServicePrincipalWithRole.

@Test
@Ignore("Need to login as user to run")
public void canCRUDServicePrincipalWithRole() throws Exception {
    ServicePrincipal servicePrincipal = null;
    String authFile = "someauth.azureauth";
    String subscription = "somesubscription";
    try {
        servicePrincipal = graphRbacManager.servicePrincipals().define("ansp44").withNewApplication("http://easycreate.azure.com/ansp/44").definePasswordCredential("sppass").withPasswordValue("StrongPass!12").attach().defineCertificateCredential("spcert").withAsymmetricX509Certificate().withPublicKey(Files.readAllBytes(Paths.get("/Users/user/myserver.crt"))).withDuration(Duration.standardDays(7)).withAuthFileToExport(new FileOutputStream(authFile)).withPrivateKeyFile("/Users/user/myserver.pfx").withPrivateKeyPassword("StrongPass!123").attach().withNewRoleInSubscription(BuiltInRole.CONTRIBUTOR, subscription).create();
        System.out.println(servicePrincipal.id() + " - " + Joiner.on(", ").join(servicePrincipal.servicePrincipalNames()));
        Assert.assertNotNull(servicePrincipal.id());
        Assert.assertNotNull(servicePrincipal.applicationId());
        Assert.assertEquals(2, servicePrincipal.servicePrincipalNames().size());
        Thread.sleep(10000);
        ResourceManager resourceManager = ResourceManager.authenticate(ApplicationTokenCredentials.fromFile(new File(authFile))).withSubscription(subscription);
        Assert.assertNotNull(resourceManager.resourceGroups().list());
    } finally {
        try {
            graphRbacManager.servicePrincipals().deleteById(servicePrincipal.id());
        } catch (Exception e) {
        }
        try {
            graphRbacManager.applications().deleteById(graphRbacManager.applications().getByName(servicePrincipal.applicationId()).id());
        } catch (Exception e) {
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ResourceManager(com.microsoft.azure.management.resources.implementation.ResourceManager) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with ResourceManager

use of com.microsoft.azure.management.resources.implementation.ResourceManager in project azure-sdk-for-java by Azure.

the class ProviderRegistrationInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Response response = chain.proceed(chain.request());
    if (!response.isSuccessful()) {
        String content = errorBody(response.body());
        AzureJacksonAdapter jacksonAdapter = new AzureJacksonAdapter();
        CloudError cloudError = jacksonAdapter.deserialize(content, CloudError.class);
        if (cloudError != null && "MissingSubscriptionRegistration".equals(cloudError.code())) {
            Pattern pattern = Pattern.compile("/subscriptions/([\\w-]+)/", Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(chain.request().url().toString());
            matcher.find();
            RestClient restClient = new RestClient.Builder().withBaseUrl("https://" + chain.request().url().host()).withCredentials(credentials).withSerializerAdapter(jacksonAdapter).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).build();
            ResourceManager resourceManager = ResourceManager.authenticate(restClient).withSubscription(matcher.group(1));
            pattern = Pattern.compile(".*'(.*)'");
            matcher = pattern.matcher(cloudError.message());
            matcher.find();
            Provider provider = registerProvider(matcher.group(1), resourceManager);
            while (provider.registrationState().equalsIgnoreCase("Unregistered") || provider.registrationState().equalsIgnoreCase("Registering")) {
                SdkContext.sleep(5 * 1000);
                provider = resourceManager.providers().getByName(provider.namespace());
            }
            // Retry
            response = chain.proceed(chain.request());
        }
    }
    return response;
}
Also used : Response(okhttp3.Response) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) CloudError(com.microsoft.azure.CloudError) ResourceManager(com.microsoft.azure.management.resources.implementation.ResourceManager) Provider(com.microsoft.azure.management.resources.Provider)

Aggregations

ResourceManager (com.microsoft.azure.management.resources.implementation.ResourceManager)2 CloudError (com.microsoft.azure.CloudError)1 Provider (com.microsoft.azure.management.resources.Provider)1 AzureJacksonAdapter (com.microsoft.azure.serializer.AzureJacksonAdapter)1 RestClient (com.microsoft.rest.RestClient)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Response (okhttp3.Response)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1