~ubuntu-branches/ubuntu/utopic/maas/utopic-security

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
/* Copyright 2014 Canonical Ltd.  This software is licensed under the
 * GNU Affero General Public License version 3 (see the file LICENSE).
 *
 * Image views.
 *
 * @module Y.maas.image_views
 */

YUI.add('maas.image_views', function(Y) {

Y.log('loading maas.image_views');
var module = Y.namespace('maas.image_views');

var BOOT_RESOURCE_TYPE = Y.maas.enums.BOOT_RESOURCE_TYPE;


/**
 * A base view class to display a set of Images (Y.maas.image.Image).
 *
 * It will load the list of images (in this.modelList) when rendered
 * for the first time and changes to this.modelList will trigger
 * re-rendering.
 *
 * You can provide your custom rendering method by defining a 'render'
 * method (also, you can provide methods named 'loadImagesStarted' and
 * 'loadImagesEnded' to customize the display during the initial loading of the
 * visible images and a method named 'displayGlobalError' to display a message
 * when errors occur during loading).
 *
 */
module.ImageListLoader = Y.Base.create('imageListLoader', Y.View, [], {

    initializer: function(config) {
        this.modelList = new Y.maas.image.ImageList();
        this.loaded = false;
    },

    render: function () {
    },

    /**
     * Add a loader, a Y.IO object. Events fired by this IO object will
     * be followed, and will drive updates to this object's model.
     *
     * It may be wiser to remodel this to consume a YUI DataSource. That
     * would make testing easier, for one, but it would also mean we can
     * eliminate our polling code: DataSource has support for polling
     * via the datasource-polling module.
     *
     * @method addLoader
     */
    addLoader: function(loader) {
        loader.on("io:start", this.loadImagesStarted, this);
        loader.on("io:end", this.loadImagesEnded, this);
        loader.on("io:failure", this.loadImagesFailed, this);
        loader.on("io:success", function(id, request) {
            this.loadImages(request.responseText);
        }, this);
    },

    /**
     * Load the images from the given data.
     *
     * @method loadImages
     */
    loadImages: function(data) {
        try {
            var parsed = JSON.parse(data);
            this.regionImportRunning = parsed.region_import_running;
            this.clusterImportRunning = parsed.cluster_import_running;
            this.mergeImages(parsed.resources);
        }
        catch(e) {
            this.loadImagesFailed();
        }
        this.loaded = true;
        this.render();
    },

    /**
     * Process an array of images, merging them into modelList with the
     * fewest modifications possible.
     *
     * @method mergeImages
     */
    mergeImages: function(images) {
        var self = this;
        var imagesByID = {};
        Y.Array.each(images, function(image) {
            imagesByID[image.id] = image;
        });
        var modelsByID = {};
        this.modelList.each(function(model) {
            modelsByID[model.get("id")] = model;
        });

        Y.each(imagesByID, function(image, id) {
            var model = modelsByID[id];
            if (Y.Lang.isValue(model)) {
                // Compare the image and the model.
                var modelAttrs = model.getAttrs();
                var modelChanges = {};
                Y.each(modelAttrs, function(value, key) {
                    if (image[key] !== value) {
                        modelChanges[key] = image[key];
                    }
                });
                // Update the image.
                model.setAttrs(modelChanges);
            }
            else {
                // Add the image.
                self.modelList.add(image);
            }
        });

        Y.each(modelsByID, function(model, id) {
            // Remove models that don't correspond to a image.
            if (!Y.Object.owns(imagesByID, id)) {
                self.modelList.remove(model);
            }
        });
    },

   /**
    * Function called if an error occurs during the initial loading.
    *
    * @method displayGlobalError
    */
    displayGlobalError: function (error_message) {
    },

   /**
    * Function called when the Image list starts loading.
    *
    * @method loadImagesStarted
    */
    loadImagesStarted: function() {
    },

   /**
    * Function called when the Image list has loaded.
    *
    * @method loadImagesEnded
    */
    loadImagesEnded: function() {
    },

    /**
     * Function called when the Image list failed to load.
     *
     * @method loadImagesFailed
     */
    loadImagesFailed: function() {
        this.displayGlobalError('Unable to load boot images.');
    }

});

/**
 * A customized view based on ImageListLoader that will display the
 * images view.
 */
module.ImagesView = Y.Base.create(
    'imagesView', module.ImageListLoader, [], {

    regionImportingText: 'Step 1/2: Region importing',
    clusterImportingText: 'Step 2/2: Clusters importing',


    initializer: function(config) {
        this.srcNode = Y.one(config.srcNode);
        this.loader = this.srcNode.one(config.loader);
        this.content = this.srcNode.one(config.content);
        this.importer = this.srcNode.one(config.importer);
        this.ubuntuOptions = this.srcNode.one(config.ubuntuOptions);
        this.ubuntuSpinner = this.srcNode.one(config.ubuntuSpinner);
        this.ubuntuTable = this.srcNode.one(config.ubuntuTable);
        this.ubuntuMissingImages = this.srcNode.one(config.ubuntuMissingImages);
        this.ubuntuButton = this.srcNode.one(config.ubuntuButton);
    },

   /**
    * Return all Ubuntu images.
    *
    * @ method getUbuntuImages
    */
    getUbuntuImages: function() {
        images = this.modelList.filter(function(model) {
            return model.get('rtype') === BOOT_RESOURCE_TYPE.SYNCED &&
                model.get('name').indexOf('ubuntu/') === 0;
        });
        // Sort the images decending, so newest Ubuntu version is on top.
        images.sort(function(a, b) {
            return -(a.get('title').localeCompare(b.get('title')));
        });
        return images;
    },

   /**
    * Display images page.
    *
    * @method render
    */
    render: function () {
        if(!this.loaded) {
            this.loader.removeClass('hidden');
            this.content.addClass('hidden');
        } else {
            this.loader.addClass('hidden');
            this.content.removeClass('hidden');
        }
        this.renderImporting();
        this.renderUbuntuView();
    },

   /**
    * Render the importing header.
    *
    * @method renderUbuntuView
    */
    renderImporting: function() {
        var importingText = this.importer.one('.importing-text');
        if(!this.regionImportRunning && !this.clusterImportRunning) {
            this.importer.addClass('hidden');
            importingText.setContent('');
        } else if (this.regionImportRunning) {
            this.importer.removeClass('hidden');
            importingText.setContent(this.regionImportingText);
        } else if (this.clusterImportRunning) {
            this.importer.removeClass('hidden');
            importingText.setContent(this.clusterImportingText);
        }
    },

   /**
    * Render the Ubuntu section of the view.
    *
    * @method renderUbuntuView
    */
    renderUbuntuView: function() {
        if(this.regionImportRunning) {
            if(Y.Lang.isValue(this.ubuntuOptions)) {
                this.ubuntuOptions.addClass('hidden');
            }
            if(Y.Lang.isValue(this.ubuntuButton)) {
                this.ubuntuButton.addClass('hidden');
            }
        } else {
            if(Y.Lang.isValue(this.ubuntuOptions)) {
                this.ubuntuOptions.removeClass('hidden');
            }
            if(Y.Lang.isValue(this.ubuntuButton)) {
                this.ubuntuButton.removeClass('hidden');
            }
        }
        var ubuntuImages = this.getUbuntuImages();
        if(ubuntuImages.length === 0) {
            this.ubuntuMissingImages.removeClass('hidden');
            this.ubuntuTable.addClass('hidden');
            this.updateUbuntuButton(false);
        } else {
            this.ubuntuMissingImages.addClass('hidden');
            this.ubuntuTable.removeClass('hidden');
            this.updateUbuntuButton(true);
        }
        var self = this;
        var innerTable = "";
        Y.each(ubuntuImages, function(model) {
            innerTable += "<tr><td>" + self.getSpinner(model) + "</td>";
            innerTable += "<td>" + model.get('title') + "</td>";
            innerTable += "<td>" + model.get('arch') + "</td>";
            innerTable += "<td>" + model.get('size') + "</td>";
            innerTable += "<td>" + model.get('numberOfNodes') + "</td>";
            innerTable += "<td>" + model.get('lastUpdate') + "</td>";
            innerTable += "</tr>";
        });
        this.ubuntuTable.one('tbody').setHTML(innerTable);
    },

   /**
    * Update the value of the ubuntuButton.
    *
    * The value of the button can be locked meaning it should not change, this
    * is done using the data attribute lock-value. data-lock-value="true"
    *
    * @method updateUbuntuButton
    */
    updateUbuntuButton: function(showApply) {
        if(!Y.Lang.isValue(this.ubuntuButton)) {
            return;
        }
        if(this.ubuntuButton.getData('lock-value') === "true") {
            return;
        }
        if(showApply) {
            this.ubuntuButton.set('value', 'Apply changes');
        }
        else {
            this.ubuntuButton.set('value', 'Import images');
        }
    },

   /**
    * Return the HTML for the downloading spinner for the given model.
    *
    * @method getSpinner
    */
    getSpinner: function(model) {
        // Spinner is not rendered when the model is complete.
        if(model.get('complete')) {
            return '';
        }
        html = '<div title="' + model.get('status') + '" class="spinner';
        if(model.get('downloading')) {
            html += ' spin';
        }
        html += '"></div>';
        return html;
    }
});

}, '0.1', {'requires': [
    'view', 'io', 'maas.enums', 'maas.image']}
);