Java Code Examples for org.jclouds.compute.domain.TemplateBuilder#hardwareId()

The following examples show how to use org.jclouds.compute.domain.TemplateBuilder#hardwareId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TemplateBuilderCustomizers.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Override
public void apply(TemplateBuilder tb, ConfigBag props, Object v) {
    tb.hardwareId(v.toString());
}
 
Example 2
Source File: OpenstackIaas.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public void buildTemplate() {
    IaasProvider iaasProvider = getIaasProvider();

    if (iaasProvider.getComputeService() == null) {
        throw new CloudControllerException("Compute service is null for IaaS provider: " + iaasProvider.getName());
    }

    TemplateBuilder templateBuilder = iaasProvider.getComputeService().templateBuilder();
    templateBuilder.imageId(iaasProvider.getImage());

    // To avoid creation of template objects in each and every time, we create all at once!
    String instanceType;

    // set instance type
    if (((instanceType = iaasProvider.getProperty(CloudControllerConstants.INSTANCE_TYPE)) != null)) {
        templateBuilder.hardwareId(instanceType);
    }
    Template template = templateBuilder.build();

    // In Openstack the call to IaaS should be blocking, in order to retrieve IP addresses.
    boolean blockUntilRunning = true;
    if (iaasProvider.getProperty(CloudControllerConstants.BLOCK_UNTIL_RUNNING) != null) {
        blockUntilRunning = Boolean
                .parseBoolean(iaasProvider.getProperty(CloudControllerConstants.BLOCK_UNTIL_RUNNING));
    }
    template.getOptions().as(TemplateOptions.class).blockUntilRunning(blockUntilRunning);

    // this is required in order to avoid creation of additional security groups by Jclouds.
    template.getOptions().as(TemplateOptions.class).inboundPorts();

    if (iaasProvider.getProperty(CloudControllerConstants.SECURITY_GROUPS) != null) {
        template.getOptions().as(NovaTemplateOptions.class).securityGroupNames(
                iaasProvider.getProperty(CloudControllerConstants.SECURITY_GROUPS)
                        .split(CloudControllerConstants.ENTRY_SEPARATOR));
    }

    if (iaasProvider.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
        template.getOptions().as(NovaTemplateOptions.class)
                .keyPairName(iaasProvider.getProperty(CloudControllerConstants.KEY_PAIR));
    }

    if (iaasProvider.getNetworkInterfaces() != null) {
        Set<Network> novaNetworksSet = new LinkedHashSet<Network>(iaasProvider.getNetworkInterfaces().length);
        for (NetworkInterface ni : iaasProvider.getNetworkInterfaces()) {
            novaNetworksSet.add(Network.builder().networkUuid(ni.getNetworkUuid()).fixedIp(ni.getFixedIp())
                    .portUuid(ni.getPortUuid()).build());
        }
        template.getOptions().as(NovaTemplateOptions.class).novaNetworks(novaNetworksSet);
    }

    if (iaasProvider.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
        template.getOptions().as(NovaTemplateOptions.class)
                .availabilityZone(iaasProvider.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    }

    //TODO
    //		if (iaas.getProperty(CloudControllerConstants.HOST) != null) {
    //            template.getOptions().as(NovaTemplateOptions.class)
    //                    .(CloudControllerConstants.HOST);
    //        }

    // set Template
    iaasProvider.setTemplate(template);
}
 
Example 3
Source File: GCEIaas.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public void buildTemplate() {
    IaasProvider iaasInfo = getIaasProvider();

    if (iaasInfo.getComputeService() == null) {
        String msg = "Compute service is null for IaaS provider: "
                + iaasInfo.getName();
        log.fatal(msg);
        throw new CloudControllerException(msg);
    }

    if (log.isDebugEnabled()) {
        log.debug("Building template for Google Compute Engine IaaS");
    }
    TemplateBuilder templateBuilder = iaasInfo.getComputeService().templateBuilder();

    // set image id specified
    templateBuilder.imageId(iaasInfo.getImage());

    String zone = iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE);
    if (zone != null) {
        Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
        for (Location location : locations) {
            if (location.getScope().toString().equalsIgnoreCase(CloudControllerConstants.ZONE_ELEMENT) &&
                    location.getId().equals(zone)) {
                templateBuilder.locationId(location.getId());
                log.info("zone has been set as " + zone + " with id: " + location.getId());
                break;
            }
        }
    }

    if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
        // set instance type eg: m1.large
        templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    }
    // build the Template
    Template template = templateBuilder.build();

    if (zone != null) {
        if (!template.getLocation().getId().equals(zone)) {
            log.warn("couldn't find assignable zone of id :" + zone +
                    " in the IaaS. Hence using the default location as " +
                    template.getLocation().getScope().toString() +
                    " with the id " + template.getLocation().getId());
        }
    }

    // if you wish to auto assign IPs, instance spawning call should be
    // blocking, but if you
    // wish to assign IPs manually, it can be non-blocking.
    // is auto-assign-ip mode or manual-assign-ip mode? - default mode is
    // non-blocking
    boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo.getProperty("autoAssignIp"));
    template.getOptions().as(GoogleComputeEngineTemplateOptions.class).blockUntilRunning(blockUntilRunning);

    if (zone != null) {
        templateBuilder.locationId(zone);
        log.debug("setting location to " + zone);
    }

    // ability to define tags with Key-value pairs
    Map<String, String> keyValuePairTagsMap = new HashMap<String, String>();

    for (String propertyKey : iaasInfo.getProperties().keySet()) {
        if (propertyKey.startsWith(CloudControllerConstants.TAGS_AS_KEY_VALUE_PAIRS_PREFIX)) {
            keyValuePairTagsMap.put(propertyKey.substring(CloudControllerConstants.TAGS_AS_KEY_VALUE_PAIRS_PREFIX
                    .length()), iaasInfo.getProperties().get(propertyKey));
            template.getOptions().as(GoogleComputeEngineTemplateOptions.class).userMetadata(keyValuePairTagsMap);
        }
        log.info("User defined property [key]" + propertyKey + ", [value] " +
                iaasInfo.getProperties().get(propertyKey));
    }

    if (iaasInfo.getNetworkInterfaces() != null) {
        List<String> networks = new ArrayList<String>(iaasInfo.getNetworkInterfaces().length);
        for (NetworkInterface ni : iaasInfo.getNetworkInterfaces()) {
            networks.add(ni.getNetworkUuid());
        }
        template.getOptions().as(GoogleComputeEngineTemplateOptions.class).networks(networks);
        log.info("Using network interfaces: " + networks);
    }

    if (iaasInfo.getProperty(GCEIaas.GCE_NETWORK_PROPERTY) != null) {
        try {
            URI networkURI = new URI(iaasInfo.getProperty(GCEIaas.GCE_NETWORK_PROPERTY));
            template.getOptions().as(GoogleComputeEngineTemplateOptions.class).network(networkURI);
            log.info("Using GCE network: " + networkURI.toString());
        }
        catch (Exception e) {
            log.error("Error while adding the network", e);
        }
    }
    // set Template
    iaasInfo.setTemplate(template);
}