Merge "Update cirros image to 0.3.5"
[parser.git] / tosca2heat / heat-translator / translator / common / flavors.py
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import logging
14
15 try:
16     import novaclient.client
17     client_available = True
18 except ImportError:
19     client_available = False
20     pass
21
22 log = logging.getLogger('heat-translator')
23
24
25 PREDEF_FLAVORS = {
26     'm1.xlarge': {'mem_size': 16384, 'disk_size': 160, 'num_cpus': 8},
27     'm1.large': {'mem_size': 8192, 'disk_size': 80, 'num_cpus': 4},
28     'm1.medium': {'mem_size': 4096, 'disk_size': 40, 'num_cpus': 2},
29     'm1.small': {'mem_size': 2048, 'disk_size': 20, 'num_cpus': 1},
30     'm1.tiny': {'mem_size': 512, 'disk_size': 1, 'num_cpus': 1},
31     'm1.micro': {'mem_size': 128, 'disk_size': 0, 'num_cpus': 1},
32     'm1.nano': {'mem_size': 64, 'disk_size': 0, 'num_cpus': 1}
33 }
34
35 SESSION = None
36
37 FLAVORS = {}
38
39
40 def get_flavors():
41     global FLAVORS
42
43     if FLAVORS:
44         return FLAVORS
45
46     if SESSION is not None and client_available:
47         try:
48             client = novaclient.client.Client("2", session=SESSION)
49         except Exception as e:
50             # Handles any exception coming from openstack
51             log.warn(_('Choosing predefined flavors since received '
52                        'Openstack Exception: %s') % str(e))
53         else:
54             for flv in client.flavors.list(detailed=True):
55                 FLAVORS[str(flv.name)] = {
56                     "mem_size": flv.ram,
57                     "disk_size": flv.disk,
58                     "num_cpus": flv.vcpus
59                 }
60
61     if not FLAVORS:
62         FLAVORS = PREDEF_FLAVORS
63
64     return FLAVORS