You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stratos.apache.org by asankasanjaya <gi...@git.apache.org> on 2014/10/28 07:15:46 UTC

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

GitHub user asankasanjaya opened a pull request:

    https://github.com/apache/stratos/pull/97

    Apache cloudstack integration for Stratos

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/asankasanjaya/stratos master

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/stratos/pull/97.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #97
    
----
commit 384e293461f1e382646c96e1fec0abc17a5242e5
Author: sanjaya <sanjaya@sanjaya-thinkpad-t520.(none)>
Date:   2014-10-03T12:58:12Z

    adding iaas classes

commit 92f9abfc7b561d1d3967212f72435ba1be68ee85
Author: asankasa <as...@wso2.com>
Date:   2014-10-06T04:16:33Z

    adding cloudstack dependancy

commit 1b81c15e87739ad42baa7a69c9c9dfd37761f5b2
Author: asankasa <as...@wso2.com>
Date:   2014-10-06T05:15:30Z

    Merge branch 'master' of https://github.com/apache/stratos

commit c9779fae3fd8ac0074cb791fa235c8ff7c35ecc7
Author: asankasa <as...@wso2.com>
Date:   2014-10-06T06:26:58Z

    Merge branch 'master' of https://github.com/apache/stratos

commit f75e1cc78d6b262d7b4b0ac8171b14d1fc359654
Author: asankasa <as...@wso2.com>
Date:   2014-10-06T14:04:34Z

    works 6 oct

commit d60f77b93b88df6936d78ff2cec12979d4637294
Author: asankasa <as...@wso2.com>
Date:   2014-10-09T14:38:54Z

    work on 9 oct

commit b43fe3effba4c48f502bb629a1ccafbcb48e5363
Author: asankasa <as...@wso2.com>
Date:   2014-10-13T05:04:52Z

    changes 10th oct

commit c43b824e2ce9392b3c28e11ec46fdfda3b69eb2d
Author: asankasa <as...@wso2.com>
Date:   2014-10-13T05:09:02Z

    remove cricket files

commit 15b282f5c29e801bb33a93590128a4ba201b469b
Author: asankasa <as...@wso2.com>
Date:   2014-10-16T06:21:52Z

    untill oct 16

commit 3698437b7211b8960f2de2159c05a5442b4309df
Author: asankasa <as...@wso2.com>
Date:   2014-10-27T12:53:11Z

    works untill 27th oct--added public ip method and network ids

commit 7b911dbb7305b9fbe94138dc5c378581294dde40
Author: asankasa <as...@wso2.com>
Date:   2014-10-28T05:58:16Z

    reformated code

commit 087d9d5ef9933f6b8537f6c1256014fdfac17563
Author: asankasa <as...@wso2.com>
Date:   2014-10-28T06:07:11Z

    reformated code again

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by R-Rajkumar <gi...@git.apache.org>.
Github user R-Rajkumar commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r19654686
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.error(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("Zone has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    +                            + " with id: " + location.getId());
    +                    break;
    +                }
    +            }
    +        }
    +
    +        /**
    +         * PROPERTY-3
    +         * if user has specified an instance type in cloud-controller.xml, set the instance type into templateBuilder
    +         * object.(service offering)
    +         *Important:Specify the Service Offering type ID. Not the name. Because the name is not unique in cloudstack.
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
    +            templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    +        }
    +
    +        //build the template
    +        Template template = templateBuilder.build();
    +
    +        /**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?
    +         */
    +        boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo
    +                .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    +        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(new int[]{});
    +
    +
    +        //**SET CLOUDSTACK SPECIFIC PROPERTIES TO TEMPLATE OBJECT**//
    +
    +        //set security group - If you are using basic zone
    +        if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .securityGroupIds(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +
    +        /**
    +         * set network ID - If you are using advanced zone
    +         * in cloudstack sometimes we get unautorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .networks(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +        //set user name
    +        if (iaasInfo.getProperty(CloudControllerConstants.USER_NAME) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .account(iaasInfo.getProperty(CloudControllerConstants.USER_NAME));
    +        }
    +        //set domain ID
    +        if (iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .domainId(iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID));
    +        }
    +
    +        /**
    +         *Set key pair
    +         * in cloudstack sometimes we get unauthorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    +        }
    +
    +        // ability to define tags
    +        if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .tags(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.TAGS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +        //set disk offering to the instance
    +        if (iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .diskOfferingId(iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING));
    +        }
    +
    +        // set Template
    +        iaasInfo.setTemplate(template);
    +    }
    +
    +    @Override
    +    public void setDynamicPayload() {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        if (iaasInfo.getTemplate() != null && iaasInfo.getPayload() != null) {
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .userMetadata(convertByteArrayToHashMap(iaasInfo.getPayload()));
    +        }
    +    }
    +
    +    /**
    +     * IMPORTANT
    +     * In cloudstack we can assign public IPs, if we are using an advanced zone only. If we are using a basic zone
    +     * we cannot assign public ips.
    +     * <p/>
    +     * When we use an advanced zone, a public IP address will get automatically assigned to the vm. So we don't need
    +     * to find an unallocated IP address and assign that address to the vm (Not like in ec2 and openstack).
    +     * <p/>
    +     * So  this method will find the IP that has been assigned to the vm and return it.
    +     */
    +    @Override
    +    public String associateAddress(NodeMetadata node) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        String ip = null;
    +
    +        // get all allocated IPs
    +        ListPublicIPAddressesOptions listPublicIPAddressesOptions = new ListPublicIPAddressesOptions();
    +        listPublicIPAddressesOptions.zoneId(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    +
    +        Set<PublicIPAddress> publicIPAddresses = cloudStackApi.getAddressApi()
    +                .listPublicIPAddresses(listPublicIPAddressesOptions);
    +
    +        String id = node.getProviderId(); //vm ID
    +
    +        for (PublicIPAddress publicIPAddress : publicIPAddresses) {
    +            if (publicIPAddress.getVirtualMachineId().equals(id)) { //check whether this instance has
    +                // already got an public ip or not
    +                ip = publicIPAddress.getIPAddress(); //A public ip has been successfully assigned to the vm
    +                log.debug("Successfully associated an IP address " + ip
    +                        + " for node with id: " + node.getId());
    +                break;
    +            }
    +
    +        }
    +
    +        if (ip == null || ip.isEmpty()) { //IP has not been successfully assigned to VM(That means there are
    +            //  no more IPs  available for the VM)
    +            String msg = "No address associated for node with id: " + node.getId();
    +            log.debug(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        return ip;
    +    }
    +
    +    @Override
    +    public String associatePredefinedAddress(NodeMetadata node, String ip) {
    +        return "";
    +    }
    +
    +    @Override
    +    public void releaseAddress(String ip) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        cloudStackApi.getAddressApi().disassociateIPAddress(ip);
    +    }
    +
    +    @Override
    +    public boolean createKeyPairFromPublicKey(String region, String keyPairName, String publicKey) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        SshKeyPair sshKeyPair = cloudStackApi.getSSHKeyPairApi().createSSHKeyPair(keyPairName);
    +
    +        if (sshKeyPair != null) {
    +
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(sshKeyPair.getName());
    +
    +            log.info("A key-pair is created successfully - Key Pair Name: " + sshKeyPair.getName());
    +            return true;
    +        }
    +        log.error("Key-pair is unable to create");
    +        return false;
    +    }
    +
    +    @Override
    +    public boolean isValidRegion(String region) throws InvalidRegionException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        //no such method in Jclouds cloudstack api
    +        String msg = "Invalid region: " + region + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidRegionException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidZone(String region, String zone) throws InvalidZoneException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        ListZonesOptions listZonesOptions = new ListZonesOptions();
    +        listZonesOptions.available(true);
    +        Set<Zone> zoneSet = cloudStackApi.getZoneApi().listZones(listZonesOptions);
    +
    +        for (org.jclouds.cloudstack.domain.Zone configuredZone : zoneSet) {
    +            if (configuredZone.getName().equalsIgnoreCase(zone)) {
    +                return true;
    +            }
    +        }
    +        String msg = "Invalid zone: " + zone + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidZoneException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidHost(String zone, String host) throws InvalidHostException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        // there's no such method in jclouds cloustack api
    +        String msg = "Invalid host: " + host + " in the zone: " + zone + " and of the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidHostException(msg);
    +
    +    }
    +
    +    @Override
    +    public PartitionValidator getPartitionValidator() {
    +        return new CloudstackPartitionValidator();
    +    }
    +
    +    @Override
    +    public String createVolume(int sizeGB, String snapshotId) {
    +
    +        //todo return volume ID if volume is created
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +
    +        String zone = ComputeServiceBuilderUtil.extractZone(iaasInfo);
    +        String diskOfferingID = iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                .getDiskOfferingId();
    +        if (zone == null && diskOfferingID == null) {
    +            log.fatal("Cannot create a new volume in the , [zone] : " + zone + " of Iaas : " + iaasInfo);
    +            return null;
    +        }
    +
    +        VolumeApi volumeApi = context.unwrapApi(CloudStackApi.class).getVolumeApi();
    +
    +        Volume volume;
    +        if (StringUtils.isEmpty(snapshotId)) {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone);
    +            }
    +
    +            //cloudstack jcloud api does not return a volume object
    +            volumeApi.createVolumeFromCustomDiskOfferingInZone(null, diskOfferingID, zone, sizeGB);
    +
    +            //  volume = blockStoreApi.createVolumeInAvailabilityZone(zone, sizeGB);
    +        } else {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone + " from the snapshot " + snapshotId);
    +            }
    +            volumeApi.createVolumeFromSnapshotInZone(null, diskOfferingID, zone);
    +        }
    +
    +        return null;
    +    }
    +
    +    @Override
    +    public String attachVolume(String instanceId, String volumeId, String deviceName) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +
    +        //get volume
    +        org.jclouds.cloudstack.domain.Volume volume = cloudStackApi.getVolumeApi().getVolume(volumeId);
    +
    +        //get current volume state
    +        Volume.State volumeState = volume.getState();
    +
    +        if (log.isDebugEnabled()) {
    +            log.debug("Volume " + volumeId + " is in state " + volumeState);
    +        }
    +
    +        //if volume is not available, not allocated or cannot use
    +        //volume state ALLOCATED   means that volume has not been attached to any instance.
    +
    +        //TODO there is an error with logic.
    +        if (!(volumeState == Volume.State.ALLOCATED || volumeState == Volume.State.CREATING
    +                || volumeState == Volume.State.READY)) {
    +            log.error(String.format("Volume %s can not be attached. Volume status is %s", volumeId, volumeState));
    +        }
    +
    +        //check whether the account of volume and instance is same
    +        if (!volume.getAccount().equals(cloudStackApi.getVirtualMachineApi()
    +                .getVirtualMachine(instanceId).getAccount())) {
    +            log.error(String.format("Volume %s can not be attached. Instance account and Volume account " +
    +                    "are not the same ", volumeId));
    +        }
    +
    +        boolean volumeBecameAvailable = false, volumeBecameAttached = false;
    +
    +        try {
    +            if (volumeState == Volume.State.CREATING) {
    +
    +                volumeBecameAvailable = waitForStatus(volumeId, Volume.State.ALLOCATED, 5);
    +
    +            } else if (volumeState == Volume.State.READY) {
    +                volumeBecameAvailable = true;
    +            }
    +
    +        } catch (TimeoutException e) {
    +            log.error("[Volume ID] " + volumeId + "did not become ALLOCATED within expected timeout");
    +        }
    +
    +        //if volume state is 'ALLOCATED'
    +        if (volumeBecameAvailable) {
    +
    +            //attach volume into instance
    +            cloudStackApi.getVolumeApi().attachVolume(volumeId, instanceId);
    +
    +            try {
    +                volumeBecameAttached = waitForStatus(volumeId, Volume.State.READY, 2);
    +            } catch (TimeoutException e) {
    +                log.error("[Volume ID] " + volumeId + "did not become READY within expected timeout");
    +            }
    +        }
    +
    +        try {
    +            // waiting 5seconds till volumes are actually attached.
    +            Thread.sleep(5000);
    +        } catch (InterruptedException e) {
    +            e.printStackTrace();
    --- End diff --
    
    I think we don't print the stack trace. We can just ignore this exception right?
    try{
          //
    } catch (InterruptedException ignored) {
    }


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r20060547
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.error(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("Zone has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    +                            + " with id: " + location.getId());
    +                    break;
    +                }
    +            }
    +        }
    +
    +        /**
    +         * PROPERTY-3
    +         * if user has specified an instance type in cloud-controller.xml, set the instance type into templateBuilder
    +         * object.(service offering)
    +         *Important:Specify the Service Offering type ID. Not the name. Because the name is not unique in cloudstack.
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
    +            templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    +        }
    +
    +        //build the template
    +        Template template = templateBuilder.build();
    +
    +        /**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?
    +         */
    +        boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo
    +                .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    +        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(new int[]{});
    +
    +
    +        //**SET CLOUDSTACK SPECIFIC PROPERTIES TO TEMPLATE OBJECT**//
    +
    +        //set security group - If you are using basic zone
    +        if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .securityGroupIds(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +
    +        /**
    +         * set network ID - If you are using advanced zone
    +         * in cloudstack sometimes we get unautorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .networks(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +        //set user name
    +        if (iaasInfo.getProperty(CloudControllerConstants.USER_NAME) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .account(iaasInfo.getProperty(CloudControllerConstants.USER_NAME));
    +        }
    +        //set domain ID
    +        if (iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .domainId(iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID));
    +        }
    +
    +        /**
    +         *Set key pair
    +         * in cloudstack sometimes we get unauthorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    +        }
    +
    +        // ability to define tags
    +        if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .tags(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.TAGS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +        //set disk offering to the instance
    +        if (iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .diskOfferingId(iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING));
    +        }
    +
    +        // set Template
    +        iaasInfo.setTemplate(template);
    +    }
    +
    +    @Override
    +    public void setDynamicPayload() {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        if (iaasInfo.getTemplate() != null && iaasInfo.getPayload() != null) {
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .userMetadata(convertByteArrayToHashMap(iaasInfo.getPayload()));
    +        }
    +    }
    +
    +    /**
    +     * IMPORTANT
    +     * In cloudstack we can assign public IPs, if we are using an advanced zone only. If we are using a basic zone
    +     * we cannot assign public ips.
    +     * <p/>
    +     * When we use an advanced zone, a public IP address will get automatically assigned to the vm. So we don't need
    +     * to find an unallocated IP address and assign that address to the vm (Not like in ec2 and openstack).
    +     * <p/>
    +     * So  this method will find the IP that has been assigned to the vm and return it.
    +     */
    +    @Override
    +    public String associateAddress(NodeMetadata node) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        String ip = null;
    +
    +        // get all allocated IPs
    +        ListPublicIPAddressesOptions listPublicIPAddressesOptions = new ListPublicIPAddressesOptions();
    +        listPublicIPAddressesOptions.zoneId(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    +
    +        Set<PublicIPAddress> publicIPAddresses = cloudStackApi.getAddressApi()
    +                .listPublicIPAddresses(listPublicIPAddressesOptions);
    +
    +        String id = node.getProviderId(); //vm ID
    +
    +        for (PublicIPAddress publicIPAddress : publicIPAddresses) {
    +            if (publicIPAddress.getVirtualMachineId().equals(id)) { //check whether this instance has
    +                // already got an public ip or not
    +                ip = publicIPAddress.getIPAddress(); //A public ip has been successfully assigned to the vm
    +                log.debug("Successfully associated an IP address " + ip
    +                        + " for node with id: " + node.getId());
    +                break;
    +            }
    +
    +        }
    +
    +        if (ip == null || ip.isEmpty()) { //IP has not been successfully assigned to VM(That means there are
    +            //  no more IPs  available for the VM)
    +            String msg = "No address associated for node with id: " + node.getId();
    +            log.debug(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        return ip;
    +    }
    +
    +    @Override
    +    public String associatePredefinedAddress(NodeMetadata node, String ip) {
    +        return "";
    +    }
    +
    +    @Override
    +    public void releaseAddress(String ip) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        cloudStackApi.getAddressApi().disassociateIPAddress(ip);
    +    }
    +
    +    @Override
    +    public boolean createKeyPairFromPublicKey(String region, String keyPairName, String publicKey) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        SshKeyPair sshKeyPair = cloudStackApi.getSSHKeyPairApi().createSSHKeyPair(keyPairName);
    +
    +        if (sshKeyPair != null) {
    +
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(sshKeyPair.getName());
    +
    +            log.info("A key-pair is created successfully - Key Pair Name: " + sshKeyPair.getName());
    +            return true;
    +        }
    +        log.error("Key-pair is unable to create");
    +        return false;
    +    }
    +
    +    @Override
    +    public boolean isValidRegion(String region) throws InvalidRegionException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        //no such method in Jclouds cloudstack api
    +        String msg = "Invalid region: " + region + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidRegionException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidZone(String region, String zone) throws InvalidZoneException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        ListZonesOptions listZonesOptions = new ListZonesOptions();
    +        listZonesOptions.available(true);
    +        Set<Zone> zoneSet = cloudStackApi.getZoneApi().listZones(listZonesOptions);
    +
    +        for (org.jclouds.cloudstack.domain.Zone configuredZone : zoneSet) {
    +            if (configuredZone.getName().equalsIgnoreCase(zone)) {
    +                return true;
    +            }
    +        }
    +        String msg = "Invalid zone: " + zone + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidZoneException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidHost(String zone, String host) throws InvalidHostException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        // there's no such method in jclouds cloustack api
    +        String msg = "Invalid host: " + host + " in the zone: " + zone + " and of the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidHostException(msg);
    +
    +    }
    +
    +    @Override
    +    public PartitionValidator getPartitionValidator() {
    +        return new CloudstackPartitionValidator();
    +    }
    +
    +    @Override
    +    public String createVolume(int sizeGB, String snapshotId) {
    +
    +        //todo return volume ID if volume is created
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +
    +        String zone = ComputeServiceBuilderUtil.extractZone(iaasInfo);
    +        String diskOfferingID = iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                .getDiskOfferingId();
    +        if (zone == null && diskOfferingID == null) {
    +            log.fatal("Cannot create a new volume in the , [zone] : " + zone + " of Iaas : " + iaasInfo);
    +            return null;
    +        }
    +
    +        VolumeApi volumeApi = context.unwrapApi(CloudStackApi.class).getVolumeApi();
    +
    +        Volume volume;
    +        if (StringUtils.isEmpty(snapshotId)) {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone);
    +            }
    +
    +            //cloudstack jcloud api does not return a volume object
    +            volumeApi.createVolumeFromCustomDiskOfferingInZone(null, diskOfferingID, zone, sizeGB);
    +
    +            //  volume = blockStoreApi.createVolumeInAvailabilityZone(zone, sizeGB);
    +        } else {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone + " from the snapshot " + snapshotId);
    +            }
    +            volumeApi.createVolumeFromSnapshotInZone(null, diskOfferingID, zone);
    +        }
    +
    +        return null;
    +    }
    +
    +    @Override
    +    public String attachVolume(String instanceId, String volumeId, String deviceName) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +
    +        //get volume
    +        org.jclouds.cloudstack.domain.Volume volume = cloudStackApi.getVolumeApi().getVolume(volumeId);
    +
    +        //get current volume state
    +        Volume.State volumeState = volume.getState();
    +
    +        if (log.isDebugEnabled()) {
    +            log.debug("Volume " + volumeId + " is in state " + volumeState);
    +        }
    +
    +        //if volume is not available, not allocated or cannot use
    +        //volume state ALLOCATED   means that volume has not been attached to any instance.
    +
    +        //TODO there is an error with logic.
    +        if (!(volumeState == Volume.State.ALLOCATED || volumeState == Volume.State.CREATING
    +                || volumeState == Volume.State.READY)) {
    +            log.error(String.format("Volume %s can not be attached. Volume status is %s", volumeId, volumeState));
    +        }
    +
    +        //check whether the account of volume and instance is same
    +        if (!volume.getAccount().equals(cloudStackApi.getVirtualMachineApi()
    +                .getVirtualMachine(instanceId).getAccount())) {
    +            log.error(String.format("Volume %s can not be attached. Instance account and Volume account " +
    +                    "are not the same ", volumeId));
    +        }
    +
    +        boolean volumeBecameAvailable = false, volumeBecameAttached = false;
    +
    +        try {
    +            if (volumeState == Volume.State.CREATING) {
    +
    +                volumeBecameAvailable = waitForStatus(volumeId, Volume.State.ALLOCATED, 5);
    +
    +            } else if (volumeState == Volume.State.READY) {
    +                volumeBecameAvailable = true;
    +            }
    +
    +        } catch (TimeoutException e) {
    +            log.error("[Volume ID] " + volumeId + "did not become ALLOCATED within expected timeout");
    +        }
    +
    +        //if volume state is 'ALLOCATED'
    +        if (volumeBecameAvailable) {
    +
    +            //attach volume into instance
    +            cloudStackApi.getVolumeApi().attachVolume(volumeId, instanceId);
    +
    +            try {
    +                volumeBecameAttached = waitForStatus(volumeId, Volume.State.READY, 2);
    +            } catch (TimeoutException e) {
    +                log.error("[Volume ID] " + volumeId + "did not become READY within expected timeout");
    +            }
    +        }
    +
    +        try {
    +            // waiting 5seconds till volumes are actually attached.
    +            Thread.sleep(5000);
    +        } catch (InterruptedException e) {
    +            e.printStackTrace();
    --- End diff --
    
    Yes please remove the stack trace.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Re: [GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by Imesh Gunaratne <im...@apache.org>.
Thanks Asanka!

On Mon, Nov 10, 2014 at 7:21 PM, Asanka sanjaya Herath <an...@gmail.com>
wrote:

> Hi Imesh,
>
> I will correct those and update the code soon.
>
> Thank you.
>
> On Mon, Nov 10, 2014 at 12:27 AM, imesh <gi...@git.apache.org> wrote:
>
>> Github user imesh commented on the pull request:
>>
>>     https://github.com/apache/stratos/pull/97#issuecomment-62314458
>>
>>     @asankasanjaya We have identified several minor issues, appreciate if
>> you could correct them and re-submit the PR. I also noticed that this PR
>> now conflicts with the latest codebase in the master branch. Can you please
>> verify it and resolve the conflicts?
>>
>>     Thanks
>>
>>
>> ---
>> If your project is set up for it, you can reply to this email and have
>> your
>> reply appear on GitHub as well. If your project does not have this feature
>> enabled and wishes so, or if the feature is enabled but not working,
>> please
>> contact infrastructure at infrastructure@apache.org or file a JIRA ticket
>> with INFRA.
>> ---
>>
>
>
>
> --
> Thanks,
> Regards,
> ASH
>



-- 
Imesh Gunaratne

Technical Lead, WSO2
Committer & PMC Member, Apache Stratos

Re: [GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by Asanka sanjaya Herath <an...@gmail.com>.
Hi Imesh,

I will correct those and update the code soon.

Thank you.

On Mon, Nov 10, 2014 at 12:27 AM, imesh <gi...@git.apache.org> wrote:

> Github user imesh commented on the pull request:
>
>     https://github.com/apache/stratos/pull/97#issuecomment-62314458
>
>     @asankasanjaya We have identified several minor issues, appreciate if
> you could correct them and re-submit the PR. I also noticed that this PR
> now conflicts with the latest codebase in the master branch. Can you please
> verify it and resolve the conflicts?
>
>     Thanks
>
>
> ---
> If your project is set up for it, you can reply to this email and have your
> reply appear on GitHub as well. If your project does not have this feature
> enabled and wishes so, or if the feature is enabled but not working, please
> contact infrastructure at infrastructure@apache.org or file a JIRA ticket
> with INFRA.
> ---
>



-- 
Thanks,
Regards,
ASH

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on the pull request:

    https://github.com/apache/stratos/pull/97#issuecomment-62314458
  
    @asankasanjaya We have identified several minor issues, appreciate if you could correct them and re-submit the PR. I also noticed that this PR now conflicts with the latest codebase in the master branch. Can you please verify it and resolve the conflicts?
    
    Thanks


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by nirmal070125 <gi...@git.apache.org>.
Github user nirmal070125 commented on the pull request:

    https://github.com/apache/stratos/pull/97#issuecomment-60723928
  
    I'd like to review this PR. Await my feedback.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by asankasanjaya <gi...@git.apache.org>.
Github user asankasanjaya closed the pull request at:

    https://github.com/apache/stratos/pull/97


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by R-Rajkumar <gi...@git.apache.org>.
Github user R-Rajkumar commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r19654378
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/util/CloudControllerConstants.java ---
    @@ -97,6 +97,12 @@
     	public static final String CLOUD_CONTROLLER_COL_FAMILY = CLOUD_CONTROLLER_EVENT_STREAM
     			.replaceAll("[/.]", "_");
     
    +    //cloudstack specific
    +    public static final String USER_NAME = "userName";
    --- End diff --
    
    Can we use username instead of userName?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by R-Rajkumar <gi...@git.apache.org>.
Github user R-Rajkumar commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r19654970
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.error(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("Zone has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    +                            + " with id: " + location.getId());
    +                    break;
    +                }
    +            }
    +        }
    +
    +        /**
    +         * PROPERTY-3
    +         * if user has specified an instance type in cloud-controller.xml, set the instance type into templateBuilder
    +         * object.(service offering)
    +         *Important:Specify the Service Offering type ID. Not the name. Because the name is not unique in cloudstack.
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
    +            templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    +        }
    +
    +        //build the template
    +        Template template = templateBuilder.build();
    +
    +        /**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?
    +         */
    +        boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo
    +                .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    +        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(new int[]{});
    +
    +
    +        //**SET CLOUDSTACK SPECIFIC PROPERTIES TO TEMPLATE OBJECT**//
    +
    +        //set security group - If you are using basic zone
    +        if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .securityGroupIds(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +
    +        /**
    +         * set network ID - If you are using advanced zone
    +         * in cloudstack sometimes we get unautorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .networks(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +        //set user name
    +        if (iaasInfo.getProperty(CloudControllerConstants.USER_NAME) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .account(iaasInfo.getProperty(CloudControllerConstants.USER_NAME));
    +        }
    +        //set domain ID
    +        if (iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .domainId(iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID));
    +        }
    +
    +        /**
    +         *Set key pair
    +         * in cloudstack sometimes we get unauthorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    +        }
    +
    +        // ability to define tags
    +        if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .tags(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.TAGS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +        //set disk offering to the instance
    +        if (iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .diskOfferingId(iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING));
    +        }
    +
    +        // set Template
    +        iaasInfo.setTemplate(template);
    +    }
    +
    +    @Override
    +    public void setDynamicPayload() {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        if (iaasInfo.getTemplate() != null && iaasInfo.getPayload() != null) {
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .userMetadata(convertByteArrayToHashMap(iaasInfo.getPayload()));
    +        }
    +    }
    +
    +    /**
    +     * IMPORTANT
    +     * In cloudstack we can assign public IPs, if we are using an advanced zone only. If we are using a basic zone
    +     * we cannot assign public ips.
    +     * <p/>
    +     * When we use an advanced zone, a public IP address will get automatically assigned to the vm. So we don't need
    +     * to find an unallocated IP address and assign that address to the vm (Not like in ec2 and openstack).
    +     * <p/>
    +     * So  this method will find the IP that has been assigned to the vm and return it.
    +     */
    +    @Override
    +    public String associateAddress(NodeMetadata node) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        String ip = null;
    +
    +        // get all allocated IPs
    +        ListPublicIPAddressesOptions listPublicIPAddressesOptions = new ListPublicIPAddressesOptions();
    +        listPublicIPAddressesOptions.zoneId(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    +
    +        Set<PublicIPAddress> publicIPAddresses = cloudStackApi.getAddressApi()
    +                .listPublicIPAddresses(listPublicIPAddressesOptions);
    +
    +        String id = node.getProviderId(); //vm ID
    +
    +        for (PublicIPAddress publicIPAddress : publicIPAddresses) {
    +            if (publicIPAddress.getVirtualMachineId().equals(id)) { //check whether this instance has
    +                // already got an public ip or not
    +                ip = publicIPAddress.getIPAddress(); //A public ip has been successfully assigned to the vm
    +                log.debug("Successfully associated an IP address " + ip
    +                        + " for node with id: " + node.getId());
    +                break;
    +            }
    +
    +        }
    +
    +        if (ip == null || ip.isEmpty()) { //IP has not been successfully assigned to VM(That means there are
    +            //  no more IPs  available for the VM)
    +            String msg = "No address associated for node with id: " + node.getId();
    +            log.debug(msg);
    --- End diff --
    
    shouldn't it be error log?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on the pull request:

    https://github.com/apache/stratos/pull/97#issuecomment-62843947
  
    Thanks @asankasanjaya for your quick response. We have now merged your changes to the master branch. Please close this PR.
    
    Thanks


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r20060533
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.error(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("Zone has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    +                            + " with id: " + location.getId());
    +                    break;
    +                }
    +            }
    +        }
    +
    +        /**
    +         * PROPERTY-3
    +         * if user has specified an instance type in cloud-controller.xml, set the instance type into templateBuilder
    +         * object.(service offering)
    +         *Important:Specify the Service Offering type ID. Not the name. Because the name is not unique in cloudstack.
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
    +            templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    +        }
    +
    +        //build the template
    +        Template template = templateBuilder.build();
    +
    +        /**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?
    +         */
    +        boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo
    +                .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    +        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(new int[]{});
    +
    +
    +        //**SET CLOUDSTACK SPECIFIC PROPERTIES TO TEMPLATE OBJECT**//
    +
    +        //set security group - If you are using basic zone
    +        if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .securityGroupIds(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +
    +        /**
    +         * set network ID - If you are using advanced zone
    +         * in cloudstack sometimes we get unautorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .networks(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +        //set user name
    +        if (iaasInfo.getProperty(CloudControllerConstants.USER_NAME) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .account(iaasInfo.getProperty(CloudControllerConstants.USER_NAME));
    +        }
    +        //set domain ID
    +        if (iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .domainId(iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID));
    +        }
    +
    +        /**
    +         *Set key pair
    +         * in cloudstack sometimes we get unauthorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    +        }
    +
    +        // ability to define tags
    +        if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .tags(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.TAGS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +        //set disk offering to the instance
    +        if (iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .diskOfferingId(iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING));
    +        }
    +
    +        // set Template
    +        iaasInfo.setTemplate(template);
    +    }
    +
    +    @Override
    +    public void setDynamicPayload() {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        if (iaasInfo.getTemplate() != null && iaasInfo.getPayload() != null) {
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .userMetadata(convertByteArrayToHashMap(iaasInfo.getPayload()));
    +        }
    +    }
    +
    +    /**
    +     * IMPORTANT
    +     * In cloudstack we can assign public IPs, if we are using an advanced zone only. If we are using a basic zone
    +     * we cannot assign public ips.
    +     * <p/>
    +     * When we use an advanced zone, a public IP address will get automatically assigned to the vm. So we don't need
    +     * to find an unallocated IP address and assign that address to the vm (Not like in ec2 and openstack).
    +     * <p/>
    +     * So  this method will find the IP that has been assigned to the vm and return it.
    +     */
    +    @Override
    +    public String associateAddress(NodeMetadata node) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        String ip = null;
    +
    +        // get all allocated IPs
    +        ListPublicIPAddressesOptions listPublicIPAddressesOptions = new ListPublicIPAddressesOptions();
    +        listPublicIPAddressesOptions.zoneId(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    +
    +        Set<PublicIPAddress> publicIPAddresses = cloudStackApi.getAddressApi()
    +                .listPublicIPAddresses(listPublicIPAddressesOptions);
    +
    +        String id = node.getProviderId(); //vm ID
    +
    +        for (PublicIPAddress publicIPAddress : publicIPAddresses) {
    +            if (publicIPAddress.getVirtualMachineId().equals(id)) { //check whether this instance has
    +                // already got an public ip or not
    +                ip = publicIPAddress.getIPAddress(); //A public ip has been successfully assigned to the vm
    +                log.debug("Successfully associated an IP address " + ip
    +                        + " for node with id: " + node.getId());
    +                break;
    +            }
    +
    +        }
    +
    +        if (ip == null || ip.isEmpty()) { //IP has not been successfully assigned to VM(That means there are
    +            //  no more IPs  available for the VM)
    +            String msg = "No address associated for node with id: " + node.getId();
    +            log.debug(msg);
    --- End diff --
    
    May be we could make this a warning. Asanka can you please update?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r20060545
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.error(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("Zone has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    +                            + " with id: " + location.getId());
    +                    break;
    +                }
    +            }
    +        }
    +
    +        /**
    +         * PROPERTY-3
    +         * if user has specified an instance type in cloud-controller.xml, set the instance type into templateBuilder
    +         * object.(service offering)
    +         *Important:Specify the Service Offering type ID. Not the name. Because the name is not unique in cloudstack.
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
    +            templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    +        }
    +
    +        //build the template
    +        Template template = templateBuilder.build();
    +
    +        /**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?
    +         */
    +        boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo
    +                .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    +        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(new int[]{});
    +
    +
    +        //**SET CLOUDSTACK SPECIFIC PROPERTIES TO TEMPLATE OBJECT**//
    +
    +        //set security group - If you are using basic zone
    +        if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .securityGroupIds(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +
    +        /**
    +         * set network ID - If you are using advanced zone
    +         * in cloudstack sometimes we get unautorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .networks(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +        //set user name
    +        if (iaasInfo.getProperty(CloudControllerConstants.USER_NAME) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .account(iaasInfo.getProperty(CloudControllerConstants.USER_NAME));
    +        }
    +        //set domain ID
    +        if (iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .domainId(iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID));
    +        }
    +
    +        /**
    +         *Set key pair
    +         * in cloudstack sometimes we get unauthorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    +        }
    +
    +        // ability to define tags
    +        if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .tags(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.TAGS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +        //set disk offering to the instance
    +        if (iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .diskOfferingId(iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING));
    +        }
    +
    +        // set Template
    +        iaasInfo.setTemplate(template);
    +    }
    +
    +    @Override
    +    public void setDynamicPayload() {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        if (iaasInfo.getTemplate() != null && iaasInfo.getPayload() != null) {
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .userMetadata(convertByteArrayToHashMap(iaasInfo.getPayload()));
    +        }
    +    }
    +
    +    /**
    +     * IMPORTANT
    +     * In cloudstack we can assign public IPs, if we are using an advanced zone only. If we are using a basic zone
    +     * we cannot assign public ips.
    +     * <p/>
    +     * When we use an advanced zone, a public IP address will get automatically assigned to the vm. So we don't need
    +     * to find an unallocated IP address and assign that address to the vm (Not like in ec2 and openstack).
    +     * <p/>
    +     * So  this method will find the IP that has been assigned to the vm and return it.
    +     */
    +    @Override
    +    public String associateAddress(NodeMetadata node) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        String ip = null;
    +
    +        // get all allocated IPs
    +        ListPublicIPAddressesOptions listPublicIPAddressesOptions = new ListPublicIPAddressesOptions();
    +        listPublicIPAddressesOptions.zoneId(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    +
    +        Set<PublicIPAddress> publicIPAddresses = cloudStackApi.getAddressApi()
    +                .listPublicIPAddresses(listPublicIPAddressesOptions);
    +
    +        String id = node.getProviderId(); //vm ID
    +
    +        for (PublicIPAddress publicIPAddress : publicIPAddresses) {
    +            if (publicIPAddress.getVirtualMachineId().equals(id)) { //check whether this instance has
    +                // already got an public ip or not
    +                ip = publicIPAddress.getIPAddress(); //A public ip has been successfully assigned to the vm
    +                log.debug("Successfully associated an IP address " + ip
    +                        + " for node with id: " + node.getId());
    +                break;
    +            }
    +
    +        }
    +
    +        if (ip == null || ip.isEmpty()) { //IP has not been successfully assigned to VM(That means there are
    +            //  no more IPs  available for the VM)
    +            String msg = "No address associated for node with id: " + node.getId();
    +            log.debug(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        return ip;
    +    }
    +
    +    @Override
    +    public String associatePredefinedAddress(NodeMetadata node, String ip) {
    +        return "";
    +    }
    +
    +    @Override
    +    public void releaseAddress(String ip) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        cloudStackApi.getAddressApi().disassociateIPAddress(ip);
    +    }
    +
    +    @Override
    +    public boolean createKeyPairFromPublicKey(String region, String keyPairName, String publicKey) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        SshKeyPair sshKeyPair = cloudStackApi.getSSHKeyPairApi().createSSHKeyPair(keyPairName);
    +
    +        if (sshKeyPair != null) {
    +
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(sshKeyPair.getName());
    +
    +            log.info("A key-pair is created successfully - Key Pair Name: " + sshKeyPair.getName());
    +            return true;
    +        }
    +        log.error("Key-pair is unable to create");
    +        return false;
    +    }
    +
    +    @Override
    +    public boolean isValidRegion(String region) throws InvalidRegionException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        //no such method in Jclouds cloudstack api
    +        String msg = "Invalid region: " + region + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidRegionException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidZone(String region, String zone) throws InvalidZoneException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        ListZonesOptions listZonesOptions = new ListZonesOptions();
    +        listZonesOptions.available(true);
    +        Set<Zone> zoneSet = cloudStackApi.getZoneApi().listZones(listZonesOptions);
    +
    +        for (org.jclouds.cloudstack.domain.Zone configuredZone : zoneSet) {
    +            if (configuredZone.getName().equalsIgnoreCase(zone)) {
    +                return true;
    +            }
    +        }
    +        String msg = "Invalid zone: " + zone + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidZoneException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidHost(String zone, String host) throws InvalidHostException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        // there's no such method in jclouds cloustack api
    +        String msg = "Invalid host: " + host + " in the zone: " + zone + " and of the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidHostException(msg);
    +
    +    }
    +
    +    @Override
    +    public PartitionValidator getPartitionValidator() {
    +        return new CloudstackPartitionValidator();
    +    }
    +
    +    @Override
    +    public String createVolume(int sizeGB, String snapshotId) {
    +
    +        //todo return volume ID if volume is created
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +
    +        String zone = ComputeServiceBuilderUtil.extractZone(iaasInfo);
    +        String diskOfferingID = iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                .getDiskOfferingId();
    +        if (zone == null && diskOfferingID == null) {
    +            log.fatal("Cannot create a new volume in the , [zone] : " + zone + " of Iaas : " + iaasInfo);
    +            return null;
    +        }
    +
    +        VolumeApi volumeApi = context.unwrapApi(CloudStackApi.class).getVolumeApi();
    +
    +        Volume volume;
    +        if (StringUtils.isEmpty(snapshotId)) {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone);
    +            }
    +
    +            //cloudstack jcloud api does not return a volume object
    +            volumeApi.createVolumeFromCustomDiskOfferingInZone(null, diskOfferingID, zone, sizeGB);
    +
    +            //  volume = blockStoreApi.createVolumeInAvailabilityZone(zone, sizeGB);
    +        } else {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone + " from the snapshot " + snapshotId);
    --- End diff --
    
    Asanka can you please correct this? Will update the if condition to log.isInfoEnabled() and keep the log.info as it is.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r19653512
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.fatal(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("ZONE has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    --- End diff --
    
    A minor issue, shall we change the text "ZONE" to "Zone"?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by R-Rajkumar <gi...@git.apache.org>.
Github user R-Rajkumar commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r19655083
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.error(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("Zone has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    +                            + " with id: " + location.getId());
    +                    break;
    +                }
    +            }
    +        }
    +
    +        /**
    +         * PROPERTY-3
    +         * if user has specified an instance type in cloud-controller.xml, set the instance type into templateBuilder
    +         * object.(service offering)
    +         *Important:Specify the Service Offering type ID. Not the name. Because the name is not unique in cloudstack.
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
    +            templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    +        }
    +
    +        //build the template
    +        Template template = templateBuilder.build();
    +
    +        /**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?
    +         */
    +        boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo
    +                .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    +        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(new int[]{});
    +
    +
    +        //**SET CLOUDSTACK SPECIFIC PROPERTIES TO TEMPLATE OBJECT**//
    +
    +        //set security group - If you are using basic zone
    +        if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .securityGroupIds(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +
    +        /**
    +         * set network ID - If you are using advanced zone
    +         * in cloudstack sometimes we get unautorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .networks(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +        //set user name
    +        if (iaasInfo.getProperty(CloudControllerConstants.USER_NAME) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .account(iaasInfo.getProperty(CloudControllerConstants.USER_NAME));
    +        }
    +        //set domain ID
    +        if (iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .domainId(iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID));
    +        }
    +
    +        /**
    +         *Set key pair
    +         * in cloudstack sometimes we get unauthorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    +        }
    +
    +        // ability to define tags
    +        if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .tags(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.TAGS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +        //set disk offering to the instance
    +        if (iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .diskOfferingId(iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING));
    +        }
    +
    +        // set Template
    +        iaasInfo.setTemplate(template);
    +    }
    +
    +    @Override
    +    public void setDynamicPayload() {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        if (iaasInfo.getTemplate() != null && iaasInfo.getPayload() != null) {
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .userMetadata(convertByteArrayToHashMap(iaasInfo.getPayload()));
    +        }
    +    }
    +
    +    /**
    +     * IMPORTANT
    +     * In cloudstack we can assign public IPs, if we are using an advanced zone only. If we are using a basic zone
    +     * we cannot assign public ips.
    +     * <p/>
    +     * When we use an advanced zone, a public IP address will get automatically assigned to the vm. So we don't need
    +     * to find an unallocated IP address and assign that address to the vm (Not like in ec2 and openstack).
    +     * <p/>
    +     * So  this method will find the IP that has been assigned to the vm and return it.
    +     */
    +    @Override
    +    public String associateAddress(NodeMetadata node) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        String ip = null;
    +
    +        // get all allocated IPs
    +        ListPublicIPAddressesOptions listPublicIPAddressesOptions = new ListPublicIPAddressesOptions();
    +        listPublicIPAddressesOptions.zoneId(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    +
    +        Set<PublicIPAddress> publicIPAddresses = cloudStackApi.getAddressApi()
    +                .listPublicIPAddresses(listPublicIPAddressesOptions);
    +
    +        String id = node.getProviderId(); //vm ID
    +
    +        for (PublicIPAddress publicIPAddress : publicIPAddresses) {
    +            if (publicIPAddress.getVirtualMachineId().equals(id)) { //check whether this instance has
    +                // already got an public ip or not
    +                ip = publicIPAddress.getIPAddress(); //A public ip has been successfully assigned to the vm
    +                log.debug("Successfully associated an IP address " + ip
    +                        + " for node with id: " + node.getId());
    +                break;
    +            }
    +
    +        }
    +
    +        if (ip == null || ip.isEmpty()) { //IP has not been successfully assigned to VM(That means there are
    +            //  no more IPs  available for the VM)
    +            String msg = "No address associated for node with id: " + node.getId();
    +            log.debug(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        return ip;
    +    }
    +
    +    @Override
    +    public String associatePredefinedAddress(NodeMetadata node, String ip) {
    +        return "";
    +    }
    +
    +    @Override
    +    public void releaseAddress(String ip) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        cloudStackApi.getAddressApi().disassociateIPAddress(ip);
    +    }
    +
    +    @Override
    +    public boolean createKeyPairFromPublicKey(String region, String keyPairName, String publicKey) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        SshKeyPair sshKeyPair = cloudStackApi.getSSHKeyPairApi().createSSHKeyPair(keyPairName);
    +
    +        if (sshKeyPair != null) {
    +
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(sshKeyPair.getName());
    +
    +            log.info("A key-pair is created successfully - Key Pair Name: " + sshKeyPair.getName());
    +            return true;
    +        }
    +        log.error("Key-pair is unable to create");
    +        return false;
    +    }
    +
    +    @Override
    +    public boolean isValidRegion(String region) throws InvalidRegionException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        //no such method in Jclouds cloudstack api
    +        String msg = "Invalid region: " + region + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidRegionException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidZone(String region, String zone) throws InvalidZoneException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        ListZonesOptions listZonesOptions = new ListZonesOptions();
    +        listZonesOptions.available(true);
    +        Set<Zone> zoneSet = cloudStackApi.getZoneApi().listZones(listZonesOptions);
    +
    +        for (org.jclouds.cloudstack.domain.Zone configuredZone : zoneSet) {
    +            if (configuredZone.getName().equalsIgnoreCase(zone)) {
    +                return true;
    +            }
    +        }
    +        String msg = "Invalid zone: " + zone + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidZoneException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidHost(String zone, String host) throws InvalidHostException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        // there's no such method in jclouds cloustack api
    +        String msg = "Invalid host: " + host + " in the zone: " + zone + " and of the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidHostException(msg);
    +
    +    }
    +
    +    @Override
    +    public PartitionValidator getPartitionValidator() {
    +        return new CloudstackPartitionValidator();
    +    }
    +
    +    @Override
    +    public String createVolume(int sizeGB, String snapshotId) {
    +
    +        //todo return volume ID if volume is created
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +
    +        String zone = ComputeServiceBuilderUtil.extractZone(iaasInfo);
    +        String diskOfferingID = iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                .getDiskOfferingId();
    +        if (zone == null && diskOfferingID == null) {
    +            log.fatal("Cannot create a new volume in the , [zone] : " + zone + " of Iaas : " + iaasInfo);
    +            return null;
    +        }
    +
    +        VolumeApi volumeApi = context.unwrapApi(CloudStackApi.class).getVolumeApi();
    +
    +        Volume volume;
    +        if (StringUtils.isEmpty(snapshotId)) {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone);
    +            }
    +
    +            //cloudstack jcloud api does not return a volume object
    +            volumeApi.createVolumeFromCustomDiskOfferingInZone(null, diskOfferingID, zone, sizeGB);
    +
    +            //  volume = blockStoreApi.createVolumeInAvailabilityZone(zone, sizeGB);
    +        } else {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone + " from the snapshot " + snapshotId);
    --- End diff --
    
    Why doing a isDebugEnabled() to do an info log? :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by asankasanjaya <gi...@git.apache.org>.
Github user asankasanjaya commented on the pull request:

    https://github.com/apache/stratos/pull/97#issuecomment-60893834
  
    HI Imash,
    
    I fixed that issue and committed again.
    
     Thank you.
    
    On Wed, Oct 29, 2014 at 2:33 PM, Asanka Sanjaya <as...@wso2.com> wrote:
    
    > Without having that dependancy jclouds is not able to recognize cloudstack
    > as one of the
    > providers/apis. I followed this mail thread.
    >
    > http://mail-archives.apache.org/mod_mbox/stratos-dev/201406.mbox/%3CCA+-yGg=_kCXz0oh7kR7Zfu9a-ESheXecNaNh3ZfF+VQfF6HqiQ@mail.gmail.com%3E
    >
    > On Wed, Oct 29, 2014 at 1:58 PM, Imesh Guaratne <no...@github.com>
    > wrote:
    >
    >> Asanka: What was the reason for bringing in jclouds cloudstack API to
    >> dependencies? Did you fix any issues in jclouds?
    >>
    >> —
    >> Reply to this email directly or view it on GitHub
    >> <https://github.com/apache/stratos/pull/97#issuecomment-60888259>.
    >>
    >
    >


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r19653496
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.fatal(msg);
    --- End diff --
    
    In general we use log.fatal() in scenarios where the system cannot work due to some reason. Shall we change this to log.error()?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r20060557
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.error(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("Zone has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    +                            + " with id: " + location.getId());
    +                    break;
    +                }
    +            }
    +        }
    +
    +        /**
    +         * PROPERTY-3
    +         * if user has specified an instance type in cloud-controller.xml, set the instance type into templateBuilder
    +         * object.(service offering)
    +         *Important:Specify the Service Offering type ID. Not the name. Because the name is not unique in cloudstack.
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
    +            templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    +        }
    +
    +        //build the template
    +        Template template = templateBuilder.build();
    +
    +        /**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?
    +         */
    +        boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo
    +                .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    +        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(new int[]{});
    +
    +
    +        //**SET CLOUDSTACK SPECIFIC PROPERTIES TO TEMPLATE OBJECT**//
    +
    +        //set security group - If you are using basic zone
    +        if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .securityGroupIds(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +
    +        /**
    +         * set network ID - If you are using advanced zone
    +         * in cloudstack sometimes we get unautorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .networks(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +        //set user name
    +        if (iaasInfo.getProperty(CloudControllerConstants.USER_NAME) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .account(iaasInfo.getProperty(CloudControllerConstants.USER_NAME));
    +        }
    +        //set domain ID
    +        if (iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .domainId(iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID));
    +        }
    +
    +        /**
    +         *Set key pair
    +         * in cloudstack sometimes we get unauthorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    +        }
    +
    +        // ability to define tags
    +        if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .tags(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.TAGS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +        //set disk offering to the instance
    +        if (iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .diskOfferingId(iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING));
    +        }
    +
    +        // set Template
    +        iaasInfo.setTemplate(template);
    +    }
    +
    +    @Override
    +    public void setDynamicPayload() {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        if (iaasInfo.getTemplate() != null && iaasInfo.getPayload() != null) {
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .userMetadata(convertByteArrayToHashMap(iaasInfo.getPayload()));
    +        }
    +    }
    +
    +    /**
    +     * IMPORTANT
    +     * In cloudstack we can assign public IPs, if we are using an advanced zone only. If we are using a basic zone
    +     * we cannot assign public ips.
    +     * <p/>
    +     * When we use an advanced zone, a public IP address will get automatically assigned to the vm. So we don't need
    +     * to find an unallocated IP address and assign that address to the vm (Not like in ec2 and openstack).
    +     * <p/>
    +     * So  this method will find the IP that has been assigned to the vm and return it.
    +     */
    +    @Override
    +    public String associateAddress(NodeMetadata node) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        String ip = null;
    +
    +        // get all allocated IPs
    +        ListPublicIPAddressesOptions listPublicIPAddressesOptions = new ListPublicIPAddressesOptions();
    +        listPublicIPAddressesOptions.zoneId(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    +
    +        Set<PublicIPAddress> publicIPAddresses = cloudStackApi.getAddressApi()
    +                .listPublicIPAddresses(listPublicIPAddressesOptions);
    +
    +        String id = node.getProviderId(); //vm ID
    +
    +        for (PublicIPAddress publicIPAddress : publicIPAddresses) {
    +            if (publicIPAddress.getVirtualMachineId().equals(id)) { //check whether this instance has
    +                // already got an public ip or not
    +                ip = publicIPAddress.getIPAddress(); //A public ip has been successfully assigned to the vm
    +                log.debug("Successfully associated an IP address " + ip
    +                        + " for node with id: " + node.getId());
    +                break;
    +            }
    +
    +        }
    +
    +        if (ip == null || ip.isEmpty()) { //IP has not been successfully assigned to VM(That means there are
    +            //  no more IPs  available for the VM)
    +            String msg = "No address associated for node with id: " + node.getId();
    +            log.debug(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        return ip;
    +    }
    +
    +    @Override
    +    public String associatePredefinedAddress(NodeMetadata node, String ip) {
    +        return "";
    +    }
    +
    +    @Override
    +    public void releaseAddress(String ip) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        cloudStackApi.getAddressApi().disassociateIPAddress(ip);
    +    }
    +
    +    @Override
    +    public boolean createKeyPairFromPublicKey(String region, String keyPairName, String publicKey) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        SshKeyPair sshKeyPair = cloudStackApi.getSSHKeyPairApi().createSSHKeyPair(keyPairName);
    +
    +        if (sshKeyPair != null) {
    +
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(sshKeyPair.getName());
    +
    +            log.info("A key-pair is created successfully - Key Pair Name: " + sshKeyPair.getName());
    +            return true;
    +        }
    +        log.error("Key-pair is unable to create");
    +        return false;
    +    }
    +
    +    @Override
    +    public boolean isValidRegion(String region) throws InvalidRegionException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        //no such method in Jclouds cloudstack api
    +        String msg = "Invalid region: " + region + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidRegionException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidZone(String region, String zone) throws InvalidZoneException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        ListZonesOptions listZonesOptions = new ListZonesOptions();
    +        listZonesOptions.available(true);
    +        Set<Zone> zoneSet = cloudStackApi.getZoneApi().listZones(listZonesOptions);
    +
    +        for (org.jclouds.cloudstack.domain.Zone configuredZone : zoneSet) {
    +            if (configuredZone.getName().equalsIgnoreCase(zone)) {
    +                return true;
    +            }
    +        }
    +        String msg = "Invalid zone: " + zone + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidZoneException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidHost(String zone, String host) throws InvalidHostException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        // there's no such method in jclouds cloustack api
    +        String msg = "Invalid host: " + host + " in the zone: " + zone + " and of the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidHostException(msg);
    +
    +    }
    +
    +    @Override
    +    public PartitionValidator getPartitionValidator() {
    +        return new CloudstackPartitionValidator();
    +    }
    +
    +    @Override
    +    public String createVolume(int sizeGB, String snapshotId) {
    +
    +        //todo return volume ID if volume is created
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +
    +        String zone = ComputeServiceBuilderUtil.extractZone(iaasInfo);
    +        String diskOfferingID = iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                .getDiskOfferingId();
    +        if (zone == null && diskOfferingID == null) {
    +            log.fatal("Cannot create a new volume in the , [zone] : " + zone + " of Iaas : " + iaasInfo);
    --- End diff --
    
    We may need to change log.fatal() to log.error(). Asanka can you please update?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by asankasanjaya <gi...@git.apache.org>.
Github user asankasanjaya commented on the pull request:

    https://github.com/apache/stratos/pull/97#issuecomment-62967690
  
    Thank you Imesh. Closed the PR.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on the pull request:

    https://github.com/apache/stratos/pull/97#issuecomment-60715290
  
    Thanks Asanka for your contribution. Please add a description to this PR with the features you have implemented to support CloudStack. We will review this and merge once 4.1.0 M3 milestone is done.
    
    Thanks


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by asankasanjaya <gi...@git.apache.org>.
Github user asankasanjaya commented on the pull request:

    https://github.com/apache/stratos/pull/97#issuecomment-60891685
  
    Without having that dependancy jclouds is not able to recognize cloudstack
    as one of the
    providers/apis. I followed this mail thread.
    http://mail-archives.apache.org/mod_mbox/stratos-dev/201406.mbox/%3CCA+-yGg=_kCXz0oh7kR7Zfu9a-ESheXecNaNh3ZfF+VQfF6HqiQ@mail.gmail.com%3E
    
    On Wed, Oct 29, 2014 at 1:58 PM, Imesh Guaratne <no...@github.com>
    wrote:
    
    > Asanka: What was the reason for bringing in jclouds cloudstack API to
    > dependencies? Did you fix any issues in jclouds?
    >
    > —
    > Reply to this email directly or view it on GitHub
    > <https://github.com/apache/stratos/pull/97#issuecomment-60888259>.
    >


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on the pull request:

    https://github.com/apache/stratos/pull/97#issuecomment-60913153
  
    Great! Thanks Asanka! Yes the only reason why we have forked some of the jclouds APIs in Stratos is that we have fixed issues on those.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by asankasanjaya <gi...@git.apache.org>.
Github user asankasanjaya commented on the pull request:

    https://github.com/apache/stratos/pull/97#issuecomment-60719171
  
    Sample configuration:
    
    <iaasProvider type="cloudstack" name="cloudstack specific details">
                    	<className>org.apache.stratos.cloud.controller.iaases.CloudstackIaas</className>
                            <provider>cloudstack</provider>
                            <identity svns:secretAlias="cloud.controller.cloudstack.identity">api-key</identity>
                            <credential svns:secretAlias="cloud.controller.cloudstack.credential">secret-key</credential>
    			<property name="jclouds.endpoint" value="ip:port" />		
    			<property name="availabilityZone" value="zone_Id"/>
    			<property name="instanceType" value="instance_type_Id"/>
    			<property name="securityGroupIds" value="security_group_Id1,security_group_id2"/>
    			<property name="networkIds" value="network_Id1,network_Id2"/>
    			<property name="diskOffering" value="disk_offering_Id"/>
    			<property name="userName" value="username"/>
    			<property name="domainId" value="domainId"/>
    			<property name="keyPair" value="asanka-key"/>
    			<property name="tags" value="tag1,tag2,tag3" />
    			<property name="autoAssignIp" value="false" />
              	 </iaasProvider>


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by R-Rajkumar <gi...@git.apache.org>.
Github user R-Rajkumar commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r19655062
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.error(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("Zone has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    +                            + " with id: " + location.getId());
    +                    break;
    +                }
    +            }
    +        }
    +
    +        /**
    +         * PROPERTY-3
    +         * if user has specified an instance type in cloud-controller.xml, set the instance type into templateBuilder
    +         * object.(service offering)
    +         *Important:Specify the Service Offering type ID. Not the name. Because the name is not unique in cloudstack.
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
    +            templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    +        }
    +
    +        //build the template
    +        Template template = templateBuilder.build();
    +
    +        /**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?
    +         */
    +        boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo
    +                .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    +        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(new int[]{});
    +
    +
    +        //**SET CLOUDSTACK SPECIFIC PROPERTIES TO TEMPLATE OBJECT**//
    +
    +        //set security group - If you are using basic zone
    +        if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .securityGroupIds(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +
    +        /**
    +         * set network ID - If you are using advanced zone
    +         * in cloudstack sometimes we get unautorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .networks(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +        //set user name
    +        if (iaasInfo.getProperty(CloudControllerConstants.USER_NAME) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .account(iaasInfo.getProperty(CloudControllerConstants.USER_NAME));
    +        }
    +        //set domain ID
    +        if (iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .domainId(iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID));
    +        }
    +
    +        /**
    +         *Set key pair
    +         * in cloudstack sometimes we get unauthorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    +        }
    +
    +        // ability to define tags
    +        if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .tags(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.TAGS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +        //set disk offering to the instance
    +        if (iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .diskOfferingId(iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING));
    +        }
    +
    +        // set Template
    +        iaasInfo.setTemplate(template);
    +    }
    +
    +    @Override
    +    public void setDynamicPayload() {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        if (iaasInfo.getTemplate() != null && iaasInfo.getPayload() != null) {
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .userMetadata(convertByteArrayToHashMap(iaasInfo.getPayload()));
    +        }
    +    }
    +
    +    /**
    +     * IMPORTANT
    +     * In cloudstack we can assign public IPs, if we are using an advanced zone only. If we are using a basic zone
    +     * we cannot assign public ips.
    +     * <p/>
    +     * When we use an advanced zone, a public IP address will get automatically assigned to the vm. So we don't need
    +     * to find an unallocated IP address and assign that address to the vm (Not like in ec2 and openstack).
    +     * <p/>
    +     * So  this method will find the IP that has been assigned to the vm and return it.
    +     */
    +    @Override
    +    public String associateAddress(NodeMetadata node) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        String ip = null;
    +
    +        // get all allocated IPs
    +        ListPublicIPAddressesOptions listPublicIPAddressesOptions = new ListPublicIPAddressesOptions();
    +        listPublicIPAddressesOptions.zoneId(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    +
    +        Set<PublicIPAddress> publicIPAddresses = cloudStackApi.getAddressApi()
    +                .listPublicIPAddresses(listPublicIPAddressesOptions);
    +
    +        String id = node.getProviderId(); //vm ID
    +
    +        for (PublicIPAddress publicIPAddress : publicIPAddresses) {
    +            if (publicIPAddress.getVirtualMachineId().equals(id)) { //check whether this instance has
    +                // already got an public ip or not
    +                ip = publicIPAddress.getIPAddress(); //A public ip has been successfully assigned to the vm
    +                log.debug("Successfully associated an IP address " + ip
    +                        + " for node with id: " + node.getId());
    +                break;
    +            }
    +
    +        }
    +
    +        if (ip == null || ip.isEmpty()) { //IP has not been successfully assigned to VM(That means there are
    +            //  no more IPs  available for the VM)
    +            String msg = "No address associated for node with id: " + node.getId();
    +            log.debug(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        return ip;
    +    }
    +
    +    @Override
    +    public String associatePredefinedAddress(NodeMetadata node, String ip) {
    +        return "";
    +    }
    +
    +    @Override
    +    public void releaseAddress(String ip) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        cloudStackApi.getAddressApi().disassociateIPAddress(ip);
    +    }
    +
    +    @Override
    +    public boolean createKeyPairFromPublicKey(String region, String keyPairName, String publicKey) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        SshKeyPair sshKeyPair = cloudStackApi.getSSHKeyPairApi().createSSHKeyPair(keyPairName);
    +
    +        if (sshKeyPair != null) {
    +
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(sshKeyPair.getName());
    +
    +            log.info("A key-pair is created successfully - Key Pair Name: " + sshKeyPair.getName());
    +            return true;
    +        }
    +        log.error("Key-pair is unable to create");
    +        return false;
    +    }
    +
    +    @Override
    +    public boolean isValidRegion(String region) throws InvalidRegionException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        //no such method in Jclouds cloudstack api
    +        String msg = "Invalid region: " + region + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidRegionException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidZone(String region, String zone) throws InvalidZoneException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        ListZonesOptions listZonesOptions = new ListZonesOptions();
    +        listZonesOptions.available(true);
    +        Set<Zone> zoneSet = cloudStackApi.getZoneApi().listZones(listZonesOptions);
    +
    +        for (org.jclouds.cloudstack.domain.Zone configuredZone : zoneSet) {
    +            if (configuredZone.getName().equalsIgnoreCase(zone)) {
    +                return true;
    +            }
    +        }
    +        String msg = "Invalid zone: " + zone + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidZoneException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidHost(String zone, String host) throws InvalidHostException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        // there's no such method in jclouds cloustack api
    +        String msg = "Invalid host: " + host + " in the zone: " + zone + " and of the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidHostException(msg);
    +
    +    }
    +
    +    @Override
    +    public PartitionValidator getPartitionValidator() {
    +        return new CloudstackPartitionValidator();
    +    }
    +
    +    @Override
    +    public String createVolume(int sizeGB, String snapshotId) {
    +
    +        //todo return volume ID if volume is created
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +
    +        String zone = ComputeServiceBuilderUtil.extractZone(iaasInfo);
    +        String diskOfferingID = iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                .getDiskOfferingId();
    +        if (zone == null && diskOfferingID == null) {
    +            log.fatal("Cannot create a new volume in the , [zone] : " + zone + " of Iaas : " + iaasInfo);
    +            return null;
    +        }
    +
    +        VolumeApi volumeApi = context.unwrapApi(CloudStackApi.class).getVolumeApi();
    +
    +        Volume volume;
    +        if (StringUtils.isEmpty(snapshotId)) {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone);
    --- End diff --
    
    Why doing a isDebugEnabled() to do an info log? :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on the pull request:

    https://github.com/apache/stratos/pull/97#issuecomment-60888259
  
    Asanka: What was the reason for bringing in jclouds cloudstack API to dependencies? Did you fix any issues in jclouds?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by R-Rajkumar <gi...@git.apache.org>.
Github user R-Rajkumar commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r19655520
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.error(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("Zone has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    +                            + " with id: " + location.getId());
    +                    break;
    +                }
    +            }
    +        }
    +
    +        /**
    +         * PROPERTY-3
    +         * if user has specified an instance type in cloud-controller.xml, set the instance type into templateBuilder
    +         * object.(service offering)
    +         *Important:Specify the Service Offering type ID. Not the name. Because the name is not unique in cloudstack.
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
    +            templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    +        }
    +
    +        //build the template
    +        Template template = templateBuilder.build();
    +
    +        /**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?
    +         */
    +        boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo
    +                .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    +        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(new int[]{});
    +
    +
    +        //**SET CLOUDSTACK SPECIFIC PROPERTIES TO TEMPLATE OBJECT**//
    +
    +        //set security group - If you are using basic zone
    +        if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .securityGroupIds(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +
    +        /**
    +         * set network ID - If you are using advanced zone
    +         * in cloudstack sometimes we get unautorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .networks(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +        //set user name
    +        if (iaasInfo.getProperty(CloudControllerConstants.USER_NAME) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .account(iaasInfo.getProperty(CloudControllerConstants.USER_NAME));
    +        }
    +        //set domain ID
    +        if (iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .domainId(iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID));
    +        }
    +
    +        /**
    +         *Set key pair
    +         * in cloudstack sometimes we get unauthorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    +        }
    +
    +        // ability to define tags
    +        if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .tags(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.TAGS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +        //set disk offering to the instance
    +        if (iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .diskOfferingId(iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING));
    +        }
    +
    +        // set Template
    +        iaasInfo.setTemplate(template);
    +    }
    +
    +    @Override
    +    public void setDynamicPayload() {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        if (iaasInfo.getTemplate() != null && iaasInfo.getPayload() != null) {
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .userMetadata(convertByteArrayToHashMap(iaasInfo.getPayload()));
    +        }
    +    }
    +
    +    /**
    +     * IMPORTANT
    +     * In cloudstack we can assign public IPs, if we are using an advanced zone only. If we are using a basic zone
    +     * we cannot assign public ips.
    +     * <p/>
    +     * When we use an advanced zone, a public IP address will get automatically assigned to the vm. So we don't need
    +     * to find an unallocated IP address and assign that address to the vm (Not like in ec2 and openstack).
    +     * <p/>
    +     * So  this method will find the IP that has been assigned to the vm and return it.
    +     */
    +    @Override
    +    public String associateAddress(NodeMetadata node) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        String ip = null;
    +
    +        // get all allocated IPs
    +        ListPublicIPAddressesOptions listPublicIPAddressesOptions = new ListPublicIPAddressesOptions();
    +        listPublicIPAddressesOptions.zoneId(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    +
    +        Set<PublicIPAddress> publicIPAddresses = cloudStackApi.getAddressApi()
    +                .listPublicIPAddresses(listPublicIPAddressesOptions);
    +
    +        String id = node.getProviderId(); //vm ID
    +
    +        for (PublicIPAddress publicIPAddress : publicIPAddresses) {
    +            if (publicIPAddress.getVirtualMachineId().equals(id)) { //check whether this instance has
    +                // already got an public ip or not
    +                ip = publicIPAddress.getIPAddress(); //A public ip has been successfully assigned to the vm
    +                log.debug("Successfully associated an IP address " + ip
    +                        + " for node with id: " + node.getId());
    +                break;
    +            }
    +
    +        }
    +
    +        if (ip == null || ip.isEmpty()) { //IP has not been successfully assigned to VM(That means there are
    +            //  no more IPs  available for the VM)
    +            String msg = "No address associated for node with id: " + node.getId();
    +            log.debug(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        return ip;
    +    }
    +
    +    @Override
    +    public String associatePredefinedAddress(NodeMetadata node, String ip) {
    +        return "";
    +    }
    +
    +    @Override
    +    public void releaseAddress(String ip) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        cloudStackApi.getAddressApi().disassociateIPAddress(ip);
    +    }
    +
    +    @Override
    +    public boolean createKeyPairFromPublicKey(String region, String keyPairName, String publicKey) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        SshKeyPair sshKeyPair = cloudStackApi.getSSHKeyPairApi().createSSHKeyPair(keyPairName);
    +
    +        if (sshKeyPair != null) {
    +
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(sshKeyPair.getName());
    +
    +            log.info("A key-pair is created successfully - Key Pair Name: " + sshKeyPair.getName());
    +            return true;
    +        }
    +        log.error("Key-pair is unable to create");
    +        return false;
    +    }
    +
    +    @Override
    +    public boolean isValidRegion(String region) throws InvalidRegionException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        //no such method in Jclouds cloudstack api
    +        String msg = "Invalid region: " + region + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidRegionException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidZone(String region, String zone) throws InvalidZoneException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        ListZonesOptions listZonesOptions = new ListZonesOptions();
    +        listZonesOptions.available(true);
    +        Set<Zone> zoneSet = cloudStackApi.getZoneApi().listZones(listZonesOptions);
    +
    +        for (org.jclouds.cloudstack.domain.Zone configuredZone : zoneSet) {
    +            if (configuredZone.getName().equalsIgnoreCase(zone)) {
    +                return true;
    +            }
    +        }
    +        String msg = "Invalid zone: " + zone + " in the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidZoneException(msg);
    +    }
    +
    +    @Override
    +    public boolean isValidHost(String zone, String host) throws InvalidHostException {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        // there's no such method in jclouds cloustack api
    +        String msg = "Invalid host: " + host + " in the zone: " + zone + " and of the iaas: " + iaasInfo.getType();
    +        log.error(msg);
    +        throw new InvalidHostException(msg);
    +
    +    }
    +
    +    @Override
    +    public PartitionValidator getPartitionValidator() {
    +        return new CloudstackPartitionValidator();
    +    }
    +
    +    @Override
    +    public String createVolume(int sizeGB, String snapshotId) {
    +
    +        //todo return volume ID if volume is created
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +
    +        String zone = ComputeServiceBuilderUtil.extractZone(iaasInfo);
    +        String diskOfferingID = iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                .getDiskOfferingId();
    +        if (zone == null && diskOfferingID == null) {
    +            log.fatal("Cannot create a new volume in the , [zone] : " + zone + " of Iaas : " + iaasInfo);
    +            return null;
    +        }
    +
    +        VolumeApi volumeApi = context.unwrapApi(CloudStackApi.class).getVolumeApi();
    +
    +        Volume volume;
    +        if (StringUtils.isEmpty(snapshotId)) {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone);
    +            }
    +
    +            //cloudstack jcloud api does not return a volume object
    +            volumeApi.createVolumeFromCustomDiskOfferingInZone(null, diskOfferingID, zone, sizeGB);
    +
    +            //  volume = blockStoreApi.createVolumeInAvailabilityZone(zone, sizeGB);
    +        } else {
    +            if (log.isDebugEnabled()) {
    +                log.info("Creating a volume in the zone " + zone + " from the snapshot " + snapshotId);
    +            }
    +            volumeApi.createVolumeFromSnapshotInZone(null, diskOfferingID, zone);
    +        }
    +
    +        return null;
    +    }
    +
    +    @Override
    +    public String attachVolume(String instanceId, String volumeId, String deviceName) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +
    +        //get volume
    +        org.jclouds.cloudstack.domain.Volume volume = cloudStackApi.getVolumeApi().getVolume(volumeId);
    +
    +        //get current volume state
    +        Volume.State volumeState = volume.getState();
    +
    +        if (log.isDebugEnabled()) {
    +            log.debug("Volume " + volumeId + " is in state " + volumeState);
    +        }
    +
    +        //if volume is not available, not allocated or cannot use
    +        //volume state ALLOCATED   means that volume has not been attached to any instance.
    +
    +        //TODO there is an error with logic.
    +        if (!(volumeState == Volume.State.ALLOCATED || volumeState == Volume.State.CREATING
    +                || volumeState == Volume.State.READY)) {
    +            log.error(String.format("Volume %s can not be attached. Volume status is %s", volumeId, volumeState));
    +        }
    +
    +        //check whether the account of volume and instance is same
    +        if (!volume.getAccount().equals(cloudStackApi.getVirtualMachineApi()
    +                .getVirtualMachine(instanceId).getAccount())) {
    +            log.error(String.format("Volume %s can not be attached. Instance account and Volume account " +
    +                    "are not the same ", volumeId));
    +        }
    +
    +        boolean volumeBecameAvailable = false, volumeBecameAttached = false;
    +
    +        try {
    +            if (volumeState == Volume.State.CREATING) {
    +
    +                volumeBecameAvailable = waitForStatus(volumeId, Volume.State.ALLOCATED, 5);
    +
    +            } else if (volumeState == Volume.State.READY) {
    +                volumeBecameAvailable = true;
    +            }
    +
    +        } catch (TimeoutException e) {
    +            log.error("[Volume ID] " + volumeId + "did not become ALLOCATED within expected timeout");
    +        }
    +
    +        //if volume state is 'ALLOCATED'
    +        if (volumeBecameAvailable) {
    +
    +            //attach volume into instance
    +            cloudStackApi.getVolumeApi().attachVolume(volumeId, instanceId);
    +
    +            try {
    +                volumeBecameAttached = waitForStatus(volumeId, Volume.State.READY, 2);
    +            } catch (TimeoutException e) {
    +                log.error("[Volume ID] " + volumeId + "did not become READY within expected timeout");
    +            }
    +        }
    +
    +        try {
    +            // waiting 5seconds till volumes are actually attached.
    +            Thread.sleep(5000);
    +        } catch (InterruptedException e) {
    +            e.printStackTrace();
    +        }
    +
    +        //If volume state is not 'READY'
    +        if (!volumeBecameAttached) {
    +            log.error(String.format("[Volume ID] %s attachment is called, but not yet became attached", volumeId));
    +        }
    +
    +        log.info(String.format("Volume [id]: %s attachment for instance [id]: %s was successful [status]: Attaching." +
    +                " of Iaas : %s", volumeId, instanceId, iaasInfo));
    +
    +        return "Attaching";
    +
    +    }
    +
    +    @Override
    +    public void detachVolume(String instanceId, String volumeId) {
    +
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +
    +        if (log.isDebugEnabled()) {
    +            log.debug(String.format("Starting to detach volume %s from the instance %s", volumeId, instanceId));
    +        }
    +
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +
    +        cloudStackApi.getVolumeApi().detachVolume(volumeId);
    +
    +        try {
    +            //TODO this is true only for newly created volumes
    +            if (waitForStatus(volumeId, Volume.State.ALLOCATED, 5)) {
    +                log.info(String.format("Detachment of Volume [id]: %s from instance [id]: %s was successful of Iaas : %s", volumeId, instanceId, iaasInfo));
    +            }
    +        } catch (TimeoutException e) {
    +            log.error(String.format("Detachment of Volume [id]: %s from instance [id]: %s was unsuccessful. [volume Status] : %s", volumeId, instanceId, iaasInfo));
    +        }
    +
    +    }
    +
    +    @Override
    +    public void deleteVolume(String volumeId) {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService()
    +                .getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        cloudStackApi.getVolumeApi().deleteVolume(volumeId);
    +        log.info("Deletion of Volume [id]: " + volumeId + " was successful. "
    +                + " of Iaas : " + iaasInfo);
    +    }
    +
    +    @Override
    +    public String getIaasDevice(String device) {//todo implement this method(auto generated method)
    +        return null;
    +    }
    +
    +    private boolean waitForStatus(String volumeId, Volume.State expectedStatus, int timeoutInMins) throws TimeoutException {
    +        int timeout = 1000 * 60 * timeoutInMins;
    --- End diff --
    
    So this method expects timeout in Minutes.
    then what if I want 1.5 min timeout? Shalle we make this parameter to timeoutInMilliseconds ? This is how we are having in other places.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

RE: [GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by Suresh Sadhu <Su...@citrix.com>.
Great news   and thanks for sharing tuna.

Regards
Sadhu


On Wed, Oct 29, 2014 at 5:41 AM, Nguyen Anh Tu <ng...@gmail.com> wrote:
> Hi guys,
>
> Apache Stratos now supported CloudStack integration.
>
> https://github.com/apache/stratos/pull/97
>
> Cheers,
> --Tuna
>
> ---------- Forwarded message ----------
> From: asankasanjaya <gi...@git.apache.org>
> Date: Tue, Oct 28, 2014 at 1:15 PM
> Subject: [GitHub] stratos pull request: Apache cloudstack integration 
> for Stratos
> To: dev@stratos.apache.org
>
>
> GitHub user asankasanjaya opened a pull request:
>
>     https://github.com/apache/stratos/pull/97
>
>     Apache cloudstack integration for Stratos
>
>
>
> You can merge this pull request into a Git repository by running:
>
>     $ git pull https://github.com/asankasanjaya/stratos master
>
> Alternatively you can review and apply these changes as the patch at:
>
>     https://github.com/apache/stratos/pull/97.patch
>
> To close this pull request, make a commit to your master/trunk branch 
> with (at least) the following in the commit message:
>
>     This closes #97
>
> ----
> commit 384e293461f1e382646c96e1fec0abc17a5242e5
> Author: sanjaya <sanjaya@sanjaya-thinkpad-t520.(none)>
> Date:   2014-10-03T12:58:12Z
>
>     adding iaas classes
>
> commit 92f9abfc7b561d1d3967212f72435ba1be68ee85
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-06T04:16:33Z
>
>     adding cloudstack dependancy
>
> commit 1b81c15e87739ad42baa7a69c9c9dfd37761f5b2
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-06T05:15:30Z
>
>     Merge branch 'master' of https://github.com/apache/stratos
>
> commit c9779fae3fd8ac0074cb791fa235c8ff7c35ecc7
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-06T06:26:58Z
>
>     Merge branch 'master' of https://github.com/apache/stratos
>
> commit f75e1cc78d6b262d7b4b0ac8171b14d1fc359654
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-06T14:04:34Z
>
>     works 6 oct
>
> commit d60f77b93b88df6936d78ff2cec12979d4637294
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-09T14:38:54Z
>
>     work on 9 oct
>
> commit b43fe3effba4c48f502bb629a1ccafbcb48e5363
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-13T05:04:52Z
>
>     changes 10th oct
>
> commit c43b824e2ce9392b3c28e11ec46fdfda3b69eb2d
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-13T05:09:02Z
>
>     remove cricket files
>
> commit 15b282f5c29e801bb33a93590128a4ba201b469b
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-16T06:21:52Z
>
>     untill oct 16
>
> commit 3698437b7211b8960f2de2159c05a5442b4309df
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-27T12:53:11Z
>
>     works untill 27th oct--added public ip method and network ids
>
> commit 7b911dbb7305b9fbe94138dc5c378581294dde40
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-28T05:58:16Z
>
>     reformated code
>
> commit 087d9d5ef9933f6b8537f6c1256014fdfac17563
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-28T06:07:11Z
>
>     reformated code again
>
> ----
>
>
> ---
> If your project is set up for it, you can reply to this email and have 
> your reply appear on GitHub as well. If your project does not have 
> this feature enabled and wishes so, or if the feature is enabled but 
> not working, please contact infrastructure at 
> infrastructure@apache.org or file a JIRA ticket with INFRA.
> ---

Re: [GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by David Nalley <da...@gnsa.us>.
Thats awesome Tuna! Thanks for sharing.

--David

On Wed, Oct 29, 2014 at 5:41 AM, Nguyen Anh Tu <ng...@gmail.com> wrote:
> Hi guys,
>
> Apache Stratos now supported CloudStack integration.
>
> https://github.com/apache/stratos/pull/97
>
> Cheers,
> --Tuna
>
> ---------- Forwarded message ----------
> From: asankasanjaya <gi...@git.apache.org>
> Date: Tue, Oct 28, 2014 at 1:15 PM
> Subject: [GitHub] stratos pull request: Apache cloudstack integration for
> Stratos
> To: dev@stratos.apache.org
>
>
> GitHub user asankasanjaya opened a pull request:
>
>     https://github.com/apache/stratos/pull/97
>
>     Apache cloudstack integration for Stratos
>
>
>
> You can merge this pull request into a Git repository by running:
>
>     $ git pull https://github.com/asankasanjaya/stratos master
>
> Alternatively you can review and apply these changes as the patch at:
>
>     https://github.com/apache/stratos/pull/97.patch
>
> To close this pull request, make a commit to your master/trunk branch
> with (at least) the following in the commit message:
>
>     This closes #97
>
> ----
> commit 384e293461f1e382646c96e1fec0abc17a5242e5
> Author: sanjaya <sanjaya@sanjaya-thinkpad-t520.(none)>
> Date:   2014-10-03T12:58:12Z
>
>     adding iaas classes
>
> commit 92f9abfc7b561d1d3967212f72435ba1be68ee85
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-06T04:16:33Z
>
>     adding cloudstack dependancy
>
> commit 1b81c15e87739ad42baa7a69c9c9dfd37761f5b2
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-06T05:15:30Z
>
>     Merge branch 'master' of https://github.com/apache/stratos
>
> commit c9779fae3fd8ac0074cb791fa235c8ff7c35ecc7
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-06T06:26:58Z
>
>     Merge branch 'master' of https://github.com/apache/stratos
>
> commit f75e1cc78d6b262d7b4b0ac8171b14d1fc359654
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-06T14:04:34Z
>
>     works 6 oct
>
> commit d60f77b93b88df6936d78ff2cec12979d4637294
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-09T14:38:54Z
>
>     work on 9 oct
>
> commit b43fe3effba4c48f502bb629a1ccafbcb48e5363
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-13T05:04:52Z
>
>     changes 10th oct
>
> commit c43b824e2ce9392b3c28e11ec46fdfda3b69eb2d
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-13T05:09:02Z
>
>     remove cricket files
>
> commit 15b282f5c29e801bb33a93590128a4ba201b469b
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-16T06:21:52Z
>
>     untill oct 16
>
> commit 3698437b7211b8960f2de2159c05a5442b4309df
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-27T12:53:11Z
>
>     works untill 27th oct--added public ip method and network ids
>
> commit 7b911dbb7305b9fbe94138dc5c378581294dde40
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-28T05:58:16Z
>
>     reformated code
>
> commit 087d9d5ef9933f6b8537f6c1256014fdfac17563
> Author: asankasa <as...@wso2.com>
> Date:   2014-10-28T06:07:11Z
>
>     reformated code again
>
> ----
>
>
> ---
> If your project is set up for it, you can reply to this email and have your
> reply appear on GitHub as well. If your project does not have this feature
> enabled and wishes so, or if the feature is enabled but not working, please
> contact infrastructure at infrastructure@apache.org or file a JIRA ticket
> with INFRA.
> ---

Fwd: [GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by Nguyen Anh Tu <ng...@gmail.com>.
Hi guys,

Apache Stratos now supported CloudStack integration.

https://github.com/apache/stratos/pull/97

Cheers,
--Tuna

---------- Forwarded message ----------
From: asankasanjaya <gi...@git.apache.org>
Date: Tue, Oct 28, 2014 at 1:15 PM
Subject: [GitHub] stratos pull request: Apache cloudstack integration for
Stratos
To: dev@stratos.apache.org


GitHub user asankasanjaya opened a pull request:

    https://github.com/apache/stratos/pull/97

    Apache cloudstack integration for Stratos



You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/asankasanjaya/stratos master

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/stratos/pull/97.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #97

----
commit 384e293461f1e382646c96e1fec0abc17a5242e5
Author: sanjaya <sanjaya@sanjaya-thinkpad-t520.(none)>
Date:   2014-10-03T12:58:12Z

    adding iaas classes

commit 92f9abfc7b561d1d3967212f72435ba1be68ee85
Author: asankasa <as...@wso2.com>
Date:   2014-10-06T04:16:33Z

    adding cloudstack dependancy

commit 1b81c15e87739ad42baa7a69c9c9dfd37761f5b2
Author: asankasa <as...@wso2.com>
Date:   2014-10-06T05:15:30Z

    Merge branch 'master' of https://github.com/apache/stratos

commit c9779fae3fd8ac0074cb791fa235c8ff7c35ecc7
Author: asankasa <as...@wso2.com>
Date:   2014-10-06T06:26:58Z

    Merge branch 'master' of https://github.com/apache/stratos

commit f75e1cc78d6b262d7b4b0ac8171b14d1fc359654
Author: asankasa <as...@wso2.com>
Date:   2014-10-06T14:04:34Z

    works 6 oct

commit d60f77b93b88df6936d78ff2cec12979d4637294
Author: asankasa <as...@wso2.com>
Date:   2014-10-09T14:38:54Z

    work on 9 oct

commit b43fe3effba4c48f502bb629a1ccafbcb48e5363
Author: asankasa <as...@wso2.com>
Date:   2014-10-13T05:04:52Z

    changes 10th oct

commit c43b824e2ce9392b3c28e11ec46fdfda3b69eb2d
Author: asankasa <as...@wso2.com>
Date:   2014-10-13T05:09:02Z

    remove cricket files

commit 15b282f5c29e801bb33a93590128a4ba201b469b
Author: asankasa <as...@wso2.com>
Date:   2014-10-16T06:21:52Z

    untill oct 16

commit 3698437b7211b8960f2de2159c05a5442b4309df
Author: asankasa <as...@wso2.com>
Date:   2014-10-27T12:53:11Z

    works untill 27th oct--added public ip method and network ids

commit 7b911dbb7305b9fbe94138dc5c378581294dde40
Author: asankasa <as...@wso2.com>
Date:   2014-10-28T05:58:16Z

    reformated code

commit 087d9d5ef9933f6b8537f6c1256014fdfac17563
Author: asankasa <as...@wso2.com>
Date:   2014-10-28T06:07:11Z

    reformated code again

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by imesh <gi...@git.apache.org>.
Github user imesh commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r19653786
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.fatal(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("ZONE has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    +                            + " with id: " + location.getId());
    +                    break;
    +                }
    +            }
    +        }
    +
    +        /**
    +         * PROPERTY-3
    +         * if user has specified an instance type in cloud-controller.xml, set the instance type into templateBuilder
    +         * object.(service offering)
    +         *Important:Specify the Service Offering type ID. Not the name. Because the name is not unique in cloudstack.
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
    +            templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    +        }
    +
    +        //build the template
    +        Template template = templateBuilder.build();
    +
    +        /**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?
    +         */
    +        boolean blockUntilRunning = Boolean.parseBoolean(iaasInfo
    +                .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    +        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(new int[]{});
    +
    +
    +        //**SET CLOUDSTACK SPECIFIC PROPERTIES TO TEMPLATE OBJECT**//
    +
    +        //set security group - If you are using basic zone
    +        if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .securityGroupIds(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +
    +        /**
    +         * set network ID - If you are using advanced zone
    +         * in cloudstack sometimes we get unautorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .networks(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.NETWORK_IDS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +
    +        //set user name
    +        if (iaasInfo.getProperty(CloudControllerConstants.USER_NAME) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .account(iaasInfo.getProperty(CloudControllerConstants.USER_NAME));
    +        }
    +        //set domain ID
    +        if (iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .domainId(iaasInfo.getProperty(CloudControllerConstants.DOMAIN_ID));
    +        }
    +
    +        /**
    +         *Set key pair
    +         * in cloudstack sometimes we get unauthorized exception if we didn't specify the
    +         * domain ID and user name
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
    +            template.getOptions().as(CloudStackTemplateOptions.class)
    +                    .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    +        }
    +
    +        // ability to define tags
    +        if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .tags(Arrays.asList(iaasInfo.getProperty(CloudControllerConstants.TAGS)
    +                            .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    +        }
    +        //set disk offering to the instance
    +        if (iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING) != null) {
    +            template.getOptions()
    +                    .as(CloudStackTemplateOptions.class)
    +                    .diskOfferingId(iaasInfo.getProperty(CloudControllerConstants.DISK_OFFERING));
    +        }
    +
    +        // set Template
    +        iaasInfo.setTemplate(template);
    +    }
    +
    +    @Override
    +    public void setDynamicPayload() {
    +        IaasProvider iaasInfo = getIaasProvider();
    +        if (iaasInfo.getTemplate() != null && iaasInfo.getPayload() != null) {
    +            iaasInfo.getTemplate().getOptions().as(CloudStackTemplateOptions.class)
    +                    .userMetadata(convertByteArrayToHashMap(iaasInfo.getPayload()));
    +        }
    +    }
    +
    +    /**
    +     * IMPORTANT
    +     * In cloudstack we can assign public IPs, if we are using an advanced zone only. If we are using a basic zone
    +     * we cannot assign public ips.
    +     * <p/>
    +     * When we use an advanced zone, a public IP address will get automatically assigned to the vm. So we don't need
    +     * to find an unallocated IP address and assign that address to the vm (Not like in ec2 and openstack).
    +     * <p/>
    +     * So  this method will find the IP that has been assigned to the vm and return it.
    +     */
    +    @Override
    +    public String associateAddress(NodeMetadata node) {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    +        CloudStackApi cloudStackApi = context.unwrapApi(CloudStackApi.class);
    +        String ip = null;
    +
    +        // get all allocated IPs
    +        ListPublicIPAddressesOptions listPublicIPAddressesOptions = new ListPublicIPAddressesOptions();
    +        listPublicIPAddressesOptions.zoneId(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    +
    +        Set<PublicIPAddress> publicIPAddresses = cloudStackApi.getAddressApi()
    +                .listPublicIPAddresses(listPublicIPAddressesOptions);
    +
    +        String id = node.getProviderId(); //vm ID
    +
    +        for (PublicIPAddress publicIPAddress : publicIPAddresses) {
    +            if (publicIPAddress.getVirtualMachineId().equals(id)) { //check whether this instance has
    +                // already got an public ip or not
    +                ip = publicIPAddress.getIPAddress(); //A public ip has been successfully assigned to the vm
    +                log.debug("Successfully associated an IP address " + ip
    --- End diff --
    
    May be this needs to be an info log? WDYT?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] stratos pull request: Apache cloudstack integration for Stratos

Posted by asankasanjaya <gi...@git.apache.org>.
Github user asankasanjaya commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/97#discussion_r19653702
  
    --- Diff: components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/CloudstackIaas.java ---
    @@ -0,0 +1,539 @@
    +package org.apache.stratos.cloud.controller.iaases;
    +
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.stratos.cloud.controller.exception.CloudControllerException;
    +import org.apache.stratos.cloud.controller.exception.InvalidHostException;
    +import org.apache.stratos.cloud.controller.exception.InvalidRegionException;
    +import org.apache.stratos.cloud.controller.exception.InvalidZoneException;
    +import org.apache.stratos.cloud.controller.interfaces.Iaas;
    +import org.apache.stratos.cloud.controller.jcloud.ComputeServiceBuilderUtil;
    +import org.apache.stratos.cloud.controller.pojo.IaasProvider;
    +import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
    +import org.apache.stratos.cloud.controller.validate.CloudstackPartitionValidator;
    +import org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
    +import org.jclouds.cloudstack.CloudStackApi;
    +import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
    +import org.jclouds.cloudstack.domain.*;
    +import org.jclouds.cloudstack.features.VolumeApi;
    +import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
    +import org.jclouds.cloudstack.options.ListZonesOptions;
    +import org.jclouds.compute.ComputeServiceContext;
    +import org.jclouds.compute.domain.NodeMetadata;
    +import org.jclouds.compute.domain.Template;
    +import org.jclouds.compute.domain.TemplateBuilder;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.domain.Location;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.TimeoutException;
    +
    +public class CloudstackIaas extends Iaas {
    +
    +    private static final Log log = LogFactory.getLog(CloudstackIaas.class);
    +
    +    public CloudstackIaas(IaasProvider iaasProvider) {
    +        super(iaasProvider);
    +    }
    +
    +    @Override
    +    public void buildComputeServiceAndTemplate() {
    +        // builds and sets Compute Service
    +        ComputeServiceBuilderUtil.buildDefaultComputeService(getIaasProvider());
    +        // builds and sets Template
    +        buildTemplate();
    +    }
    +
    +    @Override
    +    public void buildTemplate() {
    +
    +        IaasProvider iaasInfo = getIaasProvider();
    +
    +        //if compute service is not available
    +        if (iaasInfo.getComputeService() == null) {
    +            String msg = "Compute service is null for IaaS provider: "
    +                    + iaasInfo.getName();
    +            log.fatal(msg);
    +            throw new CloudControllerException(msg);
    +        }
    +
    +        //create templateBuilder
    +        TemplateBuilder templateBuilder = iaasInfo.getComputeService()
    +                .templateBuilder();
    +
    +        //**SET PROPERTIES TO templateBuilder OBJECT**//
    +
    +        /**
    +         * PROPERTY - 1
    +         * set image id specified
    +         */
    +        templateBuilder.imageId(iaasInfo.getImage());
    +
    +        /**
    +         *  PROPERTY-2
    +         *  if user has specified a zone in cloud-controller.xml, set the zone into templateBuilder object
    +         *  (user should provide the zone id for this, because zone name is not unique in cloudstack)
    +         */
    +        if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
    +            Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
    +            for (Location location : locations) {
    +                if (location.getId().equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
    +                    //if the zone is valid set the zone to templateBuilder Object
    +                    templateBuilder.locationId(location.getId());
    +                    log.info("ZONE has been set as " + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
    --- End diff --
    
    HI imesh,
    
    I fixed those issues. By the way I think Those issues should be fixed in ec2 and openstack classes also.
    
    Thank you.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---