Repository: canonical/operator
Pull request #57 (2019-11-26 15:26:29)

Change is_local to is_our and change visibility
Change is_local to is_our and change visibility
Expose is_leader as a Unit property


Patch:
diff --git a/op/model.py b/op/model.py
index d4c115f..09ea7ec 100644
--- a/op/model.py
+++ b/op/model.py
@@ -7,10 +7,10 @@ from subprocess import run, PIPE, CalledProcessError
 
 
 class Model:
-    def __init__(self, local_unit_name, relation_names, backend):
+    def __init__(self, unit_name, relation_names, backend):
         self._cache = ModelCache(backend)
         self._backend = backend
-        self.unit = self.get_unit(local_unit_name)
+        self.unit = self.get_unit(unit_name)
         self.app = self.unit.app
         self.relations = RelationMapping(relation_names, self.unit, self._backend, self._cache)
         self.config = ConfigData(self._backend)
@@ -70,7 +70,7 @@ class Application:
         self._backend = backend
         self._cache = cache
 
-        self.is_local = self.name == self._backend.local_app_name
+        self._is_our_app = self.name == self._backend.app_name
 
     def __repr__(self):
         return f'<{type(self).__module__}.{type(self).__name__} {self.name}>'
@@ -86,13 +86,13 @@ class Unit:
         self._backend = backend
         self._cache = cache
 
-        self.is_local = self.name == self._backend.local_unit_name
+        self._is_our_unit = self.name == self._backend.unit_name
 
     def __repr__(self):
         return f'<{type(self).__module__}.{type(self).__name__} {self.name}>'
 
     def is_leader(self):
-        if self.is_local:
+        if self._is_our_unit:
             # This value is not cached as it is not guaranteed to persist for the whole duration
             # of a hook execution.
             return self._backend.is_leader()
@@ -128,8 +128,8 @@ class LazyMapping(Mapping, ABC):
 
 class RelationMapping(Mapping):
     """Map of relation names to lists of Relation instances."""
-    def __init__(self, relation_names, local_unit, backend, cache):
-        self._local_unit = local_unit
+    def __init__(self, relation_names, our_unit, backend, cache):
+        self._our_unit = our_unit
         self._backend = backend
         self._cache = cache
         self._data = {relation_name: None for relation_name in relation_names}
@@ -148,12 +148,12 @@ class RelationMapping(Mapping):
         if relation_list is None:
             relation_list = self._data[relation_name] = []
             for relation_id in self._backend.relation_ids(relation_name):
-                relation_list.append(Relation(relation_name, relation_id, self._local_unit, self._backend, self._cache))
+                relation_list.append(Relation(relation_name, relation_id, self._our_unit, self._backend, self._cache))
         return relation_list
 
 
 class Relation:
-    def __init__(self, relation_name, relation_id, local_unit, backend, cache):
+    def __init__(self, relation_name, relation_id, our_unit, backend, cache):
         self.name = relation_name
         self.id = relation_id
         self.app = None
@@ -167,16 +167,16 @@ class Relation:
         except RelationNotFound:
             # If the relation is dead, just treat it as if it has no remote units.
             pass
-        self.data = RelationData(self, local_unit, backend)
+        self.data = RelationData(self, our_unit, backend)
 
     def __repr__(self):
         return f'<{type(self).__module__}.{type(self).__name__} {self.name}:{self.id}>'
 
 
 class RelationData(Mapping):
-    def __init__(self, relation, local_unit, backend):
+    def __init__(self, relation, our_unit, backend):
         self.relation = weakref.proxy(relation)
-        self._data = {local_unit: RelationUnitData(self.relation, local_unit, True, backend)}
+        self._data = {our_unit: RelationUnitData(self.relation, our_unit, True, backend)}
         self._data.update({unit: RelationUnitData(self.relation, unit, False, backend) for unit in self.relation.units})
 
     def __contains__(self, key):
@@ -259,8 +259,8 @@ class RelationNotFound(ModelError):
 
 class ModelBackend:
     def __init__(self):
-        self.local_unit_name = os.environ['JUJU_UNIT_NAME']
-        self.local_app_name = self.local_unit_name.split('/')[0]
+        self.unit_name = os.environ['JUJU_UNIT_NAME']
+        self.app_name = self.unit_name.split('/')[0]
 
     def _run(self, *args):
         result = run(args + ('--format=json',), stdout=PIPE, stderr=PIPE, check=True)


Test patch:
diff --git a/test/test_model.py b/test/test_model.py
index 050480e..3b87d94 100644
--- a/test/test_model.py
+++ b/test/test_model.py
@@ -18,11 +18,8 @@ class FakeModelBackend:
     def __init__(self):
         self.relation_set_calls = []
 
-        self.local_unit_name = 'myapp/0'
-        self.local_app_name = 'myapp'
-
-    def local_app_name(self):
-        return 'myapp'
+        self.unit_name = 'myapp/0'
+        self.app_name = 'myapp'
 
     def relation_ids(self, relation_name):
         return {
@@ -104,14 +101,14 @@ class TestModel(unittest.TestCase):
         with self.assertRaises(op.model.TooManyRelatedApps):
             self.model.get_relation('db2')
 
-    def test_remote_units_is_local(self):
+    def test_remote_units_is_our(self):
         for u in self.model.get_relation('db1').units:
-            self.assertFalse(u.is_local)
-            self.assertFalse(u.app.is_local)
+            self.assertFalse(u._is_our_unit)
+            self.assertFalse(u.app._is_our_app)
 
-    def test_local_unit_is_local(self):
-        self.assertTrue(self.model.unit.is_local)
-        self.assertTrue(self.model.unit.app.is_local)
+    def test_our_unit_is_our(self):
+        self.assertTrue(self.model.unit._is_our_unit)
+        self.assertTrue(self.model.unit.app._is_our_app)
 
     def test_relation_data(self):
         random_unit = self.model._cache.get(op.model.Unit, 'randomunit/0')
@@ -130,7 +127,7 @@ class TestModel(unittest.TestCase):
         self.assertEqual(self.backend.relation_set_calls, [])
         self.assertNotIn('foo', rel_db1.data[remoteapp1_0])
 
-    def test_relation_data_modify_local(self):
+    def test_relation_data_modify_our(self):
         rel_db1 = self.model.get_relation('db1')
         # Force memory cache to be loaded.
         self.assertIn('host', rel_db1.data[self.model.unit])