JavaCloud.Api.java Source code

Java tutorial

Introduction

Here is the source code for JavaCloud.Api.java

Source

/**
 * Copyright (c) 2014 Marta Nabozny, Maciej Nabozny
 *
 * This file is part of OverCluster project.
 *
 * OverCluster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package JavaCloud;

import JavaCloud.Models.Image;
import JavaCloud.Models.Template;
import JavaCloud.Models.VM;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;

public class Api {
    String address;
    String token;

    public Api(String address, String token) {
        this.address = address;
        this.token = token;
    }

    public ArrayList<String> functionList() throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        JSONArray jsonfuncs = (JSONArray) Utils.request(address, "/api/api/list_functions/", object);
        ArrayList<String> functions = new ArrayList<String>();
        for (int i = 0; i < jsonfuncs.size(); i++) {
            functions.add(jsonfuncs.get(i).toString());
        }

        return functions;
    }

    public ArrayList<String> apiModulesList() throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        JSONArray jsonmodules = (JSONArray) Utils.request(address, "/api/api/list_api_modules/", object);
        ArrayList<String> modules = new ArrayList<String>();
        for (int i = 0; i < jsonmodules.size(); i++) {
            modules.add(jsonmodules.get(i).toString());
        }

        return modules;
    }

    public ArrayList<String> ciModulesList() throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        JSONArray jsonmodules = (JSONArray) Utils.request(address, "/api/api/list_ci_modules/", object);
        ArrayList<String> modules = new ArrayList<String>();
        for (int i = 0; i < jsonmodules.size(); i++) {
            modules.add(jsonmodules.get(i).toString());
        }

        return modules;
    }

    public ArrayList<VM> vmList() throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        JSONArray jsonvms = (JSONArray) Utils.request(address, "/api/vm/get_list/", object);
        ArrayList<VM> vms = new ArrayList<VM>();
        for (int i = 0; i < jsonvms.size(); i++) {
            vms.add(new VM(address, token, (JSONObject) jsonvms.get(i)));
        }

        return vms;
    }

    public ArrayList<Image> imageList(String type, String access, String[] prohibited_states) throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        if (type != null)
            object.put("type", type);
        if (access != null)
            object.put("access", access);
        if (prohibited_states.length > 0)
            object.put("prohibited_states", prohibited_states);

        JSONArray jsonimages = (JSONArray) Utils.request(address, "/api/image/get_list/", object);
        ArrayList<Image> images = new ArrayList<Image>();
        for (int i = 0; i < jsonimages.size(); i++) {
            images.add(new Image(address, token, (JSONObject) jsonimages.get(i)));
        }

        return images;
    }

    public ArrayList<Template> templateList() throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        JSONArray jsontemplates = (JSONArray) Utils.request(address, "/api/template/get_list/", object);
        ArrayList<Template> templates = new ArrayList<Template>();
        for (int i = 0; i < jsontemplates.size(); i++) {
            templates.add(new Template(address, token, (JSONObject) jsontemplates.get(i)));
        }

        return templates;
    }
}