package org.embulk.input.zendesk.utils; import org.embulk.config.ConfigException; import org.embulk.input.zendesk.ZendeskInputPlugin; import org.embulk.input.zendesk.services.ZendeskSupportAPIService; import org.embulk.spi.Exec; import org.slf4j.Logger; public class ZendeskValidatorUtils { private ZendeskValidatorUtils(){} private static final Logger logger = Exec.getLogger(ZendeskValidatorUtils.class); public static void validateInputTask(final ZendeskInputPlugin.PluginTask task, final ZendeskSupportAPIService zendeskSupportAPIService) { validateAppMarketPlace(task.getAppMarketPlaceIntegrationName().isPresent(), task.getAppMarketPlaceAppId().isPresent(), task.getAppMarketPlaceOrgId().isPresent()); validateCredentials(task); validateIncremental(task); } private static void validateCredentials(final ZendeskInputPlugin.PluginTask task) { switch (task.getAuthenticationMethod()) { case OAUTH: if (!task.getAccessToken().isPresent()) { throw new ConfigException(String.format("access_token is required for authentication method '%s'", task.getAuthenticationMethod().name().toLowerCase())); } break; case TOKEN: if (!task.getUsername().isPresent() || !task.getToken().isPresent()) { throw new ConfigException(String.format("username and token are required for authentication method '%s'", task.getAuthenticationMethod().name().toLowerCase())); } break; case BASIC: if (!task.getUsername().isPresent() || !task.getPassword().isPresent()) { throw new ConfigException(String.format("username and password are required for authentication method '%s'", task.getAuthenticationMethod().name().toLowerCase())); } break; default: throw new ConfigException("Unknown authentication method"); } } private static void validateAppMarketPlace(final boolean isAppMarketIntegrationNamePresent, final boolean isAppMarketAppIdPresent, final boolean isAppMarketOrgIdPresent) { final boolean isAllAvailable = isAppMarketIntegrationNamePresent && isAppMarketAppIdPresent && isAppMarketOrgIdPresent; final boolean isAllUnAvailable = !isAppMarketIntegrationNamePresent && !isAppMarketAppIdPresent && !isAppMarketOrgIdPresent; // All or nothing needed if (!(isAllAvailable || isAllUnAvailable)) { throw new ConfigException("All of app_marketplace_integration_name, app_marketplace_org_id, " + "app_marketplace_app_id " + "are required to fill out for Apps Marketplace API header"); } } private static void validateIncremental(final ZendeskInputPlugin.PluginTask task) { if (task.getIncremental()) { if (!task.getDedup()) { logger.warn("You've selected to skip de-duplicating records, result may contain duplicated data"); } if (!ZendeskUtils.isSupportAPIIncremental(task.getTarget()) && task.getStartTime().isPresent()) { logger.warn(String.format("Target: '%s' doesn't support incremental export API. Will be ignored start_time option", task.getTarget())); } } } }