@@ -49,6 +49,8 @@ def __init__(self, name, uuid=None, generation=None, parent_uuid=None):
4949 self .inventory = {}
5050 # Set of trait names
5151 self .traits = set ()
52+ # Set of aggregate UUIDs
53+ self .aggregates = set ()
5254
5355 def get_provider_uuids (self ):
5456 """Returns a set of UUIDs of this provider and all its descendants."""
@@ -152,6 +154,31 @@ def has_traits(self, traits):
152154 """
153155 return not bool (set (traits ) - self .traits )
154156
157+ def have_aggregates_changed (self , new ):
158+ """Returns whether the provider's aggregates have changed."""
159+ return set (new ) != self .aggregates
160+
161+ def update_aggregates (self , new , generation = None ):
162+ """Update the stored aggregates for the provider along with a resource
163+ provider generation to set the provider to. The method returns whether
164+ the aggregates have changed.
165+ """
166+ self ._update_generation (generation )
167+ if self .have_aggregates_changed (new ):
168+ self .aggregates = set (new ) # create a copy of the new aggregates
169+ return True
170+ return False
171+
172+ def in_aggregates (self , aggregates ):
173+ """Query whether the provider is a member of certain aggregates.
174+
175+ :param aggregates: Iterable of string aggregate UUIDs to look for.
176+ :return: True if this provider is a member of *all* of the specified
177+ aggregates; False if any of the specified aggregates are
178+ absent. Returns True if the aggregates parameter is empty.
179+ """
180+ return not bool (set (aggregates ) - self .aggregates )
181+
155182
156183class ProviderTree (object ):
157184
@@ -352,3 +379,59 @@ def update_traits(self, name_or_uuid, traits, generation=None):
352379 with self .lock :
353380 provider = self ._find_with_lock (name_or_uuid )
354381 return provider .update_traits (traits , generation = generation )
382+
383+ def in_aggregates (self , name_or_uuid , aggregates ):
384+ """Given a name or UUID of a provider, query whether that provider is a
385+ member of *all* the specified aggregates.
386+
387+ :raises: ValueError if a provider with name_or_uuid was not found in
388+ the tree.
389+ :param name_or_uuid: Either name or UUID of the resource provider to
390+ query for aggregates.
391+ :param aggregates: Iterable of string aggregate UUIDs to search for.
392+ :return: True if this provider is associated with *all* of the
393+ specified aggregates; False if any of the specified aggregates
394+ are absent. Returns True if the aggregates parameter is
395+ empty, even if the provider has no aggregate associations.
396+ """
397+ with self .lock :
398+ provider = self ._find_with_lock (name_or_uuid )
399+ return provider .in_aggregates (aggregates )
400+
401+ def have_aggregates_changed (self , name_or_uuid , aggregates ):
402+ """Returns True if the specified aggregates list is different for the
403+ provider with the specified name or UUID.
404+
405+ :raises: ValueError if a provider with name_or_uuid was not found in
406+ the tree.
407+ :param name_or_uuid: Either name or UUID of the resource provider to
408+ query aggregates for.
409+ :param aggregates: Iterable of string aggregate UUIDs to compare
410+ against the provider's aggregates.
411+ """
412+ with self .lock :
413+ provider = self ._find_with_lock (name_or_uuid )
414+ return provider .have_aggregates_changed (aggregates )
415+
416+ def update_aggregates (self , name_or_uuid , aggregates , generation = None ):
417+ """Given a name or UUID of a provider and an iterable of string
418+ aggregate UUIDs, update the provider's aggregates and set the
419+ provider's generation.
420+
421+ :returns: True if the aggregates list has changed.
422+
423+ :note: The provider's generation is always set to the supplied
424+ generation, even if there were no changes to the aggregates.
425+
426+ :raises: ValueError if a provider with name_or_uuid was not found in
427+ the tree.
428+ :param name_or_uuid: Either name or UUID of the resource provider to
429+ update aggregates for.
430+ :param aggregates: Iterable of string aggregate UUIDs to set.
431+ :param generation: The resource provider generation to set. If None,
432+ the provider's generation is not changed.
433+ """
434+ with self .lock :
435+ provider = self ._find_with_lock (name_or_uuid )
436+ return provider .update_aggregates (aggregates ,
437+ generation = generation )
0 commit comments