Search in sources :

Example 1 with CheckNameAvailabilityResultEntity

use of com.microsoft.azure.toolkit.lib.common.entity.CheckNameAvailabilityResultEntity in project azure-tools-for-java by Microsoft.

the class SqlServerNameValidator method apply.

@Override
public AzureValidationInfo apply(ServerNameTextField textField) {
    final String value = textField.getValue();
    // validate availability
    CheckNameAvailabilityResultEntity resultEntity;
    try {
        resultEntity = Azure.az(AzureSqlServer.class).checkNameAvailability(textField.getSubscriptionId(), value);
    } catch (ManagementException e) {
        return AzureValidationInfo.builder().input(textField).message(e.getMessage()).type(AzureValidationInfo.Type.ERROR).build();
    }
    if (!resultEntity.isAvailable()) {
        String message = resultEntity.getUnavailabilityReason();
        if ("AlreadyExists".equalsIgnoreCase(resultEntity.getUnavailabilityReason())) {
            message = "The specified server name is already in use.";
        }
        return AzureValidationInfo.builder().input(textField).message(message).type(AzureValidationInfo.Type.ERROR).build();
    }
    return AzureValidationInfo.OK;
}
Also used : ManagementException(com.azure.core.management.exception.ManagementException) CheckNameAvailabilityResultEntity(com.microsoft.azure.toolkit.lib.common.entity.CheckNameAvailabilityResultEntity)

Example 2 with CheckNameAvailabilityResultEntity

use of com.microsoft.azure.toolkit.lib.common.entity.CheckNameAvailabilityResultEntity in project azure-tools-for-java by Microsoft.

the class AccountNameTextField method doValidateValue.

@Override
@Nonnull
public AzureValidationInfo doValidateValue() {
    if (StringUtils.isBlank(subscriptionId)) {
        return AzureValidationInfo.UNINITIALIZED;
    }
    final AzureValidationInfo info = super.doValidateValue();
    if (!AzureValidationInfo.OK.equals(info)) {
        return info;
    }
    final String value = this.getValue();
    // validate length
    if (StringUtils.length(value) < minLength || StringUtils.length(value) > maxLength) {
        return AzureValidationInfo.builder().input(this).message(String.format("Server name must be at least %s characters and at most %s characters.", minLength, maxLength)).type(AzureValidationInfo.Type.ERROR).build();
    }
    // validate special character
    if (!PATTERN.matcher(value).matches()) {
        return AzureValidationInfo.builder().input(this).message("The field can contain only lowercase letters and numbers. Name must be between 3 and 24 characters.").type(AzureValidationInfo.Type.ERROR).build();
    }
    // validate availability
    CheckNameAvailabilityResultEntity resultEntity;
    try {
        resultEntity = Azure.az(AzureStorageAccount.class).checkNameAvailability(subscriptionId, this.getValue());
    } catch (ManagementException e) {
        return AzureValidationInfo.builder().input(this).message(e.getMessage()).type(AzureValidationInfo.Type.ERROR).build();
    }
    if (!resultEntity.isAvailable()) {
        String message = resultEntity.getUnavailabilityReason();
        if ("AlreadyExists".equalsIgnoreCase(resultEntity.getUnavailabilityReason())) {
            message = "The specified storage account name is already taken.";
        }
        return AzureValidationInfo.builder().input(this).message(message).type(AzureValidationInfo.Type.ERROR).build();
    }
    return AzureValidationInfo.OK;
}
Also used : AzureValidationInfo(com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo) ManagementException(com.azure.core.management.exception.ManagementException) CheckNameAvailabilityResultEntity(com.microsoft.azure.toolkit.lib.common.entity.CheckNameAvailabilityResultEntity) Nonnull(javax.annotation.Nonnull)

Aggregations

ManagementException (com.azure.core.management.exception.ManagementException)2 CheckNameAvailabilityResultEntity (com.microsoft.azure.toolkit.lib.common.entity.CheckNameAvailabilityResultEntity)2 AzureValidationInfo (com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo)1 Nonnull (javax.annotation.Nonnull)1