Skip to content

Commit

Permalink
Improved recently added find_local()
Browse files Browse the repository at this point in the history
Details:

* The recently added BaseManager.find_local() method used the resource_class
  constructor to create a new resource object, but it is better to use the
  resource_object() method because that encapsulates how the properties are
  set, and sets more of them.

* Made the uri parameter in find_local() required - it is now not only used
  for creating a new resource object, but also to verify the URI when an
  existing one is found in the cache. This was changed for a more consistent
  meaning of the parameters across the two cases (found and not found).

Signed-off-by: Andreas Maier <[email protected]>
  • Loading branch information
andy-maier committed Jul 2, 2021
1 parent 20be481 commit 202ad65
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions zhmcclient/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,51 +753,47 @@ def find_local(self, name, uri, properties=None):
"""
Return a local resource object without fetching it from the HMC.
If the resource object is in the local URI name cache, take it from
there. The object is looked up by the specified name, and the uri and
properties parameters are ignored in that case.
If the resource object is not in the local URI name cache, a new
locally constructed resource object is returned from the specified
name, uri and properties parameters. That resource object is not put
into the local URI name cache, because there was no validation that the
specified properties are up to date or even valid at all.
This approach is a compromise for operations such as "List Permitted
Partitions" in which the parent CPC object may not be accessible
by the user and thus cannot be fetched.
The resource object is fully functional, for example it has the proper
parent resource object set and its properties can be fetched using
:meth:`~zhmcclient.BaseResource.pull_full_properties`.
If the resource object is in the name-to-URI cache, it is returned from
there, and dependent on the history it may have a minimal set of
properties or the full set of properties. Otherwise, a new resource
object is constructed locally from the specified name, uri and
properties parameters. That resource object is not put into the
name-to-URI cache because there was no validation that the specified
properties are up to date or even valid at all.
Parameters:
name (string):
Name of the resource.
Name of the resource. Must not be `None`.
uri (string):
Object URI of the resource, to be used when the resource is
locally constructed.
Object URI of the resource. Must not be `None`.
properties (dict):
Additional properties, to be used when the resource is locally
constructed.
Additional properties. Only used when the resource is not found
in the name-to-URI cache. It is the responsibility of the user
to ensure that the properties are valid.
Returns:
Resource object in scope of this manager object. This resource object
has a minimal set of properties.
Resource object.
"""
# Get the resource object from the name-to-URI cache, if possible.
resource_obj = self._try_optimized_lookup(dict(name=name))
if resource_obj is None:
resource_props = {
'name': name,
'object-uri': uri,
}
resource_props.update(properties)
resource_obj = self.resource_class(
manager=self,
uri=uri,
name=name,
properties=resource_props)
# Note: The object is intentionally not put into the local cache
if resource_obj:
assert uri == resource_obj.uri
return resource_obj

# Create a new local resource object.
resource_props = {
self._name_prop: name,
}
resource_props.update(properties)
resource_obj = self.resource_object(uri, resource_props)
return resource_obj

@logged_api_call
Expand Down

0 comments on commit 202ad65

Please sign in to comment.