You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2017/01/30 14:30:35 UTC
[2/2] jclouds-labs git commit: [azurecompute-arm] performance
improvements misc
[azurecompute-arm] performance improvements misc
- improve cleanup performances: by using resourceGroupApi.resources
Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/6b35f6fd
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/6b35f6fd
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/6b35f6fd
Branch: refs/heads/master
Commit: 6b35f6fd3dfbc243ff571ad9896bab2ea51ccb62
Parents: 64bdc80
Author: Andrea Turli <an...@gmail.com>
Authored: Thu Jan 26 13:08:21 2017 +0100
Committer: Ignasi Barrera <na...@apache.org>
Committed: Mon Jan 30 15:12:36 2017 +0100
----------------------------------------------------------------------
.../arm/compute/strategy/CleanupResources.java | 17 ++---
.../azurecompute/arm/domain/Resource.java | 72 ++++++++++++++++++++
.../arm/features/ResourceGroupApi.java | 13 +++-
.../arm/features/ResourceGroupApiLiveTest.java | 11 ++-
.../arm/features/ResourceGroupApiMockTest.java | 37 ++++++++--
.../test/resources/resourcegroup-resources.json | 46 +++++++++++++
6 files changed, 176 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/6b35f6fd/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CleanupResources.java
----------------------------------------------------------------------
diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CleanupResources.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CleanupResources.java
index 3914bc0..b971931 100644
--- a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CleanupResources.java
+++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CleanupResources.java
@@ -16,12 +16,6 @@
*/
package org.jclouds.azurecompute.arm.compute.strategy;
-import static com.google.common.base.Predicates.notNull;
-import static com.google.common.collect.Iterables.filter;
-import static com.google.common.collect.Iterables.transform;
-import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TIMEOUT_RESOURCE_DELETED;
-import static org.jclouds.util.Closeables2.closeQuietly;
-
import java.net.URI;
import java.util.List;
@@ -53,6 +47,12 @@ import com.google.common.cache.LoadingCache;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
+import static com.google.common.base.Predicates.notNull;
+import static com.google.common.collect.Iterables.filter;
+import static com.google.common.collect.Iterables.transform;
+import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TIMEOUT_RESOURCE_DELETED;
+import static org.jclouds.util.Closeables2.closeQuietly;
+
@Singleton
public class CleanupResources {
@@ -165,10 +165,7 @@ public class CleanupResources {
public boolean deleteResourceGroupIfEmpty(String group) {
boolean deleted = false;
- if (api.getVirtualMachineApi(group).list().isEmpty() && api.getStorageAccountApi(group).list().isEmpty()
- && api.getNetworkInterfaceCardApi(group).list().isEmpty()
- && api.getPublicIPAddressApi(group).list().isEmpty()
- && api.getNetworkSecurityGroupApi(group).list().isEmpty()) {
+ if (api.getResourceGroupApi().resources(group).isEmpty()) {
logger.debug(">> the resource group %s is empty. Deleting...", group);
deleted = resourceDeleted.apply(api.getResourceGroupApi().delete(group));
}
http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/6b35f6fd/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Resource.java
----------------------------------------------------------------------
diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Resource.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Resource.java
new file mode 100644
index 0000000..f048234
--- /dev/null
+++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/domain/Resource.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jclouds.azurecompute.arm.domain;
+
+import java.util.Map;
+
+import org.jclouds.javax.annotation.Nullable;
+import org.jclouds.json.SerializedNames;
+
+import com.google.auto.value.AutoValue;
+import com.google.common.collect.ImmutableMap;
+
+@AutoValue
+public abstract class Resource {
+
+ @AutoValue
+ public abstract static class Identity {
+
+ public abstract String principalId();
+ public abstract String tenantId();
+ public abstract String type();
+
+ @SerializedNames({"principalId", "tenantId", "type" })
+ public static Identity create(String principalId, String tenantId, String type) {
+ return new AutoValue_Resource_Identity(principalId, tenantId, type);
+ }
+ }
+
+ @AutoValue
+ public abstract static class ResourceProperties{
+ @Nullable
+ public abstract String provisioningState();
+
+ @SerializedNames({"provisioningState"})
+ public static ResourceProperties create(final String provisioningState) {
+ return new AutoValue_Resource_ResourceProperties(provisioningState);
+ }
+ }
+
+ public abstract String id();
+ public abstract String name();
+ public abstract String type();
+ public abstract String location();
+ @Nullable public abstract Map<String, String> tags();
+ @Nullable public abstract Identity identity();
+ @Nullable public abstract SKU sku();
+ @Nullable public abstract String managedBy();
+ @Nullable public abstract String kind();
+ @Nullable public abstract Plan plan();
+ @Nullable public abstract ResourceProperties properties();
+
+ @SerializedNames({"id", "name", "type", "location", "tags", "identity", "sku", "managedBy", "kind", "plan", "properties"})
+ public static Resource create(String id, String name, String type, String location, Map<String, String> tags,
+ Identity identity, SKU sku, String managedBy, String kind, Plan plan, ResourceProperties properties) {
+ return new AutoValue_Resource(id, name, type, location, tags == null ? null : ImmutableMap.copyOf(tags),
+ identity, sku, managedBy, kind, plan, properties);
+ }
+}
http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/6b35f6fd/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/ResourceGroupApi.java
----------------------------------------------------------------------
diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/ResourceGroupApi.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/ResourceGroupApi.java
index 6f718a5..2c6a8b3 100644
--- a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/ResourceGroupApi.java
+++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/ResourceGroupApi.java
@@ -15,6 +15,7 @@
* limitations under the License.
*/
package org.jclouds.azurecompute.arm.features;
+
import java.io.Closeable;
import java.net.URI;
import java.util.List;
@@ -32,6 +33,7 @@ import javax.ws.rs.core.MediaType;
import org.jclouds.Fallbacks.EmptyListOnNotFoundOr404;
import org.jclouds.Fallbacks.NullOnNotFoundOr404;
+import org.jclouds.azurecompute.arm.domain.Resource;
import org.jclouds.azurecompute.arm.domain.ResourceGroup;
import org.jclouds.azurecompute.arm.filters.ApiVersionFilter;
import org.jclouds.azurecompute.arm.functions.URIParser;
@@ -67,7 +69,7 @@ public interface ResourceGroupApi extends Closeable{
@Path("/{name}")
@Produces(MediaType.APPLICATION_JSON)
@MapBinder(BindToJsonPayload.class)
- ResourceGroup create(@PathParam("name") String name, @PayloadParam("location") String location, @Nullable @PayloadParam("tags")Map<String, String> tags);
+ ResourceGroup create(@PathParam("name") String name, @PayloadParam("location") String location, @Nullable @PayloadParam("tags") Map<String, String> tags);
@Named("resourcegroup:get")
@GET
@@ -76,12 +78,19 @@ public interface ResourceGroupApi extends Closeable{
@Nullable
ResourceGroup get(@PathParam("name") String name);
+ @Named("resourcegroup:resources")
+ @GET
+ @Path("/{name}/resources")
+ @SelectJson("value")
+ @Fallback(EmptyListOnNotFoundOr404.class)
+ List<Resource> resources(@PathParam("name") String name);
+
@Named("resourcegroup:update")
@PATCH
@Produces(MediaType.APPLICATION_JSON)
@Path("/{name}")
@MapBinder(BindToJsonPayload.class)
- ResourceGroup update(@PathParam("name") String name, @Nullable @PayloadParam("tags")Map<String, String> tags);
+ ResourceGroup update(@PathParam("name") String name, @Nullable @PayloadParam("tags") Map<String, String> tags);
@Named("resourcegroup:delete")
@DELETE
http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/6b35f6fd/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiLiveTest.java
----------------------------------------------------------------------
diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiLiveTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiLiveTest.java
index 3a0c134..2dd8097 100644
--- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiLiveTest.java
+++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiLiveTest.java
@@ -19,6 +19,7 @@ package org.jclouds.azurecompute.arm.features;
import java.net.URI;
import java.util.List;
+import org.jclouds.azurecompute.arm.domain.Resource;
import org.jclouds.azurecompute.arm.domain.ResourceGroup;
import org.jclouds.azurecompute.arm.internal.BaseAzureComputeApiLiveTest;
import org.testng.annotations.AfterClass;
@@ -43,6 +44,15 @@ public class ResourceGroupApiLiveTest extends BaseAzureComputeApiLiveTest {
}
@Test(dependsOnMethods = "testCreate")
+ public void testResources() {
+ List<Resource> resources = api().resources(RESOURCE_GROUP_NAME);
+ assertTrue(resources.isEmpty());
+ for (Resource resource : resources) {
+ assertNotNull(resource);
+ }
+ }
+
+ @Test(dependsOnMethods = "testCreate")
public void testList() {
final List<ResourceGroup> resourceGroups = api().list();
@@ -66,7 +76,6 @@ public class ResourceGroupApiLiveTest extends BaseAzureComputeApiLiveTest {
}
public void testCreate() {
-
final ResourceGroup resourceGroup = api().create(RESOURCE_GROUP_NAME, LOCATION, null);
assertEquals(resourceGroup.name(), RESOURCE_GROUP_NAME);
assertEquals(resourceGroup.location(), LOCATION);
http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/6b35f6fd/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiMockTest.java
----------------------------------------------------------------------
diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiMockTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiMockTest.java
index 0c03c75..acac271 100644
--- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiMockTest.java
+++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiMockTest.java
@@ -16,20 +16,22 @@
*/
package org.jclouds.azurecompute.arm.features;
-import static com.google.common.collect.Iterables.isEmpty;
-import static com.google.common.collect.Iterables.size;
-import static org.testng.Assert.assertNotNull;
-
import java.net.URI;
import java.util.List;
-import com.google.common.collect.ImmutableMap;
+import org.jclouds.azurecompute.arm.domain.Resource;
import org.jclouds.azurecompute.arm.domain.ResourceGroup;
import org.jclouds.azurecompute.arm.internal.BaseAzureComputeApiMockTest;
import org.testng.annotations.Test;
-import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.assertNull;
+
+import com.google.common.collect.ImmutableMap;
+
+import static com.google.common.collect.Iterables.isEmpty;
+import static com.google.common.collect.Iterables.size;
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 = "ResourceGroupApiMockTest", singleThreaded = true)
public class ResourceGroupApiMockTest extends BaseAzureComputeApiMockTest {
@@ -147,4 +149,25 @@ public class ResourceGroupApiMockTest extends BaseAzureComputeApiMockTest {
assertSent(server, "DELETE", requestUrl + "/jcloudstest" + version);
}
+ public void testListResourceGroupResources() throws InterruptedException {
+ server.enqueue(jsonResponse("/resourcegroup-resources.json"));
+
+ List<Resource> resources = api.getResourceGroupApi().resources("jcloudstest");
+
+ assertEquals(size(resources), 6);
+
+ assertSent(server, "GET", requestUrl + "/jcloudstest/resources" + version);
+ }
+
+ public void testListResourceGroupResourcesReturns404() throws InterruptedException {
+ server.enqueue(response404());
+
+ List<Resource> resources = api.getResourceGroupApi().resources("jcloudstest");
+
+ assertTrue(isEmpty(resources));
+
+ assertEquals(server.getRequestCount(), 1);
+ assertSent(server, "GET", requestUrl + "/jcloudstest/resources" + version);
+ }
+
}
http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/6b35f6fd/azurecompute-arm/src/test/resources/resourcegroup-resources.json
----------------------------------------------------------------------
diff --git a/azurecompute-arm/src/test/resources/resourcegroup-resources.json b/azurecompute-arm/src/test/resources/resourcegroup-resources.json
new file mode 100644
index 0000000..cae1a18
--- /dev/null
+++ b/azurecompute-arm/src/test/resources/resourcegroup-resources.json
@@ -0,0 +1,46 @@
+{
+ "value": [
+ {
+ "id": "/subscriptions/610bba05-d7a7-4567-96af-48ecbd09453b/resourceGroups/test/providers/Microsoft.Compute/virtualMachines/test",
+ "name": "test",
+ "type": "Microsoft.Compute/virtualMachines",
+ "location": "northeurope"
+ },
+ {
+ "id": "/subscriptions/610bba05-d7a7-4567-96af-48ecbd09453b/resourceGroups/test/providers/Microsoft.Network/networkInterfaces/test641",
+ "name": "test641",
+ "type": "Microsoft.Network/networkInterfaces",
+ "location": "northeurope"
+ },
+ {
+ "id": "/subscriptions/610bba05-d7a7-4567-96af-48ecbd09453b/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/test-nsg",
+ "name": "test-nsg",
+ "type": "Microsoft.Network/networkSecurityGroups",
+ "location": "northeurope"
+ },
+ {
+ "id": "/subscriptions/610bba05-d7a7-4567-96af-48ecbd09453b/resourceGroups/test/providers/Microsoft.Network/publicIPAddresses/test-ip",
+ "name": "test-ip",
+ "type": "Microsoft.Network/publicIPAddresses",
+ "location": "northeurope"
+ },
+ {
+ "id": "/subscriptions/610bba05-d7a7-4567-96af-48ecbd09453b/resourceGroups/test/providers/Microsoft.Network/virtualNetworks/test-vnet",
+ "name": "test-vnet",
+ "type": "Microsoft.Network/virtualNetworks",
+ "location": "northeurope"
+ },
+ {
+ "id": "/subscriptions/610bba05-d7a7-4567-96af-48ecbd09453b/resourceGroups/test/providers/Microsoft.Storage/storageAccounts/testjclo",
+ "name": "testjclo",
+ "type": "Microsoft.Storage/storageAccounts",
+ "sku": {
+ "name": "Premium_LRS",
+ "tier": "Premium"
+ },
+ "kind": "Storage",
+ "location": "northeurope",
+ "tags": {}
+ }
+ ]
+}