cloud-list.html 19.1 KB
Newer Older
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
1
<!--
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
2
Copyright (C) 2015-2016 GRNET S.A.
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16

This program 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/>.
-->
17

Efthymia Bika's avatar
Efthymia Bika committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
<script type="text/javascript">

    load_data_from_file(CLOUDS_CONF_FILE, render_clouds);

    function show_delete_cloud_modal(cloud) {
        $("#del-cloud-modal-p").html("Are you sure you want to delete "
            + cloud.name + '?');
        $("#delete-cloud-modal").foundation("reveal", "open");
        var i = 0;
        $("#delete-cloud-modal").on('close.fndtn.reveal', function(e) {
            if(e.namespace !== "fndtn.reveal") return;
            i++;
            $(this).click(function(event) {
                // event is fired more than once so have to check
                if(event.target.id == "del-cloud-modal-accept" && i == 1) {
                    delete_cloud(cloud);
Efthymia Bika's avatar
Efthymia Bika committed
34
                }
Efthymia Bika's avatar
Efthymia Bika committed
35 36 37 38 39 40 41 42 43 44 45 46 47
            });
        });
    }

    function render_clouds(data) {
        $("#clouds_list").remove();
        // clouds is undefined the first time, when
        // reading data from file
        if(typeof clouds === 'undefined') {
            if(data != "") {
                clouds = JSON.parse(data);
            } else {
                clouds = new Object();
Efthymia Bika's avatar
Efthymia Bika committed
48
            }
Efthymia Bika's avatar
Efthymia Bika committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        }
        var ul = $("<ul></ul>")
            .attr("id", "clouds_list")
            .attr("class", "side-nav");
        $.each(clouds, function(i, cloud) {
            var li = $("<li></li>")
                .attr("class", "clearfix")
                .attr("id", "li_" + cloud.name);
            var a = $("<a>" + cloud.name + "</a>")
                .attr("href", "#")
                .attr("id", cloud.name)
                .attr("class", "left")
                .click(function() {
                    load_cloud(cloud);
                    activate_li("li_" + cloud.name);
                });
            li.append(a);

            var a_del = $(
                "<a><i class='fa fa-times-circle delete_icon'></i></a>")
                .attr("href", "#")
                .attr("class", "right")
                .click(function() {
                    show_delete_cloud_modal(cloud);
Efthymia Bika's avatar
Efthymia Bika committed
73
                });
Efthymia Bika's avatar
Efthymia Bika committed
74 75 76 77 78 79 80 81 82 83 84 85 86
            li.append(a_del);

            ul.append(li);
            $("#sidebar").append(ul);
        });
    }

    function activate_li(id) {
        $("li").each(function(i, li) {
            if($(this).is("#" + id)) {
                $(this).addClass("active-li");
            } else {
                $(this).removeClass("active-li");
Efthymia Bika's avatar
Efthymia Bika committed
87
            }
Efthymia Bika's avatar
Efthymia Bika committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        });
    }

    var errors = {
        cloud_url_empty: 'Provide a Cloud Authentication URL',
        cloud_inaccessible: 'Cloud URL did not respond as expected',
        token_empty: 'Provide a user token',
        token_error: 'Failed to authenticate',
        token_cloudless: 'No cloud to try this token against',
        cloud_name_empty: 'Provide a Cloud Name',
        cloud_name_illegal: 'Invalid Entry. Allowed characters [A-Za-z0-9-_]'
    };

    function check_cloud_name() {
        var cloud_name = $('#cloud-name').val().replace(/^\s+|\s+$/gm,'');
        if(!cloud_name) {
            $('#cloud-name-error small').text(errors.cloud_name_empty);
            $('#cloud-name-error small').show();
            return false;
        } else {
            var pattern = /^[\w.\-]+$/;
            if(!cloud_name.match(pattern)) {
                $('#cloud-name-error small').text(errors.cloud_name_illegal);
                $('#cloud-name-error small').show();
                return false;
            } else {
                $('#cloud-name-error small').hide();
Efthymia Bika's avatar
Efthymia Bika committed
115
            }
Efthymia Bika's avatar
Efthymia Bika committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        }
        return true;
    }

    var url_error = null;
    var pithos_public = null;
    var account_ui = null;

    function check_cloud_url() {
        var url = $('#cloud-url').val().replace(/^\s+|\s+$/gm,'');
        if(!url) {
            $('#cloud-error small').text(errors.cloud_url_empty);
            $('#cloud-error small').show();
            $("#login").hide();
        } else {
            var cert_path = $("#cert-path").html();
            try {
                var astakos = getClient(
                        'astakos_notoken', url, null, cert_path);
            } catch (err) {
                console.log(err);
                return;
Efthymia Bika's avatar
Efthymia Bika committed
138
            }
139

Efthymia Bika's avatar
Efthymia Bika committed
140 141 142 143 144 145
            function handle_result(data) {
                $("#login").show();
                var endpoints = JSON.parse(data).access.serviceCatalog
                $.each(endpoints, function(i, endpoint) {
                    switch(endpoint.type) {
                    case 'object-store':
Efthymia Bika's avatar
Efthymia Bika committed
146
                        try {
Efthymia Bika's avatar
Efthymia Bika committed
147 148 149 150 151 152
                            pithos_public =
                                endpoint['endpoints'][0]['publicURL'];
                            $('#cloud-error small').hide();
                            url_error = null;
                        } catch(err) {
                            console.log('Failed to get pithos_public ' + err);
Efthymia Bika's avatar
Efthymia Bika committed
153
                        }
Efthymia Bika's avatar
Efthymia Bika committed
154 155 156 157 158 159 160 161 162 163
                        break;
                        case 'account':
                            try {
                                account_ui =
                                    endpoint['endpoints'][0]['SNF:uiURL'];
                            } catch(err) {
                                console.log('Failed to get account_ui '
                                    + err);
                            }
                        break;
164
                    }
Efthymia Bika's avatar
Efthymia Bika committed
165
                });
166
            }
Efthymia Bika's avatar
Efthymia Bika committed
167

Efthymia Bika's avatar
Efthymia Bika committed
168 169 170 171 172 173 174 175
            function handle_err(err) {
                pithos_public = null;
                url_error = err;
                console.log(url_error);
                $('#cloud-error small').text(errors.cloud_inaccessible +
                        ' [' + url_error  + ']');
                $('#cloud-error small').show();
                $("#login").hide();
176
            }
Efthymia Bika's avatar
Efthymia Bika committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
            astakos.post('/tokens', null, null, 200,
                    handle_result, handle_err);
        }
    }

    var auth_error = null;
    var uuid = null;

    function check_token() {
        var token = $('#token').val().replace(/^\s+|\s+$/gm,'');
        if (!token) {
            $('#token-error small').text(errors.token_empty);
            $('#token-error small').show();
        } else {
            var url = $('#cloud-url').val().replace(/^\s+|\s+$/gm,'');
            if (pithos_public && url) {
                var cert_path = $("#cert-path").html();
                try {
                    var astakos =
                        getClient('astakos', url, token, cert_path);
                } catch (err) {
                    handle_err(err);
                    return;
Efthymia Bika's avatar
Efthymia Bika committed
200
                }
201

Efthymia Bika's avatar
Efthymia Bika committed
202 203 204 205 206 207
                function handle_err(err) {
                    auth_error = err;
                    console.log(auth_error);
                    $('#token-error small').text(
                        errors.token_error + ' [' + auth_error + ']');
                    $('#token-error small').show();
Efthymia Bika's avatar
Efthymia Bika committed
208
                }
Efthymia Bika's avatar
Efthymia Bika committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
                function handle_result(data) {
                    auth_error = null;
                    $('#token-error small').hide();
                    uuid = JSON.parse(data).access.token.tenant.id;
                }
                var send_data =
                    JSON.stringify({ auth: { token: { id: token } } });
                astakos.post(
                    '/tokens', null, send_data, 200,
                    handle_result,
                    handle_err
                );
            } else {
                $('#token-error small').text(errors.token_cloudless);
                $('#token-error small').show();
Efthymia Bika's avatar
Efthymia Bika committed
224
            }
Efthymia Bika's avatar
Efthymia Bika committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        }
    }

    function check_fields() {
        check_cloud_url();
        check_token();
        if(auth_error || url_error || !pithos_public || !check_cloud_name())
            return false;
        return true;
    }

    function load_cloud(cloud) {
        $('#cloud-name-error small').hide();
        $('#cloud-error small').hide();
        $('#token-error small').hide();
        $('#cert-path-error small').hide();

242
        $("#cert-path").html(DEFAULT_CERT);
Efthymia Bika's avatar
Efthymia Bika committed
243 244 245 246 247 248
        if(cloud) {
            $("#cloud-name").val(cloud.name);
            $("#cloud-url").val(cloud.auth_url);
            $("#token").val(cloud.token);
            if(cloud.cert) {
                $("#cert-path").html(cloud.cert);
Efthymia Bika's avatar
Efthymia Bika committed
249
            }
Efthymia Bika's avatar
Efthymia Bika committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
        } else {
            $("#cloud-name").val('');
            $("#cloud-url").val('');
            $("#token").val('');
            $("#login").hide();
            activate_li("dummy");
        }

        $("#cloud_details").show();
        toggle_default_button();
    }

    function edit_add_cloud() {
        var cloud_name = $("#cloud-name").val().replace(/^\s+|\s+$/gm,'');
        var url = $("#cloud-url").val().replace(/^\s+|\s+$/gm,'');
        var token = $("#token").val().replace(/^\s+|\s+$/gm,'');
        var cert = $("#cert-path").html().replace(/^\s+|\s+$/gm,'');

        var cloud = {};
        cloud.name = cloud_name;
        cloud.auth_url = url;
        cloud.token = token;
        cloud.pithos_public = pithos_public;
        cloud.uuid = uuid;
274
        if(cert != DEFAULT_CERT) cloud.cert = cert;
275

Efthymia Bika's avatar
Efthymia Bika committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
        clouds[cloud_name] = cloud;
        render_clouds("");
        activate_li("li_" + cloud.name);
        write_conf_file(CLOUDS_CONF_FILE, clouds);
        $("#save-alert").show();
        setTimeout( function() {$("#save-alert").fadeOut()}, 1000);
    }

    function delete_cloud(cloud) {
        delete clouds[cloud.name];
        render_clouds("");
        write_conf_file(CLOUDS_CONF_FILE, clouds);
        $("#cloud_details").hide();
    }

    function remove_cookies(win, url) {
        var removed_at_least_one = false
        win.cookies.getAll({url: url}, function(cookies) {
            $.each(cookies, function(i, cookie) {
                win.cookies.remove({url: url, name: cookie.name} );
                removed_at_least_one = true;
            });
        });
        return removed_at_least_one;
    }

    var cred_win = null;
    var logout_win = null;
    var got_cookie = false;

    function extract_credentials(cookie) {
        var credentials = cookie.value.split('%7C');
        var uuid = credentials[0];
        var token = credentials[1];
        $('#token').val(token);
        $('#token').trigger('change');
    }

    function get_credentials() {
        var cookie_name = '_pithos2_a';
        var lurl = account_ui + '/logout?next=' + pithos_public
        got_cookie = false;
        cred_win = gui.Window.open(lurl, {
            focus: true, width: 820, height: 780, toolbar: false
        });
        cred_win.cookies.onChanged.addListener(function(info) {
            if (info.cookie.name === cookie_name) {
                console.log('Succesfully logged in');
                extract_credentials(info.cookie);
                got_cookie = true;
Efthymia Bika's avatar
Efthymia Bika committed
326
            }
Efthymia Bika's avatar
Efthymia Bika committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        });
        cred_win.on('loaded', function() {
            if (got_cookie) cred_win.close();
        });
        cred_win.on('closed', function() {
            logout_win = gui.Window.open(
                account_ui + '/logout',
                {focus: true, width:20, height: 20 });
            logout_win.hide();
            logout_win.on('loaded', function() {
                while(remove_cookies(logout_win, pithos_public)) {}
                logout_win.close();
            });
        });
    }

    function toggle_default_button() {
        if($("#cert-path").html() == DEFAULT_CERT) {
            $("#default-button").addClass("disabled");
            $("#default-button").prop("disabled", true);
        } else {
            $("#default-button").removeClass("disabled");
            $("#default-button").prop("disabled", false);
        }
    }

    window.setInterval(function() {
        if($("#clouds_page").attr("id") &&
            $("#cloud-url").val()) {
            check_cloud_url();
            check_token();
        }
    }, 1000);
</script>

<div class="row" id="clouds_page">
    <div class="small-9 columns">
        <p>&nbsp;</p>
    </div>
</div>
<div class="row">
    <div class="small-3 columns vertical-border" id="sidebar">
        <a href="#" onclick="load_cloud();" class="button small radius">
            <i class="fa fa-plus-circle">&nbsp;Add Cloud</i>
        </a>
    </div>
    <div class="small-9 columns">
        <div id="cloud_details" class="hide">
            <div id="save-alert" class="hide">
                <div data-alert class="alert-box success radius">
                    Successfully saved cloud configuration.
                    <a href="#" class="close">&times;</a>
                </div>
Efthymia Bika's avatar
Efthymia Bika committed
380
            </div>
Efthymia Bika's avatar
Efthymia Bika committed
381 382 383 384 385 386 387 388 389 390 391
            <form>
                <div class="clearfix">
                    <div class="small-3 columns">
                        <label id="cloud_name_label" for="cloud-name"
                            class="right inline">Name</label>
                    </div>
                    <div class="small-9 columns error" id="cloud-name-error">
                        <input type="text" id="cloud-name"
                            placeholder="Cloud Name [A-Za-z0-9-_]"
                            onchange="check_cloud_name();">
                        <small class="error"></small>
392
                    </div>
Efthymia Bika's avatar
Efthymia Bika committed
393 394 395 396 397 398 399
                </div>
                <div class="clearfix">
                    <div class="small-3 columns">
                        <label id="url_label" for="cloud-url"
                            class="right inline">
                            Cloud URL&nbsp;<i
                                class="fa fa-info-circle green-info"
400
                                onclick="$('#auth-help').slideDown();"></i>
Efthymia Bika's avatar
Efthymia Bika committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
                        </label>
                    </div>
                    <div class="small-9 columns error" id="cloud-error">
                        <input type="text" id="cloud-url"
                            placeholder="Authentication URL"
                            onchange="check_cloud_url();">
                        <small class="error"></small>
                    </div>
                </div>
                <div class="clearfix">
                    <div class="small-3 columns">
                        <label id="token_label" for="token"
                            class="right inline">
                            User token&nbsp;<i
                                class="fa fa-info-circle green-info"
                                onclick="$('#auth-help').slideDown();"></i>
                        </label>
                    </div>
                    <div class="small-9 columns error" id="token-error">
                        <input type="text" id="token" placeholder="User token"
                        onchange="check_token();">
                        <small class="error"></small>
                    </div>
                    <div class="clearfix hide" id="login">
                        <div class="small-8 columns"></div>
                        <a id="get_creds"
                            class="small-4 columns button
                            right tiny login-button"
                            onclick="get_credentials();">
                            Auto-fill token with login
                        </a>
                    </div>
                </div>
                <div class="clearfix">
                    <div class="small-3 columns"></div>
                    <div class="small-9 columns hide info-text panel"
                        id="auth-help">
                        <i class="fa fa-times right"
                            onclick="$('#auth-help').slideUp();"></i>
                        To get your cloud credentials,
                        browse to your cloud website, log in
                        and click on <u>API access</u>.
                        Copy the Authentication URL and Token above.
                    </div>
                </div>
                <div class="clearfix">
447
                    <div class="small-5 columns">
448
                        <a onclick="toggle_settings('cert-path-div',
449 450 451
                            'more_icon', 'Show Certificate Settings',
                            'Hide Certificate Settings');
                            toggle_default_button()">
452
                            <i class="fa fa-plus-square-o"
453
                                id="more_icon">&nbsp;Show Certificate Settings</i>
454
                        </a>
Efthymia Bika's avatar
Efthymia Bika committed
455 456 457
                    </div>
                </div>
                <div class="clearfix hide" id="cert-path-div">
458
                    <fieldset>
Efthymia Bika's avatar
Efthymia Bika committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
                    <div class="small-3 columns">
                        <label id="cert-path-label" for="cert-path"
                            class="right inline">
                            Certificate
                        </label>
                    </div>
                    <div id="cert-path" class="small-5 columns dir-field">
                        &nbsp;
                    </div>
                    <div id="dirdialogue_label"
                        onclick="$('#choose-cert-path').trigger('click');"
                        class="small-1 columns" id="dirpick">
                        <i class="fa fa-folder-o blue-folder">&nbsp;</i>
                    </div>
                    <input type="file" id="choose-cert-path" nwfile
                        style="display:none;"
475 476
                        onchange="if($(this).val())
                            $('#cert-path').html($(this).val());
Efthymia Bika's avatar
Efthymia Bika committed
477 478 479 480 481 482 483 484 485
                        toggle_default_button()"/>
                    <div class="small-3 columns">
                        <a href="#"
                            onclick="$('#cert-path').html(DEFAULT_CERT);
                            toggle_default_button()"
                            class="button tiny radius" id="default-button">
                            Use default
                        </a>
                    </div>
486
                    </fieldset>
Efthymia Bika's avatar
Efthymia Bika committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
                </div>
                <div class="clearfix">
                    <div class="small-12 columns">&nbsp;</div>
                </div>
                <div class="clearfix">
                    <div class="small-8 columns"></div>
                    <a id="save_button" class="button radius right small"
                        onclick="if(check_fields()) edit_add_cloud();">
                        Save
                    </a>
                </div>
        </form>
    </div>
</div>
<div id="delete-cloud-modal" class="reveal-modal medium"
    data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
    <p id="del-cloud-modal-p"></p>
    <a id="del-cloud-modal-cancel" class="button radius small"
        onclick="$('#delete-cloud-modal').foundation('reveal', 'close');">
        Cancel
    </a>
    <a id="del-cloud-modal-accept" class="button radius small alert right"
        onclick="$('#delete-cloud-modal').foundation('reveal', 'close');">
        Delete
    </a>
    <a class="close-reveal-modal" aria-label="Close">&#215;</a>
</div>
514