/** * Extracts a version string in the format xxx.yyy.zzz (with variable number of segments) out of * a raw string. E.g. it will extract <code>1.3.0</code> out of <code>v1.3.0-abcdef</code>. * * @throws IllegalArgumentException * if the extraction is not possible. */ public static String extractNumericVersion(String rawVersion) { AssertUtil.assertNotNullOrEmpty(rawVersion, "rawVersion"); Matcher matcher = EXTRACT_NUMERIC_VERSION_PATTERN.matcher(rawVersion); if (matcher.matches()) { return matcher.group(VERSION_GROUP_NAME); } throw new IllegalArgumentException( String.format("%s %s", CANNOT_EXTRACT_NUMERIC_VERSION_MESSAGE, rawVersion)); }
static String apiPrefix(KubernetesContext context, String apiVersion) { AssertUtil.assertNotNull(context.host, "context.host"); AssertUtil.assertNotNullOrEmpty(context.host.address, "context.host.address"); return context.host.address + apiVersion; }
/** * Sets the ID of the task. */ public RetriableTaskBuilder<T> withTaskId(String taskId) { assertTaskNotExecuted(); AssertUtil.assertNotNullOrEmpty(taskId, "taskId"); this.taskConfiguration.taskId = taskId; return this; }
public static AuthRole fromSuffix(String suffix) { assertNotNullOrEmpty(suffix, "suffix"); for (AuthRole r : AuthRole.values()) { if (r.suffix.equals(suffix)) { return r; } } throw new IllegalArgumentException("No matching type for:" + suffix); }
public static void validateProjectName(String name) { AssertUtil.assertNotNullOrEmpty(name, "name"); if (!Harbor.PROJECT_NAME_PATTERN.matcher(name).matches()) { throw new LocalizableValidationException(PROJECT_NAME_CRITERIA_MESSAGE, PROJECT_NAME_CRITERIA_CODE); } if (name.length() > 254) { throw new LocalizableValidationException(PROJECT_NAME_LENGTH_MESSAGE, PROJECT_NAME_LENGTH_CODE); } }
/** * Extract data from RoleState ID, which RoleState was build as duplicate RoleState when * assigning groups to project roles, with ID following the pattern: * projectId_groupdId_roleSuffix * * @param roleStateId * * @return String[] with 3 elements, the first one is the project id, the second is the group * id and third is the project role suffix. */ public static String[] extractDataFromRoleStateId(String roleStateId) { assertNotNullOrEmpty(roleStateId, "roleStateId"); int firstSeparatorIndex = roleStateId.indexOf(AuthRole.SUFFIX_SEPARATOR); int lastSeparatorIndex = roleStateId.lastIndexOf(AuthRole.SUFFIX_SEPARATOR); if (firstSeparatorIndex == -1 || lastSeparatorIndex == -1 || firstSeparatorIndex == lastSeparatorIndex) { throw new IllegalArgumentException("Provided role state id is not following the " + "pattern: projectId_groupdId_roleSuffix - " + roleStateId); } String projectId = roleStateId.substring(0, firstSeparatorIndex); String groupId = roleStateId.substring(firstSeparatorIndex + 1, lastSeparatorIndex); String roleSuffix = roleStateId.substring(lastSeparatorIndex + 1, roleStateId.length()); return new String[] { projectId, groupId, roleSuffix }; }
/** * Tries to compare the provided numeric version strings. They must be in the format * <code>xxx.yyy.zzz</code> (variable number of segments is possible) where each segment is an * integer. * * @return -1 if numericVersion1 < numericVersion1, +1 if numericVersion1 > numericVersion1 and * 0 if the provided versions are equal * @throws LocalizableValidationException * if any of the provided version strings is null or empty * @throws NumberFormatException. * if any of sections of the version string cannot be parsed as an integer with * radix 10. */ public static int compareNumericVersions(String numericVersion1, String numericVersion2) { AssertUtil.assertNotNullOrEmpty(numericVersion1, "numericVersion1"); AssertUtil.assertNotNullOrEmpty(numericVersion2, "numericVersion2"); String[] v1Tokens = numericVersion1.split("\\."); String[] v2Tokens = numericVersion2.split("\\."); int index = 0; while (index < v1Tokens.length && index < v2Tokens.length && v1Tokens[index].equals(v2Tokens[index])) { index++; } if (index < Math.min(v1Tokens.length, v2Tokens.length)) { int diff = Integer.parseInt(v1Tokens[index]) - Integer.parseInt(v2Tokens[index]); return Integer.signum(diff); } return Integer.signum(v1Tokens.length - v2Tokens.length); }
private void validateDescription(KubernetesDescription description) throws IOException { if (!isValidYaml(description.kubernetesEntity)) { throw new LocalizableValidationException("Invalid YAML input.", "compute.template.yaml.invalid"); } BaseKubernetesObject kubernetesEntity = description .getKubernetesEntity(BaseKubernetesObject.class); assertNotNullOrEmpty(kubernetesEntity.apiVersion, "apiVersion"); assertNotNullOrEmpty(kubernetesEntity.kind, "kind"); assertNotNull(kubernetesEntity.metadata, "metadata"); assertNotNullOrEmpty(kubernetesEntity.metadata.name, "metadata.name"); description.type = kubernetesEntity.kind; description.apiVersion = kubernetesEntity.apiVersion; description.name = kubernetesEntity.metadata.name; description.namespace = kubernetesEntity.metadata.namespace; }
/** * Tries to compare the provided raw version strings by first extracting numeric versions of the * form <code>xxx.yyy.zzz</code> with variable number of segments. * * @return -1 if rawVersion1 < rawVersion2, +1 if rawVersion1 > rawVersion2 and 0 if the * provided versions are equal * @throws LocalizableValidationException * if any of the provided version strings is null or empty * @throws IllegalArgumentException * if it is not possible to extract numeric versions out of the raw versions. */ public static int compareRawVersions(String rawVersion1, String rawVersion2) { AssertUtil.assertNotNullOrEmpty(rawVersion1, "rawVersion1"); AssertUtil.assertNotNullOrEmpty(rawVersion2, "rawVersion2"); return compareNumericVersions( extractNumericVersion(rawVersion1), extractNumericVersion(rawVersion2)); }
private void validate(Endpoint endpoint) { AssertUtil.assertNotNull(endpoint, "endpoint"); AssertUtil.assertNotNullOrEmpty(endpoint.uaaEndpoint, "UAA endpoint"); AssertUtil.assertNotNullOrEmpty(endpoint.apiEndpoint, "API endpoint"); logInfo("Parsing uaa endpoint: %s", endpoint.uaaEndpoint); URI uri = URI.create(endpoint.uaaEndpoint); validateUri(uri); logInfo("Parsing api endpoint: %s", endpoint.apiEndpoint); uri = URI.create(endpoint.apiEndpoint); validateUri(uri); }
@Override public DeferredResult<Set<String>> getAllGroupsForPrincipal(Operation op, String principalId) { assertNotNullOrEmpty(principalId, "principalId"); return getDirectlyAssignedGroupsForPrincipal(principalId) .thenCompose(groups -> getIndirectlyAssignedGroupsForPrincipal(groups, null, null)); }
private void validate(EndpointSpec spec) { AssertUtil.assertNotNull(spec, "endpoint spec"); AssertUtil.assertNotNull(spec.endpoint, "endpoint"); AssertUtil.assertNotNullOrEmpty(spec.endpoint.uaaEndpoint, "UAA endpoint"); AssertUtil.assertNotNullOrEmpty(spec.endpoint.apiEndpoint, "API endpoint"); URI uri; Endpoint e = spec.endpoint; try { logInfo("Parsing uaa endpoint: %s", e.uaaEndpoint); uri = URI.create(e.uaaEndpoint); } catch (Exception ex) { throw new LocalizableValidationException("Invalid host address: " + e.uaaEndpoint, "common.host.address.invalid", e.uaaEndpoint); } validateUri(uri); spec.uaaUri = uri; try { logInfo("Parsing api endpoint: %s", e.apiEndpoint); uri = URI.create(e.apiEndpoint); } catch (Exception ex) { throw new LocalizableValidationException("Invalid host address: " + e.apiEndpoint, "common.host.address.invalid", e.apiEndpoint); } validateUri(uri); spec.apiUri = uri; }
@Override public void handleGet(Operation op) { try { Map<String, String> queryParams = UriUtils.parseUriQueryParams(op.getUri()); String hostLink = queryParams.get(KUBERNETES_HOST_LINK_PARAM_NAME); AssertUtil.assertNotNullOrEmpty(hostLink, KUBERNETES_HOST_LINK_PARAM_NAME); getHostAndCredentials(op, hostLink, (compute, credentials) -> constructKubeConfig(op, compute, credentials)); } catch (Exception x) { logSevere(x); op.fail(x); } }
private void validateTaskAndCalculateOptionalFields() { assertTaskNotExecuted(); AssertUtil.assertNotNullOrEmpty(taskConfiguration.taskId, "taskId"); AssertUtil.assertNotNull(taskConfiguration.serviceHost, "serviceHost"); AssertUtil.assertNotNull(taskConfiguration.taskFunction, "taskBody"); AssertUtil.assertNotNull(taskConfiguration.retryDelays, "retryDelays"); AssertUtil.assertNotNull(taskConfiguration.retryDelaysTimeUnit, "retryDelaysTimeUnit"); if (taskConfiguration.maxRetries == null) { taskConfiguration.maxRetries = taskConfiguration.retryDelays.size(); } else { AssertUtil.assertTrue(taskConfiguration.maxRetries >= 0, "maxRetries must be a non-negative integer"); } }
public static DeferredResult<Void> freeProperty(Service service, String propertiesId, String propertyName) { assertNotNull(service, "service"); assertNotNullOrEmpty(propertiesId, "propertiesId"); assertNotNullOrEmpty(propertyName, "propertyName"); UniquePropertiesRequest request = new UniquePropertiesRequest(); request.toRemove = Collections.singletonList(propertyName); String uriPath = UriUtils.buildUriPath(UniquePropertiesService.FACTORY_LINK, propertiesId); DeferredResult<Void> result = new DeferredResult<>(); Operation patch = Operation.createPatch(service, uriPath) .setBody(request) .setCompletion((o, ex) -> { if (ex != null) { result.fail(ex); return; } result.complete(null); }); service.sendRequest(patch); return result; }
public static URI buildKubernetesFactoryUri(KubernetesDescription description, KubernetesContext context) { assertNotNull(context.host, "context.host"); assertNotNullOrEmpty(context.host.address, "context.host.address"); assertNotNull(description, "kubernetesDescription"); assertNotNull(description.type, "description.type"); assertNotNull(description.apiVersion, "description.apiVersion"); String entityPath = getKubernetesPath(description.type); if (entityPath == null) { throw new IllegalArgumentException( String.format("Kubernetes description with entity of type %s is not supported.", description.type)); } String versionPrefix = getApiVersionPrefix(description.apiVersion); String uriString; if (KubernetesUtil.NAMESPACE_TYPE.equals(description.type)) { uriString = apiPrefix(context, versionPrefix) + entityPath; } else { uriString = namespacePrefix(context, description, versionPrefix) + entityPath; } return UriUtils.buildUri(uriString); }
public void validate() { AssertUtil.assertNotNullOrEmpty(endpointLink, FIELD_NAME_ENDPOINT_LINK); AssertUtil.assertNotNull(cluster, FIELD_NAME_CLUSTER); AssertUtil.assertNotNull(cluster.uuid, "cluster.uuid"); AssertUtil.assertNotNull(cluster.name, "cluster.name"); AssertUtil.assertNotNull(getExternalAddress(), "external-hostname"); AssertUtil.assertNotEmpty(tenantLinks, FIELD_NAME_TENANT_LINKS); }
@Override public DeferredResult<Principal> deletePrincipal(Operation op, String principalId) { assertNotNullOrEmpty(principalId, "principalId"); Operation delete = Operation.createDelete(service, UriUtils.buildUriPath(LocalPrincipalFactoryService.SELF_LINK, encode(principalId))); service.setAuthorizationContext(delete, service.getSystemAuthorizationContext()); return service.sendWithDeferredResult(delete, LocalPrincipalState.class) .thenApply((s) -> fromLocalPrincipalToPrincipal(s)); }
@Override protected void validateStateOnStart(PKSProvisioningTaskState task) throws IllegalArgumentException { AssertUtil.assertNotNull(task, "task"); AssertUtil.assertNotNull(task.customProperties, "customProperties"); AssertUtil.assertNotEmpty(task.getCustomProperty(PKS_CLUSTER_NAME_PROP_NAME), "customProperties [" + PKS_CLUSTER_NAME_PROP_NAME + "]"); AssertUtil.assertNotEmpty(task.getCustomProperty(PKS_PLAN_NAME_FIELD), "customProperties [" + PKS_PLAN_NAME_FIELD + "]"); AssertUtil.assertNotEmpty(task.getCustomProperty(PKS_MASTER_HOST_FIELD), "customProperties [" + PKS_MASTER_HOST_FIELD + "]"); AssertUtil.assertNotNullOrEmpty(task.endpointLink, "endpointLink"); AssertUtil.assertNotNull(task.tenantLinks, "tenantLinks"); AssertUtil.assertNotEmpty(TenantLinksUtil.getProjectAndGroupLinks(task.tenantLinks), "tenantLinks(project/group links only)"); }
@Override public DeferredResult<Principal> getPrincipal(Operation op, String principalId) { assertNotNullOrEmpty(principalId, "principalId"); Operation get = Operation.createGet(service, UriUtils.buildUriPath(LocalPrincipalFactoryService.SELF_LINK, encode(principalId))); service.setAuthorizationContext(get, service.getSystemAuthorizationContext()); return service.sendWithDeferredResult(get, LocalPrincipalState.class) .thenApply(s -> fromLocalPrincipalToPrincipal(s)) .thenApply(p -> new Pair<Principal, Set<String>>(p, null)) .thenCompose(pair -> getAllGroupsForPrincipal(op, principalId) .thenApply(groups -> new Pair<>(pair.left, groups))) .thenApply(pair -> { pair.left.groups = pair.right; return pair.left; }); }