Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / glance_models.js @ 5ae6706e

History | View | Annotate | Download (6 kB)

1
;(function(root){
2
    
3
    // root
4
    var root = root;
5
    
6
    // setup namepsaces
7
    var snf = root.synnefo = root.synnefo || {};
8
    var glance = snf.glance = snf.glance || {};
9
    var models = glance.models = glance.models || {}
10
    var storage = glance.storage = glance.storage || {};
11
    var util = snf.util = snf.util || {};
12

    
13
    // shortcuts
14
    var bb = root.Backbone;
15
    var slice = Array.prototype.slice
16

    
17
    models.GlanceImage = snf.models.Image.extend({
18
        api_type: 'glance',
19

    
20
        get_size: function() {
21
            return this.get('size') / 1024 / 1024;
22
        },
23

    
24
        get_readable_size: function() {
25
            var unknown_title = snf.config.image_deleted_size_title || "(none)";
26
            if (this.is_deleted()) { return unknown_title }
27
            return this.get('size') > 0 ? util.readablizeBytes(this.get('size')) : unknown_title;
28
        },
29

    
30
        get_meta: function(key) {
31
            // check for lowercase keys too since glance image meta are set 
32
            // via http headers
33
            var val = models.GlanceImage.__super__.get_meta.call(this, key)
34
            if (val == null) {
35
                val = models.GlanceImage.__super__.get_meta.call(this, 
36
                                                                 key.toLowerCase())
37
            }
38
            return val;
39
        },
40

    
41
        get_owner: function() {
42
            return this.get('owner') || 'Unknown';
43
        },
44

    
45
        is_snapshot: function() {
46
          return this.get('checksum').indexOf('arch') === 0;
47
        },
48

    
49
        display_size: function() {
50
            return this.get_readable_size();
51
        },
52

    
53
        display_users: function() {
54
            try {
55
              if (this.get_meta('users')) {
56
                return this.get_meta('users').split(' ').join(", ");
57
              } else {
58
                return "";
59
              }
60
            } catch(err) { console.log(err); return ''}
61
        }
62
        
63
    })
64

    
65
    models.GlanceImages = snf.models.Images.extend({
66
        model: models.GlanceImage,
67
        api_type: 'glance',
68

    
69
        type_selections: {'personal':'My images', 
70
                          'shared': 'Shared with me', 
71
                          'public': 'Public',
72
                          'snapshot': 'Snapshots'},
73
        type_selections_order: ['system', 'personal', 'shared', 'public', 'snapshot'],
74
        display_metadata: ['size', 'users', 'osfamily', 'status', 'created_at', 'updated_at', 
75
            'filename', 'format', 'root_partition'],
76
        meta_labels: {'OS':'OS', 'osfamily':'OS Family', 'GUI':'GUI'},
77
        display_extra_metadata: true,
78
        read_method: 'head',
79

    
80
        // custom glance api parser
81
        parse: function (resp, xhr) {
82
            if (_.isArray(resp)) {
83
              resp = {'images': resp };
84
            }
85
            return models.GlanceImages.__super__.parse.call(this, resp, xhr);
86
        },
87

    
88
        _read_image_from_request: function(image, msg, xhr) {
89
            var img = {};
90
            img['metadata'] = {};
91

    
92
            var headers = snf.util.parseHeaders(xhr.getAllResponseHeaders().toLowerCase());
93

    
94
            _.each(headers, function(value, key) {
95
                if (key.indexOf("x-image-meta") == -1) {
96
                    return
97
                }
98

    
99
                if (key.indexOf("x-image-meta-property") == -1) {
100
                    img[key.replace("x-image-meta-","").replace(/-/g,"_")] = _.trim(value);
101
                } else {
102
                    img.metadata[key.replace('x-image-meta-property-',"").replace(/-/g,"_")] = _.trim(value);
103
                }
104
            
105
            })
106

    
107
            return img;
108
        },
109

    
110
        parse_meta: function(img) {
111
            if (img.properties) {
112
                img.metadata = {};
113
                img.metadata = img.properties;
114
            } else {
115
                if (!img.metadata) {
116
                    img.metadata = {};
117
                }
118
            }
119

    
120
            // fixes plankton regression (returns lowercase meta keys)
121
            if (img.metadata.os && !img.metadata.OS) {
122
                img.metadata.OS = img.metadata.os;
123
            }
124

    
125
            img = models.GlanceImages.__super__.parse_meta.call(this, img);
126
            if (img.checksum && img.checksum.indexOf('arch') == 0) {
127
              img.OS = 'snapshot';
128
              img.metadata.OS = 'snapshot';
129
            }
130
            return img;
131
        },
132

    
133
        active: function() {
134
            return this.filter(function(img) {
135
              return img.get('status') != "DELETED" && !img.is_snapshot()
136
            });
137
        },
138

    
139
        active_snapshots: function() {
140
            return this.filter(function(img) {
141
              return img.get('status') != "DELETED" && img.is_snapshot()
142
            });
143
        },
144

    
145
        get_system_images: function() {
146
            return _.filter(this.active(), function(i) { 
147
                return _.include(_.keys(snf.config.system_images_owners), 
148
                                 i.get_owner());
149
            })
150
        },
151

    
152
        get_personal_images: function() {
153
            return _.filter(this.active(), function(i) { 
154
                return i.get_owner_uuid() == snf.user.get_username();
155
            });
156
        },
157

    
158
        get_public_images: function() {
159
            return _.filter(this.active(), function(i){ return i.is_public() })
160
        },
161

    
162
        get_shared_images: function() {
163
            return _.filter(this.active(), function(i){ 
164
                return !_.include(_.keys(snf.config.system_images_owners), 
165
                                  i.get_owner()) && 
166
                               i.get_owner_uuid() != snf.user.get_username() &&
167
                               !i.is_public();
168
            });
169
        },
170

    
171
        get_snapshot_images: function() {
172
            return this.active_snapshots()
173
        },
174

    
175
    })
176
        
177
    // replace images storage collection
178
    snf.glance.register = function() {
179
        // storage initialization
180
        snf.storage.glance = {};
181
        snf.storage.glance.images = new models.GlanceImages();
182

    
183
        // use glance images
184
        snf.storage.images = snf.storage.glance.images;
185
    }
186

    
187
})(this);
188