class ClouderaManagerRangerUtil {

    public Optional<ApiCommand> setAzureCloudIdentityMapping(String stackCrn, Map<String, String> requestedAzureUserMapping) throws ApiException {
        ApiClient client = clouderaManagerProxiedClientFactory.getProxiedClouderaManagerClient(stackCrn);
        String clusterName = getClusterName(client);
        String rangerUserSyncRoleName = getRangerUserSyncRoleName(client, clusterName);
        RolesResourceApi rolesResourceApi = clouderaManagerApiFactory.getRolesResourceApi(client);

        Map<String, String> existingMappingStr = getExistingAzureUserMapping(rolesResourceApi, clusterName, rangerUserSyncRoleName);

        // We only want to go through with the operation when ONE of the following conditions are met
        // 1) When the requested azure user mapping is different than the existing one
        // 2) When the role is stale. This is for the rare case when we previously were able to set the role and
        //    failed before trigerring refresh. This ensures that we trigger refresh in the subsequent call.
        boolean operationRequired = !existingMappingStr.equals(requestedAzureUserMapping) ||
                isRoleStale(rolesResourceApi, clusterName, rangerUserSyncRoleName);

        if (!operationRequired) {
            LOGGER.info("Existing azure cloud mappings are the same, nothing to do");
            return Optional.empty();
        } else {
            LOGGER.info("Existing azure cloud mappings are different (or role is stale), setting mapping and trigerring role refresh");
            ApiConfigList configList = new ApiConfigList();
            configList.addItemsItem(newCloudIdentityConfig(AZURE_USER_MAPPING, requestedAzureUserMapping));
            rolesResourceApi.updateRoleConfig(clusterName, rangerUserSyncRoleName, RANGER_SERVICE_NAME,
                    "Updating Azure Cloud Identity Mapping through Cloudbreak",
                    configList);
            ApiCommand command = triggerRoleRefresh(client, clusterName, RANGER_SERVICE_NAME, rangerUserSyncRoleName);
            return Optional.of(command);
        }
    }

}