import operator


def ParseIpRanges(messages, ip_ranges):
  """Parse a dict of IP ranges into AdvertisedPrefix objects.

  Args:
    messages: API messages holder.
    ip_ranges: A dict of IP ranges of the form ip_range=description, where
               ip_range is a CIDR-formatted IP and description is an optional
               text label.

  Returns:
    A list of AdvertisedPrefix objects containing the specified IP ranges.
  """
  prefixes = [
      messages.RouterAdvertisedPrefix(prefix=ip_range, description=description)
      for ip_range, description in ip_ranges.items()
  ]
  # Sort the resulting list so that requests have a deterministic ordering
  # for test validations and user output.
  prefixes.sort(key=operator.attrgetter('prefix', 'description'))
  return prefixes