root / snf-cyclades-app / synnefo / ui / static / snf / js / glance_models.js @ 3e7aeb5a
History | View | Annotate | Download (7 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 |
type_selections_order: ['system', 'personal', 'shared', 'public'], |
73 |
display_metadata: ['size', 'users', 'osfamily', 'status', 'created_at', 'updated_at', |
74 |
'filename', 'format', 'root_partition'], |
75 |
meta_labels: {'OS':'OS', 'osfamily':'OS Family', 'GUI':'GUI'}, |
76 |
display_extra_metadata: true, |
77 |
read_method: 'head', |
78 |
|
79 |
// custom glance api parser
|
80 |
parse: function (resp, xhr) { |
81 |
if (_.isArray(resp)) {
|
82 |
resp = {'images': resp };
|
83 |
} |
84 |
return models.GlanceImages.__super__.parse.call(this, resp, xhr); |
85 |
}, |
86 |
|
87 |
_read_image_from_request: function(image, msg, xhr) { |
88 |
var img = {};
|
89 |
img['metadata'] = {};
|
90 |
|
91 |
var headers = snf.util.parseHeaders(xhr.getAllResponseHeaders().toLowerCase());
|
92 |
|
93 |
_.each(headers, function(value, key) {
|
94 |
if (key.indexOf("x-image-meta") == -1) { |
95 |
return
|
96 |
} |
97 |
|
98 |
if (key.indexOf("x-image-meta-property") == -1) { |
99 |
img[key.replace("x-image-meta-","").replace(/-/g,"_")] = _.trim(value); |
100 |
} else {
|
101 |
img.metadata[key.replace('x-image-meta-property-',"").replace(/-/g,"_")] = _.trim(value); |
102 |
} |
103 |
|
104 |
}) |
105 |
|
106 |
return img;
|
107 |
}, |
108 |
|
109 |
parse_meta: function(img) { |
110 |
if (img.properties) {
|
111 |
img.metadata = {}; |
112 |
img.metadata = img.properties; |
113 |
} else {
|
114 |
if (!img.metadata) {
|
115 |
img.metadata = {}; |
116 |
} |
117 |
} |
118 |
|
119 |
// fixes plankton regression (returns lowercase meta keys)
|
120 |
if (img.metadata.os && !img.metadata.OS) {
|
121 |
img.metadata.OS = img.metadata.os; |
122 |
} |
123 |
|
124 |
img = models.GlanceImages.__super__.parse_meta.call(this, img);
|
125 |
if (img.checksum && img.checksum.indexOf('arch') == 0) { |
126 |
if (!img.OS) {
|
127 |
img.OS = 'snapshot';
|
128 |
} |
129 |
if (!img.metadata || !img.metadata.OS) {
|
130 |
img.metadata.OS = 'snapshot';
|
131 |
} |
132 |
} |
133 |
return img;
|
134 |
}, |
135 |
|
136 |
active: function() { |
137 |
return this.filter(function(img) { |
138 |
return img.get('status') != "DELETED" && !img.is_snapshot() |
139 |
}); |
140 |
}, |
141 |
|
142 |
active_snapshots: function() { |
143 |
return this.filter(function(img) { |
144 |
return img.get('status') != "DELETED" && img.is_snapshot() |
145 |
}); |
146 |
}, |
147 |
|
148 |
get_system_images: function() { |
149 |
return _.filter(this.active(), function(i) { |
150 |
return _.include(_.keys(snf.config.system_images_owners),
|
151 |
i.get_owner()); |
152 |
}) |
153 |
}, |
154 |
|
155 |
get_personal_images: function() { |
156 |
return _.filter(this.active(), function(i) { |
157 |
return i.get_owner_uuid() == snf.user.get_username();
|
158 |
}); |
159 |
}, |
160 |
|
161 |
get_public_images: function() { |
162 |
return _.filter(this.active(), function(i){ return i.is_public() }) |
163 |
}, |
164 |
|
165 |
get_shared_images: function() { |
166 |
return _.filter(this.active(), function(i){ |
167 |
return !_.include(_.keys(snf.config.system_images_owners),
|
168 |
i.get_owner()) && |
169 |
i.get_owner_uuid() != snf.user.get_username() && |
170 |
!i.is_public(); |
171 |
}); |
172 |
}, |
173 |
|
174 |
get_snapshot_system_images: function() { |
175 |
return _.filter(this.active_snapshots(), function(i) { |
176 |
return _.include(_.keys(snf.config.system_images_owners),
|
177 |
i.get_owner()); |
178 |
}) |
179 |
}, |
180 |
|
181 |
get_snapshot_personal_images: function() { |
182 |
return _.filter(this.active_snapshots(), function(i) { |
183 |
return i.get_owner_uuid() == snf.user.get_username();
|
184 |
}); |
185 |
}, |
186 |
|
187 |
get_snapshot_public_images: function() { |
188 |
return _.filter(this.active_snapshots(), function(i){ return i.is_public() }) |
189 |
}, |
190 |
|
191 |
get_snapshot_shared_images: function() { |
192 |
return _.filter(this.active_snapshots(), function(i){ |
193 |
return !_.include(_.keys(snf.config.system_images_owners),
|
194 |
i.get_owner()) && |
195 |
i.get_owner_uuid() != snf.user.get_username() && |
196 |
!i.is_public(); |
197 |
}); |
198 |
}, |
199 |
|
200 |
}) |
201 |
|
202 |
// replace images storage collection
|
203 |
snf.glance.register = function() { |
204 |
// storage initialization
|
205 |
snf.storage.glance = {}; |
206 |
snf.storage.glance.images = new models.GlanceImages();
|
207 |
|
208 |
// use glance images
|
209 |
snf.storage.images = snf.storage.glance.images; |
210 |
} |
211 |
|
212 |
})(this);
|
213 |
|