Skip to content

Commit

Permalink
Migrate availability type to use descriptor (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
timja authored Nov 20, 2024
1 parent 7885be6 commit 7c08d4b
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 282 deletions.
192 changes: 11 additions & 181 deletions src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.azure.core.http.rest.PagedIterable;
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.compute.models.AvailabilitySet;
import com.azure.resourcemanager.compute.models.DiskSkuTypes;
import com.azure.resourcemanager.storage.models.SkuName;
import com.azure.resourcemanager.storage.models.StorageAccount;
Expand All @@ -28,10 +27,10 @@
import com.jcraft.jsch.OpenSSHConfig;
import com.microsoft.azure.util.AzureBaseCredentials;
import com.microsoft.azure.util.AzureCredentialUtil;
import com.microsoft.azure.vmagent.availability.AzureAvailabilityType;
import com.microsoft.azure.vmagent.availability.NoAvailabilityRequired;
import com.microsoft.azure.vmagent.builders.AdvancedImage;
import com.microsoft.azure.vmagent.builders.AdvancedImageBuilder;
import com.microsoft.azure.vmagent.builders.Availability;
import com.microsoft.azure.vmagent.builders.AvailabilityBuilder;
import com.microsoft.azure.vmagent.builders.BuiltInImage;
import com.microsoft.azure.vmagent.builders.BuiltInImageBuilder;
import com.microsoft.azure.vmagent.exceptions.AzureCloudException;
Expand Down Expand Up @@ -90,7 +89,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
Expand Down Expand Up @@ -221,40 +219,6 @@ public String getGalleryResourceGroup() {
}
}

public static class AvailabilityTypeClass implements Serializable {
private String availabilitySet;

@DataBoundConstructor
public AvailabilityTypeClass(String availabilitySet) {
this.availabilitySet = Util.fixEmpty(availabilitySet);
}

private AvailabilityTypeClass() {
// used for readResolve to maintain compatibility
}

public String getAvailabilitySet() {
return availabilitySet;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AvailabilityTypeClass that = (AvailabilityTypeClass) o;
return Objects.equals(availabilitySet, that.availabilitySet);
}

@Override
public int hashCode() {
return Objects.hash(availabilitySet);
}
}

private static final Logger LOGGER = Logger.getLogger(AzureVMAgentTemplate.class.getName());

private static final int GEN_STORAGE_ACCOUNT_UID_LENGTH = 22;
Expand All @@ -268,7 +232,7 @@ public int hashCode() {

private final String location;

private AvailabilityTypeClass availabilityType;
private AzureAvailabilityType availabilityType;

private final String virtualMachineSize;

Expand Down Expand Up @@ -393,34 +357,14 @@ public int hashCode() {
private DescribableList<NodeProperty<?>, NodePropertyDescriptor> nodeProperties =
new DescribableList<>(Saveable.NOOP);

// deprecated fields
private transient boolean isInstallDocker;
private transient boolean isInstallMaven;
private transient boolean isInstallGit;
private transient boolean isInstallQemu;

private transient String image;
private transient String imageId;
private transient String imagePublisher;
private transient String imageOffer;
private transient String imageSku;
private transient String imageVersion;

private transient String galleryName;
private transient String galleryImageDefinition;
private transient String galleryImageVersion;
private transient String gallerySubscriptionId;
private transient String galleryResourceGroup;
private transient String availabilitySet;


@DataBoundConstructor
public AzureVMAgentTemplate(
String templateName,
String templateDesc,
String labels,
String location,
AvailabilityTypeClass availabilityType,
AzureAvailabilityType availabilityType,
String virtualMachineSize,
String storageAccountNameReferenceType,
String storageAccountType,
Expand Down Expand Up @@ -452,7 +396,7 @@ public AzureVMAgentTemplate(
this.location = location;
this.availabilityType = availabilityType;
if (availabilityType == null) {
this.availabilityType = new AvailabilityTypeClass();
this.availabilityType = new NoAvailabilityRequired();
}
this.virtualMachineSize = virtualMachineSize;
this.storageAccountType = storageAccountType;
Expand Down Expand Up @@ -614,13 +558,6 @@ public String getVersion() {
return imageReference != null ? imageReference.getVersion() : null;
}

/**
* Used by jelly for loading the data, not written to.
*/
@Restricted(NoExternalUse.class)
public String getAvailabilitySet() {
return availabilityType != null ? availabilityType.getAvailabilitySet() : null;
}

public int getMaxVirtualMachinesLimit() {
return maxVirtualMachinesLimit;
Expand Down Expand Up @@ -886,82 +823,18 @@ private Object readResolve() {
retentionStrategy = new AzureVMCloudRetensionStrategy(0);
}

if (isInstallDocker) {
installDocker = true;
}

if (isInstallGit) {
installGit = true;
}

if (isInstallMaven) {
installMaven = true;
}

if (isInstallQemu) {
installQemu = true;
}

if (imageReference == null) {
imageReference = new ImageReferenceTypeClass();
}

if (availabilityType == null) {
availabilityType = new AvailabilityTypeClass();
}

if (StringUtils.isNotBlank(image)) {
imageReference.uri = image;
}

if (StringUtils.isNotBlank(imageId)) {
imageReference.id = imageId;
}

if (StringUtils.isNotBlank(imagePublisher)) {
imageReference.publisher = imagePublisher;
}

if (StringUtils.isNotBlank(imageOffer)) {
imageReference.offer = imageOffer;
}

if (StringUtils.isNotBlank(imageSku)) {
imageReference.sku = imageSku;
}

if (StringUtils.isNotBlank(imageVersion)) {
imageReference.version = imageVersion;
}

if (StringUtils.isNotBlank(galleryName)) {
imageReference.galleryName = galleryName;
}

if (StringUtils.isNotBlank(galleryImageDefinition)) {
imageReference.galleryImageDefinition = galleryImageDefinition;
}

if (StringUtils.isNotBlank(galleryImageVersion)) {
imageReference.galleryImageVersion = galleryImageVersion;
}

if (StringUtils.isNotBlank(gallerySubscriptionId)) {
imageReference.gallerySubscriptionId = gallerySubscriptionId;
}

if (StringUtils.isNotBlank(galleryResourceGroup)) {
imageReference.galleryResourceGroup = galleryResourceGroup;
availabilityType = new NoAvailabilityRequired();
}

if (imageReference.type == null) {
imageReference.type = imageReference.determineType();
}

if (StringUtils.isNotBlank(availabilitySet)) {
availabilityType.availabilitySet = availabilitySet;
}

if (tags == null) {
this.tags = new ArrayList<>();
}
Expand Down Expand Up @@ -1095,7 +968,7 @@ public boolean isShutdownOnIdle() {
return shutdownOnIdle;
}

public AvailabilityTypeClass getAvailabilityType() {
public AzureAvailabilityType getAvailabilityType() {
return availabilityType;
}

Expand Down Expand Up @@ -1398,11 +1271,6 @@ public BuiltInImage getBuiltInImageInside() {
.build();
}

public Availability getAvailabilityInside() {
return new AvailabilityBuilder().withAvailabilitySet(getAvailabilityType().getAvailabilitySet())
.build();
}

@SuppressWarnings("unchecked")
public Descriptor<AzureVMAgentTemplate> getDescriptor() {
return Jenkins.get().getDescriptor(getClass());
Expand Down Expand Up @@ -1611,6 +1479,10 @@ public ListBoxModel doFillLicenseTypeItems() {
return model;
}

public AzureAvailabilityType getDefaultAvailabilityType() {
return new NoAvailabilityRequired();

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1483 is not covered by tests
}

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

Expand Down Expand Up @@ -1668,48 +1540,6 @@ public AzureComputerLauncher getDefaultComputerLauncher() {
return new AzureSSHLauncher();
}

@POST
public ListBoxModel doFillAvailabilitySetItems(
@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;
}

String resourceGroupReferenceType = cloud.getResourceGroupReferenceType();
String newResourceGroupName = cloud.getNewResourceGroupName();
String existingResourceGroupName = cloud.getExistingResourceGroupName();


try {
AzureResourceManager azureClient = AzureResourceManagerCache.get(azureCredentialsId);
String resourceGroupName = AzureVMCloud.getResourceGroupName(
resourceGroupReferenceType, newResourceGroupName, existingResourceGroupName);
PagedIterable<AvailabilitySet> availabilitySets = azureClient.availabilitySets()
.listByResourceGroup(resourceGroupName);
for (AvailabilitySet set : availabilitySets) {
String label = set.region().label();
if (label.equals(location)) {
model.add(set.name());
}
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Cannot list availability set: ", e);
}
return model;
}

@POST
public ListBoxModel doFillStorageAccountTypeItems(@QueryParameter String virtualMachineSize) {
ListBoxModel model = new ListBoxModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
import com.fasterxml.jackson.databind.node.ObjectNode;

import com.jcraft.jsch.OpenSSHConfig;
import com.microsoft.azure.vmagent.availability.AvailabilitySet;
import com.microsoft.azure.vmagent.availability.AzureAvailabilityType;
import com.microsoft.azure.vmagent.exceptions.AzureCloudException;
import com.microsoft.azure.vmagent.launcher.AzureComputerLauncher;
import com.microsoft.azure.vmagent.launcher.AzureSSHLauncher;
Expand Down Expand Up @@ -238,8 +240,9 @@ public AzureVMDeploymentInfo createDeployment(
final boolean ephemeralOSDisk = template.isEphemeralOSDisk();
final boolean encryptionAtHost = template.isEncryptionAtHost();
final int osDiskSize = template.getOsDiskSize();
final AzureVMAgentTemplate.AvailabilityTypeClass availabilityType = template.getAvailabilityType();
final String availabilitySet = availabilityType != null ? availabilityType.getAvailabilitySet() : null;
final AzureAvailabilityType availabilityType = template.getAvailabilityType();
final String availabilitySet = availabilityType instanceof AvailabilitySet ?
((AvailabilitySet) availabilityType).getName() : null;

Check warning on line 245 in src/main/java/com/microsoft/azure/vmagent/AzureVMManagementServiceDelegate.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 243-245 are not covered by tests

if (!template.getResourceGroupName().matches(Constants.DEFAULT_RESOURCE_GROUP_PATTERN)) {
LOGGER.log(Level.SEVERE, "ResourceGroup Name {0} is invalid. It should be 1-64 alphanumeric characters",
Expand Down
Loading

0 comments on commit 7c08d4b

Please sign in to comment.