JavaCloud.Models.Image.java Source code

Java tutorial

Introduction

Here is the source code for JavaCloud.Models.Image.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.Models;

import JavaCloud.CoreException;
import JavaCloud.Utils;
import org.json.simple.JSONObject;

public class Image {
    String address;
    String token;

    public int id;
    public String name;
    public String description;
    public String state;
    public String type;
    public long size;
    public String format;

    public Image(String address, String token, JSONObject image_dict) {
        this.address = address;
        this.token = token;

        id = Integer.parseInt(image_dict.get("id").toString());
        name = image_dict.get("name").toString();
        description = image_dict.get("description").toString();
        state = image_dict.get("state").toString();
        type = image_dict.get("type").toString();
        size = Long.valueOf(image_dict.get("size").toString());
        format = image_dict.get("format").toString();

        // TODO: New variables
    }

    public String toString() {
        return name;
    }

    public void upload_data(String data, long offset) throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        object.put("image_id", id);
        object.put("offset", offset);
        object.put("data", data);
        Utils.request(address, "/api/image/upload_data/", object);
    }

    public void upload_url(String url) throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        object.put("image_id", id);
        object.put("url", url);
        Utils.request(address, "/api/image/upload_url/", object);
    }

    public void delete() throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        object.put("image_id", id);
        Utils.request(address, "/api/image/delete/", object);
    }

    public void attach(VM vm) throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        object.put("image_id", id);
        object.put("vm_id", vm.id);
        Utils.request(address, "/api/image/attach/", object);
    }

    public void detach(VM vm) throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        object.put("image_id", id);
        object.put("vm_id", vm.id);
        Utils.request(address, "/api/image/detach/", object);
    }

    public void edit(String field, String value) throws CoreException {
        JSONObject object = new JSONObject();
        object.put("token", token);
        object.put("image_id", id);
        object.put(field, value);
        Utils.request(address, "/api/image/edit/", object);
    }
}