Search in sources :

Example 1 with AzureRoleDefinitionListResponse

use of com.sequenceiq.cloudbreak.cloud.azure.AzureRoleDefinitionListResponse in project cloudbreak by hortonworks.

the class AzureRoleManager method getRoleDefinitions.

private List<AzureRoleDefinition> getRoleDefinitions(String subscriptionId, String accessToken, String filter) throws InteractiveLoginException {
    Client client = ClientBuilder.newClient();
    WebTarget resource = client.target(AZURE_MANAGEMENT);
    Builder request = resource.path("subscriptions/" + subscriptionId + "/providers/Microsoft.Authorization/roleDefinitions").queryParam("$filter", filter).queryParam("api-version", "2015-07-01").request();
    request.accept(MediaType.APPLICATION_JSON);
    request.header("Authorization", "Bearer " + accessToken);
    Response response = request.get();
    if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
        AzureRoleDefinitionListResponse azureRoleDefinitionListResponse = response.readEntity(AzureRoleDefinitionListResponse.class);
        LOGGER.info("Role definitions retrieved:" + azureRoleDefinitionListResponse.getValue());
        return azureRoleDefinitionListResponse.getValue();
    } else {
        String errorResponse = response.readEntity(String.class);
        LOGGER.error("Get role definition request with filter: {}, failed: {}", filter, errorResponse);
        if (Status.FORBIDDEN.getStatusCode() == response.getStatus()) {
            throw new InteractiveLoginException("You have no permission to access Active Directory roles, please contact with your administrator");
        } else {
            try {
                String errorMessage = new ObjectMapper().readTree(errorResponse).get("error").get("message").asText();
                throw new InteractiveLoginException("Get role definition request with filter: " + filter + " failed: " + errorMessage);
            } catch (IOException e) {
                throw new InteractiveLoginException("Get role definition request with filter: " + filter + " failed:" + errorResponse);
            }
        }
    }
}
Also used : AzureRoleDefinitionListResponse(com.sequenceiq.cloudbreak.cloud.azure.AzureRoleDefinitionListResponse) Response(javax.ws.rs.core.Response) AzureRoleDefinitionListResponse(com.sequenceiq.cloudbreak.cloud.azure.AzureRoleDefinitionListResponse) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) WebTarget(javax.ws.rs.client.WebTarget) IOException(java.io.IOException) Client(javax.ws.rs.client.Client) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AzureRoleDefinitionListResponse (com.sequenceiq.cloudbreak.cloud.azure.AzureRoleDefinitionListResponse)1 IOException (java.io.IOException)1 Client (javax.ws.rs.client.Client)1 ClientBuilder (javax.ws.rs.client.ClientBuilder)1 Builder (javax.ws.rs.client.Invocation.Builder)1 WebTarget (javax.ws.rs.client.WebTarget)1 Response (javax.ws.rs.core.Response)1