1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
|
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
# Copyright (c) 2016 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import munch
import six
_IMAGE_FIELDS = (
'checksum',
'container_format',
'direct_url',
'disk_format',
'file',
'id',
'name',
'owner',
'virtual_size',
)
_SERVER_FIELDS = (
'accessIPv4',
'accessIPv6',
'addresses',
'adminPass',
'created',
'key_name',
'metadata',
'networks',
'private_v4',
'public_v4',
'public_v6',
'status',
'updated',
'user_id',
)
_KEYPAIR_FIELDS = (
'fingerprint',
'name',
'private_key',
'public_key',
'user_id',
)
_KEYPAIR_USELESS_FIELDS = (
'deleted',
'deleted_at',
'id',
'updated_at',
)
_COMPUTE_LIMITS_FIELDS = (
('maxPersonality', 'max_personality'),
('maxPersonalitySize', 'max_personality_size'),
('maxServerGroupMembers', 'max_server_group_members'),
('maxServerGroups', 'max_server_groups'),
('maxServerMeta', 'max_server_meta'),
('maxTotalCores', 'max_total_cores'),
('maxTotalInstances', 'max_total_instances'),
('maxTotalKeypairs', 'max_total_keypairs'),
('maxTotalRAMSize', 'max_total_ram_size'),
('totalCoresUsed', 'total_cores_used'),
('totalInstancesUsed', 'total_instances_used'),
('totalRAMUsed', 'total_ram_used'),
('totalServerGroupsUsed', 'total_server_groups_used'),
)
_pushdown_fields = {
'project': [
'domain_id'
]
}
def _split_filters(obj_name='', filters=None, **kwargs):
# Handle jmsepath filters
if not filters:
filters = {}
if not isinstance(filters, dict):
return {}, filters
# Filter out None values from extra kwargs, because those are
# defaults. If you want to search for things with None values,
# they're going to need to go into the filters dict
for (key, value) in kwargs.items():
if value is not None:
filters[key] = value
pushdown = {}
client = {}
for (key, value) in filters.items():
if key in _pushdown_fields.get(obj_name, {}):
pushdown[key] = value
else:
client[key] = value
return pushdown, client
def _to_bool(value):
if isinstance(value, six.string_types):
if not value:
return False
prospective = value.lower().capitalize()
return prospective == 'True'
return bool(value)
def _pop_int(resource, key):
return int(resource.pop(key, 0) or 0)
def _pop_float(resource, key):
return float(resource.pop(key, 0) or 0)
def _pop_or_get(resource, key, default, strict):
if strict:
return resource.pop(key, default)
else:
return resource.get(key, default)
class Normalizer(object):
'''Mix-in class to provide the normalization functions.
This is in a separate class just for on-disk source code organization
reasons.
'''
def _normalize_compute_limits(self, limits, project_id=None):
""" Normalize a limits object.
Limits modified in this method and shouldn't be modified afterwards.
"""
# Copy incoming limits because of shared dicts in unittests
limits = limits['absolute'].copy()
new_limits = munch.Munch()
new_limits['location'] = self._get_current_location(
project_id=project_id)
for field in _COMPUTE_LIMITS_FIELDS:
new_limits[field[1]] = limits.pop(field[0], None)
new_limits['properties'] = limits.copy()
return new_limits
def _remove_novaclient_artifacts(self, item):
# Remove novaclient artifacts
item.pop('links', None)
item.pop('NAME_ATTR', None)
item.pop('HUMAN_ID', None)
item.pop('human_id', None)
item.pop('request_ids', None)
item.pop('x_openstack_request_ids', None)
def _normalize_flavors(self, flavors):
""" Normalize a list of flavor objects """
ret = []
for flavor in flavors:
ret.append(self._normalize_flavor(flavor))
return ret
def _normalize_flavor(self, flavor):
""" Normalize a flavor object """
new_flavor = munch.Munch()
# Copy incoming group because of shared dicts in unittests
flavor = flavor.copy()
# Discard noise
self._remove_novaclient_artifacts(flavor)
flavor.pop('links', None)
ephemeral = int(_pop_or_get(
flavor, 'OS-FLV-EXT-DATA:ephemeral', 0, self.strict_mode))
ephemeral = flavor.pop('ephemeral', ephemeral)
is_public = _to_bool(_pop_or_get(
flavor, 'os-flavor-access:is_public', True, self.strict_mode))
is_public = _to_bool(flavor.pop('is_public', is_public))
is_disabled = _to_bool(_pop_or_get(
flavor, 'OS-FLV-DISABLED:disabled', False, self.strict_mode))
extra_specs = _pop_or_get(
flavor, 'OS-FLV-WITH-EXT-SPECS:extra_specs', {}, self.strict_mode)
extra_specs = flavor.pop('extra_specs', extra_specs)
extra_specs = munch.Munch(extra_specs)
new_flavor['location'] = self.current_location
new_flavor['id'] = flavor.pop('id')
new_flavor['name'] = flavor.pop('name')
new_flavor['is_public'] = is_public
new_flavor['is_disabled'] = is_disabled
new_flavor['ram'] = _pop_int(flavor, 'ram')
new_flavor['vcpus'] = _pop_int(flavor, 'vcpus')
new_flavor['disk'] = _pop_int(flavor, 'disk')
new_flavor['ephemeral'] = ephemeral
new_flavor['swap'] = _pop_int(flavor, 'swap')
new_flavor['rxtx_factor'] = _pop_float(flavor, 'rxtx_factor')
new_flavor['properties'] = flavor.copy()
new_flavor['extra_specs'] = extra_specs
# Backwards compat with nova - passthrough values
if not self.strict_mode:
for (k, v) in new_flavor['properties'].items():
new_flavor.setdefault(k, v)
return new_flavor
def _normalize_keypairs(self, keypairs):
"""Normalize Nova Keypairs"""
ret = []
for keypair in keypairs:
ret.append(self._normalize_keypair(keypair))
return ret
def _normalize_keypair(self, keypair):
"""Normalize Ironic Machine"""
new_keypair = munch.Munch()
keypair = keypair.copy()
# Discard noise
self._remove_novaclient_artifacts(keypair)
new_keypair['location'] = self.current_location
for key in _KEYPAIR_FIELDS:
new_keypair[key] = keypair.pop(key, None)
# These are completely meaningless fields
for key in _KEYPAIR_USELESS_FIELDS:
keypair.pop(key, None)
new_keypair['type'] = keypair.pop('type', 'ssh')
# created_at isn't returned from the keypair creation. (what?)
new_keypair['created_at'] = keypair.pop(
'created_at', datetime.datetime.now().isoformat())
# Don't even get me started on this
new_keypair['id'] = new_keypair['name']
new_keypair['properties'] = keypair.copy()
return new_keypair
def _normalize_images(self, images):
ret = []
for image in images:
ret.append(self._normalize_image(image))
return ret
def _normalize_image(self, image):
new_image = munch.Munch(
location=self._get_current_location(project_id=image.get('owner')))
# This copy is to keep things from getting epically weird in tests
image = image.copy()
# Discard noise
self._remove_novaclient_artifacts(image)
# If someone made a property called "properties" that contains a
# string (this has happened at least one time in the wild), the
# the rest of the normalization here goes belly up.
properties = image.pop('properties', {})
if not isinstance(properties, dict):
properties = {'properties': properties}
visibility = image.pop('visibility', None)
protected = _to_bool(image.pop('protected', False))
if visibility:
is_public = (visibility == 'public')
else:
is_public = image.pop('is_public', False)
visibility = 'public' if is_public else 'private'
new_image['size'] = image.pop('OS-EXT-IMG-SIZE:size', 0)
new_image['size'] = image.pop('size', new_image['size'])
new_image['min_ram'] = image.pop('minRam', 0)
new_image['min_ram'] = image.pop('min_ram', new_image['min_ram'])
new_image['min_disk'] = image.pop('minDisk', 0)
new_image['min_disk'] = image.pop('min_disk', new_image['min_disk'])
new_image['created_at'] = image.pop('created', '')
new_image['created_at'] = image.pop(
'created_at', new_image['created_at'])
new_image['updated_at'] = image.pop('updated', '')
new_image['updated_at'] = image.pop(
'updated_at', new_image['updated_at'])
for field in _IMAGE_FIELDS:
new_image[field] = image.pop(field, None)
new_image['tags'] = image.pop('tags', [])
new_image['status'] = image.pop('status').lower()
for field in ('min_ram', 'min_disk', 'size', 'virtual_size'):
new_image[field] = _pop_int(new_image, field)
new_image['is_protected'] = protected
new_image['locations'] = image.pop('locations', [])
metadata = image.pop('metadata', {})
for key, val in metadata.items():
properties.setdefault(key, val)
for key, val in image.items():
properties.setdefault(key, val)
new_image['properties'] = properties
new_image['is_public'] = is_public
new_image['visibility'] = visibility
# Backwards compat with glance
if not self.strict_mode:
for key, val in properties.items():
if key != 'properties':
new_image[key] = val
new_image['protected'] = protected
new_image['metadata'] = properties
new_image['created'] = new_image['created_at']
new_image['updated'] = new_image['updated_at']
new_image['minDisk'] = new_image['min_disk']
new_image['minRam'] = new_image['min_ram']
return new_image
def _normalize_secgroups(self, groups):
"""Normalize the structure of security groups
This makes security group dicts, as returned from nova, look like the
security group dicts as returned from neutron. This does not make them
look exactly the same, but it's pretty close.
:param list groups: A list of security group dicts.
:returns: A list of normalized dicts.
"""
ret = []
for group in groups:
ret.append(self._normalize_secgroup(group))
return ret
def _normalize_secgroup(self, group):
ret = munch.Munch()
# Copy incoming group because of shared dicts in unittests
group = group.copy()
# Discard noise
self._remove_novaclient_artifacts(group)
rules = self._normalize_secgroup_rules(
group.pop('security_group_rules', group.pop('rules', [])))
project_id = group.pop('tenant_id', '')
project_id = group.pop('project_id', project_id)
ret['location'] = self._get_current_location(project_id=project_id)
ret['id'] = group.pop('id')
ret['name'] = group.pop('name')
ret['security_group_rules'] = rules
ret['description'] = group.pop('description')
ret['properties'] = group
# Backwards compat with Neutron
if not self.strict_mode:
ret['tenant_id'] = project_id
ret['project_id'] = project_id
for key, val in ret['properties'].items():
ret.setdefault(key, val)
return ret
def _normalize_secgroup_rules(self, rules):
"""Normalize the structure of nova security group rules
Note that nova uses -1 for non-specific port values, but neutron
represents these with None.
:param list rules: A list of security group rule dicts.
:returns: A list of normalized dicts.
"""
ret = []
for rule in rules:
ret.append(self._normalize_secgroup_rule(rule))
return ret
def _normalize_secgroup_rule(self, rule):
ret = munch.Munch()
# Copy incoming rule because of shared dicts in unittests
rule = rule.copy()
ret['id'] = rule.pop('id')
ret['direction'] = rule.pop('direction', 'ingress')
ret['ethertype'] = rule.pop('ethertype', 'IPv4')
port_range_min = rule.get(
'port_range_min', rule.pop('from_port', None))
if port_range_min == -1:
port_range_min = None
if port_range_min is not None:
port_range_min = int(port_range_min)
ret['port_range_min'] = port_range_min
port_range_max = rule.pop(
'port_range_max', rule.pop('to_port', None))
if port_range_max == -1:
port_range_max = None
if port_range_min is not None:
port_range_min = int(port_range_min)
ret['port_range_max'] = port_range_max
ret['protocol'] = rule.pop('protocol', rule.pop('ip_protocol', None))
ret['remote_ip_prefix'] = rule.pop(
'remote_ip_prefix', rule.pop('ip_range', {}).get('cidr', None))
ret['security_group_id'] = rule.pop(
'security_group_id', rule.pop('parent_group_id', None))
ret['remote_group_id'] = rule.pop('remote_group_id', None)
project_id = rule.pop('tenant_id', '')
project_id = rule.pop('project_id', project_id)
ret['location'] = self._get_current_location(project_id=project_id)
ret['properties'] = rule
# Backwards compat with Neutron
if not self.strict_mode:
ret['tenant_id'] = project_id
ret['project_id'] = project_id
for key, val in ret['properties'].items():
ret.setdefault(key, val)
return ret
def _normalize_servers(self, servers):
# Here instead of _utils because we need access to region and cloud
# name from the cloud object
ret = []
for server in servers:
ret.append(self._normalize_server(server))
return ret
def _normalize_server(self, server):
ret = munch.Munch()
# Copy incoming server because of shared dicts in unittests
server = server.copy()
self._remove_novaclient_artifacts(server)
ret['id'] = server.pop('id')
ret['name'] = server.pop('name')
server['flavor'].pop('links', None)
ret['flavor'] = server.pop('flavor')
# OpenStack can return image as a string when you've booted
# from volume
if str(server['image']) != server['image']:
server['image'].pop('links', None)
ret['image'] = server.pop('image')
project_id = server.pop('tenant_id', '')
project_id = server.pop('project_id', project_id)
az = _pop_or_get(
server, 'OS-EXT-AZ:availability_zone', None, self.strict_mode)
ret['location'] = self._get_current_location(
project_id=project_id, zone=az)
# Ensure volumes is always in the server dict, even if empty
ret['volumes'] = _pop_or_get(
server, 'os-extended-volumes:volumes_attached',
[], self.strict_mode)
config_drive = server.pop('config_drive', False)
ret['has_config_drive'] = _to_bool(config_drive)
host_id = server.pop('hostId', None)
ret['host_id'] = host_id
ret['progress'] = _pop_int(server, 'progress')
# Leave these in so that the general properties handling works
ret['disk_config'] = _pop_or_get(
server, 'OS-DCF:diskConfig', None, self.strict_mode)
for key in (
'OS-EXT-STS:power_state',
'OS-EXT-STS:task_state',
'OS-EXT-STS:vm_state',
'OS-SRV-USG:launched_at',
'OS-SRV-USG:terminated_at'):
short_key = key.split(':')[1]
ret[short_key] = _pop_or_get(server, key, None, self.strict_mode)
# Protect against security_groups being None
ret['security_groups'] = server.pop('security_groups', None) or []
# NOTE(mnaser): The Nova API returns the creation date in `created`
# however the Shade contract returns `created_at` for
# all resources.
ret['created_at'] = server.get('created')
for field in _SERVER_FIELDS:
ret[field] = server.pop(field, None)
if not ret['networks']:
ret['networks'] = {}
ret['interface_ip'] = ''
ret['properties'] = server.copy()
# Backwards compat
if not self.strict_mode:
ret['hostId'] = host_id
ret['config_drive'] = config_drive
ret['project_id'] = project_id
ret['tenant_id'] = project_id
ret['region'] = self.region_name
ret['cloud'] = self.name
ret['az'] = az
for key, val in ret['properties'].items():
ret.setdefault(key, val)
return ret
def _normalize_floating_ips(self, ips):
"""Normalize the structure of floating IPs
Unfortunately, not all the Neutron floating_ip attributes are available
with Nova and not all Nova floating_ip attributes are available with
Neutron.
This function extract attributes that are common to Nova and Neutron
floating IP resource.
If the whole structure is needed inside shade, shade provides private
methods that returns "original" objects (e.g.
_neutron_allocate_floating_ip)
:param list ips: A list of Neutron floating IPs.
:returns:
A list of normalized dicts with the following attributes::
[
{
"id": "this-is-a-floating-ip-id",
"fixed_ip_address": "192.0.2.10",
"floating_ip_address": "198.51.100.10",
"network": "this-is-a-net-or-pool-id",
"attached": True,
"status": "ACTIVE"
}, ...
]
"""
return [
self._normalize_floating_ip(ip) for ip in ips
]
def _normalize_floating_ip(self, ip):
ret = munch.Munch()
# Copy incoming floating ip because of shared dicts in unittests
ip = ip.copy()
fixed_ip_address = ip.pop('fixed_ip_address', ip.pop('fixed_ip', None))
floating_ip_address = ip.pop('floating_ip_address', ip.pop('ip', None))
network_id = ip.pop(
'floating_network_id', ip.pop('network', ip.pop('pool', None)))
project_id = ip.pop('tenant_id', '')
project_id = ip.pop('project_id', project_id)
instance_id = ip.pop('instance_id', None)
router_id = ip.pop('router_id', None)
id = ip.pop('id')
port_id = ip.pop('port_id', None)
created_at = ip.pop('created_at', None)
updated_at = ip.pop('updated_at', None)
# Note - description may not always be on the underlying cloud.
# Normalizing it here is easy - what do we do when people want to
# set a description?
description = ip.pop('description', '')
revision_number = ip.pop('revision_number', None)
if self._use_neutron_floating():
attached = bool(port_id)
status = ip.pop('status', 'UNKNOWN')
else:
attached = bool(instance_id)
# In neutron's terms, Nova floating IPs are always ACTIVE
status = 'ACTIVE'
ret = munch.Munch(
attached=attached,
fixed_ip_address=fixed_ip_address,
floating_ip_address=floating_ip_address,
id=id,
location=self._get_current_location(project_id=project_id),
network=network_id,
port=port_id,
router=router_id,
status=status,
created_at=created_at,
updated_at=updated_at,
description=description,
revision_number=revision_number,
properties=ip.copy(),
)
# Backwards compat
if not self.strict_mode:
ret['port_id'] = port_id
ret['router_id'] = router_id
ret['project_id'] = project_id
ret['tenant_id'] = project_id
ret['floating_network_id'] = network_id
for key, val in ret['properties'].items():
ret.setdefault(key, val)
return ret
def _normalize_projects(self, projects):
"""Normalize the structure of projects
This makes tenants from keystone v2 look like projects from v3.
:param list projects: A list of projects to normalize
:returns: A list of normalized dicts.
"""
ret = []
for project in projects:
ret.append(self._normalize_project(project))
return ret
def _normalize_project(self, project):
# Copy incoming project because of shared dicts in unittests
project = project.copy()
# Discard noise
self._remove_novaclient_artifacts(project)
# In both v2 and v3
project_id = project.pop('id')
name = project.pop('name', '')
description = project.pop('description', '')
is_enabled = project.pop('enabled', True)
# v3 additions
domain_id = project.pop('domain_id', 'default')
parent_id = project.pop('parent_id', None)
is_domain = project.pop('is_domain', False)
# Projects have a special relationship with location
location = self._get_identity_location()
location['project']['domain_id'] = domain_id
location['project']['id'] = parent_id
ret = munch.Munch(
location=location,
id=project_id,
name=name,
description=description,
is_enabled=is_enabled,
is_domain=is_domain,
domain_id=domain_id,
properties=project.copy()
)
# Backwards compat
if not self.strict_mode:
ret['enabled'] = is_enabled
ret['parent_id'] = parent_id
for key, val in ret['properties'].items():
ret.setdefault(key, val)
return ret
def _normalize_volume_type_access(self, volume_type_access):
volume_type_access = volume_type_access.copy()
volume_type_id = volume_type_access.pop('volume_type_id')
project_id = volume_type_access.pop('project_id')
ret = munch.Munch(
location=self.current_location,
project_id=project_id,
volume_type_id=volume_type_id,
properties=volume_type_access.copy(),
)
return ret
def _normalize_volume_type_accesses(self, volume_type_accesses):
ret = []
for volume_type_access in volume_type_accesses:
ret.append(self._normalize_volume_type_access(volume_type_access))
return ret
def _normalize_volume_type(self, volume_type):
volume_type = volume_type.copy()
volume_id = volume_type.pop('id')
description = volume_type.pop('description', None)
name = volume_type.pop('name', None)
old_is_public = volume_type.pop('os-volume-type-access:is_public',
False)
is_public = volume_type.pop('is_public', old_is_public)
qos_specs_id = volume_type.pop('qos_specs_id', None)
extra_specs = volume_type.pop('extra_specs', {})
ret = munch.Munch(
location=self.current_location,
is_public=is_public,
id=volume_id,
name=name,
description=description,
qos_specs_id=qos_specs_id,
extra_specs=extra_specs,
properties=volume_type.copy(),
)
return ret
def _normalize_volume_types(self, volume_types):
ret = []
for volume in volume_types:
ret.append(self._normalize_volume_type(volume))
return ret
def _normalize_volumes(self, volumes):
"""Normalize the structure of volumes
This makes tenants from cinder v1 look like volumes from v2.
:param list projects: A list of volumes to normalize
:returns: A list of normalized dicts.
"""
ret = []
for volume in volumes:
ret.append(self._normalize_volume(volume))
return ret
def _normalize_volume(self, volume):
volume = volume.copy()
# Discard noise
self._remove_novaclient_artifacts(volume)
volume_id = volume.pop('id')
name = volume.pop('display_name', None)
name = volume.pop('name', name)
description = volume.pop('display_description', None)
description = volume.pop('description', description)
is_bootable = _to_bool(volume.pop('bootable', True))
is_encrypted = _to_bool(volume.pop('encrypted', False))
can_multiattach = _to_bool(volume.pop('multiattach', False))
project_id = _pop_or_get(
volume, 'os-vol-tenant-attr:tenant_id', None, self.strict_mode)
az = volume.pop('availability_zone', None)
location = self._get_current_location(project_id=project_id, zone=az)
host = _pop_or_get(
volume, 'os-vol-host-attr:host', None, self.strict_mode)
replication_extended_status = _pop_or_get(
volume, 'os-volume-replication:extended_status',
None, self.strict_mode)
migration_status = _pop_or_get(
volume, 'os-vol-mig-status-attr:migstat', None, self.strict_mode)
migration_status = volume.pop('migration_status', migration_status)
_pop_or_get(volume, 'user_id', None, self.strict_mode)
source_volume_id = _pop_or_get(
volume, 'source_volid', None, self.strict_mode)
replication_driver = _pop_or_get(
volume, 'os-volume-replication:driver_data',
None, self.strict_mode)
ret = munch.Munch(
location=location,
id=volume_id,
name=name,
description=description,
size=_pop_int(volume, 'size'),
attachments=volume.pop('attachments', []),
status=volume.pop('status'),
migration_status=migration_status,
host=host,
replication_driver=replication_driver,
replication_status=volume.pop('replication_status', None),
replication_extended_status=replication_extended_status,
snapshot_id=volume.pop('snapshot_id', None),
created_at=volume.pop('created_at'),
updated_at=volume.pop('updated_at', None),
source_volume_id=source_volume_id,
consistencygroup_id=volume.pop('consistencygroup_id', None),
volume_type=volume.pop('volume_type', None),
metadata=volume.pop('metadata', {}),
is_bootable=is_bootable,
is_encrypted=is_encrypted,
can_multiattach=can_multiattach,
properties=volume.copy(),
)
# Backwards compat
if not self.strict_mode:
ret['display_name'] = name
ret['display_description'] = description
ret['bootable'] = is_bootable
ret['encrypted'] = is_encrypted
ret['multiattach'] = can_multiattach
ret['availability_zone'] = az
for key, val in ret['properties'].items():
ret.setdefault(key, val)
return ret
def _normalize_volume_attachment(self, attachment):
""" Normalize a volume attachment object"""
attachment = attachment.copy()
# Discard noise
self._remove_novaclient_artifacts(attachment)
return munch.Munch(**attachment)
def _normalize_volume_backups(self, backups):
ret = []
for backup in backups:
ret.append(self._normalize_volume_backup(backup))
return ret
def _normalize_volume_backup(self, backup):
""" Normalize a valume backup object"""
backup = backup.copy()
# Discard noise
self._remove_novaclient_artifacts(backup)
return munch.Munch(**backup)
def _normalize_compute_usage(self, usage):
""" Normalize a compute usage object """
usage = usage.copy()
# Discard noise
self._remove_novaclient_artifacts(usage)
project_id = usage.pop('tenant_id', None)
ret = munch.Munch(
location=self._get_current_location(project_id=project_id),
)
for key in (
'max_personality',
'max_personality_size',
'max_server_group_members',
'max_server_groups',
'max_server_meta',
'max_total_cores',
'max_total_instances',
'max_total_keypairs',
'max_total_ram_size',
'total_cores_used',
'total_hours',
'total_instances_used',
'total_local_gb_usage',
'total_memory_mb_usage',
'total_ram_used',
'total_server_groups_used',
'total_vcpus_usage'):
ret[key] = usage.pop(key, 0)
ret['started_at'] = usage.pop('start', None)
ret['stopped_at'] = usage.pop('stop', None)
ret['server_usages'] = self._normalize_server_usages(
usage.pop('server_usages', []))
ret['properties'] = usage
return ret
def _normalize_server_usage(self, server_usage):
""" Normalize a server usage object """
server_usage = server_usage.copy()
# TODO(mordred) Right now there is already a location on the usage
# object. Including one here seems verbose.
server_usage.pop('tenant_id')
ret = munch.Munch()
ret['ended_at'] = server_usage.pop('ended_at', None)
ret['started_at'] = server_usage.pop('started_at', None)
for key in (
'flavor',
'instance_id',
'name',
'state'):
ret[key] = server_usage.pop(key, '')
for key in (
'hours',
'local_gb',
'memory_mb',
'uptime',
'vcpus'):
ret[key] = server_usage.pop(key, 0)
ret['properties'] = server_usage
return ret
def _normalize_server_usages(self, server_usages):
ret = []
for server_usage in server_usages:
ret.append(self._normalize_server_usage(server_usage))
return ret
def _normalize_cluster_templates(self, cluster_templates):
ret = []
for cluster_template in cluster_templates:
ret.append(self._normalize_cluster_template(cluster_template))
return ret
def _normalize_cluster_template(self, cluster_template):
"""Normalize Magnum cluster_templates."""
cluster_template = cluster_template.copy()
# Discard noise
cluster_template.pop('links', None)
cluster_template.pop('human_id', None)
# model_name is a magnumclient-ism
cluster_template.pop('model_name', None)
ct_id = cluster_template.pop('uuid')
ret = munch.Munch(
id=ct_id,
location=self._get_current_location(),
)
ret['is_public'] = cluster_template.pop('public')
ret['is_registry_enabled'] = cluster_template.pop('registry_enabled')
ret['is_tls_disabled'] = cluster_template.pop('tls_disabled')
# pop floating_ip_enabled since we want to hide it in a future patch
fip_enabled = cluster_template.pop('floating_ip_enabled', None)
if not self.strict_mode:
ret['uuid'] = ct_id
if fip_enabled is not None:
ret['floating_ip_enabled'] = fip_enabled
ret['public'] = ret['is_public']
ret['registry_enabled'] = ret['is_registry_enabled']
ret['tls_disabled'] = ret['is_tls_disabled']
# Optional keys
for (key, default) in (
('fixed_network', None),
('fixed_subnet', None),
('http_proxy', None),
('https_proxy', None),
('labels', {}),
('master_flavor_id', None),
('no_proxy', None)):
if key in cluster_template:
ret[key] = cluster_template.pop(key, default)
for key in (
'apiserver_port',
'cluster_distro',
'coe',
'created_at',
'dns_nameserver',
'docker_volume_size',
'external_network_id',
'flavor_id',
'image_id',
'insecure_registry',
'keypair_id',
'name',
'network_driver',
'server_type',
'updated_at',
'volume_driver'):
ret[key] = cluster_template.pop(key)
ret['properties'] = cluster_template
return ret
def _normalize_magnum_services(self, magnum_services):
ret = []
for magnum_service in magnum_services:
ret.append(self._normalize_magnum_service(magnum_service))
return ret
def _normalize_magnum_service(self, magnum_service):
"""Normalize Magnum magnum_services."""
magnum_service = magnum_service.copy()
# Discard noise
magnum_service.pop('links', None)
magnum_service.pop('human_id', None)
# model_name is a magnumclient-ism
magnum_service.pop('model_name', None)
ret = munch.Munch(location=self._get_current_location())
for key in (
'binary',
'created_at',
'disabled_reason',
'host',
'id',
'report_count',
'state',
'updated_at'):
ret[key] = magnum_service.pop(key)
ret['properties'] = magnum_service
return ret
def _normalize_stacks(self, stacks):
"""Normalize Heat Stacks"""
ret = []
for stack in stacks:
ret.append(self._normalize_stack(stack))
return ret
def _normalize_stack(self, stack):
"""Normalize Heat Stack"""
stack = stack.copy()
# Discard noise
self._remove_novaclient_artifacts(stack)
# Discard things heatclient adds that aren't in the REST
stack.pop('action', None)
stack.pop('status', None)
stack.pop('identifier', None)
stack_status = stack.pop('stack_status')
(action, status) = stack_status.split('_', 1)
ret = munch.Munch(
id=stack.pop('id'),
location=self._get_current_location(),
action=action,
status=status,
)
if not self.strict_mode:
ret['stack_status'] = stack_status
for (new_name, old_name) in (
('name', 'stack_name'),
('created_at', 'creation_time'),
('deleted_at', 'deletion_time'),
('updated_at', 'updated_time'),
('description', 'description'),
('is_rollback_enabled', 'disable_rollback'),
('parent', 'parent'),
('notification_topics', 'notification_topics'),
('parameters', 'parameters'),
('outputs', 'outputs'),
('owner', 'stack_owner'),
('status_reason', 'stack_status_reason'),
('stack_user_project_id', 'stack_user_project_id'),
('tempate_description', 'template_description'),
('timeout_mins', 'timeout_mins'),
('tags', 'tags')):
value = stack.pop(old_name, None)
ret[new_name] = value
if not self.strict_mode:
ret[old_name] = value
ret['identifier'] = '{name}/{id}'.format(
name=ret['name'], id=ret['id'])
ret['properties'] = stack
return ret
def _normalize_machines(self, machines):
"""Normalize Ironic Machines"""
ret = []
for machine in machines:
ret.append(self._normalize_machine(machine))
return ret
def _normalize_machine(self, machine):
"""Normalize Ironic Machine"""
machine = machine.copy()
# Discard noise
self._remove_novaclient_artifacts(machine)
# TODO(mordred) Normalize this resource
return machine
def _normalize_roles(self, roles):
"""Normalize Keystone roles"""
ret = []
for role in roles:
ret.append(self._normalize_role(role))
return ret
def _normalize_role(self, role):
"""Normalize Identity roles."""
return munch.Munch(
id=role.get('id'),
name=role.get('name'),
domain_id=role.get('domain_id'),
location=self._get_identity_location(),
properties={},
)
|