Added support for metadata and tags to Azure ARM

This commit is contained in:
Ignasi Barrera 2016-10-17 12:38:14 +02:00
parent 00d9138864
commit 25656f5f63
8 changed files with 34 additions and 36 deletions

View File

@ -24,9 +24,11 @@ import static org.jclouds.azurecompute.arm.compute.extensions.AzureComputeImageE
import static org.jclouds.azurecompute.arm.compute.extensions.AzureComputeImageExtension.CUSTOM_IMAGE_OFFER;
import static org.jclouds.azurecompute.arm.compute.functions.VMImageToImage.decodeFieldsFromUniqueId;
import static org.jclouds.azurecompute.arm.compute.functions.VMImageToImage.encodeFieldsToUniqueIdCustom;
import static org.jclouds.compute.util.ComputeServiceUtils.metadataAndTagsAsCommaDelimitedValue;
import static org.jclouds.util.Closeables2.closeQuietly;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
@ -124,7 +126,6 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter<Virtual
// TODO Store group apart from the name to be able to identify nodes with
// custom names in the configured group
// TODO ARM specific options
// TODO user metadata and tags
// TODO network ids => create one nic in each network
// TODO inbound ports
@ -141,9 +142,11 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter<Virtual
.availabilitySet(null) // TODO
.hardwareProfile(hardwareProfile).storageProfile(storageProfile).osProfile(osProfile)
.networkProfile(networkProfile).build();
Map<String, String> metadataAndTags = metadataAndTagsAsCommaDelimitedValue(template.getOptions());
VirtualMachine virtualMachine = api.getVirtualMachineApi(azureGroup).create(name, template.getLocation().getId(),
virtualMachineProperties);
virtualMachineProperties, metadataAndTags);
// Safe to pass null credentials here, as jclouds will default populate
// the node with the default credentials from the image, or the ones in

View File

@ -23,6 +23,7 @@ import static com.google.common.collect.Iterables.tryFind;
import static org.jclouds.azurecompute.arm.compute.extensions.AzureComputeImageExtension.CONTAINER_NAME;
import static org.jclouds.azurecompute.arm.compute.extensions.AzureComputeImageExtension.CUSTOM_IMAGE_OFFER;
import static org.jclouds.azurecompute.arm.compute.functions.VMImageToImage.encodeFieldsToUniqueId;
import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTagsFromCommaDelimitedValue;
import static org.jclouds.util.Closeables2.closeQuietly;
import java.util.List;
@ -96,7 +97,6 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
.put(VirtualMachineProperties.ProvisioningState.CREATING, NodeMetadata.Status.PENDING)
.put(VirtualMachineProperties.ProvisioningState.RUNNING, NodeMetadata.Status.PENDING)
.put(VirtualMachineProperties.ProvisioningState.UPDATING, NodeMetadata.Status.PENDING)
.put(VirtualMachineProperties.ProvisioningState.SUCCEEDED, NodeMetadata.Status.RUNNING)
.put(VirtualMachineProperties.ProvisioningState.DELETED, NodeMetadata.Status.TERMINATED)
.put(VirtualMachineProperties.ProvisioningState.CANCELED, NodeMetadata.Status.TERMINATED)
.put(VirtualMachineProperties.ProvisioningState.FAILED, NodeMetadata.Status.ERROR)
@ -148,8 +148,7 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
builder.providerId(virtualMachine.id());
builder.name(virtualMachine.name());
builder.hostname(virtualMachine.name());
String group = this.nodeNamingConvention.extractGroup(virtualMachine.name());
builder.group(group);
builder.group(nodeNamingConvention.extractGroup(virtualMachine.name()));
ProvisioningState provisioningState = virtualMachine.properties().provisioningState();
if (ProvisioningState.SUCCEEDED.equals(provisioningState)) {
@ -177,10 +176,9 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
builder.privateAddresses(getPrivateIpAddresses(virtualMachine.properties().networkProfile().networkInterfaces()));
if (virtualMachine.tags() != null) {
Map<String, String> userMetaData = virtualMachine.tags();
builder.userMetadata(userMetaData);
builder.tags(Splitter.on(",").split(userMetaData.get("tags")));
addMetadataAndParseTagsFromCommaDelimitedValue(builder, virtualMachine.tags());
}
String locationName = virtualMachine.location();
builder.location(getLocation(locationName));

View File

@ -18,6 +18,7 @@ package org.jclouds.azurecompute.arm.features;
import java.net.URI;
import java.util.List;
import java.util.Map;
import javax.inject.Named;
import javax.ws.rs.Consumes;
@ -80,14 +81,15 @@ public interface VirtualMachineApi {
*/
@Named("CreateVirtualMachine")
@PUT
@Payload("%7B\"location\":\"{location}\",\"tags\":%7B%7D,\"properties\":{properties}%7D")
@Payload("%7B\"location\":\"{location}\",\"tags\":{tags},\"properties\":{properties}%7D")
@MapBinder(BindToJsonPayload.class)
@Path("/{vmname}")
@QueryParams(keys = "validating", values = "false")
@Produces(MediaType.APPLICATION_JSON)
VirtualMachine create(@PathParam("vmname") String vmname,
@PayloadParam("location") String location,
@PayloadParam("properties") VirtualMachineProperties properties);
@PayloadParam("properties") VirtualMachineProperties properties,
@PayloadParam("tags") Map<String, String> tags);
/**
* The List Virtual Machines operation

View File

@ -22,7 +22,6 @@ import java.util.Properties;
import org.jclouds.azurecompute.arm.AzureComputeProviderMetadata;
import org.jclouds.azurecompute.arm.internal.AzureLiveTestUtils;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.internal.BaseComputeServiceLiveTest;
@ -36,8 +35,6 @@ import org.jclouds.scriptbuilder.statements.login.AdminAccess;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
/**
@ -86,15 +83,4 @@ public class AzureComputeServiceLiveTest extends BaseComputeServiceLiveTest {
InstallJDK.fromOpenJDK() }));
return template;
}
@Override
protected void checkUserMetadataContains(NodeMetadata node, ImmutableMap<String, String> userMetadata) {
// User metadata not yet supported
}
@Override
protected void checkTagsInNodeEquals(NodeMetadata node, ImmutableSet<String> tags) {
// Tags not yet supported
}
}

View File

@ -23,6 +23,7 @@ import static org.testng.Assert.assertTrue;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -111,7 +112,8 @@ public class VirtualMachineApiLiveTest extends BaseAzureComputeApiLiveTest {
public void testCreate() {
String blob = storageService.storageServiceProperties().primaryEndpoints().get("blob");
VirtualMachine vm = api().create(vmName, LOCATION, getProperties(blob, nicName));
VirtualMachine vm = api().create(vmName, LOCATION, getProperties(blob, nicName),
Collections.<String, String> emptyMap());
assertTrue(!vm.name().isEmpty());
//Poll until resource is ready to be used

View File

@ -16,6 +16,12 @@
*/
package org.jclouds.azurecompute.arm.features;
import static com.google.common.collect.Iterables.isEmpty;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import java.net.URI;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@ -40,14 +46,9 @@ import org.jclouds.azurecompute.arm.internal.BaseAzureComputeApiMockTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.squareup.okhttp.mockwebserver.MockResponse;
import static com.google.common.collect.Iterables.isEmpty;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
@Test(groups = "unit", testName = "VirtualMachineApiMockTest", singleThreaded = true)
public class VirtualMachineApiMockTest extends BaseAzureComputeApiMockTest {
@ -107,11 +108,11 @@ public class VirtualMachineApiMockTest extends BaseAzureComputeApiMockTest {
server.enqueue(jsonResponse("/createvirtualmachineresponse.json"));
final VirtualMachineApi vmAPI = api.getVirtualMachineApi("groupname");
VirtualMachine vm = vmAPI.create("windowsmachine", "westus", getProperties());
VirtualMachine vm = vmAPI.create("windowsmachine", "westus", getProperties(), ImmutableMap.of("foo", "bar"));
assertEquals(vm, getVM());
assertSent(server, "PUT", "/subscriptions/SUBSCRIPTIONID/resourceGroups/groupname/providers/Microsoft.Compute" +
"/virtualMachines/windowsmachine?validating=false&api-version=2016-03-30",
"{\"location\":\"westus\",\"properties\":" +
"{\"location\":\"westus\",\"tags\":{\"foo\":\"bar\"},\"properties\":" +
"{\"vmId\":\"27ee085b-d707-xxxx-yyyy-2370e2eb1cc1\"," +
"\"hardwareProfile\":{\"vmSize\":\"Standard_D1\"}," +
"\"storageProfile\":{\"imageReference\":{\"publisher\":\"publisher\",\"offer\":\"offer\",\"sku\":\"sku\",\"version\":\"ver\"}," +
@ -240,7 +241,7 @@ public class VirtualMachineApiMockTest extends BaseAzureComputeApiMockTest {
VirtualMachineProperties properties = getProperties();
VirtualMachine machine = VirtualMachine.create("/subscriptions/SUBSCRIPTIONID/" + "" +
"resourceGroups/groupname/providers/Microsoft.Compute/virtualMachines/windowsmachine", "windowsmachine",
"Microsoft.Compute/virtualMachines", "westus", null, properties);
"Microsoft.Compute/virtualMachines", "westus", ImmutableMap.of("foo", "bar"), properties);
return machine;
}

View File

@ -43,5 +43,8 @@
"id": "/subscriptions/SUBSCRIPTIONID/resourceGroups/groupname/providers/Microsoft.Compute/virtualMachines/windowsmachine",
"name": "windowsmachine",
"type": "Microsoft.Compute/virtualMachines",
"location": "westus"
"location": "westus",
"tags": {
"foo": "bar"
}
}

View File

@ -43,5 +43,8 @@
"id": "/subscriptions/SUBSCRIPTIONID/resourceGroups/groupname/providers/Microsoft.Compute/virtualMachines/windowsmachine",
"name": "windowsmachine",
"type": "Microsoft.Compute/virtualMachines",
"location": "westus"
"location": "westus",
"tags": {
"foo": "bar"
}
}