Issue 280: added size parser that parses the compute service size from the ovf descriptor of a vApp template

This commit is contained in:
Adrian Cole 2010-08-25 16:30:23 -07:00
parent 114d188079
commit ed73f25685
5 changed files with 259 additions and 1 deletions

View File

@ -19,10 +19,13 @@
package org.jclouds.vcloud.compute.config;
import java.util.Set;
import javax.inject.Singleton;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.Size;
import org.jclouds.compute.internal.ComputeServiceContextImpl;
import org.jclouds.compute.strategy.AddNodeWithTagStrategy;
import org.jclouds.compute.strategy.GetNodeMetadataStrategy;
@ -34,14 +37,18 @@ import org.jclouds.vcloud.VCloudClient;
import org.jclouds.vcloud.compute.CommonVCloudComputeClient;
import org.jclouds.vcloud.compute.VCloudComputeClient;
import org.jclouds.vcloud.compute.functions.ImagesInOrg;
import org.jclouds.vcloud.compute.functions.SizesInOrg;
import org.jclouds.vcloud.compute.internal.VCloudComputeClientImpl;
import org.jclouds.vcloud.compute.strategy.VCloudAddNodeWithTagStrategy;
import org.jclouds.vcloud.compute.strategy.VCloudGetNodeMetadataStrategy;
import org.jclouds.vcloud.compute.strategy.VCloudListNodesStrategy;
import org.jclouds.vcloud.compute.strategy.VCloudRebootNodeStrategy;
import org.jclouds.vcloud.compute.suppliers.VCloudSizeSupplier;
import org.jclouds.vcloud.domain.Org;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
@ -68,6 +75,9 @@ public class VCloudComputeServiceContextModule extends CommonVCloudComputeServic
bind(new TypeLiteral<Function<Org, Iterable<? extends Image>>>() {
}).to(new TypeLiteral<ImagesInOrg>() {
});
bind(new TypeLiteral<Function<Org, Iterable<? extends Size>>>() {
}).to(new TypeLiteral<SizesInOrg>() {
});
bind(AddNodeWithTagStrategy.class).to(VCloudAddNodeWithTagStrategy.class);
bind(ListNodesStrategy.class).to(VCloudListNodesStrategy.class);
}
@ -77,4 +87,10 @@ public class VCloudComputeServiceContextModule extends CommonVCloudComputeServic
CommonVCloudComputeClient provideCommonVCloudComputeClient(VCloudComputeClient in) {
return in;
}
@Override
protected Supplier<Set<? extends Size>> getSourceSizeSupplier(Injector injector) {
return injector.getInstance(VCloudSizeSupplier.class);
}
}

View File

@ -0,0 +1,88 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.vcloud.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.find;
import static org.jclouds.vcloud.predicates.VCloudPredicates.resourceType;
import javax.inject.Inject;
import org.jclouds.compute.domain.Size;
import org.jclouds.compute.domain.internal.SizeImpl;
import org.jclouds.compute.predicates.ImagePredicates;
import org.jclouds.domain.Location;
import org.jclouds.vcloud.VCloudClient;
import org.jclouds.vcloud.domain.ReferenceType;
import org.jclouds.vcloud.domain.VAppTemplate;
import org.jclouds.vcloud.domain.ovf.OvfEnvelope;
import org.jclouds.vcloud.domain.ovf.ResourceAllocation;
import org.jclouds.vcloud.domain.ovf.ResourceType;
import org.jclouds.vcloud.domain.ovf.VCloudHardDisk;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
/**
* @author Adrian Cole
*/
public class SizeForVAppTemplate implements Function<VAppTemplate, Size> {
private final VCloudClient client;
private final FindLocationForResource findLocationForResource;
private ReferenceType parent;
@Inject
protected SizeForVAppTemplate(VCloudClient client, FindLocationForResource findLocationForResource) {
this.client = checkNotNull(client, "client");
this.findLocationForResource = checkNotNull(findLocationForResource, "findLocationForResource");
}
public SizeForVAppTemplate withParent(ReferenceType parent) {
this.parent = parent;
return this;
}
@Override
public Size apply(VAppTemplate from) {
OvfEnvelope ovf = client.getOvfEnvelopeForVAppTemplate(from.getHref());
Location location = findLocationForResource.apply(checkNotNull(parent, "parent"));
int ram = (int) find(ovf.getVirtualSystem().getHardware().getResourceAllocations(),
resourceType(ResourceType.MEMORY)).getVirtualQuantity();
ResourceAllocation diskR = find(ovf.getVirtualSystem().getHardware().getResourceAllocations(),
resourceType(ResourceType.DISK_DRIVE));
int disk = (int) (((diskR instanceof VCloudHardDisk) ? VCloudHardDisk.class.cast(diskR).getCapacity() : diskR
.getVirtualQuantity()) / 1024l);
double cores = (int) find(ovf.getVirtualSystem().getHardware().getResourceAllocations(),
resourceType(ResourceType.PROCESSOR)).getVirtualQuantity();
return new SizeImpl(from.getHref().toASCIIString(), from.getName()
+ String.format(": vpu(%.1f), ram(%d), disk(%d)", cores, ram, disk), from.getHref().toASCIIString(),
location, null, ImmutableMap.<String, String> of(), cores, ram, disk, ImagePredicates.idEquals(from
.getHref().toASCIIString()));
}
protected String getName(String name) {
return name;
}
}

View File

@ -0,0 +1,62 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.vcloud.compute.functions;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import org.jclouds.compute.domain.Size;
import org.jclouds.vcloud.domain.CatalogItem;
import org.jclouds.vcloud.domain.Org;
import org.jclouds.vcloud.domain.VAppTemplate;
import org.jclouds.vcloud.functions.AllCatalogItemsInOrg;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
/**
* @author Adrian Cole
*/
@Singleton
public class SizesInOrg implements Function<Org, Iterable<? extends Size>> {
private final AllCatalogItemsInOrg allCatalogItemsInOrg;
private final Function<Iterable<? extends CatalogItem>, Iterable<? extends VAppTemplate>> vAppTemplatesForCatalogItems;
private final Provider<SizeForVAppTemplate> sizeForVAppTemplateProvider;
@Inject
SizesInOrg(AllCatalogItemsInOrg allCatalogItemsInOrg,
Provider<SizeForVAppTemplate> sizeForVAppTemplateProvider,
Function<Iterable<? extends CatalogItem>, Iterable<? extends VAppTemplate>> vAppTemplatesForCatalogItems) {
this.sizeForVAppTemplateProvider = sizeForVAppTemplateProvider;
this.allCatalogItemsInOrg = allCatalogItemsInOrg;
this.vAppTemplatesForCatalogItems = vAppTemplatesForCatalogItems;
}
@Override
public Iterable<? extends Size> apply(Org from) {
Iterable<? extends CatalogItem> catalogs = allCatalogItemsInOrg.apply(from);
Iterable<? extends VAppTemplate> vAppTemplates = vAppTemplatesForCatalogItems.apply(catalogs);
return Iterables.transform(Iterables.filter(vAppTemplates, Predicates.notNull()), sizeForVAppTemplateProvider.get().withParent(from));
}
}

View File

@ -0,0 +1,92 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.vcloud.compute.suppliers;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Sets.newLinkedHashSet;
import static org.jclouds.concurrent.FutureIterables.transformParallel;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.compute.domain.Size;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.domain.Org;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
/**
* @author Adrian Cole
*/
@Singleton
public class VCloudSizeSupplier implements Supplier<Set<? extends Size>> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
public Logger logger = Logger.NULL;
private final Supplier<Map<String, ? extends Org>> orgMap;
private final Function<Org, Iterable<? extends Size>> sizesInOrg;
private final ExecutorService executor;
@Inject
VCloudSizeSupplier(Supplier<Map<String, ? extends Org>> orgMap,
Function<Org, Iterable<? extends Size>> sizesInOrg,
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
this.orgMap = checkNotNull(orgMap, "orgMap");
this.sizesInOrg = checkNotNull(sizesInOrg, "sizesInOrg");
this.executor = checkNotNull(executor, "executor");
}
@Override
public Set<? extends Size> get() {
Iterable<? extends Org> orgs = checkNotNull(orgMap.get().values(), "orgs");
Iterable<Iterable<? extends Size>> sizes = transformParallel(orgs,
new Function<Org, Future<Iterable<? extends Size>>>() {
@Override
public Future<Iterable<? extends Size>> apply(final Org from) {
checkNotNull(from, "org");
return executor.submit(new Callable<Iterable<? extends Size>>() {
@Override
public Iterable<? extends Size> call() throws Exception {
return sizesInOrg.apply(from);
}
});
}
}, executor, null, logger, "sizes in " + orgs);
return newLinkedHashSet(concat(sizes));
}
}

View File

@ -35,7 +35,7 @@ import org.testng.annotations.Test;
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "vcloud.VirtualSystemHandlerTest")
public class VirtualSystemHandlerTest extends BaseHandlerTest {
public class SystemHandlerTest extends BaseHandlerTest {
@BeforeTest
@Override