+ def _init_ssh_key(self, filename):
+ with open(filename) as ssh_file:
+ key = NodeAuthSSHKey(ssh_file.read())
+ return 'auth', key
+
+ def search_for_now(self, term, list_method, key=attrgetter('id'), **kwargs):
+ """Return one matching item from a list of cloud objects.
+
+ Raises ValueError if the number of matching objects is not exactly 1.
+
+ Arguments:
+ * term: The value that identifies a matching item.
+ * list_method: A string that names the method to call for a
+ list of objects.
+ * key: A function that accepts a cloud object and returns a
+ value search for a `term` match on each item. Returns the
+ object's 'id' attribute by default.
+ """
+ try:
+ list_func = getattr(self, list_method)
+ except AttributeError:
+ list_func = getattr(self.real, list_method)
+ items = list_func(**kwargs)
+ results = [item for item in items if key(item) == term]
+ count = len(results)
+ if count != 1:
+ raise ValueError("{} returned {} results for {!r}".format(
+ list_method, count, term))
+ return results[0]
+
+ def search_for(self, term, list_method, key=attrgetter('id'), **kwargs):
+ """Return one cached matching item from a list of cloud objects.
+
+ See search_for_now() for details of arguments and exceptions.
+ This method caches results, so it's good to find static cloud objects
+ like node sizes, regions, etc.
+ """