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) {
}
}
}
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;
}
Aggregations