Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.1 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('is_snapshot');
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
              if (!img.OS) {
128
                img.OS = 'snapshot';
129
              }
130
              if (!img.metadata || !img.metadata.OS) {
131
                img.metadata.OS = 'snapshot';
132
              }
133
            }
134
            return img;
135
        },
136

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

    
143
        active_snapshots: function() {
144
            return this.filter(function(img) {
145
              return img.get('status') != "DELETED" && img.is_snapshot()
146
            });
147
        },
148

    
149
        get_system_images: function() {
150
            return _.filter(this.active(), function(i) { 
151
                return _.include(_.keys(snf.config.system_images_owners), 
152
                                 i.get_owner());
153
            })
154
        },
155

    
156
        get_personal_images: function() {
157
            return _.filter(this.active(), function(i) { 
158
                return i.get_owner_uuid() == snf.user.get_username();
159
            });
160
        },
161

    
162
        get_public_images: function() {
163
            return _.filter(this.active(), function(i){ return i.is_public() })
164
        },
165

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

    
175
        get_snapshot_images: function() {
176
            return this.active_snapshots()
177
        },
178

    
179
    })
180
        
181
    // replace images storage collection
182
    snf.glance.register = function() {
183
        // storage initialization
184
        snf.storage.glance = {};
185
        snf.storage.glance.images = new models.GlanceImages();
186

    
187
        // use glance images
188
        snf.storage.images = snf.storage.glance.images;
189
    }
190

    
191
})(this);
192