| David Garcia | 009a5d6 | 2020-08-27 16:53:44 +0200 | [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 | from charms.reactive import Endpoint |
| 15 | from charms.reactive import when |
| 16 | from charms.reactive import set_flag, clear_flag |
| 17 | |
| 18 | |
| 19 | class KeystoneRequires(Endpoint): |
| 20 | @when("endpoint.{endpoint_name}.joined") |
| 21 | def _joined(self): |
| 22 | set_flag(self.expand_name("{endpoint_name}.joined")) |
| 23 | |
| 24 | @when("endpoint.{endpoint_name}.changed") |
| 25 | def _changed(self): |
| 26 | if len(self.keystones()) > 0: |
| 27 | set_flag(self.expand_name("{endpoint_name}.ready")) |
| 28 | else: |
| 29 | clear_flag(self.expand_name("{endpoint_name}.ready")) |
| 30 | |
| 31 | @when("endpoint.{endpoint_name}.departed") |
| 32 | def _departed(self): |
| 33 | set_flag(self.expand_name("{endpoint_name}.departed")) |
| 34 | clear_flag(self.expand_name("{endpoint_name}.joined")) |
| 35 | clear_flag(self.expand_name("{endpoint_name}.ready")) |
| 36 | |
| 37 | def keystones(self): |
| 38 | """ |
| 39 | Return Keystone Data: |
| 40 | [{ |
| 41 | 'host': <host>, |
| 42 | 'port': <port>, |
| 43 | 'keystone_db_password: <keystone_db_password>, |
| 44 | 'region_id: <region_id>, |
| 45 | 'admin_username: <admin_username>, |
| 46 | 'admin_password: <admin_password>, |
| 47 | 'admin_project_name: <admin_project_name>, |
| 48 | 'username: <username>, |
| 49 | 'password: <password>, |
| 50 | 'service: <service> |
| 51 | }] |
| 52 | """ |
| 53 | keystones = [] |
| 54 | for relation in self.relations: |
| 55 | for unit in relation.units: |
| 56 | data = { |
| 57 | "host": unit.received["host"], |
| 58 | "port": unit.received["port"], |
| 59 | "keystone_db_password": unit.received["keystone_db_password"], |
| 60 | "region_id": unit.received["region_id"], |
| 61 | "user_domain_name": unit.received["user_domain_name"], |
| 62 | "project_domain_name": unit.received["project_domain_name"], |
| 63 | "admin_username": unit.received["admin_username"], |
| 64 | "admin_password": unit.received["admin_password"], |
| 65 | "admin_project_name": unit.received["admin_project_name"], |
| 66 | "username": unit.received["username"], |
| 67 | "password": unit.received["password"], |
| 68 | "service": unit.received["service"], |
| 69 | } |
| 70 | if all(data.values()): |
| 71 | keystones.append(data) |
| 72 | return keystones |