commit bf644f91bcbe879e616a84a68894942fd11793f3
Author: Giorgos Korfiatis <gkorf@grnet.gr>
Date:   Mon Jan 20 11:46:24 2014 +0200

    astakos: Refactor resource-modify
    
    Remove interactive operations, rename default-quota to base-default.

diff --git a/snf-astakos-app/astakos/im/management/commands/resource-modify.py b/snf-astakos-app/astakos/im/management/commands/resource-modify.py
index 8abe2f5..5d506d0 100644
--- a/snf-astakos-app/astakos/im/management/commands/resource-modify.py
+++ b/snf-astakos-app/astakos/im/management/commands/resource-modify.py
@@ -1,4 +1,4 @@
-# Copyright 2013 GRNET S.A. All rights reserved.
+# Copyright 2013, 2014 GRNET S.A. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or
 # without modification, are permitted provided that the following
@@ -33,12 +33,11 @@
 
 from optparse import make_option
 from django.core.management.base import BaseCommand, CommandError
-from django.utils import simplejson as json
 
 from snf_django.management import utils
 from astakos.im.models import Resource
-from astakos.im.register import update_resources
-from ._common import show_resource_value, style_options, check_style, units
+from astakos.im import register
+from ._common import style_options, check_style, units
 
 
 class Command(BaseCommand):
@@ -46,20 +45,9 @@ class Command(BaseCommand):
     help = "Modify a resource's default base quota and boolean flags."
 
     option_list = BaseCommand.option_list + (
-        make_option('--default-quota',
+        make_option('--base-default',
                     metavar='<limit>',
                     help="Specify default base quota"),
-        make_option('--default-quota-interactive',
-                    action='store_true',
-                    default=None,
-                    help=("Prompt user to change default base quota. "
-                          "If no resource is given, prompts for all "
-                          "resources.")),
-        make_option('--default-quota-from-file',
-                    metavar='<limits_file.json>',
-                    help=("Read default base quota from a file. "
-                          "File should contain a json dict mapping resource "
-                          "names to limits")),
         make_option('--unit-style',
                     default='mb',
                     help=("Specify display unit for resource values "
@@ -74,11 +62,12 @@ class Command(BaseCommand):
 
     def handle(self, *args, **options):
         resource_name = args[0] if len(args) > 0 else None
+        if resource_name is None:
+            raise CommandError("Please provide a resource name.")
+        resource = self.get_resource(resource_name)
 
         actions = {
-            'default_quota': self.change_limit,
-            'default_quota_interactive': self.change_interactive,
-            'default_quota_from_file': self.change_from_file,
+            'base_default': self.change_base_default,
             'api_visible': self.set_api_visible,
             'ui_visible': self.set_ui_visible,
         }
@@ -87,44 +76,29 @@ class Command(BaseCommand):
                 for (key, value) in options.items()
                 if key in actions and value is not None]
 
-        if len(opts) != 1:
-            raise CommandError("Please provide exactly one of the options: "
-                               "--default-quota, --default-quota-interactive, "
-                               "--default-quota-from-file, "
-                               "--api-visible, --ui-visible.")
-
         self.unit_style = options['unit_style']
         check_style(self.unit_style)
 
-        key, value = opts[0]
-        action = actions[key]
-        action(resource_name, value)
-
-    def set_api_visible(self, resource_name, allow):
-        if resource_name is None:
-            raise CommandError("Please provide a resource name.")
+        for key, value in opts:
+            action = actions[key]
+            action(resource, value)
 
+    def set_api_visible(self, resource, allow):
         try:
             allow = utils.parse_bool(allow)
         except ValueError:
             raise CommandError("Expecting a boolean value.")
-        resource = self.get_resource(resource_name)
         resource.api_visible = allow
         if not allow and resource.ui_visible:
             self.stderr.write("Also resetting 'ui_visible' for consistency.\n")
             resource.ui_visible = False
         resource.save()
 
-    def set_ui_visible(self, resource_name, allow):
-        if resource_name is None:
-            raise CommandError("Please provide a resource name.")
-
+    def set_ui_visible(self, resource, allow):
         try:
             allow = utils.parse_bool(allow)
         except ValueError:
             raise CommandError("Expecting a boolean value.")
-        resource = self.get_resource(resource_name)
-
         resource.ui_visible = allow
         if allow and not resource.api_visible:
             self.stderr.write("Also setting 'api_visible' for consistency.\n")
@@ -138,83 +112,14 @@ class Command(BaseCommand):
             raise CommandError("Resource %s does not exist."
                                % resource_name)
 
-    def change_limit(self, resource_name, limit):
-        if resource_name is None:
-            raise CommandError("Please provide a resource name.")
-
-        resource = self.get_resource(resource_name)
-        self.change_resource_limit(resource, limit)
-
-    def change_from_file(self, resource_name, filename):
-        with open(filename) as file_data:
-            try:
-                config = json.load(file_data)
-            except json.JSONDecodeError:
-                raise CommandError("Malformed JSON file.")
-            if not isinstance(config, dict):
-                raise CommandError("Malformed JSON file.")
-            self.change_with_conf(resource_name, config)
-
-    def change_with_conf(self, resource_name, config):
-        if resource_name is None:
-            resources = Resource.objects.all().select_for_update()
-        else:
-            resources = [self.get_resource(resource_name)]
-
-        updates = []
-        for resource in resources:
-            limit = config.get(resource.name)
-            if limit is not None:
-                limit = self.parse_limit(limit)
-                updates.append((resource, limit))
-        if updates:
-            update_resources(updates)
-
-    def change_interactive(self, resource_name, _placeholder):
-        if resource_name is None:
-            resources = Resource.objects.all().select_for_update()
-        else:
-            resources = [self.get_resource(resource_name)]
-
-        updates = []
-        for resource in resources:
-            self.stdout.write("Resource '%s' (%s)\n" %
-                              (resource.name, resource.desc))
-            value = show_resource_value(resource.uplimit, resource.name,
-                                        self.unit_style)
-            self.stdout.write("Current limit: %s\n" % value)
-            while True:
-                self.stdout.write("New limit (leave blank to keep current): ")
-                try:
-                    response = raw_input()
-                except EOFError:
-                    self.stderr.write("Aborted.\n")
-                    exit()
-                if response == "":
-                    break
-                else:
-                    try:
-                        value = units.parse(response)
-                    except units.ParseError:
-                        continue
-                    updates.append((resource, value))
-                    break
-        if updates:
-            self.stderr.write("Updating...\n")
-            update_resources(updates)
+    def change_base_default(self, resource, limit):
+        limit = self.parse_limit(limit)
+        register.update_base_default(resource, limit)
 
     def parse_limit(self, limit):
         try:
-            if isinstance(limit, (int, long)):
-                return limit
-            if isinstance(limit, basestring):
-                return units.parse(limit)
-            raise units.ParseError()
+            return units.parse(limit)
         except units.ParseError:
-            m = ("Limit should be an integer, optionally followed by a unit,"
-                 " or 'inf'.")
+            m = ("Quota limit should be an integer, "
+                 "optionally followed by a unit, or 'inf'.")
             raise CommandError(m)
-
-    def change_resource_limit(self, resource, limit):
-        limit = self.parse_limit(limit)
-        update_resources([(resource, limit)])
