267e70ab4d0a5d961cabf8f5b51cf2888fcea2ba
[bottlenecks.git] / utils / env_prepare / quota_prepare.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 #
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 ##############################################################################
10
11
12 import os
13 import commands
14 import utils.logger as log
15 import utils.infra_setup.heat.manager as client_manager
16
17 LOG = log.Logger(__name__).getLogger()
18
19 neutron_quota = {"subnet": -1,
20                  "network": -1,
21                  "floatingip": -1,
22                  "subnetpool": -1,
23                  "router": -1,
24                  "port": -1,
25                  "security_group": -1,
26                  "security_group_rule": -1,
27                  "rbac_policy": -1}
28
29 nova_quota = {"ram": -1,
30               "cores": -1,
31               "instances": -1,
32               "key_pairs": -1,
33               "fixed_ips": -1,
34               "floating_ips": -1,
35               "server_groups": -1,
36               "injected_files": -1,
37               "metadata_items": -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}
43
44
45 def quota_env_prepare():
46     tenant_name = os.getenv("OS_TENANT_NAME")
47     cmd = ("openstack project list | grep " +
48            tenant_name +
49            " | awk '{print $2}'")
50
51     result = commands.getstatusoutput(cmd)
52     if result[0] == 0:
53         LOG.info("Get %s project id is %s" % (tenant_name, result[1]))
54     else:
55         LOG.error("can't get openstack project id")
56         return 1
57
58     openstack_id = result[1]
59
60     nova_client = client_manager._get_nova_client()
61     neutron_client = client_manager._get_neutron_client()
62
63     nova_q = nova_client.quotas.get(openstack_id).to_dict()
64     neutron_q = neutron_client.show_quota(openstack_id)
65     LOG.info(tenant_name + "tenant nova and neutron quota(previous) :")
66     LOG.info(nova_q)
67     LOG.info(neutron_q)
68
69     nova_client.quotas.update(openstack_id, **nova_quota)
70     neutron_client.update_quota(openstack_id,
71                                 {'quota': neutron_quota})
72     LOG.info("Quota has been changed!")
73
74     nova_q = nova_client.quotas.get(openstack_id).to_dict()
75     neutron_q = neutron_client.show_quota(openstack_id)
76     LOG.info(tenant_name + "tenant nova and neutron quota(now) :")
77     LOG.info(nova_q)
78     LOG.info(neutron_q)
79     return 0