Adds new more relaxed validator for Azure entities
Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/5ee3ae55 Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/5ee3ae55 Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/5ee3ae55 Branch: refs/heads/master Commit: 5ee3ae552d17e86d05db0aabae2803170e76de00 Parents: b76a594 Author: Dani Estevez <[email protected]> Authored: Tue May 22 15:02:41 2018 -0400 Committer: Ignasi Barrera <[email protected]> Committed: Fri May 25 09:39:21 2018 +0200 ---------------------------------------------------------------------- .../arm/compute/config/AzureNameValidator.java | 61 ++++++++++++++++++++ .../arm/config/AzureComputeHttpApiModule.java | 5 +- 2 files changed, 65 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds/blob/5ee3ae55/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/config/AzureNameValidator.java ---------------------------------------------------------------------- diff --git a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/config/AzureNameValidator.java b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/config/AzureNameValidator.java new file mode 100644 index 0000000..4b2615a --- /dev/null +++ b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/config/AzureNameValidator.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jclouds.azurecompute.arm.compute.config; + +import static com.google.common.base.CharMatcher.anyOf; +import static com.google.common.base.CharMatcher.inRange; + +import org.jclouds.predicates.Validator; + +import com.google.common.base.CharMatcher; +import com.google.inject.Singleton; + +/** + * Validates name for azure entities + * https://docs.microsoft.com/en-us/azure/architecture/best-practices/naming-conventions + * + * @see org.jclouds.predicates.Validator + */ +@Singleton +public class AzureNameValidator extends Validator<String> { + private final int min = 2; + private final int max = 63; + + public void validate(String name) { + + if (name == null || name.length() < min || name.length() > max) + throw exception(name, "Can't be null or empty. Length must be " + min + " to " + max + " symbols."); + if (CharMatcher.JAVA_LETTER_OR_DIGIT.indexIn(name) != 0) + throw exception(name, "Should start with letter/number"); + + CharMatcher range = getAcceptableRange(); + if (!range.matchesAllOf(name)) + throw exception(name, "Should have lowercase or uppercase ASCII letters, numbers, or dashes"); + } + + private CharMatcher getAcceptableRange() { + return inRange('a', 'z').or(inRange('A', 'Z')).or(inRange('0', '9')).or(anyOf("-_.")); + } + + protected IllegalArgumentException exception(String name, String reason) { + return new IllegalArgumentException( + String.format("Object '%s' doesn't match Azure naming constraints. " + "Reason: %s.", name, + reason)); + } + +} http://git-wip-us.apache.org/repos/asf/jclouds/blob/5ee3ae55/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/config/AzureComputeHttpApiModule.java ---------------------------------------------------------------------- diff --git a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/config/AzureComputeHttpApiModule.java b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/config/AzureComputeHttpApiModule.java index 7ba23db..c01fc0a 100644 --- a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/config/AzureComputeHttpApiModule.java +++ b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/config/AzureComputeHttpApiModule.java @@ -23,10 +23,10 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Matcher; import java.util.regex.Pattern; - import javax.inject.Singleton; import org.jclouds.azurecompute.arm.AzureComputeApi; +import org.jclouds.azurecompute.arm.compute.config.AzureNameValidator; import org.jclouds.azurecompute.arm.config.GraphRBAC.GraphRBACForTenant; import org.jclouds.azurecompute.arm.domain.ServicePrincipal; import org.jclouds.azurecompute.arm.handlers.AzureComputeErrorHandler; @@ -38,6 +38,7 @@ import org.jclouds.location.suppliers.ImplicitLocationSupplier; import org.jclouds.location.suppliers.implicit.FirstRegion; import org.jclouds.oauth.v2.config.OAuthConfigFactory; import org.jclouds.oauth.v2.config.OAuthScopes; +import org.jclouds.predicates.Validator; import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.ConfiguresHttpApi; import org.jclouds.rest.config.HttpApiModule; @@ -73,6 +74,8 @@ public class AzureComputeHttpApiModule extends HttpApiModule<AzureComputeApi> { super.configure(); bind(OAuthScopes.class).toInstance(OAuthScopes.NoScopes.create()); bind(OAuthConfigFactory.class).to(AzureOAuthConfigFactory.class).in(Scopes.SINGLETON); + bind(new TypeLiteral<Validator<String>>() { + }).to(AzureNameValidator.class).in(Scopes.SINGLETON); bindServiceEndpoints(); }
