Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added admin interface for managing VM's

  • Loading branch information...
commit e9394f0d5bfeb93689b0cd431a84fba6d11b92b6 1 parent 0f95381
@Licenser Licenser authored
View
201 apps/wiggle/priv/js/snarl.js
@@ -4,14 +4,15 @@ var debug;
!function ($){
permissions.data = new Object();
var selected;
- var vm_permissions = [
- "start",
- "stop",
- "reboot",
- "edit",
- "delete",
- "vnc"
- ];
+ var vm_permissions = {
+ "start": true,
+ "stop": true,
+ "reboot": true,
+ "edit": true,
+ "delete": true,
+ "vnc": true,
+ "info": true
+ };
var wiggle = {
"module": {
"about": true,
@@ -96,10 +97,34 @@ var debug;
}
}
};
+ var dataset = {
+ "create": true,
+ "Name": {
+ "get": true,
+ "delete": true
+ }
+ }
+ var pkg = {
+ "create": true,
+ "Name": {
+ "get": true,
+ "delete": true
+ }
+ }
+ var key = {
+ "create": true,
+ "Name": {
+ "get": true,
+ "delete": true
+ }
+ }
permissions.data["permission"] = permission;
permissions.data["option"] = option
permissions.data["group"] = group;
permissions.data["user"] = user;
+ permissions.data["dataset"] = dataset;
+ permissions.data["package"] = pkg;
+ permissions.data["key"] = key;
permissions.data["service"] = {
"wiggle": wiggle,
"sniffle": sniffle,
@@ -114,7 +139,8 @@ var debug;
}
};
permissions.data["vm"] = {
- "Name": vm_permissions
+ "Name": vm_permissions,
+ "create": true
};
permissions.get = function(ks, current) {
if (current == undefined) {
@@ -158,7 +184,7 @@ var debug;
};
};
if (show_delete) {
- var btn = $("<button>(revoke)</button>").
+ var btn = $("<button class='btn-mini btn-danger'>revoke</button>").
click(function () {
var type = selected.type;
var id = selected.id;
@@ -190,24 +216,66 @@ var debug;
};
}
function get_user(user) {
- $.getJSON("/my/users/" + user + "/own_permissions", function(data) {
+ $.getJSON("/my/users/" + user + "/permissions", function(data) {
get_user_groups(user);
show_user(data);
});
};
+ function add_user_group(groups, group) {
+ var li = $("<li></li>");
+ var btn = $("<button class='btn-mini btn-danger'>remove</button>");
+ var h4 = $("<h4></h4>").text(group).
+ append(btn);
+ btn.click(function (){
+ $.ajax({
+ url: "/my/users/" + selected.id + "/groups/" + group,
+ type: 'DELETE',
+ success: function (new_group) {
+ li.remove();
+ }
+ });
+ });
+ groups.append(
+ li.append(h4).
+ append($("<ul></ul>").
+ attr("id", "permissions_" + group)));
+ get_group(group, group);
+
+ }
function show_user_groups(data) {
var groups = $("<ul></ul>")
$("#user_groups").
empty().
- append($("<h3>Groups</h3>")).
+ append($("<h3>Groups<select id='group_to_add'></select><button id='add_group_btn' class='btn-mini btn-success'>+</button></h3>")).
append(groups);
+ $("#add_group_btn").click(function() {
+ var group = $("#group_to_add").val();
+ if (group != "") {
+ $.ajax({
+ url: "/my/users/" + selected.id + "/groups",
+ type: 'POST',
+ dataType: 'json',
+ data: {
+ "group": group
+ },
+ success: function (new_group) {
+ add_user_group(groups,new_group);
+ }
+ });
+ }
+ });
+ $.getJSON("/my/groups", function(groups){
+ $("#group_to_add").
+ empty().
+ append($("<option></option>"));
+ for (var i=0; i < groups.length; i++) {
+ $("#group_to_add").append(
+ $("<option></option>").
+ append(groups[i]));
+ }
+ });
for (var i = 0; i < data.length; i++) {
- groups.append(
- $("<li></li>").
- append($("<h4></h4>").text(data[i])).
- append($("<ul></ul>").
- attr("id", "permissions_" + data[i])));
- get_group(data[i], data[i]);
+ add_user_group(groups,data[i]);
};
}
function get_user_groups(user) {
@@ -238,18 +306,28 @@ var debug;
return select;
}
function add_list_user(user) {
- $("#users").append(
- $("<li></li>").
- text(user).
- click(function() {
- get_user(user);
- get_user_groups(user);
- selected = {
- type: "users",
- id: user
- };
- })
- );
+ var li=$("<li></li>").
+ text(user).
+ click(function() {
+ selected = {
+ type: "users",
+ id: user
+ };
+ get_user(user);
+ });
+ var btn = $("<button class='btn-mini btn-danger'>x</button>");
+ li.append(btn);
+ btn.click(function() {
+ li.unbind("click");
+ $.ajax({
+ url: "/my/users/" + user,
+ type: 'DELETE',
+ success: function () {
+ li.remove();
+ }
+ });
+ });
+ $("#users").append(li);
};
function list_users(data) {
$("#users").empty();
@@ -261,24 +339,37 @@ var debug;
snarl.show_users = function() {
$.getJSON("/my/users", list_users);
}
+ function add_list_group(group) {
+ var btn = $("<button class='btn-mini btn-danger'>x</button>");
+ var li=$("<li></li>").
+ text(group).
+ click(function() {
+ debug = this;
+ console.log(1);
+ selected = {
+ type: "groups",
+ id: group
+ };
+ get_group(group);
+
+ });
+ li.append(btn);
+ btn.click(function() {
+ li.unbind("click");
+ $.ajax({
+ url: "/my/groups/" + group,
+ type: 'DELETE',
+ success: function (data) {
+ li.remove();
+ }
+ });
+ });
+ $("#groups").append(li);
+ }
function list_groups(data) {
- var groups = $("#groups");
- groups.empty();
+ $("#groups").empty();
for (var i = 0; i < data.length; i++) {
-
- (function(d){
- groups.append(
- $("<li></li>").
- text(d).
- click(function() {
- selected = {
- type: "groups",
- id: d
- };
- get_group(d);
- })
- );
- })(data[i]);
+ add_list_group(data[i]);
};
}
snarl.show_groups = function() {
@@ -330,10 +421,23 @@ var debug;
"pass": pass
},
success: function (data) {
- data
+ add_list_user(data);
+ }
+ });
+ };
+ function add_group() {
+ var name = $("#group_name").val();
+ $.ajax({
+ url: "/my/groups",
+ type: 'POST',
+ dataType: 'json',
+ data: {
+ "name": name
+ },
+ success: function (data) {
+ add_list_group(data);
}
});
-
}
snarl.init = function() {
var selects = $("#new_permission select");
@@ -346,6 +450,7 @@ var debug;
snarl.show_groups();
$("#add_permission").click(add_permission);
$("#add_user").click(add_user);
+ $("#add_group").click(add_group);
};
function add_permission() {
var type = selected.type;
View
90 apps/wiggle/src/wiggle_handler.erl
@@ -288,12 +288,12 @@ request('POST', [<<"my">>, <<"users">>], Auth, Req, State) ->
Login = proplists:get_value(<<"login">>, Vals),
Pass = proplists:get_value(<<"pass">>, Vals),
case libsnarl:user_add(Auth, Login, Pass) of
- {ok, UUID} ->
- {ok, Res} = libsnarl:user_own_permissions(Auth, UUID),
- reply_json(Req, Login, State);
+ {ok, _UUID} ->
+ reply_json(Req1, Login, State);
_ ->
- error_page(403, Req, State)
+ error_page(403, Req1, State)
end;
+
request('POST', [<<"my">>, <<"users">>, User, <<"permissions">>], Auth, Req, State) ->
case libsnarl:user_get(system, User) of
{ok, UUID} ->
@@ -320,29 +320,88 @@ request('DELETE', [<<"my">>, <<"users">>, User, <<"permissions">>], Auth, Req, S
error_page(403, Req, State)
end;
-request('GET', [<<"my">>, <<"users">>, User, <<"own_permissions">>], Auth, Req, State) ->
+request('GET', [<<"my">>, <<"users">>, User, <<"groups">>], Auth, Req, State) ->
case libsnarl:user_get(system, User) of
{ok, UUID} ->
- {ok, Res} = libsnarl:user_own_permissions(Auth, UUID),
- reply_json(Req, encode_permissions(Res), State);
+ {ok, Res} = libsnarl:user_groups(Auth, UUID),
+ reply_json(Req,[Name ||
+ {ok, Name} <- [libsnarl:group_name(system, G) || G <- Res]], State);
_ ->
error_page(403, Req, State)
end;
-request('GET', [<<"my">>, <<"users">>, User, <<"groups">>], Auth, Req, State) ->
- case libsnarl:user_get(system, User) of
+request('POST', [<<"my">>, <<"users">>, User, <<"groups">>], Auth, Req, State) ->
+ {Vals, Req1} = cowboy_http_req:body_qs(Req),
+ Group = proplists:get_value(<<"group">>, Vals),
+ case {libsnarl:user_get(system, User), libsnarl:group_get(Auth, Group)} of
+ {{ok, UUID}, {ok, GUUID}} ->
+ case libsnarl:user_add_to_group(Auth, UUID, GUUID) of
+ ok ->
+ reply_json(Req1,Group, State);
+ _ ->
+ error_page(403, Req1, State)
+ end;
+ _ ->
+ error_page(403, Req, State)
+ end;
+
+request('DELETE', [<<"my">>, <<"users">>, User, <<"groups">>, Group], Auth, Req, State) ->
+ case {libsnarl:user_get(system, User), libsnarl:group_get(Auth, Group)} of
+ {{ok, UUID}, {ok, GUUID}} ->
+ case libsnarl:user_delete_from_group(Auth, UUID, GUUID) of
+ ok ->
+ {ok, Res} = libsnarl:user_groups(Auth, UUID),
+ reply_json(Req,[Name ||
+ {ok, Name} <- [libsnarl:group_name(system, G) || G <- Res]], State);
+ _ ->
+ error_page(403, Req, State)
+ end;
+ _ ->
+ error_page(403, Req, State)
+ end;
+
+request('DELETE', [<<"my">>, <<"users">>, User], Auth, Req, State) ->
+ case libsnarl:user_get(Auth, User) of
{ok, UUID} ->
- {ok, Res} = libsnarl:user_groups(Auth, UUID),
- reply_json(Req,[Name ||
- {ok, Name} <- [libsnarl:group_name(system, G) || G <- Res]], State);
+ case libsnarl:user_delete(Auth, UUID) of
+ ok ->
+ reply_json(Req, User, State);
+ _ ->
+ error_page(403, Req, State)
+ end;
_ ->
error_page(403, Req, State)
end;
+
+
request('GET', [<<"my">>, <<"groups">>], Auth, Req, State) ->
{ok, Res} = libsnarl:group_list(Auth),
reply_json(Req, Res, State);
+request('DELETE', [<<"my">>, <<"groups">>, Group], Auth, Req, State) ->
+ case libsnarl:group_get(Auth, Group) of
+ {ok, UUID} ->
+ case libsnarl:group_delete(Auth, UUID) of
+ ok ->
+ reply_json(Req, Group, State);
+ _ ->
+ error_page(403, Req, State)
+ end;
+ _ ->
+ error_page(403, Req, State)
+ end;
+
+request('POST', [<<"my">>, <<"groups">>], Auth, Req, State) ->
+ {Vals, Req1} = cowboy_http_req:body_qs(Req),
+ Name = proplists:get_value(<<"name">>, Vals),
+ case libsnarl:group_add(Auth, Name) of
+ {ok, _UUID} ->
+ reply_json(Req1, Name, State);
+ _ ->
+ error_page(403, Req1, State)
+ end;
+
request('GET', [<<"my">>, <<"groups">>, Group, <<"permissions">>], Auth, Req, State) ->
case libsnarl:group_get(system, Group) of
@@ -423,13 +482,6 @@ request('POST', [<<"my">>, <<"machines">>], Auth, Req, State) ->
Name = proplists:get_value(<<"name">>, Vals),
Package = proplists:get_value(<<"package">>, Vals),
Dataset = proplists:get_value(<<"dataset">>, Vals),
- Obj = [{<<"package">>, Package}, {<<"dataset">>, Dataset}],
- Obj1 = case Name of
- <<>> ->
- Obj;
- _ ->
- [{<<"name">>, Name} | Obj]
- end,
case libsniffle:create_machine(Auth, Name, Package, Dataset, [], []) of
{ok, Res} ->
io:format("create-ok: ~p~n", [Res]),
View
12 apps/wiggle/templates/admin.dtl
@@ -6,16 +6,16 @@
<div class="span3">
<h2>Users</h2>
<div class="well">
- <label>Name</label><input type="text" id="user_name"/></br>
- <label>Password</label><input type="password" id="user_pass"/></br>
- <button id="add_user">Add</button>
+ <label class="control-label" for="user_name">Name</label><input type="text" id="user_name"/></br>
+ <label class="control-label" for="user_pass">Password</label><input type="password" id="user_pass"/></br>
+ <button id="add_user" class="btn btn-success">Add</button>
<ul id="users">
</ul>
</div>
<h2>Groups</h2>
<div class="well">
- <label>Name</label><input type="text" id="group_name"/></br>
- <button id="add_group">Add</button>
+ <label class="control-label" for="group_name">Name</label><input type="text" id="group_name"/></br>
+ <button id="add_group" class="btn btn-success">Add</button>
<ul id="groups">
</ul>
@@ -29,7 +29,7 @@
<div id="user_groups"></div>
<div id="new_permission">
<select></select>
- </div><button id="add_permission">Add</button>
+ </div><button id="add_permission" class="btn-mini btn-success">Add</button>
</div>
</div>
{% endblock %}
Please sign in to comment.
Something went wrong with that request. Please try again.