From 7766da00a3b448eb1b04f61665d10479437c1e63 Mon Sep 17 00:00:00 2001 From: gerhard Date: Wed, 24 Apr 2013 17:01:13 +0800 Subject: [PATCH] Add create and update methods for OpenVZ Container --- src/net/elbandi/pve2api/Pve2Api.java | 30 ++++-- src/net/elbandi/pve2api/data/VmOpenvz.java | 117 ++++++++++++++++++++- 2 files changed, 136 insertions(+), 11 deletions(-) diff --git a/src/net/elbandi/pve2api/Pve2Api.java b/src/net/elbandi/pve2api/Pve2Api.java index 87917f7..ecfd0d1 100644 --- a/src/net/elbandi/pve2api/Pve2Api.java +++ b/src/net/elbandi/pve2api/Pve2Api.java @@ -79,13 +79,12 @@ public void pve_check_login_ticket() throws LoginException, JSONException, IOExc } } - private JSONObject pve_action(String Path, RestClient.RequestMethod method, - Map data) throws JSONException, LoginException, IOException { + private JSONObject pve_action(String Path, RestClient.RequestMethod method, Map data) throws JSONException, LoginException, IOException + { pve_check_login_ticket(); if (!Path.startsWith("/")) Path = "/".concat(Path); - RestClient client = new RestClient("https://" + this.pve_hostname + ":8006/api2/json" - + Path); + RestClient client = new RestClient("https://" + this.pve_hostname + ":8006/api2/json" + Path); if (!method.equals(RestClient.RequestMethod.GET)) client.addHeader("CSRFPreventionToken", pve_login_token); client.addHeader("Cookie", "PVEAuthCookie=" + pve_login_ticket); @@ -480,9 +479,26 @@ public void getOpenvzConfig(String node, int vmid, VmOpenvz vm) throws JSONExcep RestClient.RequestMethod.GET, null); vm.SetConfig(jObj.getJSONObject("data")); } - - // TODO: createOpenvz - // TODO: updateOpenvz + + + public String createOpenvz(VmOpenvz vm) throws LoginException, JSONException, IOException + { + return createOpenvz(vm.getNode(), vm); + } + + public String createOpenvz(String node, VmOpenvz vm) throws LoginException, JSONException, IOException + { + Map parameterData = vm.getCreateParams(); + System.out.println(parameterData.toString()); + String path = "/nodes/" + node + "/openvz"; + JSONObject jsonObject = pve_action(path, RestClient.RequestMethod.POST, parameterData); + return jsonObject.getString("data"); + } + + public String updateOpenvz(String node, VmOpenvz vm) throws LoginException, JSONException, IOException + { + return createOpenvz(node, vm); + } protected Map initlogOpenvz(String node, int vmid, Map data) throws LoginException, JSONException, IOException { diff --git a/src/net/elbandi/pve2api/data/VmOpenvz.java b/src/net/elbandi/pve2api/data/VmOpenvz.java index de1c2ed..c9ccee5 100644 --- a/src/net/elbandi/pve2api/data/VmOpenvz.java +++ b/src/net/elbandi/pve2api/data/VmOpenvz.java @@ -1,5 +1,8 @@ package net.elbandi.pve2api.data; +import java.util.HashMap; +import java.util.Map; + import org.json.JSONException; import org.json.JSONObject; @@ -20,23 +23,32 @@ public class VmOpenvz { private String status; private long swap; private int uptime; - private int cpuunits; private String digest; private int diskspace; // ! name collision private int memory; private String hostname; private String nameserver; - // TODO: make it object! - private String netif; + private String netif; // TODO: make netif it object! private boolean onboot; private String ostemplate; private int quotatime; private int quotaugidlimit; private String searchdomain; private String storage; + private String password; + private int vmid; + private String node; - public VmOpenvz(JSONObject data) throws JSONException { + public VmOpenvz() + { + setStandardSettings(); + } + + public VmOpenvz(JSONObject data) throws JSONException + { + setStandardSettings(); + cpu = (float) data.getDouble("cpu"); cpus = data.getInt("cpus"); disk = (float) data.getDouble("disk"); @@ -54,6 +66,42 @@ public VmOpenvz(JSONObject data) throws JSONException { swap = data.getLong("swap"); uptime = data.getInt("uptime"); } + + private void setStandardSettings() + { + cpu = 0; + cpus = 0; + disk = 0; + diskread = 0; + diskwrite = 0; + maxdisk = 0; + maxmem = 0; + maxswap = 0; + mem = 0; + name = ""; + netin = 0; + netout = 0; + nproc = 0; + status = ""; + swap = 0; + uptime = 0; + cpuunits = 0; + digest = ""; + diskspace = 0; + memory = 0; + hostname = ""; + nameserver = ""; + netif = ""; + onboot = false; + ostemplate = ""; + quotatime = 0; + quotaugidlimit = 0; + searchdomain = ""; + storage = ""; + password = ""; + vmid = 0; + node = ""; + } public void SetConfig(JSONObject data) throws JSONException { cpuunits = data.optInt("cpuunits", 1000); @@ -70,7 +118,46 @@ public void SetConfig(JSONObject data) throws JSONException { searchdomain = data.getString("searchdomain"); storage = data.getString("storage"); } + + public Map getCreateParams() + { + Map parameters = new HashMap(); + + parameters.put("ostemplate", this.ostemplate); + parameters.put("vmid", Integer.toString(this.vmid)); + + if(this.cpus > 0) + parameters.put("cpus", Integer.toString(this.cpus)); + if(this.cpuunits > 0) + parameters.put("cpuunits", Integer.toString(this.cpuunits)); + if(this.disk > 0) + parameters.put("disk", Float.toString(this.disk)); + if(this.hostname != null && this.hostname.length() > 0) + parameters.put("hostname", this.hostname); + if(this.memory > 0) + parameters.put("memory", Integer.toString(this.memory)); + if(this.nameserver != null && this.nameserver.length() > 0) + parameters.put("nameserver", this.nameserver); + if(this.netif != null && this.netif.length() > 0) + parameters.put("netif", this.netif); + if(this.password != null && this.password.length() > 0) + parameters.put("password", this.password); + if(this.quotatime > 0) + parameters.put("quotatime", Integer.toString(this.quotatime)); + if(this.quotaugidlimit > 0) + parameters.put("quotaugidlimit", Integer.toString(this.quotaugidlimit)); + if(this.searchdomain != null && this.searchdomain.length() > 0) + parameters.put("searchdomain", this.searchdomain); + if(this.storage != null && this.storage.length() > 0) + parameters.put("storage", this.storage); + if(this.swap > 0) + parameters.put("swap", Long.toString(this.swap)); + + return parameters; + } + /* getter */ + public float getCpu() { return cpu; } @@ -186,4 +273,26 @@ public String getSearchdomain() { public String getStorage() { return storage; } + + public String getNode() + { + return this.node; + } + + /* setter */ + + public void setOstemplate(String ostemplate) + { + this.ostemplate = ostemplate; + } + + public void setVmid(int vmid) + { + this.vmid = vmid; + } + + public void setNode(String node) + { + this.node = node; + } }