| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 1 | # Copyright 2020 Canonical Ltd. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from charms.reactive import RelationBase |
| 16 | from charms.reactive import hook |
| 17 | from charms.reactive import scopes |
| 18 | |
| 19 | |
| 20 | class OsmRORequires(RelationBase): |
| 21 | scope = scopes.GLOBAL |
| 22 | |
| 23 | @hook("{requires:osm-ro}-relation-joined") |
| 24 | def joined(self): |
| 25 | conv = self.conversation() |
| 26 | conv.set_state("{relation_name}.joined") |
| 27 | |
| 28 | @hook("{requires:osm-ro}-relation-changed") |
| 29 | def changed(self): |
| 30 | conv = self.conversation() |
| 31 | if self.ros(): |
| 32 | conv.set_state("{relation_name}.ready") |
| 33 | else: |
| 34 | conv.remove_state("{relation_name}.ready") |
| 35 | |
| 36 | @hook("{requires:osm-ro}-relation-departed") |
| 37 | def departed(self): |
| 38 | conv = self.conversation() |
| 39 | conv.remove_state("{relation_name}.ready") |
| 40 | conv.remove_state("{relation_name}.joined") |
| 41 | |
| 42 | def ros(self): |
| 43 | """Return the NBI's host and port. |
| 44 | |
| 45 | [{ |
| 46 | 'host': <host>, |
| 47 | 'port': <port>, |
| 48 | }] |
| 49 | """ |
| 50 | ros = [] |
| 51 | for conv in self.conversations(): |
| 52 | port = conv.get_remote("port") |
| 53 | host = conv.get_remote("host") or conv.get_remote("private-address") |
| 54 | if host and port: |
| 55 | ros.append({"host": host, "port": port}) |
| 56 | return ros |