Skip to content

Commit

Permalink
Verify the storage account name when the template is verified [FIXED …
Browse files Browse the repository at this point in the history
…JENKINS-40289]
  • Loading branch information
clguiman committed Dec 13, 2016
1 parent 7e4ef8e commit 4ee5641
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/microsoft/azure/AzureVMAgentTemplate.java
Expand Up @@ -667,6 +667,9 @@ public FormValidation doVerifyConfiguration(
@QueryParameter String jvmOptions) {

AzureCredentials.ServicePrincipal servicePrincipal = AzureCredentials.getServicePrincipal(azureCredentialsId);
if(storageAccountName.trim().isEmpty())
storageAccountName = AzureVMAgentTemplate.GenerateUniqueStorageAccountName(resourceGroupName, servicePrincipal);

LOGGER.log(Level.INFO,
"Verify configuration:\n\t"
+ "subscriptionId: {0};\n\t"
Expand Down
Expand Up @@ -73,7 +73,9 @@
import com.microsoft.azure.exceptions.UnrecoverableCloudException;
import com.microsoft.azure.management.resources.models.ResourceGroupCreateOrUpdateResult;
import com.microsoft.azure.management.storage.models.AccountType;
import com.microsoft.azure.management.storage.models.CheckNameAvailabilityResponse;
import com.microsoft.azure.management.storage.models.StorageAccountCreateParameters;
import com.microsoft.azure.management.storage.models.StorageAccountGetPropertiesResponse;
import com.microsoft.azure.retry.ExponentialRetryStrategy;
import com.microsoft.azure.retry.NoRetryStrategy;
import com.microsoft.azure.util.AzureCredentials;
Expand Down Expand Up @@ -1176,6 +1178,16 @@ public String call() throws Exception {
};
verificationTaskList.add(callVerifyVirtualMachineImage);

// Callable for storage account virtual network.
Callable<String> callVerifyStorageAccountName = new Callable<String>() {

@Override
public String call() throws Exception {
return verifyStorageAccountName(config, resourceGroupName, storageAccountName);
}
};
verificationTaskList.add(callVerifyStorageAccountName);

try {
for (Future<String> validationResult : AzureVMCloud.getThreadPool().invokeAll(verificationTaskList)) {
try {
Expand Down Expand Up @@ -1330,6 +1342,33 @@ private static String verifyVirtualMachineImage(
}
}

private static String verifyStorageAccountName(
final Configuration config,
final String resourceGroupName,
final String storageAccountName) {

final StorageManagementClient storageClient = ServiceDelegateHelper.getStorageManagementClient(config);

try {
CheckNameAvailabilityResponse response = storageClient.getStorageAccountsOperations().checkNameAvailability(storageAccountName);
if(!response.isNameAvailable()) {
// The storage account already exists. We need to check it's not in the resource group we want to use
try {
StorageAccountGetPropertiesResponse rgResponse = storageClient.getStorageAccountsOperations().getProperties(resourceGroupName, storageAccountName);
if(rgResponse.getStatusCode() == 200)
return Constants.OP_SUCCESS;
} catch (ServiceException e) {
return Messages.Azure_GC_Template_SA_Already_Exists();
}
return Messages.Azure_GC_Template_SA_Already_Exists();
}
} catch (Exception e) {
LOGGER.log(Level.INFO, e.getMessage());
return Messages.Azure_GC_Template_SA_Cant_Validate();
}
return Constants.OP_SUCCESS;
}

private static String verifyAdminPassword(final String adminPassword) {
if (StringUtils.isBlank(adminPassword)) {
return Messages.Azure_GC_Template_PWD_Null_Or_Empty();
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/com/microsoft/azure/Messages.properties
Expand Up @@ -27,6 +27,8 @@ Azure_GC_Template_LOC_Not_Found=The location is not valid
Azure_GC_Template_Name_LOC_No_Match=The cloud service location and the location selected do not match. Use a different template or location.
Azure_GC_Template_CS_NA=Cloud service name {0} is either not available or not valid. Use a different cloud service name.
Azure_GC_Template_CS_LOC_No_Match=The cloud service location and the location selected do not match. Use a different cloud service or location.
Azure_GC_Template_SA_Already_Exists=The storage account name already exists. Use a different name.
Azure_GC_Template_SA_Cant_Validate=Could not validate the storage account name.
Azure_GC_Template_SA_Null_Or_Empty=The storage account name is null or empty.
Azure_GC_Template_SA_LOC_No_Match=Error: The storage account location {0} and the location selected {1} do not match.
Azure_GC_Template_Executors_Null_Or_Empty=Missing number of executors.
Expand Down

0 comments on commit 4ee5641

Please sign in to comment.