|
| 1 | +# |
| 2 | +# Copyright (c) 2021 Nitric Technologies Pty Ltd. |
| 3 | +# |
| 4 | +# This file is part of Nitric Python 3 SDK. |
| 5 | +# See https://github.com/nitrictech/python-sdk for further info. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from typing import Union |
| 22 | + |
| 23 | +from grpclib import GRPCError |
| 24 | +from grpclib.client import Channel |
| 25 | + |
| 26 | +from nitric.exception import exception_from_grpc_error |
| 27 | +from nitric.proto.resources.v1 import ( |
| 28 | + SqlDatabaseResource, |
| 29 | + SqlDatabaseMigrations, |
| 30 | + ResourceDeclareRequest, |
| 31 | + ResourceIdentifier, |
| 32 | + ResourceType, |
| 33 | +) |
| 34 | +from nitric.resources.resource import Resource as BaseResource |
| 35 | +from nitric.utils import new_default_channel |
| 36 | +from nitric.application import Nitric |
| 37 | + |
| 38 | +from nitric.proto.sql.v1 import SqlStub, SqlConnectionStringRequest |
| 39 | + |
| 40 | + |
| 41 | +class Sql(BaseResource): |
| 42 | + """A SQL Database.""" |
| 43 | + |
| 44 | + _channel: Channel |
| 45 | + _sql_stub: SqlStub |
| 46 | + name: str |
| 47 | + migrations: Union[str, None] |
| 48 | + |
| 49 | + def __init__(self, name: str, migrations: Union[str, None] = None): |
| 50 | + """Construct a new SQL Database.""" |
| 51 | + super().__init__(name) |
| 52 | + |
| 53 | + self._channel: Union[Channel, None] = new_default_channel() |
| 54 | + self._sql_stub = SqlStub(channel=self._channel) |
| 55 | + self.name = name |
| 56 | + self.migrations = migrations |
| 57 | + |
| 58 | + async def _register(self) -> None: |
| 59 | + try: |
| 60 | + await self._resources_stub.declare( |
| 61 | + resource_declare_request=ResourceDeclareRequest( |
| 62 | + id=ResourceIdentifier(name=self.name, type=ResourceType.SqlDatabase), |
| 63 | + sql_database=SqlDatabaseResource( |
| 64 | + migrations=SqlDatabaseMigrations(migrations_path=self.migrations if self.migrations else "") |
| 65 | + ), |
| 66 | + ), |
| 67 | + ) |
| 68 | + except GRPCError as grpc_err: |
| 69 | + raise exception_from_grpc_error(grpc_err) from grpc_err |
| 70 | + |
| 71 | + async def connection_string(self) -> str: |
| 72 | + """Return the connection string for this SQL Database.""" |
| 73 | + response = await self._sql_stub.connection_string(SqlConnectionStringRequest(database_name=self.name)) |
| 74 | + |
| 75 | + return response.connection_string |
| 76 | + |
| 77 | + |
| 78 | +def sql(name: str, migrations: Union[str, None] = None) -> Sql: |
| 79 | + """ |
| 80 | + Create and register a sql database. |
| 81 | +
|
| 82 | + If a sql databse has already been registered with the same name, the original reference will be reused. |
| 83 | + """ |
| 84 | + return Nitric._create_resource(Sql, name, migrations) |
0 commit comments