Revision 028f4731 snf-cyclades-app/synnefo/plankton/views.py

b/snf-cyclades-app/synnefo/plankton/views.py
61 61

  
62 62
DETAIL_FIELDS = ('name', 'disk_format', 'container_format', 'size', 'checksum',
63 63
                 'location', 'created_at', 'updated_at', 'deleted_at',
64
                 'status', 'is_public', 'owner', 'properties', 'id')
64
                 'status', 'is_public', 'owner', 'id')
65 65

  
66 66
ADD_FIELDS = ('name', 'id', 'store', 'disk_format', 'container_format', 'size',
67
              'checksum', 'is_public', 'owner', 'properties', 'location')
67
              'checksum', 'is_public', 'owner', 'location')
68 68

  
69 69
UPDATE_FIELDS = ('name', 'disk_format', 'container_format', 'is_public',
70
                 'owner', 'properties', 'status')
70
                 'owner',  'status')
71

  
72
PROPERTY_FIELD_PREFIX = 'property_'
71 73

  
72 74

  
73 75
log = getLogger('synnefo.plankton')
......
77 79
    response = HttpResponse()
78 80

  
79 81
    for key in DETAIL_FIELDS:
80
        if key == 'properties':
81
            for k, v in image.get('properties', {}).items():
82
                name = 'x-image-meta-property-' + k.replace('_', '-')
83
                response[name] = v
84
        else:
85
            name = 'x-image-meta-' + key.replace('_', '-')
86
            response[name] = image.get(key, '')
82
        name = 'x-image-meta-' + key.replace('_', '-')
83
        response[name] = image.get(key, '')
84
    for key in [k for k in image.keys() if
85
                k.startswith(PROPERTY_FIELD_PREFIX)]:
86
        name = 'x-image-meta-' + key.replace('_', '-')
87
        response[name] = image.get(key, '')
87 88

  
88 89
    return response
89 90

  
......
94 95

  
95 96
    META_PREFIX = 'HTTP_X_IMAGE_META_'
96 97
    META_PREFIX_LEN = len(META_PREFIX)
97
    META_PROPERTY_PREFIX = 'HTTP_X_IMAGE_META_PROPERTY_'
98
    META_PROPERTY_PREFIX_LEN = len(META_PROPERTY_PREFIX)
99 98

  
100 99
    headers = {'properties': {}}
101 100

  
102 101
    for key, val in request.META.items():
103
        if key.startswith(META_PROPERTY_PREFIX):
104
            name = normalize(key[META_PROPERTY_PREFIX_LEN:])
105
            headers['properties'][unquote(name)] = unquote(val)
106
        elif key.startswith(META_PREFIX):
102
        if key.startswith(META_PREFIX):
107 103
            name = normalize(key[META_PREFIX_LEN:])
108 104
            headers[unquote(name)] = unquote(val)
109 105

  
......
117 113
    return headers
118 114

  
119 115

  
116
def _assert_allowed_keys(d, allowed):
117
    # Filter out property fields
118

  
119
    for k in d:
120
        if k.startswith(PROPERTY_FIELD_PREFIX):
121
            continue
122
        assert k in allowed
123

  
124

  
120 125
@api.api_method(http_method="POST", user_required=True, logger=log)
121 126
def add_image(request):
122 127
    """Add a new virtual machine image
......
140 145
    log.debug('add_image %s', params)
141 146

  
142 147
    assert 'name' in params
143
    assert set(params.keys()).issubset(set(ADD_FIELDS))
148
    _assert_allowed_keys(params, ADD_FIELDS)
144 149

  
145 150
    name = params.pop('name')
146 151
    location = params.pop('location', None)
......
301 306
    # Remove keys that should not be returned
302 307
    fields = DETAIL_FIELDS if detail else LIST_FIELDS
303 308
    for image in images:
309
        properties = {}
304 310
        for key in image.keys():
305
            if key not in fields:
311
            if key.startswith(PROPERTY_FIELD_PREFIX):
312
                properties[key.replace(PROPERTY_FIELD_PREFIX, '', 1)] =\
313
                    image.pop(key)
314
            elif key not in fields:
306 315
                del image[key]
316
        if detail and properties:
317
            image['properties'] = properties
307 318

  
308 319
    data = json.dumps(images, indent=settings.DEBUG)
309 320
    return HttpResponse(data)
......
363 374
    meta = _get_image_headers(request)
364 375
    log.debug('update_image %s', meta)
365 376

  
366
    assert set(meta.keys()).issubset(set(UPDATE_FIELDS))
377
    _assert_allowed_keys(meta, UPDATE_FIELDS)
367 378

  
368 379
    with image_backend(request.user_uniq) as backend:
369 380
        image = backend.update_metadata(image_id, meta)

Also available in: Unified diff