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