diff options
author | spisarski <s.pisarski@cablelabs.com> | 2017-08-07 14:14:06 -0600 |
---|---|---|
committer | spisarski <s.pisarski@cablelabs.com> | 2017-08-09 14:24:07 -0600 |
commit | 79c0075b2e3872f9cde8101403b19bd963f7f992 (patch) | |
tree | e1c57261dac38964cc851dc001eb9f35be97e214 /snaps/openstack/utils | |
parent | 430905e7f76e4a074167a49ca2bfbf727eebcefd (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/utils')
-rw-r--r-- | snaps/openstack/utils/keystone_utils.py | 6 | ||||
-rw-r--r-- | snaps/openstack/utils/neutron_utils.py | 34 | ||||
-rw-r--r-- | snaps/openstack/utils/nova_utils.py | 35 |
3 files changed, 74 insertions, 1 deletions
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 |