public TemplateBuilder apply(TemplateBuilder b) { return b.locationId(locationId); }}); return (T)this;
private static void applyLocation(TemplateBuilder builder, CreateJCloudsContainerOptions options) { //Define Location & Hardware if (!Strings.isNullOrEmpty(options.getLocationId())) { builder.locationId(options.getLocationId()); } } }
public void testParse_locationId() { TemplateBuilderSpec spec = parse("locationId=stub"); assertNull(spec.hardwareId); assertNull(spec.minCores); assertNull(spec.minRam); assertNull(spec.minDisk); assertNull(spec.hypervisorMatches); assertNull(spec.imageId); assertNull(spec.imageNameMatches); assertNull(spec.osFamily); assertNull(spec.osVersionMatches); assertNull(spec.os64Bit); assertNull(spec.osArchMatches); assertNull(spec.osDescriptionMatches); assertNull(spec.loginUser); assertNull(spec.authenticateSudo); assertEquals(spec.locationId, "stub"); assertTemplateBuilderEquivalence(templateBuilders.get().locationId("stub"), templateBuilders.get().from(spec)); }
public void testParse_locationId() { TemplateBuilderSpec spec = parse("locationId=stub"); assertNull(spec.hardwareId); assertNull(spec.minCores); assertNull(spec.minRam); assertNull(spec.minDisk); assertNull(spec.hypervisorMatches); assertNull(spec.imageId); assertNull(spec.imageNameMatches); assertNull(spec.osFamily); assertNull(spec.osVersionMatches); assertNull(spec.os64Bit); assertNull(spec.osArchMatches); assertNull(spec.osDescriptionMatches); assertNull(spec.loginUser); assertNull(spec.authenticateSudo); assertEquals(spec.locationId, "stub"); assertNull(spec.forceCacheReload); assertTemplateBuilderEquivalence(templateBuilders.get().locationId("stub"), templateBuilders.get().from(spec)); }
@SuppressWarnings("unchecked") @Test public void testSuppliedLocationAndOptions() { TemplateOptions from = provideTemplateOptions(); Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(region)); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of()); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of()); Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); expect(optionsProvider.get()).andReturn(from).atLeastOnce(); replay(optionsProvider); replay(templateBuilderProvider); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); try { template.imageId("foo").options(provideTemplateOptions()).locationId("location").build(); fail("Expected NoSuchElementException"); } catch (NoSuchElementException e) { } verify(optionsProvider); verify(templateBuilderProvider); }
@SuppressWarnings("unchecked") @Test public void testSuppliedImageLocationWiderThanDefault() { TemplateOptions from = provideTemplateOptions(); Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals(image.getId())).build(); Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(provider, region)); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet .<Image> of(image)); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of(hardware)); Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class); expect(optionsProvider.get()).andReturn(from).atLeastOnce(); replay(defaultOptions, optionsProvider, templateBuilderProvider); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); assertEquals(template.imageId(image.getId()).locationId(provider.getId()).build().getLocation(), region); verify(defaultOptions, optionsProvider, templateBuilderProvider); }
@SuppressWarnings("unchecked") @Test public void testSuppliedImageLocationWiderThanDefault() { TemplateOptions from = provideTemplateOptions(); Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals(image.getId())).build(); Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(provider, region)); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet .<Image> of(image)); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of(hardware)); Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class); GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class); expect(optionsProvider.get()).andReturn(from).atLeastOnce(); replay(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider, getImageStrategy); assertEquals(template.imageId(image.getId()).locationId(provider.getId()).build().getLocation(), region); verify(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy); }
static Template createTemplateFromElement(NodeElement nodeElement, ComputeService computeService) throws IOException { TemplateBuilder templateBuilder = computeService.templateBuilder(); if (nodeElement.getLocation() != null && !"".equals(nodeElement.getLocation())) templateBuilder.locationId(nodeElement.getLocation()); if (nodeElement.getImage() != null && !"".equals(nodeElement.getImage())) { final String imageId = nodeElement.getImage(); try { templateBuilder.imageId(imageId); } catch (NoSuchElementException e) { throw new BuildException("image not found " + nodeElement.getImage()); } } else { templateBuilder.osFamily(OsFamily.valueOf(nodeElement.getOs())); } addHardwareFromElementToTemplate(nodeElement, templateBuilder); templateBuilder.options(getNodeOptionsFromElement(nodeElement)); return templateBuilder.build(); }
@Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript") public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception { initializeContext(); Location existingLocation = Iterables.get(this.nodes, 0).getLocation(); boolean existingLocationIsAssignable = Iterables.any(client.listAssignableLocations(), Predicates.equalTo(existingLocation)); if (existingLocationIsAssignable) { getAnonymousLogger().info("creating another node based on existing nodes' location: " + existingLocation); template = addRunScriptToTemplate(client.templateBuilder().fromTemplate(template) .locationId(existingLocation.getId()).build()); } else { refreshTemplate(); getAnonymousLogger().info( format("%s is not assignable; using template's location %s as ", existingLocation, template.getLocation())); } Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, template); assertEquals(nodes.size(), 1); checkNodes(nodes, group, "bootstrap"); NodeMetadata node = Iterables.getOnlyElement(nodes); if (existingLocationIsAssignable) assertEquals(node.getLocation(), existingLocation); else this.assertLocationSameOrChild(checkNotNull(node.getLocation(), "location of %s", node), template.getLocation()); checkOsMatchesTemplate(node); this.nodes.add(node); }
@Override public Template getNodeTemplate() { return view.getComputeService().templateBuilder().locationId(zone.getId()).build(); } }
/** * Verifies that {@link TemplateBuilderImpl} would choose the correct size of the instance, based * on physical attributes (# of cores, ram, etc). * * Expected size: CC1_4XLARGE */ @Test public void testTemplateChoiceForInstanceByAttributes() throws Exception { Template template = newTemplateBuilder().os64Bit(true).minRam(17510).minCores(6.5).smallest().locationId( "us-east-1").build(); assert template != null : "The returned template was null, but it should have a value."; assertEquals(template.getHardware().getId(), "cc1.4xlarge"); }
public void start(String id) throws Exception { ComputeService computeService = m_computeServiceContext.getComputeService(); Template template = computeService.templateBuilder() .imageId(m_location + "/" + m_amiId) .hardwareId(InstanceType.C1_MEDIUM) .locationId(m_location) .build(); int[] extraPorts = parseExtraPorts(m_extraPorts); int[] inboundPorts = mergePorts(DEFAULT_PORTS, extraPorts); template.getOptions().as(EC2TemplateOptions.class).inboundPorts(inboundPorts); template.getOptions().blockOnComplete(false); template.getOptions().runAsRoot(m_runAsRoot); Set<? extends NodeMetadata> tag = computeService.createNodesInGroup(m_tagPrefix + id, 1, template); System.out.println("In case you need it, this is the key to ssh to " + id + ":\n" + tag.iterator().next().getCredentials().credential); computeService.runScriptOnNodesMatching(runningInGroup(m_tagPrefix + id), Statements.exec(buildStartupScript(id)), RunScriptOptions.Builder.blockOnComplete(false)); }
/** * Verifies that {@link TemplateBuilderImpl} would choose the correct size of the instance, based * on {@link org.jclouds.compute.domain.Hardware} from {@link EC2Hardware}. * * Expected size: m2.xlarge */ @Test public void testTemplateChoiceForInstanceByhardwareId() throws Exception { Template template = newTemplateBuilder().os64Bit(true).hardwareId("m2.xlarge").locationId("us-east-1").build(); assert template != null : "The returned template was null, but it should have a value."; // assert m2_xlarge().build().equals(template.getHardware()) : format( // "Incorrect image determined by the template. Expected: %s. Found: %s.", "m2.xlarge", // String.valueOf(template.getHardware())); assertEquals(m2_xlarge().build().getId(), template.getHardware().getId()); }
/** * Negative test version of {@link #testTemplateChoiceForInstanceByAttributes}. * * Verifies that {@link TemplateBuilderImpl} would not choose the insufficient size of the * instance, based on physical attributes (# of cores, ram, etc). * * Expected size: anything but m2.xlarge */ @Test public void testNegativeTemplateChoiceForInstanceByAttributes() throws Exception { Template template = newTemplateBuilder().os64Bit(true).minRam(17510).minCores(6.7).smallest().locationId( "us-east-1").build(); assert template != null : "The returned template was null, but it should have a value."; assert !m2_xlarge().build().equals(template.getHardware()) : format( "Incorrect image determined by the template. Expected: not %s. Found: %s.", "m2.xlarge", template .getHardware().getId()); }
/** * Verifies that {@link TemplateBuilderImpl} would choose the correct size of the instance, based * on {@link org.jclouds.compute.domain.Hardware} from {@link EC2Hardware}. * * Expected size: m2.xlarge */ @Test public void testTemplateChoiceForInstanceByHardwareId() throws Exception { Template template = newTemplateBuilder().os64Bit(true).hardwareId("m2.xlarge").locationId("us-east-1").build(); assert template != null : "The returned template was null, but it should have a value."; // assert m2_xlarge().build().equals(template.getHardware()) : format( // "Incorrect image determined by the template. Expected: %s. Found: %s.", "m2.xlarge", // String.valueOf(template.getHardware())); assertEquals(template.getHardware().getId(), m2_xlarge().build().getId()); }
/** * Negative test version of {@link #testTemplateChoiceForInstanceByAttributes}. * * Verifies that {@link TemplateBuilderImpl} would not choose the insufficient size of the * instance, based on physical attributes (# of cores, ram, etc). * * Expected size: anything but m2.xlarge */ @Test public void testNegativeTemplateChoiceForInstanceByAttributes() throws Exception { Template template = newTemplateBuilder().os64Bit(true).minRam(17510).minCores(6.7).smallest().locationId( "us-east-1").build(); assert template != null : "The returned template was null, but it should have a value."; assert !m2_xlarge().build().equals(template.getHardware()) : format( "Incorrect image determined by the template. Expected: not %s. Found: %s.", "m2.xlarge", template .getHardware().getId()); }
@Test public void testTemplateBuilderCanUseImageIdAndHardwareIdAndAZ() { Template template = view.getComputeService().templateBuilder().imageId("us-east-1/ami-ccb35ea5") .hardwareId(InstanceType.M2_2XLARGE).locationId("us-east-1b").build(); assert template.getImage().getProviderId().startsWith("ami-") : template; assertEquals(template.getImage().getOperatingSystem().getVersion(), "5.4"); assertEquals(template.getImage().getOperatingSystem().is64Bit(), true); assertEquals(template.getImage().getOperatingSystem().getFamily(), OsFamily.CENTOS); assertEquals(template.getImage().getVersion(), "4.4.10"); assertEquals(template.getImage().getUserMetadata().get("rootDeviceType"), "instance-store"); assertEquals(template.getLocation().getId(), "us-east-1b"); assertEquals(template.getImage().getLocation().getId(), "us-east-1"); assertEquals(getCores(template.getHardware()), 4.0d); assertEquals(template.getHardware().getId(), InstanceType.M2_2XLARGE); assertEquals(template.getImage().getOperatingSystem().getArch(), "paravirtual"); }
/** * Verifies that {@link TemplateBuilderImpl} would choose the correct size of the instance, based * on physical attributes (# of cores, ram, etc). * * Expected size: CC1_4XLARGE */ @Test public void testTemplateChoiceForInstanceByAttributes() throws Exception { Template template = newTemplateBuilder().os64Bit(true).minRam(17510).minCores(6.5).smallest().locationId( "us-east-1").build(); assert template != null : "The returned template was null, but it should have a value."; assertEquals(template.getHardware().getId(), m2_4xlarge().build().getId()); }
@Test public void testTemplateBuilderCanUseImageId() throws Exception { Template defaultTemplate = view.getComputeService().templateBuilder().build(); view.close(); setupContext(); Template template = view.getComputeService().templateBuilder().imageId(defaultTemplate.getImage().getId()) .locationId(defaultTemplate.getLocation().getId()).build(); assertEquals(template.getImage(), defaultTemplate.getImage()); }
@Test public void testTemplateBuilderCanUseImageId() throws Exception { Template defaultTemplate = view.getComputeService().templateBuilder().build(); view.close(); setupContext(); Template template = view.getComputeService().templateBuilder().imageId(defaultTemplate.getImage().getId()) .locationId(defaultTemplate.getLocation().getId()).build(); assertEquals(template.getImage(), defaultTemplate.getImage()); }