diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-08-29 10:52:44 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-08-29 10:52:44 -0700 |
commit | ff60020fa3d8e457cf9d1d543af9193376bf598c (patch) | |
tree | 274a01085f432aee75bb8ecd7727f9a6f4c734f3 /cloudinit | |
parent | 2e51e2efc292870479a7b972c7ebc9ceac85da6f (diff) | |
download | vyos-cloud-init-ff60020fa3d8e457cf9d1d543af9193376bf598c.tar.gz vyos-cloud-init-ff60020fa3d8e457cf9d1d543af9193376bf598c.zip |
Don't use walk and handle the walking ourselves to
ensure that we recreate all child messages correctly
if they also contain submessages, ensuring that
we don't flatten the message list when we previously
used walk.
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/filters/launch_index.py | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/cloudinit/filters/launch_index.py b/cloudinit/filters/launch_index.py index b9e108c4..4299fb46 100644 --- a/cloudinit/filters/launch_index.py +++ b/cloudinit/filters/launch_index.py @@ -35,8 +35,6 @@ class Filter(object): self.allow_none = allow_none def _select(self, message): - if ud.is_skippable(message): - return False msg_idx = message.get('Launch-Index', None) if self.allow_none and msg_idx is None: return True @@ -45,19 +43,33 @@ class Filter(object): return False return True - def apply(self, base_message): - if not base_message.is_multipart() or self.wanted_idx is None: - return base_message - prev_msgs = base_message.get_payload(decode=False) - to_attach = [] - for sub_msg in base_message.walk(): - if self._select(sub_msg): - to_attach.append(sub_msg) - if len(prev_msgs) != len(to_attach): + def _do_filter(self, message): + # Don't use walk() here since we want to do the reforming of the + # messages ourselves and not flatten the message listings... + if not self._select(message): + return None + if message.is_multipart(): + # Recreate it and its child messages + prev_msgs = message.get_payload(decode=False) + new_msgs = [] + discarded = 0 + for m in prev_msgs: + m = self._do_filter(m) + if m is not None: + new_msgs.append(m) + else: + discarded += 1 LOG.debug(("Discarding %s multipart messages " "which do not match launch index %s"), - (len(prev_msgs) - len(to_attach)), self.wanted_idx) - filtered_msg = copy.deepcopy(base_message) - filtered_msg.set_payload(to_attach) - filtered_msg[ud.ATTACHMENT_FIELD] = str(len(to_attach)) - return filtered_msg + discarded, self.wanted_idx) + new_message = copy.copy(message) + new_message.set_payload(new_msgs) + new_message[ud.ATTACHMENT_FIELD] = str(len(new_msgs)) + return new_message + else: + return copy.copy(message) + + def apply(self, root_message): + if self.wanted_idx is None: + return root_message + return self._do_filter(root_message) |