You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by an...@apache.org on 2016/08/06 12:54:26 UTC
[1/2] jclouds-labs git commit: add unit test for storage account name
generation
Repository: jclouds-labs
Updated Branches:
refs/heads/master ff73c4e22 -> d0b07a668
add unit test for storage account name generation
Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/b0536c2e
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/b0536c2e
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/b0536c2e
Branch: refs/heads/master
Commit: b0536c2ebe95a8b7de7bc6acd17e01b35f92004d
Parents: ff73c4e
Author: Andrea Turli <an...@gmail.com>
Authored: Thu Jul 14 13:05:03 2016 +0200
Committer: Andrea Turli <an...@gmail.com>
Committed: Sat Aug 6 14:41:01 2016 +0200
----------------------------------------------------------------------
.../features/DeploymentTemplateBuilderTest.java | 55 +++++++++++++++++---
1 file changed, 49 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/b0536c2e/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/DeploymentTemplateBuilderTest.java
----------------------------------------------------------------------
diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/DeploymentTemplateBuilderTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/DeploymentTemplateBuilderTest.java
index 07ab6ef..1657899 100644
--- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/DeploymentTemplateBuilderTest.java
+++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/DeploymentTemplateBuilderTest.java
@@ -43,13 +43,14 @@ import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
+import org.testng.Assert;
import org.testng.annotations.Test;
+import com.google.common.collect.Iterables;
+
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.fail;
-
@Test(groups = "unit", testName = "DeploymentTemplateBuilderTest", singleThreaded = true)
public class DeploymentTemplateBuilderTest extends BaseAzureComputeApiMockTest {
@@ -106,7 +107,6 @@ public class DeploymentTemplateBuilderTest extends BaseAzureComputeApiMockTest {
assertTrue(variables.containsKey(parseVariableName(resource.name())));
}
-
@Test
void testVirtualMachine() {
DeploymentTemplateBuilder builder = getMockDeploymentTemplateBuilderWithEmptyOptions();
@@ -132,6 +132,41 @@ public class DeploymentTemplateBuilderTest extends BaseAzureComputeApiMockTest {
}
@Test
+ void testAddStorageResourceWhenNameIsLongerThan24Chars() {
+ String name = "thishasmorethan24characters";
+ DeploymentTemplateBuilder builder = getMockDeploymentTemplateBuilderWithEmptyOptions(name);
+
+ DeploymentBody deploymentBody = builder.getDeploymentTemplate();
+ assertTrue(Iterables.contains(deploymentBody.template().variables().keySet(), "storageAccountName"));
+ String storageAccountName = deploymentBody.template().variables().get("storageAccountName");
+ assertEquals(storageAccountName.length(), 24);
+ assertEquals(storageAccountName.substring(0, 10), "thishasmor");
+ assertEquals(storageAccountName.substring(storageAccountName.length() - 10, storageAccountName.length()), "characters");
+ }
+
+ @Test
+ void testAddStorageResourceWhenNameIsExactly24Chars() {
+ String name = "ithasexactly24characters";
+ DeploymentTemplateBuilder builder = getMockDeploymentTemplateBuilderWithEmptyOptions(name);
+
+ DeploymentBody deploymentBody = builder.getDeploymentTemplate();
+ assertTrue(Iterables.contains(deploymentBody.template().variables().keySet(), "storageAccountName"));
+ assertEquals(deploymentBody.template().variables().get("storageAccountName").length(), 24);
+ assertEquals(deploymentBody.template().variables().get("storageAccountName"), name);
+ }
+
+ @Test
+ void testAddStorageResourceWhenNameIsLessThan3Chars() {
+ String name = "3c";
+ DeploymentTemplateBuilder builder = getMockDeploymentTemplateBuilderWithEmptyOptions(name);
+
+ DeploymentBody deploymentBody = builder.getDeploymentTemplate();
+ assertTrue(Iterables.contains(deploymentBody.template().variables().keySet(), "storageAccountName"));
+ assertEquals(deploymentBody.template().variables().get("storageAccountName").length(), 6);
+ assertEquals(deploymentBody.template().variables().get("storageAccountName").substring(0, 2), name);
+ }
+
+ @Test
void testCustomOptions(){
final String dnsLabelPrefix = "mydnslabel";
final String customData = "echo customData";
@@ -207,21 +242,29 @@ public class DeploymentTemplateBuilderTest extends BaseAzureComputeApiMockTest {
}
private DeploymentTemplateBuilder getMockDeploymentTemplateBuilderWithEmptyOptions() {
+ return getMockDeploymentTemplateBuilderWithEmptyOptions("mydeployment");
+ }
+
+ private DeploymentTemplateBuilder getMockDeploymentTemplateBuilderWithEmptyOptions(String name) {
AzureTemplateOptions options = new AzureTemplateOptions();
options.virtualNetworkName(vnetName);
options.subnetId(subnetId);
Template template = getMockTemplate(options);
- DeploymentTemplateBuilder templateBuilder = api.deploymentTemplateFactory().create(group, "mydeployment", template);
+ DeploymentTemplateBuilder templateBuilder = api.deploymentTemplateFactory().create(group, name, template);
return templateBuilder;
}
private DeploymentTemplateBuilder getMockDeploymentTemplateBuilderWithOptions(TemplateOptions options) {
+ return getMockDeploymentTemplateBuilderWithOptions("mydeployment", options);
+ }
+
+ private DeploymentTemplateBuilder getMockDeploymentTemplateBuilderWithOptions(String name, TemplateOptions options) {
((AzureTemplateOptions)options).virtualNetworkName(vnetName);
((AzureTemplateOptions)options).subnetId(subnetId);
Template template = getMockTemplate(options);
- DeploymentTemplateBuilder templateBuilder = api.deploymentTemplateFactory().create(group, "mydeployment", template);
+ DeploymentTemplateBuilder templateBuilder = api.deploymentTemplateFactory().create(group, name, template);
return templateBuilder;
}
@@ -231,7 +274,7 @@ public class DeploymentTemplateBuilderTest extends BaseAzureComputeApiMockTest {
return r;
}
}
- fail("Resource with type: " + type + " not found");
+ Assert.fail("Resource with type: " + type + " not found");
return null;
}
[2/2] jclouds-labs git commit: [azure-arm] add controlled storage
account name generation
Posted by an...@apache.org.
[azure-arm] add controlled storage account name generation
- add unit test for storage account name generation
Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/d0b07a66
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/d0b07a66
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/d0b07a66
Branch: refs/heads/master
Commit: d0b07a6684b9fd270b96155306b57a75374e0832
Parents: b0536c2
Author: Andrea Turli <an...@gmail.com>
Authored: Wed Jul 13 14:42:51 2016 +0200
Committer: Andrea Turli <an...@gmail.com>
Committed: Sat Aug 6 14:51:26 2016 +0200
----------------------------------------------------------------------
.../arm/util/DeploymentTemplateBuilder.java | 51 ++++++++++++++++++--
1 file changed, 47 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/d0b07a66/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/util/DeploymentTemplateBuilder.java
----------------------------------------------------------------------
diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/util/DeploymentTemplateBuilder.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/util/DeploymentTemplateBuilder.java
index 89de398..d0750bd 100644
--- a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/util/DeploymentTemplateBuilder.java
+++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/util/DeploymentTemplateBuilder.java
@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.UUID;
import org.jclouds.azurecompute.arm.compute.config.AzureComputeServiceContextModule;
import org.jclouds.azurecompute.arm.compute.extensions.AzureComputeImageExtension;
@@ -54,6 +55,8 @@ import org.jclouds.azurecompute.arm.domain.VHD;
import org.jclouds.azurecompute.arm.domain.VirtualMachineProperties;
import org.jclouds.compute.domain.Template;
import org.jclouds.json.Json;
+import org.jclouds.predicates.Validator;
+import org.jclouds.predicates.validators.DnsNameValidator;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
@@ -175,13 +178,20 @@ public class DeploymentTemplateBuilder {
}
private void addStorageResource() {
- String storageAccountName = name.replaceAll("[^A-Za-z0-9 ]", "") + "stor";
+ String storageAccountName = null;
- String storageName = template.getImage().getName();
- if (storageName.startsWith(CUSTOM_IMAGE_PREFIX)) {
- storageAccountName = storageName.substring(CUSTOM_IMAGE_PREFIX.length()); // get group name
+ String imageName = template.getImage().getName();
+ if (imageName.startsWith(CUSTOM_IMAGE_PREFIX)) {
+ storageAccountName = imageName.substring(CUSTOM_IMAGE_PREFIX.length()); // get group name
+ }
+
+ if (Strings.isNullOrEmpty(storageAccountName)) {
+ storageAccountName = generateStorageAccountName(name);
}
+ Validator<String> validator = new DnsNameValidator(3, 24);
+ validator.validate(storageAccountName);
+
variables.put("storageAccountName", storageAccountName);
ResourceDefinition storageAccount = ResourceDefinition.builder()
@@ -528,4 +538,37 @@ public class DeploymentTemplateBuilder {
return !Strings.isNullOrEmpty(options.getKeyVaultIdAndSecret());
}
+ /**
+ * Generates a valid storage account
+ *
+ * Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.
+ *
+ * @param name the node name
+ * @return the storage account name starting from a sanitized name (with only numbers and lowercase letters only ).
+ * If sanitized name is between 3 and 24 characters, storage account name is equals to sanitized name.
+ * If sanitized name is less than 3 characters, storage account is sanitized name plus 4 random chars.
+ * If sanitized name is more than 24 characters, storage account is first 10 chars of sanitized name plus 4 random chars plus last 10 chars of sanitized name.
+ */
+ private static String generateStorageAccountName(String name) {
+ String storageAccountName = name.replaceAll("[^a-z0-9]", "");
+ int nameLength = storageAccountName.length();
+ if (nameLength >= 3 && nameLength <= 24) {
+ return storageAccountName;
+ }
+
+ String random = UUID.randomUUID().toString().replaceAll("[^a-z0-9]", "").substring(0, 4);
+ if (nameLength < 3) {
+ storageAccountName = new StringBuilder().append(storageAccountName).append(random).toString();
+ }
+ if (nameLength > 24) {
+ storageAccountName = shorten(storageAccountName, random);
+ }
+ return storageAccountName;
+ }
+
+ private static String shorten(String storageAccountName, String random) {
+ String prefix = storageAccountName.substring(0, 10);
+ String suffix = storageAccountName.substring(storageAccountName.length() - 10, storageAccountName.length());
+ return String.format("%s%s%s", prefix, random, suffix);
+ }
}