Skip to content

Commit

Permalink
Split agent templates into multiple pages
Browse files Browse the repository at this point in the history
  • Loading branch information
timja committed Jan 4, 2024
1 parent 0d6e57b commit ce77b97
Show file tree
Hide file tree
Showing 13 changed files with 632 additions and 347 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<properties>
<changelist>9999-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/azure-vm-agents-plugin</gitHubRepo>
<jenkins.version>2.387.3</jenkins.version>
<jenkins.version>2.426.1</jenkins.version>
<maven.javadoc.skip>true</maven.javadoc.skip>
<hpi.compatibleSinceVersion>883</hpi.compatibleSinceVersion>
</properties>
Expand All @@ -54,8 +54,8 @@
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.387.x</artifactId>
<version>2543.vfb_1a_5fb_9496d</version>
<artifactId>bom-2.426.x</artifactId>
<version>2675.v1515e14da_7a_6</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down
136 changes: 107 additions & 29 deletions src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,24 @@
import hudson.model.TaskListener;
import hudson.model.labels.LabelAtom;
import hudson.security.ACL;
import hudson.slaves.Cloud;
import hudson.slaves.RetentionStrategy;
import hudson.util.FormApply;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.verb.POST;

import javax.servlet.ServletException;
Expand Down Expand Up @@ -1421,6 +1428,47 @@ public List<String> verifyTemplate() throws Exception {
nsgName);
}

/**
* Deletes the template.
*/
@POST
public HttpResponse doDoDelete(@AncestorInPath AzureVMCloud azureVMCloud) throws IOException {
Jenkins j = Jenkins.get();
j.checkPermission(Jenkins.ADMINISTER);
if (azureVMCloud == null) {
throw new IllegalStateException("Cloud could not be found");
}
azureVMCloud.removeTemplate(this);
j.save();
// take the user back.
return new HttpRedirect("../../templates");
}

@POST
public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath AzureVMCloud azureVMCloud) throws IOException, ServletException, Descriptor.FormException {

Check warning on line 1448 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: Line is longer than 120 characters (found 166). [LineLength]

Check warning on line 1448 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: (sizes) LineLength: Line is longer than 120 characters (found 166).

Check warning on line 1448 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / Java Compiler

jacoco:prepare-agent

ERROR: Line is longer than 120 characters (found 166). [LineLength]

Check warning on line 1448 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / Java Compiler

jacoco:prepare-agent

ERROR: (sizes) LineLength: Line is longer than 120 characters (found 166).

Check warning on line 1448 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / CheckStyle

LineLengthCheck

ERROR: Line is longer than 120 characters (found 166).
Raw output
<p>Since Checkstyle 3.0</p><p> Checks for long lines. </p><p> Rationale: Long lines are hard to read in printouts or if developers have limited screen space for the source code, e.g. if the IDE displays additional information like project tree, class hierarchy, etc. </p>
Jenkins j = Jenkins.get();
j.checkPermission(Jenkins.ADMINISTER);
if (azureVMCloud == null) {
throw new IllegalStateException("Cloud could not be found");
}
azureVMCloud.removeTemplate(this);
AzureVMAgentTemplate newTemplate = reconfigure(req, req.getSubmittedForm());
if (StringUtils.isBlank(newTemplate.getTemplateName())) {
throw new Descriptor.FormException("Template name is mandatory", "templateName");
}
azureVMCloud.addTemplate(newTemplate);
j.save();
// take the user back.
return FormApply.success("../../templates");
}

private AzureVMAgentTemplate reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException {

Check warning on line 1465 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: Line is longer than 120 characters (found 130). [LineLength]

Check warning on line 1465 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: (sizes) LineLength: Line is longer than 120 characters (found 130).

Check warning on line 1465 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / Java Compiler

jacoco:prepare-agent

ERROR: Line is longer than 120 characters (found 130). [LineLength]

Check warning on line 1465 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / Java Compiler

jacoco:prepare-agent

ERROR: (sizes) LineLength: Line is longer than 120 characters (found 130).

Check warning on line 1465 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / CheckStyle

LineLengthCheck

ERROR: Line is longer than 120 characters (found 130).
Raw output
<p>Since Checkstyle 3.0</p><p> Checks for long lines. </p><p> Rationale: Long lines are hard to read in printouts or if developers have limited screen space for the source code, e.g. if the IDE displays additional information like project tree, class hierarchy, etc. </p>
if (form == null) {
return null;
}
return getDescriptor().newInstance(req, form);
}

@Extension
public static final class DescriptorImpl extends Descriptor<AzureVMAgentTemplate> {

Expand All @@ -1440,10 +1488,12 @@ public List<Descriptor<RetentionStrategy<?>>> getAzureVMRetentionStrategy() {

@POST
public ListBoxModel doFillVirtualMachineSizeItems(
@RelativePath("..") @QueryParameter String azureCredentialsId,
@QueryParameter("cloudName") String cloudName,
@QueryParameter String location) {
Jenkins.get().checkPermission(Jenkins.SYSTEM_READ);

String azureCredentialsId = getAzureCredentialsIdFromCloud(cloudName);

ListBoxModel model = new ListBoxModel();
if (StringUtils.isBlank(azureCredentialsId)) {
return model;
Expand Down Expand Up @@ -1476,10 +1526,34 @@ public ListBoxModel doFillOsTypeItems() throws IOException, ServletException {
return model;
}

private String getAzureCredentialsIdFromCloud(String cloudName) {
AzureVMCloud cloud = getAzureCloud(cloudName);

if (cloud != null) {
return cloud.getAzureCredentialsId();
}

return null;
}

private AzureVMCloud getAzureCloud(String cloudName) {
Cloud cloud = Jenkins.get().getCloud(cloudName);

if (cloud instanceof AzureVMCloud) {
return (AzureVMCloud) cloud;
}

return null;
}

@POST
public ListBoxModel doFillLocationItems(@RelativePath("..") @QueryParameter String azureCredentialsId) {
public ListBoxModel doFillLocationItems(
@QueryParameter("cloudName") String cloudName
) {
Jenkins.get().checkPermission(Jenkins.SYSTEM_READ);

String azureCredentialsId = getAzureCredentialsIdFromCloud(cloudName);

ListBoxModel model = new ListBoxModel();
if (StringUtils.isBlank(azureCredentialsId)) {
return model;
Expand Down Expand Up @@ -1511,21 +1585,27 @@ public AzureComputerLauncher getDefaultComputerLauncher() {

@POST
public ListBoxModel doFillAvailabilitySetItems(
@RelativePath("../..") @QueryParameter String azureCredentialsId,
@RelativePath("../..") @QueryParameter String resourceGroupReferenceType,
@RelativePath("../..") @QueryParameter String newResourceGroupName,
@RelativePath("../..") @QueryParameter String existingResourceGroupName,
@QueryParameter("cloudName") String cloudName,
@RelativePath("..") @QueryParameter String location) {
Jenkins.get().checkPermission(Jenkins.SYSTEM_READ);

ListBoxModel model = new ListBoxModel();
model.add("--- Select Availability Set in current resource group and location ---", "");

AzureVMCloud cloud = getAzureCloud(cloudName);
if (cloud == null) {
return model;
}

String azureCredentialsId = cloud.getAzureCredentialsId();
if (StringUtils.isBlank(azureCredentialsId)) {
return model;
}

//resourceGroupReferenceType passed wrong value in 2.60.1-LTS, we won't use this value until bug resolved.
resourceGroupReferenceType = null;
String resourceGroupReferenceType = cloud.getResourceGroupReferenceType();
String newResourceGroupName = cloud.getNewResourceGroupName();
String existingResourceGroupName = cloud.getExistingResourceGroupName();


try {
AzureResourceManager azureClient = AzureResourceManagerCache.get(azureCredentialsId);
Expand Down Expand Up @@ -1585,19 +1665,23 @@ public ListBoxModel doFillUsageModeItems() throws IOException, ServletException

@POST
public ListBoxModel doFillExistingStorageAccountNameItems(
@RelativePath("..") @QueryParameter String azureCredentialsId,
@RelativePath("..") @QueryParameter String resourceGroupReferenceType,
@RelativePath("..") @QueryParameter String newResourceGroupName,
@RelativePath("..") @QueryParameter String existingResourceGroupName,
@QueryParameter("cloudName") String cloudName,
@QueryParameter String storageAccountType) {
Jenkins.get().checkPermission(Jenkins.SYSTEM_READ);

AzureVMCloud cloud = getAzureCloud(cloudName);
ListBoxModel model = new ListBoxModel();
if (cloud == null) {
return model;
}
String azureCredentialsId = cloud.getAzureCredentialsId();

if (StringUtils.isBlank(azureCredentialsId)) {
return model;
}
//resourceGroupReferenceType passed wrong value in 2.60.1-LTS, we won't use this value until bug resolved.
resourceGroupReferenceType = null;
String resourceGroupReferenceType = cloud.getResourceGroupReferenceType();
String newResourceGroupName = cloud.getNewResourceGroupName();
String existingResourceGroupName = cloud.getExistingResourceGroupName();

try {
AzureResourceManager azureClient = AzureResourceManagerCache.get(azureCredentialsId);
Expand Down Expand Up @@ -1775,12 +1859,7 @@ public FormValidation doCheckJvmOptions(@QueryParameter String value) {

@POST
public FormValidation doVerifyConfiguration(
@RelativePath("..") @QueryParameter String azureCredentialsId,
@RelativePath("..") @QueryParameter String resourceGroupReferenceType,
@RelativePath("..") @QueryParameter String newResourceGroupName,
@RelativePath("..") @QueryParameter String existingResourceGroupName,
@RelativePath("..") @QueryParameter String maxVirtualMachinesLimit,
@RelativePath("..") @QueryParameter String deploymentTimeout,
@QueryParameter String cloudName,
@QueryParameter String templateName,
@QueryParameter String labels,
@QueryParameter String location,
Expand All @@ -1802,7 +1881,6 @@ public FormValidation doVerifyConfiguration(
@QueryParameter String galleryName,
@QueryParameter String galleryImageDefinition,
@QueryParameter String galleryImageVersion,
@QueryParameter boolean galleryImageSpecialized,
@QueryParameter String gallerySubscriptionId,
@QueryParameter String galleryResourceGroup,
@RelativePath("..") @QueryParameter String sshConfig,
Expand Down Expand Up @@ -1830,13 +1908,13 @@ public FormValidation doVerifyConfiguration(
galleryResourceGroup
);

String resourceGroupName = AzureVMCloud.getResourceGroupName(
resourceGroupReferenceType, newResourceGroupName, existingResourceGroupName);
AzureVMCloud cloud = (AzureVMCloud) Jenkins.get().getCloud(cloudName);

String storageAccountName = getStorageAccountName(
storageAccountNameReferenceType, newStorageAccountName, existingStorageAccountName);
if (storageAccountName.trim().isEmpty()) {
storageAccountName = AzureVMAgentTemplate.generateUniqueStorageAccountName(
resourceGroupName, templateName);
cloud.getResourceGroupName(), templateName);
}

LOGGER.log(Level.INFO,
Expand Down Expand Up @@ -1876,7 +1954,7 @@ public FormValidation doVerifyConfiguration(
"",
"",
"",
resourceGroupName,
cloud.getResourceGroupName(),
templateName,
labels,
location,
Expand Down Expand Up @@ -1909,8 +1987,8 @@ public FormValidation doVerifyConfiguration(

// First validate the subscription info. If it is not correct,
// then we can't validate the
String result = AzureClientHolder.getDelegate(azureCredentialsId)
.verifyConfiguration(resourceGroupName, deploymentTimeout);
String result = AzureClientHolder.getDelegate(cloud.getAzureCredentialsId())
.verifyConfiguration(cloud.getResourceGroupName(), String.valueOf(cloud.getDeploymentTimeout()));
if (!result.equals(Constants.OP_SUCCESS)) {
return FormValidation.error(result);
}
Expand All @@ -1920,7 +1998,7 @@ public FormValidation doVerifyConfiguration(
azureComputerLauncher.setSshConfig(sshConfig);
}

final List<String> errors = AzureClientHolder.getDelegate(azureCredentialsId).verifyTemplate(
final List<String> errors = AzureClientHolder.getDelegate(cloud.getAzureCredentialsId()).verifyTemplate(
templateName,
labels,
location,
Expand All @@ -1940,7 +2018,7 @@ public FormValidation doVerifyConfiguration(
subnetName,
new AzureVMCloudRetensionStrategy(0),
jvmOptions,
resourceGroupName,
cloud.getResourceGroupName(),

Check warning on line 2021 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 1436-2021 are not covered by tests
false,
usePrivateIP,
nsgName);
Expand Down
61 changes: 51 additions & 10 deletions src/main/java/com/microsoft/azure/vmagent/AzureVMCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@
import hudson.slaves.Cloud;
import hudson.slaves.NodeProvisioner.PlannedNode;
import hudson.slaves.SlaveComputer;
import hudson.util.FormApply;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.StreamTaskListener;
import javax.servlet.ServletException;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.cloudstats.CloudStatistics;
import org.jenkinsci.plugins.cloudstats.ProvisioningActivity;
Expand All @@ -59,7 +62,10 @@
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.kohsuke.stapler.verb.POST;

Expand Down Expand Up @@ -321,6 +327,16 @@ public final void setVmTemplates(List<AzureVMAgentTemplate> newTemplates) {
this.vmTemplates = new CopyOnWriteArrayList<>(newTemplates);
}

public void addTemplate(AzureVMAgentTemplate template) {
template.addAzureCloudReference(this);
ensureVmTemplateList();
vmTemplates.add(template);
}

public void removeTemplate(AzureVMAgentTemplate t) {
this.vmTemplates.remove(t);
}

public List<AzureTagPair> getCloudTags() {
return cloudTags;
}
Expand Down Expand Up @@ -483,23 +499,19 @@ public AzureVMAgentTemplate getAzureAgentTemplate(Label label) {
return null;
}

@SuppressWarnings("unused") // called by jelly
public AzureVMAgentTemplate getTemplate(String name) {
return getAzureAgentTemplate(name);
}

/**
* Returns agent template associated with the name.
*
* @param name Name to use for search
* @return Agent template that has the name assigned
*/
public AzureVMAgentTemplate getAzureAgentTemplate(String name) {
if (StringUtils.isBlank(name)) {
return null;
}

for (AzureVMAgentTemplate agentTemplate : vmTemplates) {
if (name.equals(agentTemplate.getTemplateName())) {
return agentTemplate;
}
}
return null;
return getVmTemplates().stream().filter(t -> name.equals(t.getTemplateName())).findFirst().orElse(null);
}

/**
Expand Down Expand Up @@ -1036,6 +1048,35 @@ public AzureVMManagementServiceDelegate getServiceDelegate() {
return AzureVMManagementServiceDelegate.getInstance(getAzureClient(), credentialsId);
}

@Restricted(NoExternalUse.class) // jelly
public AzureVMAgentTemplate.DescriptorImpl getTemplateDescriptor() {
return (AzureVMAgentTemplate.DescriptorImpl) Jenkins.get().getDescriptorOrDie(AzureVMAgentTemplate.class);
}

@POST
public HttpResponse doCreate(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException {

Check warning on line 1057 in src/main/java/com/microsoft/azure/vmagent/AzureVMCloud.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: Line is longer than 120 characters (found 138). [LineLength]

Check warning on line 1057 in src/main/java/com/microsoft/azure/vmagent/AzureVMCloud.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: (sizes) LineLength: Line is longer than 120 characters (found 138).

Check warning on line 1057 in src/main/java/com/microsoft/azure/vmagent/AzureVMCloud.java

View check run for this annotation

ci.jenkins.io / Java Compiler

jacoco:prepare-agent

ERROR: Line is longer than 120 characters (found 138). [LineLength]

Check warning on line 1057 in src/main/java/com/microsoft/azure/vmagent/AzureVMCloud.java

View check run for this annotation

ci.jenkins.io / Java Compiler

jacoco:prepare-agent

ERROR: (sizes) LineLength: Line is longer than 120 characters (found 138).

Check warning on line 1057 in src/main/java/com/microsoft/azure/vmagent/AzureVMCloud.java

View check run for this annotation

ci.jenkins.io / CheckStyle

LineLengthCheck

ERROR: Line is longer than 120 characters (found 138).
Raw output
<p>Since Checkstyle 3.0</p><p> Checks for long lines. </p><p> Rationale: Long lines are hard to read in printouts or if developers have limited screen space for the source code, e.g. if the IDE displays additional information like project tree, class hierarchy, etc. </p>
Jenkins j = Jenkins.get();
j.checkPermission(Jenkins.ADMINISTER);
AzureVMAgentTemplate newTemplate = getTemplateDescriptor().newInstance(req, req.getSubmittedForm());

if (StringUtils.isBlank(newTemplate.getTemplateName())) {
throw new Descriptor.FormException("Template name is mandatory", "templateName");
}

addTemplate(newTemplate);
j.save();
// take the user back.
return FormApply.success("templates");
}

@Override
public Cloud reconfigure(@NonNull StaplerRequest req, JSONObject form) throws Descriptor.FormException {
// cloud configuration doesn't contain templates anymore, so just keep existing ones.
var newInstance = (AzureVMCloud) super.reconfigure(req, form);
newInstance.setVmTemplates(this.vmTemplates);
return newInstance;

Check warning on line 1077 in src/main/java/com/microsoft/azure/vmagent/AzureVMCloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 331-1077 are not covered by tests
}

@Extension
public static class DescriptorImpl extends Descriptor<Cloud> {

Expand Down
Loading

0 comments on commit ce77b97

Please sign in to comment.