Commit 74f28723 authored by Efthymia Bika's avatar Efthymia Bika

Display projects on cloud selection

parent d1ccdf4a
......@@ -92,7 +92,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
} else {
$("#full").prop("checked", true);
$("#inc").prop("disabled", true);
}
} else {
$("#selected-bar").hide();
......@@ -217,6 +216,77 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
}
return true;
}
function humanFileSize(bytes, si) {
var thresh = si ? 1000 : 1024;
if(Math.abs(bytes) < thresh) {
return bytes + ' B';
}
var units = si
? ['kB','MB','GB','TB','PB','EB','ZB','YB']
: ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while(Math.abs(bytes) >= thresh && u < units.length - 1);
return bytes.toFixed(1)+' '+units[u];
}
function calc_limit(q) {
var taken_by_others = q.project_usage - q.usage;
var effective_limit =
Math.min(q.limit, q.project_limit - taken_by_others);
var remaining = effective_limit - q.usage;
return humanFileSize(remaining, false);
}
function populate_projects(data) {
var project = JSON.parse(data);
$("#project").append($("<option></option>")
.attr("value", project.id)
.text(project.name + " (" +
projects[project.id].effective_limit + " left)"));
$("#loader").hide();
}
var projects = [];
function get_quotas(data) {
var cloud = $("#cloud").val();
var project_ids = JSON.parse(data);
projects = project_ids;
$.each(project_ids, function(project_id, i) {
projects[project_id].effective_limit =
calc_limit(i['pithos.diskspace']);
var astakos = getClient('astakos_account',
clouds[cloud].astakos_account, clouds[cloud].token,
clouds[cloud].cert);
astakos.get('/projects/' + project_id,
{'X-Auth-Token' : clouds[cloud].token}, 200,
populate_projects, handle_err);
});
}
function handle_err(data) {
console.error("error === " + data);
toggle_msgs(data, "msg", false);
$("#loader").hide();
}
function get_projects() {
$("#loader").show();
var cloud = $("#cloud").val();
$("#project").empty();
if(cloud) {
var astakos = getClient('astakos_account',
clouds[cloud].astakos_account, clouds[cloud].token,
clouds[cloud].cert);
astakos.get('/quotas',
{'X-Auth-Token' : clouds[cloud].token}, 200,
get_quotas, handle_err);
}
}
</script>
<div class="row">
......@@ -299,7 +369,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</div>
<div class="small-9 columns error"
id="cloud-error">
<select id="cloud" onchange="check_cloud();">
<select id="cloud" onchange="check_cloud();
get_projects()">
</select>
<small class="error"></small>
</div>
</div>
<div class="clearfix">
<div class="small-3 columns">
<label id="project_label" for="project"
class="right inline">Project</label>
</div>
<div class="small-9 columns error"
id="project-error">
<select id="project">
</select>
<small class="error"></small>
</div>
......
......@@ -81,6 +81,7 @@ function hide_error_divs() {
$('#directory-error small').hide();
$('#res-directory-error small').hide();
$('#cloud-error small').hide();
$('#project-error small').hide();
$('#res-cloud-error small').hide();
$('#passphrase-error small').hide();
$('#res-passphrase-error small').hide();
......
......@@ -120,6 +120,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
var url_error = null;
var pithos_public = null;
var account_ui = null;
var astakos_account = null;
function check_cloud_url() {
var url = $('#cloud-url').val().replace(/^\s+|\s+$/gm,'');
......@@ -160,6 +161,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
console.log('Failed to get account_ui '
+ err);
}
try {
astakos_account =
endpoint['endpoints'][0]['publicURL'];
} catch(err) {
console.log('Failed to get astakos_account '
+ err);
}
break;
}
});
......@@ -270,6 +278,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
cloud.auth_url = url;
cloud.token = token;
cloud.pithos_public = pithos_public;
cloud.astakos_account = astakos_account;
cloud.uuid = uuid;
if(cert != DEFAULT_CERT) cloud.cert = cert;
......
......@@ -122,6 +122,37 @@ var Client = function(endpointURL, token, CAPath) {
_req.end();
_req.on('error', handle_err);
};
this.get = function(path, headers, status, handle_res, handle_err) {
handle_res = handle_res || function(r){ console.log(r); };
handle_err = handle_err || function(e){ console.log(e); };
var get_opts = util._extend({
path: _endpoint + path,
}, _options);
get_opts.headers = util._extend({}, _options.headers);
util._extend(get_opts.headers, headers);
h = get_opts.headers;
util._extend(get_opts.headers, {
'Content-Type': h['Content-Type'] || 'application/json'
});
_req = _protocol.request(get_opts, function(res){
_recv_data = '';
res.on('data', function(d) { _recv_data+=d; });
res.on('end', function() {
if ((status || 200) !== res.statusCode) {
handle_err(res.statusCode + " " +
res.statusMessage, res.headers);
} else handle_res(_recv_data, res.headers);
});
});
_req.end();
_req.on('error', handle_err);
};
};
module.exports = { Client: Client };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment