Adds new more relaxed validator for Azure entities

This commit is contained in:
Dani Estevez 2018-05-22 15:02:41 -04:00 committed by Ignasi Barrera
parent b76a594e81
commit 5ee3ae552d
2 changed files with 65 additions and 1 deletions

View File

@ -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));
}
}

View File

@ -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();
}