use of com.sequenceiq.cloudbreak.cloud.azure.AzureTenant in project cloudbreak by hortonworks.
the class TenantChecker method checkTenant.
public void checkTenant(String tenantId, String accessToken) throws InteractiveLoginException {
if (tenantId == null) {
throw new InteractiveLoginException("Parameter tenantId is required and cannot be null.");
}
Client client = ClientBuilder.newClient();
WebTarget resource = client.target(AZURE_MANAGEMENT);
Builder request = resource.path("/tenants").queryParam("api-version", "2016-06-01").request();
request.accept(MediaType.APPLICATION_JSON);
request.header("Authorization", "Bearer " + accessToken);
Response response = request.get();
if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
String entity = response.readEntity(String.class);
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode tenantArray = mapper.readTree(entity).get("value");
ObjectReader reader = mapper.readerFor(new TypeReference<ArrayList<AzureTenant>>() {
});
List<AzureTenant> tenants = reader.readValue(tenantArray);
for (AzureTenant tenant : tenants) {
if (tenant.getTenantId().equals(tenantId)) {
LOGGER.debug("Tenant definitions successfully retrieved:" + tenant.getTenantId());
return;
}
}
} catch (IOException e) {
throw new InteractiveLoginException(e.toString());
}
throw new InteractiveLoginException("Tenant specified in Profile file not found with id: " + tenantId);
} else {
String errorResponse = response.readEntity(String.class);
try {
String errorMessage = new ObjectMapper().readTree(errorResponse).get("error").get("message").asText();
LOGGER.error("Tenant retrieve error:" + errorMessage);
throw new InteractiveLoginException("Error with the tenant specified in Profile file id: " + tenantId + ", message: " + errorMessage);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
use of com.sequenceiq.cloudbreak.cloud.azure.AzureTenant in project cloudbreak by hortonworks.
the class AzureInteractiveLoginStatusCheckerTask method doCall.
@Override
protected Boolean doCall() {
Response response = createPollingRequest();
if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
String tokenResponseString = response.readEntity(String.class);
try {
String refreshToken = new ObjectMapper().readTree(tokenResponseString).get("refresh_token").asText();
LOGGER.debug("Access token received");
ExtendedCloudCredential extendedCloudCredential = armInteractiveLoginStatusCheckerContext.getExtendedCloudCredential();
AzureCredentialView armCredentialView = new AzureCredentialView(extendedCloudCredential);
try {
String tenantId = armCredentialView.getTenantId();
String graphApiAccessToken = createResourceToken(refreshToken, tenantId, GRAPH_WINDOWS);
String managementApiToken = createResourceToken(refreshToken, tenantId, MANAGEMENT_CORE_WINDOWS);
subscriptionChecker.checkSubscription(armCredentialView.getSubscriptionId(), managementApiToken);
List<AzureTenant> tenants = tenantChecker.getTenants(managementApiToken);
tenantChecker.checkTenant(tenantId, tenants);
String deploymentAddress = armCredentialView.getDeploymentAddress();
AzureApplication application = applicationCreator.createApplication(graphApiAccessToken, tenantId, deploymentAddress);
sendStatusMessage(extendedCloudCredential, "Cloudbreak application created");
applicationCreator.waitApplicationCreated(graphApiAccessToken, tenantId, application.getObjectId());
ServicePrincipalInner sp = principalCreator.createServicePrincipal(graphApiAccessToken, application.getAppId(), tenantId);
principalCreator.waitPrincipalCreated(graphApiAccessToken, sp.objectId(), tenantId, application);
String notification = new StringBuilder("Principal created for application!").append(" Name: ").append(sp.displayName()).append(", AppId: ").append(sp.appId()).toString();
sendStatusMessage(extendedCloudCredential, notification);
saveNewParametersIntoExtendedCloudCred(extendedCloudCredential, application, sp);
armInteractiveLoginStatusCheckerContext.getCredentialNotifier().createCredential(getAuthenticatedContext().getCloudContext(), extendedCloudCredential);
} catch (InteractiveLoginException e) {
LOGGER.info("Interactive login failed", e);
sendErrorStatusMessage(extendedCloudCredential, e.getMessage());
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
return true;
} else {
LOGGER.debug("Polling request failed this time, status code {}, response: {}", response.getStatus(), response.readEntity(String.class));
return false;
}
}
use of com.sequenceiq.cloudbreak.cloud.azure.AzureTenant in project cloudbreak by hortonworks.
the class TenantChecker method collectTenants.
private List<AzureTenant> collectTenants(String accessToken, Response response) throws InteractiveLoginException {
if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
AzureTenantListResult azureTenantListResult = response.readEntity(AzureTenantListResult.class);
List<AzureTenant> tenantList = azureTenantListResult.getValue();
if (azureTenantListResult.getNextLink() != null) {
tenantList.addAll(getNextSetOfTenants(azureTenantListResult.getNextLink(), accessToken));
}
return tenantList;
} else {
String errorResponse = response.readEntity(String.class);
try {
String errorMessage = new ObjectMapper().readTree(errorResponse).get("error").get("message").asText();
LOGGER.info("Tenant retrieve error:" + errorMessage);
throw new InteractiveLoginException("Error with the tenants, message: " + errorMessage);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
Aggregations