Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create and update methods for OpenVZ Container #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/net/elbandi/pve2api/Pve2Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,12 @@ public void pve_check_login_ticket() throws LoginException, JSONException, IOExc
}
}

private JSONObject pve_action(String Path, RestClient.RequestMethod method,
Map<String, String> data) throws JSONException, LoginException, IOException {
private JSONObject pve_action(String Path, RestClient.RequestMethod method, Map<String, String> 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);
Expand Down Expand Up @@ -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<String, String> 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<Integer, String> initlogOpenvz(String node, int vmid, Map<String, String> data)
throws LoginException, JSONException, IOException {
Expand Down
117 changes: 113 additions & 4 deletions src/net/elbandi/pve2api/data/VmOpenvz.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -70,7 +118,46 @@ public void SetConfig(JSONObject data) throws JSONException {
searchdomain = data.getString("searchdomain");
storage = data.getString("storage");
}

public Map<String, String> getCreateParams()
{
Map<String, String> parameters = new HashMap<String, String>();

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;
}
Expand Down Expand Up @@ -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;
}
}