Skip to content
Permalink
Browse files
Better encapsulated the image id encoding logic
  • Loading branch information
nacx committed Apr 25, 2017
1 parent 11f3804 commit 7a7db49b75ff71ebed96d2dd3adc9454a676e694
Showing 7 changed files with 78 additions and 87 deletions.
@@ -25,7 +25,6 @@
import static com.google.common.collect.Lists.newArrayList;
import static org.jclouds.azurecompute.arm.compute.domain.LocationAndName.fromSlashEncoded;
import static org.jclouds.azurecompute.arm.compute.domain.ResourceGroupAndName.fromResourceGroupAndName;
import static org.jclouds.azurecompute.arm.compute.functions.VMImageToImage.decodeFieldsFromUniqueId;
import static org.jclouds.azurecompute.arm.compute.functions.VMImageToImage.getMarketplacePlanFromImageMetadata;
import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.IMAGE_PUBLISHERS;
import static org.jclouds.azurecompute.arm.util.VMImages.isCustom;
@@ -256,7 +255,7 @@ public String apply(Location location) {

@Override
public VMImage getImage(final String id) {
VMImage image = decodeFieldsFromUniqueId(id);
VMImage image = VMImage.decodeFieldsFromUniqueId(id);

if (image.custom()) {
org.jclouds.azurecompute.arm.domain.Image vmImage = api.getVirtualMachineImageApi(image.resourceGroup()).get(
@@ -20,7 +20,6 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static org.jclouds.azurecompute.arm.compute.domain.ResourceGroupAndName.fromSlashEncoded;
import static org.jclouds.azurecompute.arm.compute.functions.VMImageToImage.decodeFieldsFromUniqueId;
import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TIMEOUT_RESOURCE_DELETED;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED;

@@ -125,7 +124,7 @@ public Image call() throws Exception {

@Override
public boolean deleteImage(String id) {
VMImage image = decodeFieldsFromUniqueId(id);
VMImage image = VMImage.decodeFieldsFromUniqueId(id);
checkArgument(image.custom(), "Only custom images can be deleted");

logger.debug(">> deleting image %s", id);
@@ -61,7 +61,7 @@ public SecurityGroup apply(NetworkSecurityGroup input) {
builder.location(getLocation(locations, input.location()));

if (input.properties().securityRules() != null) {
// We just supoprt security groups that allow traffic to a set of
// We just support security groups that allow traffic to a set of
// targets. We don't support deny rules or origin based rules in the
// security group api.
builder.ipPermissions(transform(filter(input.properties().securityRules(), InboundRule), ruleToPermission));
@@ -19,14 +19,12 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.tryFind;
import static java.util.Arrays.asList;
import static org.jclouds.azurecompute.arm.domain.IdReference.extractResourceGroup;
import static org.jclouds.azurecompute.arm.util.VMImages.isCustom;
import static org.jclouds.location.predicates.LocationPredicates.idEquals;

import java.util.Map;
import java.util.Set;

import org.jclouds.azurecompute.arm.compute.extensions.AzureComputeImageExtension;
import org.jclouds.azurecompute.arm.domain.ImageReference;
import org.jclouds.azurecompute.arm.domain.Plan;
import org.jclouds.azurecompute.arm.domain.VMImage;
import org.jclouds.collect.Memoized;
@@ -36,7 +34,6 @@
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.domain.Location;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.predicates.LocationPredicates;

import com.google.common.base.Function;
import com.google.common.base.Optional;
@@ -57,51 +54,6 @@ public class VMImageToImage implements Function<VMImage, Image> {

private final Supplier<Set<? extends org.jclouds.domain.Location>> locations;

public static String encodeFieldsToUniqueId(boolean globallyAvailable, String locationName,
ImageReference imageReference) {
return (globallyAvailable ? "global" : locationName) + "/" + imageReference.publisher() + "/"
+ imageReference.offer() + "/" + imageReference.sku();
}

public static String encodeFieldsToUniqueIdCustom(boolean globallyAvailable, String locationName,
ImageReference imageReference) {
return extractResourceGroup(imageReference.customImageId()) + "/" + (globallyAvailable ? "global" : locationName)
+ "/" + imageReference.customImageId().substring(imageReference.customImageId().lastIndexOf("/") + 1);
}

public static String encodeFieldsToUniqueId(VMImage imageReference) {
return (imageReference.globallyAvailable() ? "global" : imageReference.location()) + "/"
+ imageReference.publisher() + "/" + imageReference.offer() + "/" + imageReference.sku();
}

public static String encodeFieldsToUniqueIdCustom(VMImage imageReference) {
return imageReference.resourceGroup() + "/"
+ (imageReference.globallyAvailable() ? "global" : imageReference.location()) + "/" + imageReference.name();
}

public static VMImage decodeFieldsFromUniqueId(final String id) {
VMImage vmImage;
String[] fields = checkNotNull(id, "id").split("/");
if (isCustom(id)) {
/* id fields indexes
0: imageReference.resourceGroup
1: imageReference.location + "/" +
2: imageReference.name
*/
vmImage = VMImage.customImage().resourceGroup(fields[0]).location(fields[1]).name(fields[2]).build();
} else {
/* id fields indexes
0: imageReference.location + "/" +
1: imageReference.publisher + "/" +
2: imageReference.offer + "/" +
3: imageReference.sku + "/" +
*/
vmImage = VMImage.azureImage().location(fields[0]).publisher(fields[1]).offer(fields[2]).sku(fields[3])
.build();
}
return vmImage;
}

@Inject
VMImageToImage(@Memoized Supplier<Set<? extends Location>> locations) {
this.locations = locations;
@@ -111,29 +63,32 @@ public static VMImage decodeFieldsFromUniqueId(final String id) {
public Image apply(final VMImage image) {
final ImageBuilder builder = new ImageBuilder();
addMarketplacePlanToMetadataIfPresent(builder, image);

Location location = FluentIterable.from(locations.get()).firstMatch(idEquals(image.location())).get();

if (image.custom()) {
builder.location(
FluentIterable.from(locations.get()).firstMatch(LocationPredicates.idEquals(image.location()))
.get()).name(image.name()).description(image.group()).status(Image.Status.AVAILABLE)
.version("latest").providerId(image.customImageId()).id(encodeFieldsToUniqueIdCustom(image));

final OperatingSystem.Builder osBuilder = osFamily().apply(image);
builder.operatingSystem(osBuilder.build());
builder
.id(image.encodeFieldsToUniqueIdCustom())
.providerId(image.customImageId())
.name(image.name())
.location(location)
.description(image.group())
.status(Image.Status.AVAILABLE)
.version("latest");
} else {
builder
.name(image.offer())
.description(image.sku())
.status(Image.Status.AVAILABLE)
.version(image.sku())
.id(encodeFieldsToUniqueId(image))
.providerId(image.publisher())
.location(
image.globallyAvailable() ? null : FluentIterable.from(locations.get())
.firstMatch(LocationPredicates.idEquals(image.location())).get());

final OperatingSystem.Builder osBuilder = osFamily().apply(image);
builder.operatingSystem(osBuilder.build());
.id(image.encodeFieldsToUniqueId())
.providerId(image.publisher())
.name(image.offer())
.location(location)
.description(image.sku())
.status(Image.Status.AVAILABLE)
.version(image.sku());
}

final OperatingSystem.Builder osBuilder = osFamily().apply(image);
builder.operatingSystem(osBuilder.build());

return builder.build();
}

@@ -22,8 +22,6 @@
import static org.jclouds.azurecompute.arm.compute.AzureComputeServiceAdapter.GROUP_KEY;
import static org.jclouds.azurecompute.arm.compute.domain.LocationAndName.fromLocationAndName;
import static org.jclouds.azurecompute.arm.compute.domain.ResourceGroupAndName.fromResourceGroupAndName;
import static org.jclouds.azurecompute.arm.compute.functions.VMImageToImage.encodeFieldsToUniqueId;
import static org.jclouds.azurecompute.arm.compute.functions.VMImageToImage.encodeFieldsToUniqueIdCustom;
import static org.jclouds.azurecompute.arm.domain.IdReference.extractResourceGroup;
import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTagsFromCommaDelimitedValue;
import static org.jclouds.location.predicates.LocationPredicates.idEquals;
@@ -185,10 +183,9 @@ protected static Location getLocation(Supplier<Set<? extends Location>> location

protected Optional<? extends Image> findImage(final StorageProfile storageProfile, String locatioName) {
if (storageProfile.imageReference() != null) {
// FIXME check this condition
String imageId = storageProfile.imageReference().customImageId() != null ?
encodeFieldsToUniqueIdCustom(false, locatioName, storageProfile.imageReference()) :
encodeFieldsToUniqueId(false, locatioName, storageProfile.imageReference());
storageProfile.imageReference().encodeFieldsToUniqueIdCustom(locatioName) :
storageProfile.imageReference().encodeFieldsToUniqueId(locatioName);
return imageCache.get(imageId);
} else {
logger.warn("could not find image for storage profile %s", storageProfile);
@@ -16,6 +16,9 @@
*/
package org.jclouds.azurecompute.arm.domain;

import static org.jclouds.azurecompute.arm.domain.IdReference.extractName;
import static org.jclouds.azurecompute.arm.domain.IdReference.extractResourceGroup;

import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;

@@ -90,4 +93,14 @@ public abstract static class Builder {

public abstract ImageReference build();
}

public String encodeFieldsToUniqueId(String location) {
return VMImage.azureImage().location(location).publisher(publisher()).offer(offer()).sku(sku()).build()
.encodeFieldsToUniqueId();
}

public String encodeFieldsToUniqueIdCustom(String location) {
return VMImage.customImage().resourceGroup(extractResourceGroup(customImageId())).location(location)
.name(extractName(customImageId())).build().encodeFieldsToUniqueIdCustom();
}
}
@@ -16,6 +16,9 @@
*/
package org.jclouds.azurecompute.arm.domain;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.azurecompute.arm.util.VMImages.isCustom;

import org.jclouds.azurecompute.arm.domain.Version.VersionProperties;
import org.jclouds.javax.annotation.Nullable;

@@ -53,11 +56,6 @@ public abstract class VMImage {
@Nullable
public abstract String location();

/**
* Specifies if this image is globally available
*/
public abstract boolean globallyAvailable();

/**
* The group of the custom image
*/
@@ -112,16 +110,16 @@ public abstract class VMImage {
@Nullable
public abstract VersionProperties versionProperties();

public static Builder builder() {
private static Builder builder() {
return new AutoValue_VMImage.Builder();
}

public static Builder azureImage() {
return builder().globallyAvailable(false).custom(false);
return builder().custom(false);
}

public static Builder customImage() {
return builder().globallyAvailable(false).custom(true);
return builder().custom(true);
}

VMImage() {
@@ -140,7 +138,6 @@ public abstract static class Builder {
public abstract Builder sku(String sku);
public abstract Builder version(String version);
public abstract Builder location(String location);
public abstract Builder globallyAvailable(boolean globallyAvailable);
public abstract Builder group(String group);
public abstract Builder storage(String storage);
public abstract Builder vhd1(String vhd1);
@@ -151,4 +148,35 @@ public abstract static class Builder {

public abstract VMImage build();
}

public String encodeFieldsToUniqueId() {
return String.format("%s/%s/%s/%s", location(), publisher(), offer(), sku());
}

public String encodeFieldsToUniqueIdCustom() {
return String.format("%s/%s/%s", resourceGroup(), location(), name());
}

public static VMImage decodeFieldsFromUniqueId(final String id) {
VMImage vmImage;
String[] fields = checkNotNull(id, "id").split("/");
if (isCustom(id)) {
/* id fields indexes
0: imageReference.resourceGroup
1: imageReference.location + "/" +
2: imageReference.name
*/
vmImage = VMImage.customImage().resourceGroup(fields[0]).location(fields[1]).name(fields[2]).build();
} else {
/* id fields indexes
0: imageReference.location + "/" +
1: imageReference.publisher + "/" +
2: imageReference.offer + "/" +
3: imageReference.sku + "/" +
*/
vmImage = VMImage.azureImage().location(fields[0]).publisher(fields[1]).offer(fields[2]).sku(fields[3])
.build();
}
return vmImage;
}
}

0 comments on commit 7a7db49

Please sign in to comment.