Revision 623a4ceb

b/kamaki/cli/commands/image.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.command
33 33

  
34
from json import load, dumps
35
from os.path import abspath
36
from logging import getLogger
37

  
34 38
from kamaki.cli import command
35 39
from kamaki.cli.command_tree import CommandTree
36 40
from kamaki.cli.utils import print_dict, print_items, print_json
......
52 56
    'To see a list of available image ids: /image list']
53 57

  
54 58

  
59
log = getLogger(__name__)
60

  
61

  
55 62
class _init_image(_command_init):
56 63
    @errors.generic.all
57 64
    def _run(self):
......
72 79
# Plankton Image Commands
73 80

  
74 81

  
82
def _validate_image_props(json_dict, return_str=False):
83
    """
84
    :param json_dict" (dict) json-formated, of the form
85
        {"key1": "val1", "key2": "val2", ...}
86

  
87
    :param return_str: (boolean) if true, return a json dump
88

  
89
    :returns: (dict)
90

  
91
    :raises TypeError, AttributeError: Invalid json format
92

  
93
    :raises AssertionError: Valid json but invalid image properties dict
94
    """
95
    json_str = dumps(json_dict)
96
    for k, v in json_dict.items():
97
        dealbreaker = isinstance(v, dict) or isinstance(v, list)
98
        assert not dealbreaker, 'Invalid property value for key %s' % k
99
        dealbreaker = ' ' in k
100
        assert not dealbreaker, 'Invalid key [%s]' % k
101
        json_dict[k] = '%s' % v
102
    return json_str if return_str else json_dict
103

  
104

  
105
def _load_image_props(filepath):
106
    """
107
    :param filepath: (str) the (relative) path of the metafile
108

  
109
    :returns: (dict) json_formated
110

  
111
    :raises TypeError, AttributeError: Invalid json format
112

  
113
    :raises AssertionError: Valid json but invalid image properties dict
114
    """
115
    with open(abspath(filepath)) as f:
116
        meta_dict = load(f)
117
        try:
118
            return _validate_image_props(meta_dict)
119
        except AssertionError:
120
            log.debug('Failed to load properties from file %s' % filepath)
121
            raise
122

  
123

  
75 124
@command(image_cmds)
76 125
class image_list(_init_image):
77 126
    """List images accessible by user"""
......
211 260
        #update=FlagArgument(
212 261
        #    'update existing image properties',
213 262
        #    ('-u', '--update')),
214
        json_output=FlagArgument('Show results in json', ('-j', '--json'))
263
        json_output=FlagArgument('Show results in json', ('-j', '--json')),
264
        property_file=ValueArgument(
265
            'Load properties from a json-formated file. Contents:'
266
            '{"key1": "val1", "key2": "val2", ...}',
267
            ('--property-file'))
215 268
    )
216 269

  
217 270
    @errors.generic.all
......
240 293
                'is_public']).intersection(self.arguments):
241 294
            params[key] = self[key]
242 295

  
243
            properties = self['properties']
296
            #load properties
297
            properties = _load_image_props(self['property_file']) if (
298
                self['property_file']) else dict()
299
            properties.update(self['properties'])
244 300

  
245 301
        printer = print_json if self['json_output'] else print_dict
246 302
        printer(self.client.register(name, location, params, properties))

Also available in: Unified diff