8/3/13

Delta cloud Java API integration

In my previous post i would given you an idea of what delta cloud is about. In this post we will see the integration of delta cloud api in java . The example makes use of openstack integrated deltacloud.


Main.java : 

import sun.misc.BASE64Encoder;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        try {

            DeltaAPICall deltaapi = new DeltaAPICall();

            String url = "";
            String auth = "";
            String result = "";

            BASE64Encoder enc = new sun.misc.BASE64Encoder();
            //--- The below username and password is openstack user credential --//
            String userpassword = "appteam@corenttech.com+Appteam:corent.123$";
            auth = enc.encode(userpassword.getBytes());
            auth = "Basic "+auth;
            System.out.println("auth >>> "+auth);
            //For Ex: auth = "Basic YXBwdGVhbUBjb3JlbnR0ZWNoLmNvbStBcHB0ZWFtOmNvcmVudC4xMjMk";

           
            //--- Below lines to list the api's using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------API'S---------------------");
            url = "http://192.168.1.102:3001/api?format=xml";
            result = deltaapi.getDeltaAPI(url, auth);
            System.out.println("\n API's Final Result >>>> \n" + result);
            System.out.println("######################################");

            //--- Below lines to list the images using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------ALL IMAGES-----------------");
            url = "http://192.168.1.102:3001/api/images?format=xml";
            result = deltaapi.getDeltaAllImages(url, auth);
            System.out.println("\n All Images Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to list the hardware profiles using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------ALL HARDWARE PROFILES[FLAVORS]-----------------");
            url = "http://192.168.1.102:3001/api/hardware_profiles?format=xml";
            result = deltaapi.getDeltaProfile(url, auth);
            System.out.println("\n Hardware Profiles Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to list the instances using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------ALL INSTANCES-----------------");
            url = "http://192.168.1.102:3001/api/instances?format=xml";
            result = deltaapi.getDeltaAllInstances(url, auth);
            System.out.println("\n All Instances Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to create the instnace using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------LAUNCH INSTANCE-----------------");
            url = "http://192.168.1.102:3001/api/instances?format=xml";
            String image_id = "";
            String hwp_id = "";
            String keyname = "";
            result = deltaapi.launchDeltaInstance(url, auth, image_id, hwp_id, keyname );
            System.out.println("\n Instance Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to reboot the instnace using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------REBOOT INSTANCE-----------------");
            String instance_id = "";
            url = "http://192.168.1.102:3001/api/instances/"+instance_id+"/reboot?format=xml";
            result = deltaapi.rebootDeltaInstnace(url, auth);
            System.out.println("\n Reboot Instance Final Result >>>> \n" + result);
            System.out.println("######################################");


             //--- Below lines to stop the instnace using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------STOP INSTANCE[DELETE]-----------------");
            url = "http://192.168.1.102:3001/api/instances/"+instance_id+"/stop?format=xml";
            result = deltaapi.stopDeltaInstnace(url, auth);
            System.out.println("\n Stop Instance Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to create the image instnace using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------CREATE IMAGE INSTANCE-----------------");
            url = "http://192.168.1.102:3001/api/images?format=xml";
            String image_name = "DeltaCloud_Image1";
            result = deltaapi.createDeltaImageInstnace(url, auth, instance_id, image_name);
            System.out.println("\n Create Images Instance Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to delete the image using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------DELETE IMAGE-----------------");
            url = "http://192.168.1.102:3001/api/images/"+image_id+"?format=xml";
            result = deltaapi.delteDeltaImage(url, auth);
            System.out.println("\n Images Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to list the volumes using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------ALL VOLUMES-----------------");
            url = "http://192.168.1.102:3001/api/storage_volumes?format=xml";
            result = deltaapi.getDeltaAllVolumes(url, auth);
            System.out.println("\n All Volumes Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to create a volume using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n----------------CREATE VOLUME-------------------");
            String name = "";
            String capacity = "";
            String realm_id = "";
            url = "http://192.168.1.102:3001/api/storage_volumes?format=xml";
            result = deltaapi.createDeltaVolume(url, auth, name, capacity, realm_id);
            System.out.println("\n Create Volume Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to delete the specified volume using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n----------------DELETE VOLUME-------------------");
            String volume_id = "";
            url = "http://192.168.1.102:3001/api/storage_volumes/"+volume_id+"?format=xml";
            result = deltaapi.deleteDeltaVolume(url, auth);
            System.out.println("\n Delete Volume Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to create a volume attachment using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n----------------CREATE VOLUME ATTACHMENT-------------------");
            String device = "/dev/sdi";
            url = "http://192.168.1.102:3001/api/storage_volumes/"+volume_id+"/attach?format=xml";
            result = deltaapi.atttachDeltaVolume(url, auth, instance_id, device);
            System.out.println("\n Create Volume Attachment Final Result >>>> \n" + result);
            System.out.println("######################################");


            //--- Below lines to create a snapshot volume using openstack user crendetail in deltacloud server machine ---//
            //System.out.println("\n----------------CREATE SNAPSHOT VOLUME-------------------");
            //url = "http://192.168.1.102:3001/api/storage_snapshots?format=xml";
            //result = deltaapi.createDeltasnapshotVolume(url, auth, volume_id);
            //System.out.println("\n Create Snapshot Volume Final Result >>>> \n" + result);
            //System.out.println("######################################");


            //--- Below lines to list the instnace states using openstack user crendetail in deltacloud server machine ---//
            System.out.println("\n--------------INSTANCE STATES-----------------");
            url = "http://192.168.1.102:3001/api/instance_states?format=xml";
            result = deltaapi.getDeltaInstnacestates(url, auth);
            System.out.println("\n Instance States Final Result >>>> \n" + result);
            System.out.println("######################################");

           
           
        } catch (Exception e) {
            e.printStackTrace();
        }    
    }   

}


DeltaAPICall .java :

public class DeltaAPICall {

    private CloudAPICall cloudapi = null;

    public DeltaAPICall()
    {
        cloudapi = new CloudAPICall();
    }

    /**
     * This method will fetch the all available api service in deltacloud server machine
     * @param url
     * @param Authentication
     * @return
     */
    public String getDeltaAPI(String url, String Authentication)
    {
        String result = "";
        result = cloudapi.getAPI(url, Authentication);
        return result;
    }

    /**
     * This method will fetch the all images in deltacloud server machine
     * @param url
     * @param Authentication
     * @return
     */
    public String getDeltaAllImages(String url, String Authentication)
    {
        String result = "";
        result = cloudapi.getAllImages(url, Authentication);
        return result;
    }

    /**
     * This method will fetch the all hardware profiles in deltacloud server machine
     * @param url
     * @param Authentication
     * @return
     */
    public String getDeltaProfile(String url, String Authentication)
    {
        String result = "";
        result = cloudapi.getHardwareProfiles(url, Authentication);
        return result;
    }

    /**
     * This method will fetch the all instances in deltacloud server machine
     * @param url
     * @param Authentication
     * @return
     */
    public String getDeltaAllInstances(String url, String Authentication)
    {
        String result = "";
        result = cloudapi.getAllInstances(url, Authentication);
        return result;
    }

    /**
     * This method will launch the instance using specified parameters in deltacloud server machine
     * @param url
     * @param Authentication
     * @param image_id
     * @param hwp_id
     * @param keyname
     * @return
     */
    public String launchDeltaInstance(String url, String Authentication, String image_id, String hwp_id, String keyname)
    {
        String result = "";
        result = cloudapi.launchInstance(url, Authentication, image_id, hwp_id, keyname);
        return result;
    }

    /**
     * This method will reboot the specified instance in deltacloud server machine
     * @param url
     * @param Authentication
     * @return
     */
    public String rebootDeltaInstnace(String url, String Authentication)
    {
        String result = "";
        result = cloudapi.rebootInstance(url, Authentication);
        return result;
    }

    /**
     * This method will stop the spcified instance in deltacloud server machine
     * @param url
     * @param Authentication
     * @return
     */
    public String stopDeltaInstnace(String url, String Authentication)
    {
        String result = "";
        result = cloudapi.stopInstance(url, Authentication);
        return result;
    }

    /**
     * This method will create the image instance using specified below parameters in deltacloud server machine
     * @param url
     * @param Authentication
     * @param instance_id
     * @param image_name
     * @return
     */
    public String createDeltaImageInstnace(String url, String Authentication, String instance_id, String image_name)
    {
        String result = "";
        result = cloudapi.createImageInstance(url, Authentication, instance_id, image_name);
        return result;
    }

    /**
     * This method will delete the specified image in deltacloud server machine
     * @param url
     * @param Authentication
     * @return
     */
    public String delteDeltaImage(String url, String Authentication)
    {
        String result = "";
        result = cloudapi.deleteImage(url, Authentication);
        return result;
    }

    /**
     * This method will fetch the all volumes in deltacloud server machine
     * @param url
     * @param Authentication
     * @return
     */
    public String getDeltaAllVolumes(String url, String Authentication)
    {
        String result = "";
        result = cloudapi.getAllVolumes(url, Authentication);
        return result;
    }

    /**
     * This method will create the volume using specified parameters in deltacloud server machine
     * @param url
     * @param Authentication
     * @param name
     * @param capacity
     * @param realm_id
     * @return
     */
    public String createDeltaVolume(String url, String Authentication, String name, String capacity, String realm_id)
    {
        String result = "";
        result = cloudapi.createVolume(url, Authentication, name, capacity, realm_id);
        return result;
    }

    /**
     * This method will delete the specified volume in deltacloud server machine
     * @param url
     * @param Authentication
     * @return
     */
    public String deleteDeltaVolume(String url, String Authentication)
    {
        String result = "";
        result = cloudapi.deleteVolume(url, Authentication);
        return result;
    }

    /**
     * This method will attach the volume using specified parameters in deltacloud server machine
     * @param url
     * @param Authentication
     * @param instance_id
     * @param device
     * @return
     */
    public String atttachDeltaVolume(String url, String Authentication, String instance_id, String device)
    {
        String result = "";
        result = cloudapi.attachVolume(url, Authentication, instance_id, device);
        return result;
    }

    /**
     * This method will create the snapshot volume using volume_id in deltacloud server machine
     * @param url
     * @param Authentication
     * @param volume_id
     * @return
     */
    public String createDeltasnapshotVolume(String url, String Authentication, String volume_id)
    {
        String result = "";
        result = cloudapi.snapshotVolume(url, Authentication, volume_id);
        return result;
    }

    /**
     * This method will fetch the all available instance states in deltacloud server machine
     * @param url
     * @param Authentication
     * @return
     */
    public String getDeltaInstnacestates(String url, String Authentication)
    {
        String result = "";
        result = cloudapi.getInstanceStates(url, Authentication);
        return result;
    }
}
 CloudAPICall.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;


public class CloudAPICall {

    /**
     * This method will take url information , authentication and calls the deltacloud Gateways with GET method and list the all available services.
     * @param url
     * @param Authentication
     * @return
     */
    public String getAPI(String url, String Authentication) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api?format=xml";
          
            System.out.println("\n url >>> " + url);
            System.out.println(" auth  >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);
            request.setHeader("Authorization", Authentication);
            HttpResponse httpResponse = client.execute(request);
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information , authentication and calls the deltacloud Gateways with GET method and list the all images.
     * @param url
     * @param Authentication
     * @return
     */
    public String getAllImages(String url, String Authentication) {
        String result = "";

        try {
            //String url = "http://192.168.1.102:3001/api/images?format=xml";
        
            System.out.println("\n url >>> " + url);
            System.out.println(" auth  >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);
            request.setHeader("Authorization", Authentication);
            HttpResponse httpResponse = client.execute(request);
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information , authentication and calls the deltacloud Gateways with GET method and list the all hardware profiles.
     * @param url
     * @param Authentication
     * @return
     */
    public String getHardwareProfiles(String url, String Authentication) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/hardware_profiles?format=xml";
    
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);
            request.setHeader("Authorization", Authentication);
            HttpResponse httpResponse = client.execute(request);

            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();               
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information , authentication and calls the deltacloud Gateways with GET method and list the all instances.
     * @param url
     * @param Authentication
     * @return
     */
    public String getAllInstances(String url, String Authentication) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/instances?format=xml";
           
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);

            request.setHeader("Authorization", Authentication);
            HttpResponse httpResponse = client.execute(request);

            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();               
            }

            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information,authentication,image_id,profile_id,keyname and calls the deltacloud Gateways with POST method and launch the instance.
     * @param url
     * @param Authentication
     * @param image_id
     * @param hwp_id
     * @param keyname
     * @return
     */
    public String launchInstance(String url, String Authentication, String image_id, String hwp_id, String keyname) {
        String result = "";

        try {
            //String url = "http://192.168.1.102:3001/api/instances?format=xml";
          
            System.out.println("\n url >>> " + url);
            System.out.println(" auth  >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(url);
            request.addHeader("Authorization", "Basic " + Authentication);

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("image_id", "09eb0769-e6f8-44c4-9875-59aaa1ddaa6c"));
            nvps.add(new BasicNameValuePair("hwp_id", "10"));
            nvps.add(new BasicNameValuePair("keyname", "deltaKey"));
            request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            HttpResponse httpResponse = client.execute(request);
            System.out.println("httpResponse.getStatusLine().getStatusCode()---->>>>" + httpResponse.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();               
            }

            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information , authentication and calls the deltacloud Gateways with POST method and reboot the specified instance.
     * @param url
     * @param Authentication
     * @return
     */
    public String rebootInstance(String url, String Authentication) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/instances/8df2f3c6-52ff-46c9-acc6-3163baf64c06/reboot?format=xml";
        
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(url);
            request.addHeader("Authorization", Authentication);

            HttpResponse httpResponse = client.execute(request);
            System.out.println("httpResponse.getStatusLine().getStatusCode()---->>>>" + httpResponse.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();               
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information , authentication and calls the deltacloud Gateways with POST method and stop the specified instance.
     * @param url
     * @param Authentication
     * @return
     */
    public String stopInstance(String url, String Authentication) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/instances/8df2f3c6-52ff-46c9-acc6-3163baf64c06/stop?format=xml";
           
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(url);
            request.addHeader("Authorization", Authentication);

            HttpResponse httpResponse = client.execute(request);
            System.out.println("httpResponse.getStatusLine().getStatusCode()---->>>>" + httpResponse.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();              
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information,authentication,instance_id,image_name and calls the deltacloud Gateways with POST method and create the image instance.
     * @param url
     * @param Authentication
     * @param instance_id
     * @param image_name
     * @return
     */
    public String createImageInstance(String url, String Authentication, String instance_id, String image_name) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/images?format=xml";
          
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(url);
            request.addHeader("Authorization", Authentication);

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("instance_id", "8df2f3c6-52ff-46c9-acc6-3163baf64c06"));
            nvps.add(new BasicNameValuePair("name", "gopal-delta"));
            request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            HttpResponse httpResponse = client.execute(request);
            System.out.println("httpResponse.getStatusLine().getStatusCode()---->>>>" + httpResponse.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();              
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information , authentication and calls the deltacloud Gateways with DELETE method and delete the specified image.
     * @param url
     * @param Authentication
     * @return
     */
    public String deleteImage(String url, String Authentication) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/images/29a74e07-8863-4855-9e12-21a7b28e0ff8?format=xml";
       
            System.out.println("\n url >>> " + url);
            System.out.println("tokid >>> " + Authentication);

            HttpClient httpClient = new DefaultHttpClient();
            HttpDelete del = new HttpDelete(url);
            del.addHeader("Authorization", Authentication);
            HttpResponse response = httpClient.execute(del);

            result = "Successfuly Instance Deleted.";
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information , authentication and calls the deltacloud Gateways with GET method and list the all volumes.
     * @param url
     * @param Authentication
     * @return
     */
    public String getAllVolumes(String url, String Authentication) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/storage_volumes?format=xml";
          
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);

            request.setHeader("Authorization", Authentication);
            HttpResponse httpResponse = client.execute(request);

            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();               
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information,authentication,name,capacity,realm_id and calls the deltacloud Gateways with POST method and create the volume.
     * @param url
     * @param Authentication
     * @param name
     * @param capacity
     * @param realm_id
     * @return
     */
    public String createVolume(String url, String Authentication, String name, String capacity, String realm_id) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/storage_volumes?format=xml";
         
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(url);
            request.addHeader("Authorization", Authentication);

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("name", "Vol1"));
            nvps.add(new BasicNameValuePair("capacity", "1"));
            nvps.add(new BasicNameValuePair("realm_id", "default"));
            request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            HttpResponse httpResponse = client.execute(request);
            System.out.println("httpResponse.getStatusLine().getStatusCode()---->>>>" + httpResponse.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();              
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information , authentication and calls the deltacloud Gateways with DELETE method and delete the specified volume.
     * @param url
     * @param Authentication
     * @return
     */
    public String deleteVolume(String url, String Authentication) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/storage_volumes/7?format=xml";
          
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient httpClient = new DefaultHttpClient();
            HttpDelete del = new HttpDelete(url);
            del.addHeader("Authorization", Authentication);

            HttpResponse httpResponse = httpClient.execute(del);
            System.out.println("httpResponse.getStatusLine().getStatusCode()---->>>>" + httpResponse.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information,authentication,instance_id,device and calls the deltacloud Gateways with POST method and attache the volume.
     * @param url
     * @param Authentication
     * @param instance_id
     * @param device
     * @return
     */
    public String attachVolume(String url, String Authentication, String instance_id, String device) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/storage_volumes/7/attach?format=xml";
           
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(url);
            request.addHeader("Authorization", Authentication);

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("instance_id", "ca08bd52-5049-4f41-bcfe-7140e8052c28"));
            nvps.add(new BasicNameValuePair("device", "/dev/sdi"));
            request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            HttpResponse httpResponse = client.execute(request);
            System.out.println("httpResponse.getStatusLine().getStatusCode()---->>>>" + httpResponse.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();               
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information,authentication,volume_id and calls the deltacloud Gateways with POST method and snapshot the specified volume.
     * @param url
     * @param Authentication
     * @param volume_id
     * @return
     */
    public String snapshotVolume(String url, String Authentication, String volume_id) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/storage_snapshots?format=xml";
          
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(url);
            request.addHeader("Authorization", Authentication);

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("volume_id", "6"));
            request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            HttpResponse httpResponse = client.execute(request);
            System.out.println("httpResponse.getStatusLine().getStatusCode()---->>>>" + httpResponse.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();               
            }
            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

    /**
     * This method will take url information , authentication and calls the deltacloud Gateways with GET method and list the all available instance states.
     * @param url
     * @param Authentication
     * @return
     */
    public String getInstanceStates(String url, String Authentication) {
        String result = "";
        try {
            //String url = "http://192.168.1.102:3001/api/instance_states?format=xml";
           
            System.out.println("\n url >>> " + url);
            System.out.println(" auth >>> " + Authentication);

            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);

            request.setHeader("Authorization", Authentication);
            HttpResponse httpResponse = client.execute(request);

            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            while (rd.readLine() != null) {
                result += rd.readLine();               
            }

            System.out.println("\n Result >>> " + result);
        } catch (Exception e) {
            e.printStackTrace();
            result = "Caught in Exception";
        }
        return result;
    }

  
}



No comments:

Post a Comment

Popular Posts