def _CombinePerInstanceConfigMessage(self, holder, per_instance_config,
                                         instance_ref, args):
        update_stateful_disks = args.stateful_disk
        remove_stateful_disks = args.remove_stateful_disks
        update_stateful_metadata = args.stateful_metadata
        remove_stateful_metadata = args.remove_stateful_metadata

        messages = holder.client.messages
        # Patch stateful disks.
        disk_getter = instance_disk_getter.InstanceDiskGetter(
            instance_ref=instance_ref, holder=holder)
        disks_to_remove_set = set(remove_stateful_disks or [])
        disks_to_update_dict = {
            update_stateful_disk.get('device-name'): update_stateful_disk
            for update_stateful_disk in (update_stateful_disks or [])
        }
        new_stateful_disks = UpdateGA._UpdateStatefulDisks(
            messages, per_instance_config, disks_to_update_dict,
            disks_to_remove_set, disk_getter)

        # Patch stateful metadata.
        new_stateful_metadata = UpdateGA._UpdateStatefulMetadata(
            messages, per_instance_config, update_stateful_metadata,
            remove_stateful_metadata)

        per_instance_config.preservedState.disks = (
            messages.PreservedState.DisksValue(
                additionalProperties=new_stateful_disks))

        per_instance_config.preservedState.metadata = (
            messages.PreservedState.MetadataValue(additionalProperties=[
                instance_configs_messages.MakePreservedStateMetadataEntry(
                    messages, key=key, value=value)
                for key, value in sorted(six.iteritems(new_stateful_metadata))
            ]))

        return per_instance_config
예제 #2
0
    def _CombinePerInstanceConfigMessage(holder, configs_getter, igm_ref,
                                         instance_ref, update_stateful_disks,
                                         remove_stateful_disks,
                                         update_stateful_metadata,
                                         remove_stateful_metadata):
        # Patch stateful disks.
        disk_getter = instance_disk_getter.InstanceDiskGetter(
            instance_ref=instance_ref, holder=holder)
        messages = holder.client.messages
        per_instance_config = configs_getter.get_instance_config(
            igm_ref=igm_ref, instance_ref=instance_ref)
        disks_to_remove_set = set(remove_stateful_disks or [])
        disks_to_update_dict = {
            update_stateful_disk.get('device-name'): update_stateful_disk
            for update_stateful_disk in (update_stateful_disks or [])
        }
        new_stateful_disks = UpdateBeta._UpdateStatefulDisks(
            messages, per_instance_config, disks_to_update_dict,
            disks_to_remove_set, disk_getter)
        # Patch stateful metadata.
        new_stateful_metadata = UpdateBeta._UpdateStatefulMetadata(
            messages, per_instance_config, update_stateful_metadata,
            remove_stateful_metadata)

        # Create preserved state
        preserved_state = messages.PreservedState()
        preserved_state.disks = messages.PreservedState.DisksValue(
            additionalProperties=new_stateful_disks)
        preserved_state.metadata = messages.PreservedState.MetadataValue(
            additionalProperties=[
                instance_configs_messages.MakePreservedStateMetadataEntry(
                    messages, key=key, value=value)
                for key, value in sorted(six.iteritems(new_stateful_metadata))
            ])
        per_instance_config.preservedState = preserved_state
        return per_instance_config
예제 #3
0
파일: update.py 프로젝트: twinDev/order-api
    def _CombinePerInstanceConfigMessage(holder, configs_getter, igm_ref,
                                         instance_ref, update_stateful_disks,
                                         remove_stateful_disks,
                                         update_stateful_metadata,
                                         remove_stateful_metadata):
        disk_getter = instance_disk_getter.InstanceDiskGetter(
            instance_ref=instance_ref, holder=holder)
        messages = holder.client.messages
        per_instance_config = configs_getter.get_instance_config(
            igm_ref=igm_ref, instance_ref=instance_ref)
        remove_stateful_disks_set = set(remove_stateful_disks or [])
        removed_stateful_disks_set = set()
        update_stateful_disks_dict = Update._UpdateStatefulDisksToDict(
            update_stateful_disks)
        new_stateful_disks = []
        existing_disks = []
        if per_instance_config.preservedState.disks:
            existing_disks =\
                per_instance_config.preservedState.disks.additionalProperties
        for current_stateful_disk in existing_disks:
            disk_name = current_stateful_disk.key
            # Disk to be removed
            if disk_name in remove_stateful_disks_set:
                removed_stateful_disks_set.add(disk_name)
                continue
            # Disk to be updated
            if disk_name in update_stateful_disks_dict:
                update_disk_data = update_stateful_disks_dict[disk_name]
                source = update_disk_data.get('source')
                mode = update_disk_data.get('mode')
                auto_delete = update_disk_data.get('auto-delete')
                if not (source or mode):
                    raise exceptions.InvalidArgumentException(
                        parameter_name='--update-stateful-disk',
                        message=(
                            '[source] or [mode] is required when updating'
                            ' [device-name] already existing in instance config'
                        ))
                preserved_disk = current_stateful_disk.value
                if source:
                    preserved_disk.source = source
                if mode:
                    preserved_disk.mode = instance_configs_messages.GetMode(
                        messages=messages, mode=mode)
                if auto_delete:
                    preserved_disk.autoDelete = auto_delete.GetAutoDeleteEnumValue(
                        messages.PreservedStatePreservedDisk.
                        AutoDeleteValueValuesEnum)
                del update_stateful_disks_dict[disk_name]
            new_stateful_disks.append(current_stateful_disk)
        unremoved_stateful_disks = (
            remove_stateful_disks_set.difference(removed_stateful_disks_set))
        if unremoved_stateful_disks:
            raise exceptions.InvalidArgumentException(
                parameter_name='--remove-stateful-disk',
                message=(
                    'The following are invalid stateful disks: `{0}`'.format(
                        ','.join(unremoved_stateful_disks))))
        for update_stateful_disk in update_stateful_disks_dict.values():
            new_stateful_disks.append(
                instance_configs_messages.MakePreservedStateDiskEntry(
                    messages=messages,
                    stateful_disk_data=update_stateful_disk,
                    disk_getter=disk_getter))

        existing_metadata = []
        if per_instance_config.preservedState.metadata:
            existing_metadata = per_instance_config.preservedState\
                .metadata.additionalProperties
        new_stateful_metadata = {
            metadata.key: metadata.value
            for metadata in existing_metadata
        }
        for stateful_metadata_key_to_remove in remove_stateful_metadata or []:
            if stateful_metadata_key_to_remove in new_stateful_metadata:
                del new_stateful_metadata[stateful_metadata_key_to_remove]
            else:
                raise exceptions.InvalidArgumentException(
                    parameter_name='--remove-stateful-metadata',
                    message=
                    ('stateful metadata key to remove `{0}` does not exist in'
                     ' the given instance config'.format(
                         stateful_metadata_key_to_remove)))
        new_stateful_metadata.update(update_stateful_metadata)

        # Create preserved state
        preserved_state = messages.PreservedState()
        preserved_state.disks = messages.PreservedState.DisksValue(
            additionalProperties=new_stateful_disks)
        preserved_state.metadata = messages.PreservedState.MetadataValue(
            additionalProperties=[
                instance_configs_messages.MakePreservedStateMetadataEntry(
                    messages, key=key, value=value)
                for key, value in sorted(six.iteritems(new_stateful_metadata))
            ])
        per_instance_config.preservedState = preserved_state
        return per_instance_config