|
| 1 | +/* |
| 2 | + * Copyright © 2019 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package io.cdap.plugin.sendgrid.common.config; |
| 17 | + |
| 18 | +import com.google.common.base.Strings; |
| 19 | +import io.cdap.cdap.etl.api.FailureCollector; |
| 20 | +import io.cdap.cdap.etl.api.validation.ValidationFailure; |
| 21 | +import io.cdap.plugin.sendgrid.common.SendGridClient; |
| 22 | +import io.cdap.plugin.sendgrid.common.helpers.ObjectHelper; |
| 23 | +import io.cdap.plugin.sendgrid.common.helpers.ObjectInfo; |
| 24 | +import io.cdap.plugin.sendgrid.common.objects.DataSourceGroupType; |
| 25 | +import io.cdap.plugin.sendgrid.common.objects.SendGridAuthType; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.List; |
| 29 | +import java.util.stream.Collectors; |
| 30 | + |
| 31 | +/** |
| 32 | + * Validates configuration |
| 33 | + */ |
| 34 | +public class BaseConfigValidator { |
| 35 | + protected FailureCollector failureCollector; |
| 36 | + protected SendGridClient client = null; |
| 37 | + private BaseConfig config; |
| 38 | + |
| 39 | + public BaseConfigValidator(FailureCollector failureCollector, BaseConfig config) { |
| 40 | + this.failureCollector = failureCollector; |
| 41 | + this.config = config; |
| 42 | + } |
| 43 | + |
| 44 | + private void checkAuthType() { |
| 45 | + try { |
| 46 | + config.getAuthType(); |
| 47 | + } catch (IllegalArgumentException e) { |
| 48 | + failureCollector.addFailure(String.format("Wrong authentication method selected: %s", e.getMessage()), null) |
| 49 | + .withConfigProperty(BaseConfig.PROPERTY_AUTH_TYPE); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private void checkAuthData() { |
| 54 | + boolean tryToLogin = true; |
| 55 | + |
| 56 | + switch (config.getAuthType()) { |
| 57 | + case BASIC: |
| 58 | + if (Strings.isNullOrEmpty(config.getAuthUserName())) { |
| 59 | + failureCollector.addFailure("User name is not set", null) |
| 60 | + .withConfigProperty(BaseConfig.PROPERTY_AUTH_USERNAME); |
| 61 | + tryToLogin = false; |
| 62 | + } |
| 63 | + if (Strings.isNullOrEmpty(config.getAuthPassword())) { |
| 64 | + failureCollector.addFailure("Password is not set", null) |
| 65 | + .withConfigProperty(BaseConfig.PROPERTY_AUTH_PASSWORD); |
| 66 | + tryToLogin = false; |
| 67 | + } |
| 68 | + |
| 69 | + if (tryToLogin) { |
| 70 | + client = new SendGridClient(config.getAuthUserName(), config.getAuthPassword()); |
| 71 | + } |
| 72 | + break; |
| 73 | + case API: |
| 74 | + if (Strings.isNullOrEmpty(config.getSendGridApiKey())) { |
| 75 | + failureCollector.addFailure("API Key is not set", null) |
| 76 | + .withConfigProperty(BaseConfig.PROPERTY_SENDGRID_API_KEY); |
| 77 | + tryToLogin = false; |
| 78 | + } |
| 79 | + |
| 80 | + if (tryToLogin) { |
| 81 | + client = new SendGridClient(config.getSendGridApiKey()); |
| 82 | + } |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void checkCategoriesSelection() { |
| 88 | + if (config.getDataSourceTypes().isEmpty()) { |
| 89 | + failureCollector.addFailure("Object categories are not set", null) |
| 90 | + .withConfigProperty(BaseConfig.PROPERTY_DATA_SOURCE_TYPES); |
| 91 | + } |
| 92 | + |
| 93 | + config.getDataSourceTypes() |
| 94 | + .forEach(x -> { |
| 95 | + try { |
| 96 | + DataSourceGroupType.fromString(x); |
| 97 | + } catch (IllegalStateException e) { |
| 98 | + failureCollector.addFailure( |
| 99 | + String.format("Unknown '%s' data source type: %s", x , e.getMessage()), null) |
| 100 | + .withStacktrace(e.getStackTrace()); |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + private void checkObjectsSelection() { |
| 106 | + List<ObjectInfo> objects = config.getDataSource().stream() |
| 107 | + .filter(x -> !Strings.isNullOrEmpty(x)) |
| 108 | + .map(ObjectHelper::getObjectInfo) |
| 109 | + .collect(Collectors.toList()); |
| 110 | + List<DataSourceGroupType> categories = config.getDataSourceTypes().stream() |
| 111 | + .map(DataSourceGroupType::fromString) |
| 112 | + .collect(Collectors.toList()); |
| 113 | + |
| 114 | + categories.forEach(category -> { |
| 115 | + if (objects.stream().noneMatch(x -> x.getDataSourceGroupType() == category)) { |
| 116 | + failureCollector.addFailure( |
| 117 | + String.format("No objects selected for the category: %s", category.name()), null) |
| 118 | + .withConfigProperty(BaseConfig.PROPERTY_DATA_SOURCE); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + private void checkFieldSelection() { |
| 124 | + List<ObjectInfo> objects = config.getDataSource().stream() |
| 125 | + .map(ObjectHelper::getObjectInfo) |
| 126 | + .collect(Collectors.toList()); |
| 127 | + |
| 128 | + List<String> fields = config.getFields(); |
| 129 | + |
| 130 | + objects.forEach(object -> { |
| 131 | + if (object.getFieldsDefinitions(fields).isEmpty()) { |
| 132 | + failureCollector.addFailure( |
| 133 | + String.format("No fields selected for object '%s'", object.getCdapObjectName()), null) |
| 134 | + .withConfigProperty(BaseConfig.PROPERTY_DATA_SOURCE_FIELDS); |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + private void checkClientConnectivity() { |
| 140 | + try { |
| 141 | + client.checkConnection(); |
| 142 | + } catch (IOException e) { |
| 143 | + ValidationFailure failure = failureCollector |
| 144 | + .addFailure(String.format("Issues with API connectivity: %s", e.getMessage()), "Check login settings") |
| 145 | + .withStacktrace(e.getStackTrace()); |
| 146 | + |
| 147 | + if (config.getAuthType() == SendGridAuthType.BASIC) { |
| 148 | + failure |
| 149 | + .withConfigProperty(BaseConfig.PROPERTY_AUTH_USERNAME) |
| 150 | + .withConfigProperty(BaseConfig.PROPERTY_AUTH_PASSWORD); |
| 151 | + } |
| 152 | + if (config.getAuthType() == SendGridAuthType.API) { |
| 153 | + failure.withConfigProperty(BaseConfig.PROPERTY_SENDGRID_API_KEY); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public void validate() { |
| 159 | + client = null; |
| 160 | + |
| 161 | + checkAuthType(); |
| 162 | + checkAuthData(); |
| 163 | + checkCategoriesSelection(); |
| 164 | + checkObjectsSelection(); |
| 165 | + checkFieldSelection(); |
| 166 | + |
| 167 | + if (client != null) { // client could be not constructed, if any of checkAuth tests failed |
| 168 | + checkClientConnectivity(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | +} |
0 commit comments