Commit a125b747 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

Import resources from definition and config files

Register a service's resources giving option --json <file>.
The json file is expected to represent a dictionary with keys
"service" and "resources".

Currently the default base quota (uplimit) for each resource is read
from a configuration json file, given with --conf <file>. This file
represents a dictionary with numeric values for each resource name.

Upon importing the resources, the quotaholder has to be updated due to
the modified resource limits.
parent 863cbecf
......@@ -919,3 +919,10 @@ def qh_sync(user_ids):
users = AstakosUser.forupdate.filter(id__in=user_ids).select_for_update()
astakos_quotas = users_quotas(list(users))
set_user_quota(astakos_quotas)
def qh_sync_all_users():
users = AstakosUser.forupdate.filter(
email_verified=True).select_for_update()
astakos_quotas = users_quotas(list(users))
set_user_quota(astakos_quotas)
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.db.utils import IntegrityError
from django.utils import simplejson as json
from synnefo.lib.db.transaction import commit_on_success_strict
from astakos.im.resources import add_resources
class Command(BaseCommand):
args = "<service> <resource> <desc> <unit>"
help = "Import resources"
option_list = BaseCommand.option_list + (
make_option('--json',
dest='json',
metavar='<json.file>',
help="Load resource info from a json file"),
make_option('--conf',
dest='conf',
metavar='<conf.json>',
help="Limit configuration file"),
)
@commit_on_success_strict()
def handle(self, *args, **options):
conf_file = options['conf']
if not conf_file:
m = "Please provide a configuation file."
raise CommandError(m)
with open(conf_file) as file_data:
config = json.load(file_data)
json_file = options['json']
if json_file:
with open(json_file) as file_data:
data = json.load(file_data)
service = data.get('service')
resources = data.get('resources')
if service is None or resources is None:
m = "JSON file should contain service and resource data."
raise CommandError(m)
add_resources(service, resources, config)
......@@ -160,6 +160,8 @@ class Resource(models.Model):
group = models.CharField(_('Group'), null=True, max_length=255)
uplimit = intDecimalField(default=0)
objects = ForUpdateManager()
def __str__(self):
return self.name
......
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from astakos.im.models import Service, Resource
from astakos.im.functions import qh_sync_all_users
def add_resources(service, resources, conf):
try:
s = Service.objects.get(name=service)
except Service.DoesNotExist:
raise Exception("Service %s is not registered." % (service))
names = [resource['name'] for resource in resources]
rs = Resource.objects.filter(name__in=names).select_for_update()
rs = dict((r.name, r) for r in rs)
for resource in resources:
name = resource['name']
existing = rs.get(name)
r = existing if existing is not None else Resource()
uplimit = conf.get(name)
if uplimit is None:
raise Exception("Limit for resource %s is missing." % (name))
if not isinstance(uplimit, (int, long)):
raise Exception("Limit for resource %s is not an integer." %
(name))
r.uplimit = uplimit
r.service = s
for key, value in resource.iteritems():
setattr(r, key, value)
r.save()
qh_sync_all_users()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment