| Gulsum Atici | e7e0b75 | 2022-09-30 14:31:26 +0300 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # |
| 3 | # Copyright 2022 Canonical Ltd. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import abc |
| 18 | import asyncio |
| 19 | import logging |
| 20 | |
| 21 | |
| 22 | def paas_connector_factory( |
| 23 | uuid: str, |
| 24 | name: str, |
| 25 | db: object, |
| 26 | fs: object, |
| 27 | loop: object, |
| 28 | log: object, |
| 29 | config: dict, |
| 30 | paas_type="juju", |
| 31 | ): |
| 32 | """Factory Method to create the paas_connector objects according to PaaS Type. |
| 33 | Args: |
| 34 | uuid (str): Internal id of PaaS account |
| 35 | name (str): name assigned to PaaS account, can be used for logging |
| 36 | db (object): Database object to write current operation status |
| 37 | fs (object): Filesystem object to use during operations |
| 38 | loop (object): Async event loop object |
| 39 | log (object): Logger for tracing |
| 40 | config (dict): Dictionary with extra PaaS information. |
| 41 | paas_type (str): Identifier to create paas_connector object using correct PaaS Connector Class |
| 42 | |
| 43 | Returns: |
| 44 | paas_connector (object): paas_connector objects created according to given PaaS Type |
| 45 | |
| 46 | Raises: |
| 47 | PaasConnException |
| 48 | """ |
| 49 | connectors = { |
| 50 | "juju": JujuPaasConnector, |
| 51 | } |
| 52 | if paas_type not in connectors.keys(): |
| 53 | raise PaasConnException(f"PaaS type: {paas_type} is not available.") |
| 54 | |
| 55 | return connectors[paas_type](uuid, name, db, fs, loop, log, config) |
| 56 | |
| 57 | |
| 58 | class PaasConnException(Exception): |
| 59 | """PaaS Connector Exception Base Class""" |
| 60 | |
| 61 | def __init__(self, message: str = ""): |
| 62 | """Constructor of PaaS Connector Exception |
| 63 | Args: |
| 64 | message (str): error message to be raised |
| 65 | """ |
| 66 | Exception.__init__(self, message) |
| 67 | self.message = message |
| 68 | |
| 69 | def __str__(self): |
| 70 | return self.message |
| 71 | |
| 72 | def __repr__(self): |
| 73 | return "{}({})".format(type(self), self.message) |
| 74 | |
| 75 | |
| 76 | class JujuPaasConnException(PaasConnException): |
| 77 | """Juju PaaS Connector Exception Class""" |
| 78 | |
| 79 | |
| 80 | class AbstractPaasConnector(abc.ABC): |
| 81 | """Abstract PaaS Connector class to perform operations using PaaS Orchestrator.""" |
| 82 | |
| 83 | def __init__( |
| 84 | self, |
| 85 | uuid=None, |
| 86 | name=None, |
| 87 | db=None, |
| 88 | fs=None, |
| 89 | logger=None, |
| 90 | loop=None, |
| 91 | config=None, |
| 92 | ): |
| 93 | """Constructor of PaaS Connector. |
| 94 | Args: |
| 95 | uuid (str): internal id of PaaS account |
| 96 | name (str): name assigned to this account, can be used for logging |
| 97 | db (object): database object to write current operation status |
| 98 | fs (object): Filesystem object to use during operations |
| 99 | logger (object): Logger for tracing |
| 100 | loop (object): Async event loop object |
| 101 | config (dict): Dictionary with extra PaaS information. |
| 102 | """ |
| 103 | self.id = uuid |
| 104 | self.name = name |
| 105 | self.db = db |
| 106 | self.fs = fs |
| 107 | self.config = config or {} |
| 108 | self.logger = logger |
| 109 | |
| 110 | @abc.abstractmethod |
| 111 | async def connect(self, endpoints: str, user: str = None, secret: str = None): |
| 112 | """Abstract method to connect PaaS account using endpoints, user and secret. |
| 113 | Args: |
| 114 | endpoints (str): Endpoint/URL to connect PaaS account |
| 115 | user (str): User which is used to connect PaaS account |
| 116 | secret (str): Used for authentication |
| 117 | """ |
| 118 | |
| 119 | @abc.abstractmethod |
| 120 | async def instantiate(self, nsr_id: str, nslcmop_id: str): |
| 121 | """Abstract method to perform PaaS Service instantiation. |
| 122 | Args: |
| 123 | nsr_id (str): NS service record to be used |
| 124 | nslcmop_id (str): NS LCM operation id |
| 125 | """ |
| 126 | |
| 127 | @abc.abstractmethod |
| 128 | async def terminate(self, nsr_id: str, nslcmop_id: str): |
| 129 | """Abstract method to perform PaaS Service termination. |
| 130 | Args: |
| 131 | nsr_id (str): NS service record to be used |
| 132 | nslcmop_id (str): NS LCM operation id |
| 133 | """ |
| 134 | |
| 135 | @abc.abstractmethod |
| 136 | async def action(self, nsr_id: str, nslcmop_id: str): |
| 137 | """Abstract method to perform action on PaaS Service. |
| 138 | Args: |
| 139 | nsr_id (str): NS service record to be used |
| 140 | nslcmop_id (str): NS LCM operation id |
| 141 | """ |
| 142 | |
| 143 | |
| 144 | class JujuPaasConnector(AbstractPaasConnector): |
| 145 | """Concrete PaaS Connector class to perform operations using the Juju PaaS Orchestrator.""" |
| 146 | |
| 147 | def __init__( |
| 148 | self, |
| 149 | uuid=None, |
| 150 | name=None, |
| 151 | db=None, |
| 152 | fs=None, |
| 153 | logger=None, |
| 154 | loop=None, |
| 155 | config=None, |
| 156 | ): |
| 157 | self.logger = logging.getLogger("lcm.juju_paas_connector") |
| 158 | super(JujuPaasConnector, self).__init__(logger=self.logger) |
| 159 | |
| 160 | async def connect(self, endpoints: str, user: str = None, secret: str = None): |
| 161 | """Connect Juju PaaS account using endpoints, user and secret. |
| 162 | Args: |
| 163 | endpoints (str): Endpoint/URL to connect PaaS account |
| 164 | user (str): User which is used to connect PaaS account |
| 165 | secret (str): Used for authentication |
| 166 | |
| 167 | Raises: |
| 168 | NotImplementedError |
| 169 | """ |
| 170 | raise NotImplementedError( |
| 171 | "Juju Paas Connector connect method is not implemented" |
| 172 | ) |
| 173 | |
| 174 | async def instantiate(self, nsr_id: str, nslcmop_id: str): |
| 175 | """Perform Service instantiation. |
| 176 | Args: |
| 177 | nsr_id (str): NS service record to be used |
| 178 | nslcmop_id (str): NS LCM operation id |
| 179 | |
| 180 | Raises: |
| 181 | JujuPaasConnException |
| 182 | """ |
| 183 | # This is not the real implementation |
| 184 | # Sample code blocks to validate method execution |
| 185 | await asyncio.sleep(1) |
| 186 | self.logger.debug("Juju Paas Connector instantiate method is called") |
| 187 | |
| 188 | async def terminate(self, nsr_id: str, nslcmop_id: str): |
| 189 | """Perform PaaS Service termination. |
| 190 | Args: |
| 191 | nsr_id (str): NS service record to be used |
| 192 | nslcmop_id (str): NS LCM operation id |
| 193 | |
| 194 | Raises: |
| 195 | JujuPaasConnException |
| 196 | """ |
| 197 | # This is not the real implementation |
| 198 | # Sample code blocks to validate method execution |
| 199 | await asyncio.sleep(1) |
| 200 | self.logger.debug("Juju Paas Connector terminate method is called") |
| 201 | |
| 202 | async def action(self, nsr_id: str, nslcmop_id: str): |
| 203 | """Perform action on PaaS Service. |
| 204 | Args: |
| 205 | nsr_id (str): NS service record to be used |
| 206 | nslcmop_id (str): NS LCM operation id |
| 207 | |
| 208 | Raises: |
| 209 | NotImplementedError |
| 210 | """ |
| 211 | raise NotImplementedError( |
| 212 | "Juju Paas Connector instantiate method is not implemented" |
| 213 | ) |