2 ##############################################################################
3 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
14 import utils.logger as log
15 import utils.infra_setup.heat.manager as client_manager
17 LOG = log.Logger(__name__).getLogger()
19 neutron_quota = {"subnet": -1,
26 "security_group_rule": -1,
29 nova_quota = {"ram": -1,
38 "security_groups": -1,
39 "security_group_rules": -1,
40 "server_group_members": -1,
41 "injected_file_content_bytes": -1,
42 "injected_file_path_bytes": -1}
45 def check_https_enabled():
46 LOG.debug("Check if https is enabled in OpenStack")
47 os_auth_url = os.getenv('OS_AUTH_URL')
48 if os_auth_url.startswith('https'):
49 LOG.debug("https is enabled")
51 LOG.debug("https is not enabled")
55 def quota_env_prepare():
56 https_enabled = check_https_enabled()
58 insecure = os.getenv('OS_INSECURE',)
60 LOG.info("https is enabled")
62 if insecure.lower() == "true":
63 insecure_option = ' --insecure '
65 LOG.warn("Env variable OS_INSECURE is {}: if https + no "
66 "credential used, it should be set as True."
69 quota_name = os.getenv("OS_PROJECT_NAME")
71 cmd = ("openstack {} project list | grep ".format(insecure_option) +
73 " | awk '{print $2}'")
74 result = commands.getstatusoutput(cmd)
75 if result[0] == 0 and 'exception' not in result[1]:
76 LOG.info("Get %s project name is %s" % (quota_name, result[1]))
78 LOG.error("can't get openstack project name")
81 quota_name = os.getenv("OS_TENANT_NAME")
82 cmd = ("openstack {} tenant list | grep ".format(insecure_option) +
84 " | awk '{print $2}'")
85 result = commands.getstatusoutput(cmd)
86 if result[0] == 0 and 'exception' not in result[1]:
87 LOG.info("Get %s tenant name is %s" % (quota_name, result[1]))
89 LOG.error("can't get openstack tenant name")
92 openstack_id = result[1]
94 nova_client = client_manager._get_nova_client()
95 neutron_client = client_manager._get_neutron_client()
97 nova_q = nova_client.quotas.get(openstack_id).to_dict()
98 neutron_q = neutron_client.show_quota(openstack_id)
99 LOG.info(quota_name + " nova and neutron quotas (previous) :")
103 nova_client.quotas.update(openstack_id, **nova_quota)
104 neutron_client.update_quota(openstack_id,
105 {'quota': neutron_quota})
106 LOG.info("Quota has been changed!")
108 nova_q = nova_client.quotas.get(openstack_id).to_dict()
109 neutron_q = neutron_client.show_quota(openstack_id)
110 LOG.info(quota_name + " nova and neutron quotas (now) :")