Merge lp:~dan-prince/glance/purge_props into lp:~glance-coresec/glance/cactus-trunk

Proposed by Dan Prince
Status: Merged
Approved by: Jay Pipes
Approved revision: 100
Merged at revision: 100
Proposed branch: lp:~dan-prince/glance/purge_props
Merge into: lp:~glance-coresec/glance/cactus-trunk
Diff against target: 189 lines (+42/-29)
6 files modified
glance/registry/__init__.py (+2/-2)
glance/registry/client.py (+5/-2)
glance/registry/db/api.py (+15/-12)
glance/registry/server.py (+6/-1)
glance/server.py (+2/-1)
tests/stubs.py (+12/-11)
To merge this branch: bzr merge lp:~dan-prince/glance/purge_props
Reviewer Review Type Date Requested Status
Jay Pipes (community) Approve
Review via email: mp+55351@code.launchpad.net

Description of the change

  Updates to the Registry API such that only external requests to
  update image properties purge existing properties. The update_image
  call now contains an extra flag to purge_props which is set to
  True for external requests but False internally.

  This resolves with revision 99 where Glance API incidentally deleted
  image metadata when uploading new images.

To post a comment you must log in.
Revision history for this message
Jay Pipes (jaypipes) wrote :

merging this as a hotfix. we can discuss a long-term solution later.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'glance/registry/__init__.py'
2--- glance/registry/__init__.py 2011-03-06 17:26:34 +0000
3+++ glance/registry/__init__.py 2011-03-29 14:42:55 +0000
4@@ -63,13 +63,13 @@
5 return new_image_meta
6
7
8-def update_image_metadata(options, image_id, image_meta):
9+def update_image_metadata(options, image_id, image_meta, purge_props=False):
10 if options['debug']:
11 logger.debug("Updating image metadata for image %s...", image_id)
12 _debug_print_metadata(image_meta)
13
14 c = get_registry_client(options)
15- new_image_meta = c.update_image(image_id, image_meta)
16+ new_image_meta = c.update_image(image_id, image_meta, purge_props)
17
18 if options['debug']:
19 logger.debug("Returned image metadata from call to "
20
21=== modified file 'glance/registry/client.py'
22--- glance/registry/client.py 2011-01-26 20:44:36 +0000
23+++ glance/registry/client.py 2011-03-29 14:42:55 +0000
24@@ -87,7 +87,7 @@
25 data = json.loads(res.read())
26 return data['image']
27
28- def update_image(self, image_id, image_metadata):
29+ def update_image(self, image_id, image_metadata, purge_props=False):
30 """
31 Updates Registry's information about an image
32 """
33@@ -96,7 +96,10 @@
34
35 body = json.dumps(image_metadata)
36
37- res = self.do_request("PUT", "/images/%s" % image_id, body)
38+ headers = {}
39+ if purge_props:
40+ headers["X-Glance-Registry-Purge-Props"] = "true"
41+ res = self.do_request("PUT", "/images/%s" % image_id, body, headers)
42 data = json.loads(res.read())
43 image = data['image']
44 return image
45
46=== modified file 'glance/registry/db/api.py'
47--- glance/registry/db/api.py 2011-03-28 18:02:21 +0000
48+++ glance/registry/db/api.py 2011-03-29 14:42:55 +0000
49@@ -97,16 +97,16 @@
50
51 def image_create(context, values):
52 """Create an image from the values dictionary."""
53- return _image_update(context, values, None)
54-
55-
56-def image_update(context, image_id, values):
57+ return _image_update(context, values, None, False)
58+
59+
60+def image_update(context, image_id, values, purge_props=False):
61 """Set the given properties on an image and update it.
62
63 Raises NotFound if image does not exist.
64
65 """
66- return _image_update(context, values, image_id)
67+ return _image_update(context, values, image_id, purge_props)
68
69
70 def image_destroy(context, image_id):
71@@ -188,7 +188,7 @@
72 raise exception.Invalid(msg)
73
74
75-def _image_update(context, values, image_id):
76+def _image_update(context, values, image_id, purge_props=False):
77 """Used internally by image_create and image_update
78
79 :param context: Request context
80@@ -227,12 +227,14 @@
81
82 image_ref.save(session=session)
83
84- _set_properties_for_image(context, image_ref, properties, session)
85+ _set_properties_for_image(context, image_ref, properties, purge_props,
86+ session)
87
88 return image_get(context, image_ref.id)
89
90
91-def _set_properties_for_image(context, image_ref, properties, session=None):
92+def _set_properties_for_image(context, image_ref, properties,
93+ purge_props=False, session=None):
94 """
95 Create or update a set of image_properties for a given image
96
97@@ -256,10 +258,11 @@
98 else:
99 image_property_create(context, prop_values, session=session)
100
101- for key in orig_properties.keys():
102- if not key in properties:
103- prop_ref = orig_properties[key]
104- image_property_delete(context, prop_ref, session=session)
105+ if purge_props:
106+ for key in orig_properties.keys():
107+ if not key in properties:
108+ prop_ref = orig_properties[key]
109+ image_property_delete(context, prop_ref, session=session)
110
111
112 def image_property_create(context, values, session=None):
113
114=== modified file 'glance/registry/server.py'
115--- glance/registry/server.py 2011-03-23 14:16:10 +0000
116+++ glance/registry/server.py 2011-03-29 14:42:55 +0000
117@@ -157,11 +157,16 @@
118 """
119 image_data = json.loads(req.body)['image']
120
121+ purge_props = req.headers.get("X-Glance-Registry-Purge-Props", "false")
122 context = None
123 try:
124 logger.debug("Updating image %(id)s with metadata: %(image_data)r"
125 % locals())
126- updated_image = db_api.image_update(context, id, image_data)
127+ if purge_props == "true":
128+ updated_image = db_api.image_update(context, id, image_data,
129+ True)
130+ else:
131+ updated_image = db_api.image_update(context, id, image_data)
132 return dict(image=make_image_dict(updated_image))
133 except exception.Invalid, e:
134 msg = ("Failed to update image metadata. "
135
136=== modified file 'glance/server.py'
137--- glance/server.py 2011-03-25 18:33:54 +0000
138+++ glance/server.py 2011-03-29 14:42:55 +0000
139@@ -418,7 +418,8 @@
140 try:
141 image_meta = registry.update_image_metadata(self.options,
142 id,
143- new_image_meta)
144+ new_image_meta,
145+ True)
146 if has_body:
147 image_meta = self._upload_and_activate(req, image_meta)
148
149
150=== modified file 'tests/stubs.py'
151--- tests/stubs.py 2011-03-23 14:16:10 +0000
152+++ tests/stubs.py 2011-03-29 14:42:55 +0000
153@@ -344,7 +344,7 @@
154 self.images.append(values)
155 return values
156
157- def image_update(self, _context, image_id, values):
158+ def image_update(self, _context, image_id, values, purge_props=False):
159
160 image = self.image_get(_context, image_id)
161 copy_image = image.copy()
162@@ -353,16 +353,17 @@
163 props = []
164 orig_properties = image['properties']
165
166- if 'properties' in values.keys():
167- for k, v in values['properties'].items():
168- p = {}
169- p['key'] = k
170- p['value'] = v
171- p['deleted'] = False
172- p['created_at'] = datetime.datetime.utcnow()
173- p['updated_at'] = datetime.datetime.utcnow()
174- p['deleted_at'] = None
175- props.append(p)
176+ if purge_props == False:
177+ if 'properties' in values.keys():
178+ for k, v in values['properties'].items():
179+ p = {}
180+ p['key'] = k
181+ p['value'] = v
182+ p['deleted'] = False
183+ p['created_at'] = datetime.datetime.utcnow()
184+ p['updated_at'] = datetime.datetime.utcnow()
185+ p['deleted_at'] = None
186+ props.append(p)
187
188 orig_properties = orig_properties + props
189 values['properties'] = orig_properties

Subscribers

People subscribed via source and target branches