refactored code as per iteration#1

This commit is contained in:
Arjun Roy Chaudhuri 2016-11-21 16:39:58 -08:00
parent 05acf50121
commit a125d19235
7 changed files with 82 additions and 55 deletions

View File

@ -642,6 +642,8 @@ public class AzureVMAgentTemplate implements Describable<AzureVMAgentTemplate> {
public FormValidation doVerifyConfiguration(
@RelativePath("..") @QueryParameter String azureCredentialsId,
@RelativePath("..") @QueryParameter String resourceGroupName,
@RelativePath("..") @QueryParameter String maxVirtualMachinesLimit,
@RelativePath("..") @QueryParameter String deploymentTimeout,
@QueryParameter String templateName,
@QueryParameter String labels,
@QueryParameter String location,
@ -717,7 +719,8 @@ public class AzureVMAgentTemplate implements Describable<AzureVMAgentTemplate> {
// First validate the subscription info. If it is not correct,
// then we can't validate the
String result = AzureVMManagementServiceDelegate.verifyConfiguration(servicePrincipal, resourceGroupName);
String result = AzureVMManagementServiceDelegate.verifyConfiguration(servicePrincipal, resourceGroupName,
maxVirtualMachinesLimit, deploymentTimeout);
if (!result.equals(Constants.OP_SUCCESS)) {
return FormValidation.error(result);
}

View File

@ -690,24 +690,17 @@ public class AzureVMCloud extends Cloud {
}
public FormValidation doVerifyConfiguration(
@QueryParameter String subscriptionId,
@QueryParameter String clientId,
@QueryParameter String clientSecret,
@QueryParameter String oauth2TokenEndpoint,
@QueryParameter String serviceManagementURL,
@QueryParameter String azureCredentialsId,
@QueryParameter String maxVirtualMachinesLimit,
@QueryParameter String deploymentTimeout,
@QueryParameter String azureCredentialsId,
@QueryParameter String resourceGroupName) {
if (StringUtils.isBlank(resourceGroupName)) {
resourceGroupName = Constants.DEFAULT_RESOURCE_GROUP_NAME;
}
}
AzureCredentials.ServicePrincipal credentials = AzureCredentials.getServicePrincipal(azureCredentialsId);
try {
credentials.Validate(resourceGroupName, maxVirtualMachinesLimit, deploymentTimeout);
boolean validationResult = credentials.Validate(resourceGroupName, maxVirtualMachinesLimit, deploymentTimeout);
} catch (AzureCredentialsValidationException e) {
return FormValidation.error(e.getMessage());
}

View File

@ -203,7 +203,8 @@ public final class AzureVMCloudVerificationTask extends AsyncPeriodicWork {
LOGGER.info("AzureVMCloudVerificationTask: verifyConfiguration: start");
// Check the sub and off we go
String result = AzureVMManagementServiceDelegate.verifyConfiguration(cloud.getServicePrincipal(), cloud.getResourceGroupName());
String result = AzureVMManagementServiceDelegate.verifyConfiguration(cloud.getServicePrincipal(), cloud.getResourceGroupName(),
Integer.toString(cloud.getMaxVirtualMachinesLimit()), Integer.toString(cloud.getDeploymentTimeout()));
if (result != Constants.OP_SUCCESS) {
LOGGER.log(Level.INFO, "AzureVMCloudVerificationTask: verifyConfiguration: {0}", result);
cloud.setConfigurationValid(false);

View File

@ -260,7 +260,7 @@ public class AzureVMManagementServiceDelegate {
ObjectNode.class.cast(tmp.get("variables")).put("vmSize", template.getVirtualMachineSize());
// Grab the username/pass
StandardUsernamePasswordCredentials creds = AzureUtil.getCredentials(template.getCredentialsId());
ObjectNode.class.cast(tmp.get("variables")).put("adminUsername", creds.getUsername());
ObjectNode.class.cast(tmp.get("variables")).put("adminPassword", creds.getPassword().getPlainText());
@ -344,7 +344,7 @@ public class AzureVMManagementServiceDelegate {
final StorageManagementClient storageClient = ServiceDelegateHelper.getStorageManagementClient(config);
rmClient.getResourceGroupsOperations().createOrUpdate(resourceGroupName, new ResourceGroup(location));
StorageAccountCreateParameters createParams = new StorageAccountCreateParameters();
createParams.setLocation(location);
createParams.setAccountType(AccountType.StandardLRS);
@ -357,7 +357,7 @@ public class AzureVMManagementServiceDelegate {
String blobURL = StorageServiceDelegate.uploadFileToStorage(
config, targetStorageAccount, storageAccountKey,
storageClient.getBaseUri().toString(), resourceGroupName, Constants.CONFIG_CONTAINER_NAME,
storageClient.getBaseUri().toString(), resourceGroupName, Constants.CONFIG_CONTAINER_NAME,
targetScriptName, scriptText.getBytes("UTF-8"));
return blobURL;
}
@ -401,9 +401,9 @@ public class AzureVMManagementServiceDelegate {
azureAgent.setPublicDNSName(pubIP.getDnsSettings().getFqdn());
azureAgent.setSshPort(Constants.DEFAULT_SSH_PORT);
LOGGER.log(Level.INFO, "Azure agent details:\nnodeName{0}\nadminUserName={1}\nshutdownOnIdle={2}\nretentionTimeInMin={3}\nlabels={4}",
new Object[] { azureAgent.getNodeName(), azureAgent.getVMCredentialsId(), azureAgent.isShutdownOnIdle(),
azureAgent.getRetentionTimeInMin(), azureAgent.getLabelString()});
LOGGER.log(Level.INFO, "Azure agent details:\nnodeName{0}\nadminUserName={1}\nshutdownOnIdle={2}\nretentionTimeInMin={3}\nlabels={4}",
new Object[]{azureAgent.getNodeName(), azureAgent.getVMCredentialsId(), azureAgent.isShutdownOnIdle(),
azureAgent.getRetentionTimeInMin(), azureAgent.getLabelString()});
}
/**
@ -629,28 +629,35 @@ public class AzureVMManagementServiceDelegate {
*
* @param servicePrincipal
* @param resourceGroupName
* @param maxVMLimit
* @param timeout
* @return
*/
public static String verifyConfiguration(
final AzureCredentials.ServicePrincipal servicePrincipal,
final String resourceGroupName) {
if (servicePrincipal.isBlank() || StringUtils.isBlank(resourceGroupName)) {
return Messages.Azure_GC_Template_Val_Profile_Missing();
} else {
try {
if (!resourceGroupName.matches(Constants.DEFAULT_RESOURCE_GROUP_PATTERN)) {
return Messages.Azure_GC_Template_ResourceGroupName_Err();
}
// Load up the configuration now and do a live verification
Configuration config = ServiceDelegateHelper.loadConfiguration(servicePrincipal);
if (!verifyConfiguration(config, resourceGroupName).equals(Constants.OP_SUCCESS)) {
final String resourceGroupName, final String maxVMLimit, final String timeOut) {
try {
Configuration config = ServiceDelegateHelper.loadConfiguration(servicePrincipal);
if(!AzureUtil.isValidTimeOut(timeOut))
return "Invalid Timeout, Should be a positive number, minimum value "+Constants.DEFAULT_DEPLOYMENT_TIMEOUT_SEC;
if(!AzureUtil.isValidResourceGroupName(resourceGroupName))
return "Error: "+Messages.Azure_GC_Template_ResourceGroupName_Err();
if(!AzureUtil.isValidMAxVMLimit(maxVMLimit))
return "Invalid Limit, Should be a positive number, e.g. "+Constants.DEFAULT_MAX_VM_LIMIT;
if (AzureUtil.isValidTimeOut(timeOut) && AzureUtil.isValidMAxVMLimit(maxVMLimit)
&& AzureUtil.isValidResourceGroupName(resourceGroupName)) {
String result = verifyConfiguration(config, resourceGroupName);
if (!result.matches(Constants.OP_SUCCESS))
return Messages.Azure_GC_Template_Val_Profile_Err();
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error validating profile", e);
return Messages.Azure_GC_Template_Val_Profile_Err();
}
}catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error validating profile", e);
return Messages.Azure_GC_Template_Val_Profile_Err();
}
return Constants.OP_SUCCESS;
}
@ -1059,7 +1066,7 @@ public class AzureVMManagementServiceDelegate {
try {
config = ServiceDelegateHelper.loadConfiguration(servicePrincipal);
String validationResult;
// Verify basic info about the template
//Verify number of parallel jobs
validationResult = verifyNoOfExecutors(noOfParallelJobs);
@ -1075,14 +1082,14 @@ public class AzureVMManagementServiceDelegate {
}
//verify password
String adminPassword="";
String adminPassword = "";
try {
StandardUsernamePasswordCredentials creds = AzureUtil.getCredentials(credentialsId);
adminPassword = creds.getPassword().getPlainText();
} catch(AzureCloudException e) {
} catch (AzureCloudException e) {
LOGGER.log(Level.SEVERE, "Could not load the VM credentials", e);
}
validationResult = verifyAdminPassword(adminPassword);
addValidationResultIfFailed(validationResult, errors);
if (returnOnSingleError && errors.size() > 0) {
@ -1101,7 +1108,7 @@ public class AzureVMManagementServiceDelegate {
if (returnOnSingleError && errors.size() > 0) {
return errors;
}
validationResult = verifyLocation(location, servicePrincipal.serviceManagementURL);
addValidationResultIfFailed(validationResult, errors);
if (returnOnSingleError && errors.size() > 0) {
@ -1346,6 +1353,7 @@ public class AzureVMManagementServiceDelegate {
/**
* Check the location. This location is the display name.
*
* @param location
* @return
*/

View File

@ -77,7 +77,7 @@ public class AzureCredentials extends BaseStandardCredentials {
|| StringUtils.isBlank(clientSecret.getPlainText());
}
public void Validate(String resourceGroupName, String maxVMLimit, String deploymentTimeout) throws AzureCredentialsValidationException {
public boolean Validate(String resourceGroupName, String maxVMLimit, String deploymentTimeout) throws AzureCredentialsValidationException {
if (StringUtils.isBlank(subscriptionId.getPlainText())) {
throw new AzureCredentialsValidationException("Error: Subscription ID is missing");
}
@ -90,23 +90,13 @@ public class AzureCredentials extends BaseStandardCredentials {
if (StringUtils.isBlank(oauth2TokenEndpoint.getPlainText())) {
throw new AzureCredentialsValidationException("Error: OAuth 2.0 Token Endpoint is missing");
}
if (StringUtils.isBlank(maxVMLimit) || !maxVMLimit.matches(Constants.REG_EX_DIGIT)) {
throw new AzureCredentialsValidationException("Error: Maximum Virtual Machine Limit should be a positive integer e.g. "+Constants.DEFAULT_MAX_VM_LIMIT);
}
if (StringUtils.isBlank(deploymentTimeout) || !deploymentTimeout.matches(Constants.REG_EX_DIGIT)) {
throw new AzureCredentialsValidationException("Error: Deployment Timeout should be a positive number");
}
if (deploymentTimeout.matches(Constants.REG_EX_DIGIT) && Integer.parseInt(deploymentTimeout) < Constants.DEFAULT_DEPLOYMENT_TIMEOUT_SEC) {
throw new AzureCredentialsValidationException("Error: Deployment Timeout should be at least minimum "+Constants.DEFAULT_DEPLOYMENT_TIMEOUT_SEC);
}
String response = AzureVMManagementServiceDelegate.verifyConfiguration(this, resourceGroupName);
String response = AzureVMManagementServiceDelegate.verifyConfiguration(this, resourceGroupName, maxVMLimit, deploymentTimeout);
if (!Constants.OP_SUCCESS.equalsIgnoreCase(response)) {
throw new AzureCredentialsValidationException(response);
}
return true;
}
}
@ -179,7 +169,7 @@ public class AzureCredentials extends BaseStandardCredentials {
AzureCredentials.ServicePrincipal servicePrincipal = new AzureCredentials.ServicePrincipal(subscriptionId, clientId, clientSecret, oauth2TokenEndpoint,
serviceManagementURL);
try {
servicePrincipal.Validate(Constants.DEFAULT_RESOURCE_GROUP_NAME, Integer.toString(Constants.DEFAULT_MAX_VM_LIMIT),
boolean result = servicePrincipal.Validate(Constants.DEFAULT_RESOURCE_GROUP_NAME, Integer.toString(Constants.DEFAULT_MAX_VM_LIMIT),
Integer.toString(Constants.DEFAULT_DEPLOYMENT_TIMEOUT_SEC));
} catch (AzureCredentialsValidationException e) {
return FormValidation.error(e.getMessage());

View File

@ -364,4 +364,36 @@ public class AzureUtil {
return creds;
}
/**
* Checks if the ResourceGroup Name is valid with Azure Standards
* @param resourceGroupName Resource Group Name
* @return true if the name is valid else return false
*/
public static boolean isValidResourceGroupName(String resourceGroupName) {
if (resourceGroupName.matches(Constants.DEFAULT_RESOURCE_GROUP_PATTERN))
return true;
return false;
}
/**
* Checks if the maximum virtual machines limit is valid
* @param maxVMLimit Maximum Virtual Limit
* @return true if it is valid else return false
*/
public static boolean isValidMAxVMLimit(String maxVMLimit) {
if (StringUtils.isBlank(maxVMLimit) || !maxVMLimit.matches(Constants.REG_EX_DIGIT))
return false;
return true;
}
/**
* Checks if the deployment Timeout is valid
* @param deploymentTimeout Deployment Timeout
* @return true if it is valid else return false
*/
public static boolean isValidTimeOut(String deploymentTimeout) {
if ((StringUtils.isBlank(deploymentTimeout) || !deploymentTimeout.matches(Constants.REG_EX_DIGIT)
|| Integer.parseInt(deploymentTimeout) < Constants.DEFAULT_DEPLOYMENT_TIMEOUT_SEC))
return false;
return true;
}
}

View File

@ -121,6 +121,6 @@
</div>
</f:entry>
<f:validateButton title="${%Verify_Template}" progress="${%Verifying_Template_MSG}" method="verifyConfiguration"
with="azureCredentialsId,resourceGroupName,templateName,labels,location,virtualMachineSize,storageAccountName,noOfParallelJobs,image,osType,imagePublisher,imageOffer,imageSku,imageVersion,agentLaunchMethod,initScript,credentialsId,virtualNetworkName,subnetName,retentionTimeInMin,jvmOptions" />
with="azureCredentialsId,resourceGroupName,maxVirtualMachinesLimit,deploymentTimeout,templateName,labels,location,virtualMachineSize,storageAccountName,noOfParallelJobs,image,osType,imagePublisher,imageOffer,imageSku,imageVersion,agentLaunchMethod,initScript,credentialsId,virtualNetworkName,subnetName,retentionTimeInMin,jvmOptions" />
</table>
</j:jelly>