Adoption of base framework code for azure plugin
[multicloud/azure.git] / azure / azure / swagger / nova_utils.py
1 # Copyright (c) 2018 Amdocs
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
13 import six
14
15
16 def server_formatter(server, interfaces=[]):
17     r = {
18         "id": server.id,
19         "name": server.name,
20         "tenantId": server.project_id,
21         "availabilityZone": server.availability_zone,
22         "flavorId": server.flavor_id or server.flavor['id'],
23         "volumeArray": [],
24         "metadata": [],
25         "securityGroups": [],
26         # TODO finish following attributes
27         "serverGroup": "",
28         "contextArray": [],
29         "userdata": server.user_data,
30         "nicArray": [],
31         "status": server.status
32     }
33     if interfaces:
34         r['nicArray'] = [{'portId': i.port_id} for i in interfaces]
35     elif server.networks:
36         r['nicArray'] = [{'portId': n['port']} for n in server.networks]
37     # TODO: wait sdk fix block_device_mapping
38     try:
39         if server.attached_volumes:
40             r["volumeArray"] = [{'volumeId': v['id']}
41                                 for v in server.attached_volumes]
42         elif server.block_device_mapping:
43             r["volumeArray"] = [{'volumeId': v['uuid']}
44                                 for v in server.block_device_mapping]
45     except ValueError:
46         r['volumeArray'] = [{'volumeId': ""}]
47     if server.image_id or server.image:
48         r['boot'] = {
49             'type': 2,
50             'imageId': server.image_id or server.image['id']
51         }
52     else:
53         r['boot'] = {
54             'type': 1,
55             'volumeId': r['volumeArray'][0]['volumeId']
56         }
57     if server.metadata:
58         r["metadata"] = [{'keyName': k, 'value': v}
59                          for k, v in six.iteritems(server.metadata)]
60     if server.security_groups:
61         r["securityGroups"] = [i['name'] for i in server.security_groups]
62     return r
63
64
65 def flavor_formatter(flavor, extra_specs):
66     r = {
67         "id": flavor.id,
68         "name": flavor.name,
69         "vcpu": flavor.vcpus,
70         "memory": flavor.ram,
71         "disk": flavor.disk,
72         "ephemeral": flavor.ephemeral,
73         "swap": flavor.swap,
74         "isPublic": flavor.is_public}
75     if extra_specs:
76         r["extraSpecs"] = extra_specs_formatter(extra_specs)
77     return r
78
79
80 def extra_specs_formatter(extra_specs):
81     return [{"keyName": k, "value": v}
82             for k, v in six.iteritems(extra_specs.extra_specs)]
83
84
85 def server_limits_formatter(limits):
86     return {
87         # nova
88         'maxPersonality': limits.absolute.personality,
89         'maxPersonalitySize': limits.absolute.personality_size,
90         'maxServerGroupMembers': limits.absolute.server_group_members,
91         'maxServerGroups': limits.absolute.server_groups,
92         'maxImageMeta': limits.absolute.image_meta,
93         'maxTotalCores': limits.absolute.total_cores,
94         'maxTotalInstances': limits.absolute.instances,
95         'maxTotalKeypairs': limits.absolute.keypairs,
96         'maxTotalRAMSize': limits.absolute.total_ram,
97         'security_group_rules': limits.absolute.security_group_rules,
98         'security_group': limits.absolute.security_groups,
99
100         # cinder
101         # neutron
102     }
103
104
105 def service_formatter(service):
106     return {
107         'service': service.binary,
108         'name': service.host,
109         'zone': service.zone,
110     }
111
112
113 def hypervisor_formatter(hypervisor):
114     return {
115         'name': hypervisor.name,
116         'cpu': hypervisor.vcpus,
117         'disk_gb': hypervisor.local_disk_size,
118         'memory_mb': hypervisor.memory_size,
119     }