summaryrefslogtreecommitdiffstats
path: root/snaps/openstack
diff options
context:
space:
mode:
authorspisarski <s.pisarski@cablelabs.com>2017-08-07 14:14:06 -0600
committerspisarski <s.pisarski@cablelabs.com>2017-08-09 14:24:07 -0600
commit79c0075b2e3872f9cde8101403b19bd963f7f992 (patch)
treee1c57261dac38964cc851dc001eb9f35be97e214 /snaps/openstack
parent430905e7f76e4a074167a49ca2bfbf727eebcefd (diff)
Added feature to update the quotas on a project/tenant.
JIRA: SNAPS-170 Change-Id: Icf494dd2bddc338b8e85259b0400c0950d2332bc Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/openstack')
-rw-r--r--snaps/openstack/create_project.py35
-rw-r--r--snaps/openstack/tests/create_project_tests.py47
-rw-r--r--snaps/openstack/utils/keystone_utils.py6
-rw-r--r--snaps/openstack/utils/neutron_utils.py34
-rw-r--r--snaps/openstack/utils/nova_utils.py35
5 files changed, 154 insertions, 3 deletions
diff --git a/snaps/openstack/create_project.py b/snaps/openstack/create_project.py
index 7eebbe0..7bfdad1 100644
--- a/snaps/openstack/create_project.py
+++ b/snaps/openstack/create_project.py
@@ -15,7 +15,8 @@
import logging
from keystoneclient.exceptions import NotFound
-from snaps.openstack.utils import keystone_utils, neutron_utils
+
+from snaps.openstack.utils import keystone_utils, neutron_utils, nova_utils
__author__ = 'spisarski'
@@ -112,6 +113,38 @@ class OpenStackProject:
keystone_utils.grant_user_role_to_project(self.__keystone, self.__role,
user, self.__project)
+ def get_compute_quotas(self):
+ """
+ Returns the compute quotas as an instance of the ComputeQuotas class
+ :return:
+ """
+ nova = nova_utils.nova_client(self.__os_creds)
+ return nova_utils.get_compute_quotas(nova, self.__project.id)
+
+ def get_network_quotas(self):
+ """
+ Returns the network quotas as an instance of the NetworkQuotas class
+ :return:
+ """
+ neutron = neutron_utils.neutron_client(self.__os_creds)
+ return neutron_utils.get_network_quotas(neutron, self.__project.id)
+
+ def update_compute_quotas(self, compute_quotas):
+ """
+ Updates the compute quotas for this project
+ :param compute_quotas: a ComputeQuotas object.
+ """
+ nova = nova_utils.nova_client(self.__os_creds)
+ nova_utils.update_quotas(nova, self.__project.id, compute_quotas)
+
+ def update_network_quotas(self, network_quotas):
+ """
+ Updates the network quotas for this project
+ :param network_quotas: a NetworkQuotas object.
+ """
+ neutron = neutron_utils.neutron_client(self.__os_creds)
+ neutron_utils.update_quotas(neutron, self.__project.id, network_quotas)
+
class ProjectSettings:
"""
diff --git a/snaps/openstack/tests/create_project_tests.py b/snaps/openstack/tests/create_project_tests.py
index b225e3d..dfbb0d6 100644
--- a/snaps/openstack/tests/create_project_tests.py
+++ b/snaps/openstack/tests/create_project_tests.py
@@ -17,6 +17,7 @@ import uuid
from keystoneclient.exceptions import BadRequest
+from snaps.domain.project import ComputeQuotas, NetworkQuotas
from snaps.openstack.create_project import (
OpenStackProject, ProjectSettings, ProjectSettingsError)
from snaps.openstack.create_security_group import OpenStackSecurityGroup
@@ -24,7 +25,7 @@ from snaps.openstack.create_security_group import SecurityGroupSettings
from snaps.openstack.create_user import OpenStackUser
from snaps.openstack.create_user import UserSettings
from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
-from snaps.openstack.utils import keystone_utils
+from snaps.openstack.utils import keystone_utils, nova_utils, neutron_utils
__author__ = 'spisarski'
@@ -171,6 +172,50 @@ class CreateProjectSuccessTests(OSComponentTestCase):
self.assertTrue(validate_project(self.keystone, self.project_settings,
created_project))
+ def test_update_quotas(self):
+ """
+ Tests the creation of an OpenStack project where the quotas get
+ updated.
+ """
+ self.project_creator = OpenStackProject(self.os_creds,
+ self.project_settings)
+ created_project = self.project_creator.create()
+ self.assertIsNotNone(created_project)
+
+ retrieved_project = keystone_utils.get_project(
+ keystone=self.keystone, project_settings=self.project_settings)
+ self.assertIsNotNone(retrieved_project)
+ self.assertEqual(created_project, retrieved_project)
+ self.assertTrue(validate_project(self.keystone, self.project_settings,
+ created_project))
+
+ update_compute_quotas = ComputeQuotas(
+ **{'metadata_items': 64, 'cores': 5, 'instances': 5,
+ 'injected_files': 3, 'injected_file_content_bytes': 5120,
+ 'ram': 25600, 'fixed_ips': 100, 'key_pairs': 50})
+ self.project_creator.update_compute_quotas(update_compute_quotas)
+
+ update_network_quotas = NetworkQuotas(
+ **{'security_group': 5, 'security_group_rule': 50,
+ 'floatingip': 25, 'network': 5, 'port': 25, 'router': 6,
+ 'subnet': 7})
+ self.project_creator.update_network_quotas(update_network_quotas)
+
+ self.assertEqual(update_compute_quotas,
+ self.project_creator.get_compute_quotas())
+ self.assertEqual(update_network_quotas,
+ self.project_creator.get_network_quotas())
+
+ nova = nova_utils.nova_client(self.os_creds)
+ new_compute_quotas = nova_utils.get_compute_quotas(
+ nova, self.project_creator.get_project().id)
+ self.assertEqual(update_compute_quotas, new_compute_quotas)
+
+ neutron = neutron_utils.neutron_client(self.os_creds)
+ new_network_quotas = neutron_utils.get_network_quotas(
+ neutron, self.project_creator.get_project().id)
+ self.assertEqual(update_network_quotas, new_network_quotas)
+
class CreateProjectUserTests(OSComponentTestCase):
"""
diff --git a/snaps/openstack/utils/keystone_utils.py b/snaps/openstack/utils/keystone_utils.py
index 10ad68a..f390c0f 100644
--- a/snaps/openstack/utils/keystone_utils.py
+++ b/snaps/openstack/utils/keystone_utils.py
@@ -107,7 +107,9 @@ def get_endpoint(os_creds, service_type, interface='public'):
def get_project(keystone=None, os_creds=None, project_settings=None,
project_name=None):
"""
- Returns the first project object or None if not found
+ Returns the first project where the project_settings is used for the query
+ if not None, else the project_name parameter is used for the query. If both
+ parameters are None, None is returned
:param keystone: the Keystone client
:param os_creds: the OpenStack credentials used to obtain the Keystone
client if the keystone parameter is None
@@ -131,6 +133,8 @@ def get_project(keystone=None, os_creds=None, project_settings=None,
proj_filter['description'] = project_settings.description
proj_filter['domain_name'] = project_settings.domain_name
proj_filter['enabled'] = project_settings.enabled
+ else:
+ return None
if keystone.version == V2_VERSION_STR:
projects = keystone.tenants.list()
diff --git a/snaps/openstack/utils/neutron_utils.py b/snaps/openstack/utils/neutron_utils.py
index e7b002a..c615bd5 100644
--- a/snaps/openstack/utils/neutron_utils.py
+++ b/snaps/openstack/utils/neutron_utils.py
@@ -20,6 +20,7 @@ from neutronclient.neutron.client import Client
from snaps.domain.network import (
Port, SecurityGroup, SecurityGroupRule, Router, InterfaceRouter, Subnet,
Network)
+from snaps.domain.project import NetworkQuotas
from snaps.domain.vm_inst import FloatingIp
from snaps.openstack.utils import keystone_utils
@@ -615,6 +616,39 @@ def delete_floating_ip(neutron, floating_ip):
return neutron.delete_floatingip(floating_ip.id)
+def get_network_quotas(neutron, project_id):
+ """
+ Returns a list of all available keypairs
+ :param nova: the Nova client
+ :param project_id: the project's ID of the quotas to lookup
+ :return: an object of type NetworkQuotas or None if not found
+ """
+ quota = neutron.show_quota(project_id)
+ if quota:
+ return NetworkQuotas(**quota['quota'])
+
+
+def update_quotas(neutron, project_id, network_quotas):
+ """
+ Updates the networking quotas for a given project
+ :param neutron: the Neutron client
+ :param project_id: the project's ID that requires quota updates
+ :param network_quotas: an object of type NetworkQuotas containing the
+ values to update
+ :return:
+ """
+ update_body = dict()
+ update_body['security_group'] = network_quotas.security_group
+ update_body['security_group_rule'] = network_quotas.security_group_rule
+ update_body['floatingip'] = network_quotas.floatingip
+ update_body['network'] = network_quotas.network
+ update_body['port'] = network_quotas.port
+ update_body['router'] = network_quotas.router
+ update_body['subnet'] = network_quotas.subnet
+
+ return neutron.update_quota(project_id, {'quota': update_body})
+
+
class NeutronException(Exception):
"""
Exception when calls to the Keystone client cannot be served properly
diff --git a/snaps/openstack/utils/nova_utils.py b/snaps/openstack/utils/nova_utils.py
index 5222712..0a259b0 100644
--- a/snaps/openstack/utils/nova_utils.py
+++ b/snaps/openstack/utils/nova_utils.py
@@ -24,6 +24,7 @@ from novaclient.exceptions import NotFound
from snaps.domain.flavor import Flavor
from snaps.domain.keypair import Keypair
+from snaps.domain.project import ComputeQuotas
from snaps.domain.vm_inst import VmInst
from snaps.openstack.utils import keystone_utils, glance_utils, neutron_utils
@@ -522,6 +523,40 @@ def add_floating_ip_to_server(nova, vm, floating_ip, ip_addr):
vm.add_floating_ip(floating_ip.ip, ip_addr)
+def get_compute_quotas(nova, project_id):
+ """
+ Returns a list of all available keypairs
+ :param nova: the Nova client
+ :param project_id: the project's ID of the quotas to lookup
+ :return: an object of type ComputeQuotas or None if not found
+ """
+ quotas = nova.quotas.get(tenant_id=project_id)
+ if quotas:
+ return ComputeQuotas(quotas)
+
+
+def update_quotas(nova, project_id, compute_quotas):
+ """
+ Updates the compute quotas for a given project
+ :param nova: the Nova client
+ :param project_id: the project's ID that requires quota updates
+ :param compute_quotas: an object of type ComputeQuotas containing the
+ values to update
+ :return:
+ """
+ update_values = dict()
+ update_values['metadata_items'] = compute_quotas.metadata_items
+ update_values['cores'] = compute_quotas.cores
+ update_values['instances'] = compute_quotas.instances
+ update_values['injected_files'] = compute_quotas.injected_files
+ update_values['injected_file_content_bytes'] = compute_quotas.injected_file_content_bytes
+ update_values['ram'] = compute_quotas.ram
+ update_values['fixed_ips'] = compute_quotas.fixed_ips
+ update_values['key_pairs'] = compute_quotas.key_pairs
+
+ return nova.quotas.update(project_id, **update_values)
+
+
class NovaException(Exception):
"""
Exception when calls to the Keystone client cannot be served properly