Single organization license diff.

This commit is contained in:
Luke Sneeringer
2015-05-19 16:15:58 -04:00
parent 013f59d58a
commit 0eec28c7c0
2 changed files with 50 additions and 6 deletions

26
awx/api/license.py Normal file
View File

@@ -0,0 +1,26 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved.
from rest_framework.exceptions import APIException
from awx.main.task_engine import TaskSerializer
class LicenseForbids(APIException):
status_code = 402
default_detail = 'Your Tower license does not allow that.'
def get_license(show_key=False):
"""Return a dictionary representing the license currently in
place on this Tower instance.
"""
license_reader = TaskSerializer()
return license_reader.from_file(show_key=show_key)
def feature_enabled(name):
"""Return True if the requested feature is enabled, False otherwise.
If the feature does not exist, raise KeyError.
"""
return get_license()['features'][name]

View File

@@ -483,6 +483,24 @@ class OrganizationList(ListCreateAPIView):
model = Organization model = Organization
serializer_class = OrganizationSerializer serializer_class = OrganizationSerializer
def create(self, request, *args, **kwargs):
"""Create a new organzation.
If there is already an organization and the license of the Tower
instance does not permit multiple organizations, then raise
LicenseForbids.
"""
# Sanity check: If the multiple organizations feature is disallowed
# by the license, then we are only willing to create this organization
# if no organizations exist in the system.
if (not feature_enabled('multiple_organizations') and
self.model.objects.filter(active=True).count() > 0):
raise LicenseForbids('This Tower license only permits a single '
'organization to exist.')
# Okay, create the organization as usual.
return super(OrganizationList, self).create(request, *args, **kwargs)
class OrganizationDetail(RetrieveUpdateDestroyAPIView): class OrganizationDetail(RetrieveUpdateDestroyAPIView):
model = Organization model = Organization